from . import _ctx from ._ctx import run_subprocess from ._exceptions import FailedProcessError from ._util import check_dependency
Installer = typing.Literal['pip', 'uv']
INSTALLERS = typing.get_args(Installer)
class IsolatedEnv(typing.Protocol): """Isolated build environment ABC."""
@property
@abc.abstractmethod def python_executable(self) -> str: """The Python executable of the isolated environment."""
@abc.abstractmethod def make_extra_environ(self) -> Mapping[str, str] | None: """Generate additional env vars specific to the isolated environment."""
def _has_dependency(name: str, minimum_version_str: str | None = None, /, **distargs: object) -> bool | None: """
Given a path, see if a package is present andreturnTrueif the version is
sufficient for build, Falseif it isnot, Noneif the package is missing. """ from packaging.version import Version
from ._compat import importlib
try:
distribution = next(iter(importlib.metadata.distributions(name=name, **distargs))) except StopIteration: returnNone
def __enter__(self) -> DefaultIsolatedEnv: try:
path = tempfile.mkdtemp(prefix='build-env-') # Call ``realpath`` to prevent spurious warning from being emitted # that the venv location has changed on Windows for the venv impl. # The username is DOS-encoded in the output of tempfile - the location is the same # but the representation of it is different, which confuses venv. # Ref: https://bugs.python.org/issue46171
path = os.path.realpath(path)
self._path = path
self._env_backend: _EnvBackend
# uv is opt-in only. if self.installer == 'uv':
self._env_backend = _UvBackend() else:
self._env_backend = _PipBackend()
def install(self, requirements: Collection[str]) -> None: """
Install packages from PEP 508 requirements in the isolated build environment.
:param requirements: PEP 508 requirement specification to install
:note: Passing non-PEP 508 strings will result in undefined behavior, you *should not* rely on it. It is
merely an implementation detail, it may change any time without warning. """ ifnot requirements: return
_ctx.log('Installing packages in isolated environment:\n' + '\n'.join(f'- {r}'for r in sorted(requirements)))
self._env_backend.install_requirements(requirements)
class _EnvBackend(typing.Protocol): # pragma: no cover
python_executable: str
scripts_dir: str
class _PipBackend(_EnvBackend): def __init__(self) -> None:
self._create_with_virtualenv = not self._has_valid_outer_pip and self._has_virtualenv
@functools.cached_property def _has_valid_outer_pip(self) -> bool | None: """
This checks for a valid global pip. Returns Noneif pip is missing, False if pip is too old, andTrueif it can be used. """ # Version to have added the `--python` option. return _has_dependency('pip', '22.3')
@functools.cached_property def _has_virtualenv(self) -> bool: """
virtualenv might be incompatible if it was installed separately from build. This verifies that virtualenv and all of its
dependencies are installed as required by build. """ from packaging.requirements import Requirement
name = 'virtualenv'
return importlib.util.find_spec(name) isnotNoneandnot any(
Requirement(d[1]).name == name for d in check_dependency(f'build[{name}]') if len(d) > 1
)
@staticmethod def _get_minimum_pip_version_str() -> str: if platform.system() == 'Darwin':
release, _, machine = platform.mac_ver() if int(release[: release.find('.')]) >= 11: # macOS 11+ name scheme change requires 20.3. Intel macOS 11.0 can be # told to report 10.16 for backwards compatibility; but that also fixes # earlier versions of pip so this is only needed for 11+.
is_apple_silicon_python = machine != 'x86_64' return'21.0.1'if is_apple_silicon_python else'20.3.0'
# PEP-517 and manylinux1 was first implemented in 19.1 return'19.1.0'
def create(self, path: str) -> None: if self._create_with_virtualenv: import virtualenv
# Uninstall setuptools from the build env to prevent depending on it implicitly. # Pythons 3.12 and up do not install setuptools, check if it exists first. if _has_dependency( 'setuptools',
path=[purelib],
):
run_subprocess([self.python_executable, '-Im', 'pip', 'uninstall', '-y', 'setuptools'])
def install_requirements(self, requirements: Collection[str]) -> None: # pip does not honour environment markers in command line arguments # but it does from requirement files. with tempfile.NamedTemporaryFile('w', prefix='build-reqs-', suffix='.txt', delete=False, encoding='utf-8') as req_file:
req_file.write(os.linesep.join(requirements))
@functools.lru_cache(maxsize=None) def _fs_supports_symlink() -> bool: """Return True if symlinks are supported""" # Using definition used by venv.main() if os.name != 'nt': returnTrue
# Windows may support symlinks (setting in Windows 10) with tempfile.NamedTemporaryFile(prefix='build-symlink-') as tmp_file:
dest = f'{tmp_file}-b' try:
os.symlink(tmp_file.name, dest)
os.unlink(dest) except (OSError, NotImplementedError, AttributeError): returnFalse returnTrue
def _find_executable_and_scripts(path: str) -> tuple[str, str, str]: """
Detect the Python executable and script folder of a virtual environment.
:param path: The location of the virtual environment
:return: The Python executable, script folder, and purelib folder """
config_vars = sysconfig.get_config_vars().copy() # globally cached, copy before altering it
config_vars['base'] = path
scheme_names = sysconfig.get_scheme_names() if'venv'in scheme_names: # Python distributors with custom default installation scheme can set a # scheme that can't be used to expand the paths in a venv. # This can happen if build itself is not installed in a venv. # The distributors are encouraged to set a "venv" scheme to be used for this. # See https://bugs.python.org/issue45413 # and https://github.com/pypa/virtualenv/issues/2208
paths = sysconfig.get_paths(scheme='venv', vars=config_vars) elif'posix_local'in scheme_names: # The Python that ships on Debian/Ubuntu varies the default scheme to # install to /usr/local # But it does not (yet) set the "venv" scheme. # If we're the Debian "posix_local" scheme is available, but "venv" # is not, we use "posix_prefix" instead which is venv-compatible there.
paths = sysconfig.get_paths(scheme='posix_prefix', vars=config_vars) elif'osx_framework_library'in scheme_names: # The Python that ships with the macOS developer tools varies the # default scheme depending on whether the ``sys.prefix`` is part of a framework. # But it does not (yet) set the "venv" scheme. # If the Apple-custom "osx_framework_library" scheme is available but "venv" # is not, we use "posix_prefix" instead which is venv-compatible there.
paths = sysconfig.get_paths(scheme='posix_prefix', vars=config_vars) else:
paths = sysconfig.get_paths(vars=config_vars)
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.