diff options
author | 2022-09-22 21:29:50 +0300 | |
---|---|---|
committer | 2022-11-07 19:39:26 +0200 | |
commit | 24369e749e874cb871727dbdd02ca95b3eb65a04 (patch) | |
tree | 9f96ce672e84dd75d26c89d44d16f332baa3ef4a /py_build.py | |
parent | drop py3.8 and make py3.11 official (diff) | |
download | pkgcore-24369e749e874cb871727dbdd02ca95b3eb65a04.tar.gz pkgcore-24369e749e874cb871727dbdd02ca95b3eb65a04.tar.bz2 pkgcore-24369e749e874cb871727dbdd02ca95b3eb65a04.zip |
build backend: use custom wrapper around flit
For pkgcore we need to run multiple preparations of generating files
before creating sdist or wheel. Flit is a very simple and nice build
backend, much more than setuptools.
Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
Diffstat (limited to 'py_build.py')
-rw-r--r-- | py_build.py | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/py_build.py b/py_build.py new file mode 100644 index 000000000..f844b1c2a --- /dev/null +++ b/py_build.py @@ -0,0 +1,99 @@ +import os +import subprocess +import sys +import textwrap +from contextlib import contextmanager +from functools import partial +from pathlib import Path + +from flit_core import buildapi + + +@contextmanager +def sys_path(): + orig_path = sys.path[:] + sys.path.insert(0, str(Path.cwd() / 'src')) + try: + yield + finally: + sys.path = orig_path + + +def write_pkgcore_lookup_configs(cleanup_files): + """Generate file of install path constants.""" + cleanup_files.append(path := Path.cwd() / "src/pkgcore/_const.py") + path.parent.mkdir(parents=True, exist_ok=True) + print(f"writing lookup config to {path}") + + with open(path, "w") as f: + os.chmod(path, 0o644) + f.write(textwrap.dedent("""\ + from os.path import join, abspath + import sys + + from snakeoil import process + + INSTALL_PREFIX = abspath(sys.prefix) + DATA_PATH = join(INSTALL_PREFIX, 'share/pkgcore') + CONFIG_PATH = join(DATA_PATH, 'config') + LIBDIR_PATH = join(INSTALL_PREFIX, 'lib/pkgcore') + EBD_PATH = join(LIBDIR_PATH, 'ebd') + INJECTED_BIN_PATH = () + """)) + + +def write_verinfo(cleanup_files): + cleanup_files.append(path := Path.cwd() / "src/pkgcore/_verinfo.py") + path.parent.mkdir(parents=True, exist_ok=True) + print(f"generating version info: {path}") + from snakeoil.version import get_git_version + path.write_text(f"version_info={get_git_version(Path.cwd())!r}") + + +def prepare_pkgcore(callback, consts: bool, plugincache: bool): + cleanup_files = [] + try: + with sys_path(): + write_verinfo(cleanup_files) + + # Install module plugincache + if plugincache: + from pkgcore import plugin, plugins + print('Generating plugin cache') + cleanup_files.append(path := Path.cwd() / "src/pkgcore/plugins") + plugin.initialize_cache(plugins, force=True, cache_dir=path) + + # Install configuration data so pkgcore knows where to find its content, + # rather than assuming it is running from a tarball/git repo. + if consts: + write_pkgcore_lookup_configs(cleanup_files) + + # generate function lists so they don't need to be created on install + if subprocess.call(['make', f'PYTHON={sys.executable}'], cwd=Path.cwd() / 'data/lib/pkgcore/ebd'): + raise Exception("Running makefile failed") + + return callback() + finally: + for path in cleanup_files: + try: + path.unlink() + except OSError: + pass + + +def build_wheel(wheel_directory, config_settings=None, metadata_directory=None): + """Builds a wheel, places it in wheel_directory""" + callback = partial(buildapi.build_wheel, wheel_directory, config_settings, metadata_directory) + return prepare_pkgcore(callback, consts=True, plugincache=True) + + +def build_editable(wheel_directory, config_settings=None, metadata_directory=None): + """Builds an "editable" wheel, places it in wheel_directory""" + callback = partial(buildapi.build_editable, wheel_directory, config_settings, metadata_directory) + return prepare_pkgcore(callback, consts=False, plugincache=True) + + +def build_sdist(sdist_directory, config_settings=None): + """Builds an sdist, places it in sdist_directory""" + callback = partial(buildapi.build_sdist, sdist_directory, config_settings) + return prepare_pkgcore(callback, consts=False, plugincache=False) |