From c4d95bcf3429fe847c2fb717319820965bdd5949 Mon Sep 17 00:00:00 2001 From: axel-kah Date: Thu, 2 May 2024 21:22:11 +0200 Subject: [PATCH] feat: adds support for python and pypy distribution mirrors --- docs/plugins/environment/virtual.md | 4 ++++ src/hatch/cli/python/install.py | 5 +++++ src/hatch/python/resolve.py | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/docs/plugins/environment/virtual.md b/docs/plugins/environment/virtual.md index 50ca5aa04..e5889e38d 100644 --- a/docs/plugins/environment/virtual.md +++ b/docs/plugins/environment/virtual.md @@ -75,6 +75,8 @@ Some distributions have [variants](https://gregoryszorc.com/docs/python-build-st | Linux | | | Windows | | +You can configure Hatch to use a mirror of these distributions by setting the `HATCH_CUSTOM_CPYTHON_SOURCE` environment variable to your mirror. The custom source is expected to contain the releases following the upstreams structure `/cpython-X.Y.Z%2B-aarch64-unknown-linux-gnu-install_only.tar.gz`. + ### PyPy | ID | @@ -85,6 +87,8 @@ Some distributions have [variants](https://gregoryszorc.com/docs/python-build-st The source of distributions is the [PyPy](https://www.pypy.org) project. +You can configure Hatch to use a mirror of these distributions by setting the `HATCH_CUSTOM_PYPY_SOURCE` environment variable to your mirror. The custom source is expected to contain the releases following the upstreams structure `pypyX.Y-vA.B.C-aarch64.tar.bz2`. + ## Troubleshooting ### macOS SIP diff --git a/src/hatch/cli/python/install.py b/src/hatch/cli/python/install.py index 04cfe5ace..dc9b6d878 100644 --- a/src/hatch/cli/python/install.py +++ b/src/hatch/cli/python/install.py @@ -36,6 +36,11 @@ def install(app: Application, *, names: tuple[str, ...], private: bool, update: ``` hatch python install all ``` + + If you run your own mirror you can tell Hatch to use it by setting these environment variables to your mirrors URL: + + * ``HATCH_CUSTOM_CPYTHON_SOURCE`` + * ``HATCH_CUSTOM_PYPY_SOURCE`` """ from hatch.errors import PythonDistributionResolutionError, PythonDistributionUnknownError from hatch.python.distributions import ORDERED_DISTRIBUTIONS diff --git a/src/hatch/python/resolve.py b/src/hatch/python/resolve.py index 173a74ec6..7dc47cac2 100644 --- a/src/hatch/python/resolve.py +++ b/src/hatch/python/resolve.py @@ -83,6 +83,17 @@ def python_path(self) -> str: class CPythonStandaloneDistribution(Distribution): + @property + def source(self) -> str: + source = super().source + custom_source = os.environ.get('HATCH_CUSTOM_CPYTHON_SOURCE') + if custom_source: + source = super().source.replace( + 'https://github.com/indygreg/python-build-standalone/releases/download/', + custom_source.rstrip('/') + '/', + ) + return source + @cached_property def version(self) -> Version: from packaging.version import Version @@ -110,6 +121,14 @@ def python_path(self) -> str: class PyPyOfficialDistribution(Distribution): + @property + def source(self) -> str: + source = super().source + custom_source = os.environ.get('HATCH_CUSTOM_PYPY_SOURCE') + if custom_source: + source = super().source.replace('https://downloads.python.org/pypy/', custom_source.rstrip('/') + '/') + return source + @cached_property def version(self) -> Version: from packaging.version import Version