Skip to content

Commit

Permalink
generate .pyc files from .hy files on install (and include them in wh…
Browse files Browse the repository at this point in the history
…eel distributions)
  • Loading branch information
scauligi committed Jun 4, 2022
1 parent 02e02c2 commit eb3fcab
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import fastentrypoints # Monkey-patches setuptools.
from get_version import __version__
from setuptools import find_packages, setup
from setuptools.command.install import install as _install

os.chdir(os.path.split(os.path.abspath(__file__))[0])

Expand All @@ -14,6 +15,36 @@
make things work nicer, and lets Python and the Hy lisp variant play
nice together. """


class install(_install):
def run(self):
super().run()
import contextlib
import importlib.util
import py_compile
from glob import glob

import hy # for compile hooks

mode = py_compile.PycInvalidationMode.CHECKED_HASH

# use timestamp mode if we detect the python files have been compiled that way
init_file = os.path.join(self.install_lib, "hy", "__init__.py")
init_pyc = importlib.util.cache_from_source(init_file)
with contextlib.suppress(FileNotFoundError), open(init_pyc, "rb") as fp:
fp.seek(4)
flags = int.from_bytes(fp.read(4), "little")

mode = (
py_compile.PycInvalidationMode.CHECKED_HASH
if flags & 0b1
else py_compile.PycInvalidationMode.TIMESTAMP
)

for path in glob(os.path.join(self.install_lib, "**/*.hy"), recursive=True):
py_compile.compile(path, invalidation_mode=mode)


setup(
name=PKG,
version=__version__,
Expand All @@ -35,10 +66,7 @@
},
packages=find_packages(exclude=["tests*"]),
package_data={
"hy": ["*.hy", "__pycache__/*"],
"hy.contrib": ["*.hy", "__pycache__/*"],
"hy.core": ["*.hy", "__pycache__/*"],
"hy.extra": ["*.hy", "__pycache__/*"],
"": ["*.hy"],
},
data_files=[("get_version", ["get_version.py"])],
author="Paul Tagliamonte",
Expand Down Expand Up @@ -69,4 +97,7 @@
"Documentation": "https://docs.hylang.org/",
"Source": "https://github.com/hylang/hy",
},
cmdclass={
"install": install,
},
)

0 comments on commit eb3fcab

Please sign in to comment.