|
| 1 | +"""Virtualenv seeder plugin for Fromager |
| 2 | +
|
| 3 | +The plugin installs ``build`` and its dependencies ``packaging`` + |
| 4 | +``pyproject_hooks`` into a new virtual environment. The wheel files for |
| 5 | +the three packages are bundled and shipped with Fromager. This solves |
| 6 | +problems with bootstrapping. |
| 7 | +
|
| 8 | +Either Fromager would have to implement a special bootstrapping mode to |
| 9 | +build ``build``, ``packaging``, ``pyproject_hooks``, and ``flit-core`` |
| 10 | +wheels first, then switch its wheel build command to ``build``. |
| 11 | +
|
| 12 | +Or Fromager would have to download three pre-built wheels from PyPI. |
| 13 | +
|
| 14 | +2. Fromager can create a build environment with the ``build`` command without |
| 15 | + downloading any content from PyPI. Instead it uses the same bundle approach |
| 16 | + as ``virtualenv`` and ``ensurepip``. |
| 17 | +
|
| 18 | +
|
| 19 | +""" |
| 20 | + |
| 21 | +import pathlib |
| 22 | +import typing |
| 23 | + |
| 24 | +from packaging.utils import parse_wheel_filename |
| 25 | + |
| 26 | +from virtualenv.config.cli.parser import VirtualEnvOptions |
| 27 | +from virtualenv.seed.embed.via_app_data.via_app_data import FromAppData |
| 28 | +from virtualenv.seed.wheels import Version |
| 29 | +from virtualenv.seed.wheels.embed import BUNDLE_SUPPORT |
| 30 | + |
| 31 | +BUNDLED_DIR = pathlib.Path(__file__).parent.resolve() / "bundled" |
| 32 | +# `build`` depends on packaging and pyproject_hooks |
| 33 | +BUNDLED_PACKAGES = ["build", "packaging", "pyproject_hooks"] |
| 34 | + |
| 35 | + |
| 36 | +class FromagerSeeder(FromAppData): |
| 37 | + """Custom virtualenv seeder to install build command""" |
| 38 | + |
| 39 | + def __init__(self, options: VirtualEnvOptions) -> None: |
| 40 | + # register our packages |
| 41 | + for whl, name, version in self.list_extra_packages(): |
| 42 | + # add option defaults |
| 43 | + setattr(self, f"no_{name}", False) |
| 44 | + setattr(self, f"{name}_version", version) |
| 45 | + # register wheel files with virtualenv's bundle support |
| 46 | + for py_version in BUNDLE_SUPPORT: |
| 47 | + BUNDLE_SUPPORT[py_version][name] = str(whl) |
| 48 | + |
| 49 | + # virtualenv no longer installs setuptool and wheels for |
| 50 | + # Python >= 3.12, force installation. |
| 51 | + for opt in ("setuptools", "wheel"): |
| 52 | + if getattr(options, opt) == "none": |
| 53 | + setattr(options, opt, Version.bundle) |
| 54 | + |
| 55 | + super().__init__(options) |
| 56 | + |
| 57 | + @classmethod |
| 58 | + def list_extra_packages(cls) -> typing.Iterable[tuple[pathlib.Path, str, str]]: |
| 59 | + for whl in sorted(BUNDLED_DIR.glob("*.whl")): |
| 60 | + name, version, _, _ = parse_wheel_filename(whl.name) |
| 61 | + yield whl, name, str(version) |
| 62 | + |
| 63 | + @classmethod |
| 64 | + def distributions(cls) -> dict[str, str]: |
| 65 | + dist = super().distributions() |
| 66 | + for _, name, version in cls.list_extra_packages(): |
| 67 | + dist[name] = version |
| 68 | + return dist |
0 commit comments