"""Build Environment used for isolation during sdist building """
import logging import os import pathlib import site import sys import textwrap from collections import OrderedDict from types import TracebackType from typing import TYPE_CHECKING, Iterable, List, Optional, Set, Tuple, Type, Union
from pip._vendor.certifi import where from pip._vendor.packaging.requirements import Requirement from pip._vendor.packaging.version import Version
from pip import __file__ as pip_location from pip._internal.cli.spinners import open_spinner from pip._internal.locations import get_platlib, get_purelib, get_scheme from pip._internal.metadata import get_default_environment, get_environment from pip._internal.utils.subprocess import call_subprocess from pip._internal.utils.temp_dir import TempDirectory, tempdir_kinds
if TYPE_CHECKING: from pip._internal.index.package_finder import PackageFinder
logger = logging.getLogger(__name__)
def _dedup(a: str, b: str) -> Union[Tuple[str], Tuple[str, str]]: return (a, b) if a != b else (a,)
def get_runnable_pip() -> str: """Get a file to pass to a Python executable, to run the currently-running pip.
This is used to run a pip subprocess, for installing requirements into the build
environment. """
source = pathlib.Path(pip_location).resolve().parent
ifnot source.is_dir(): # This would happen if someone is using pip from inside a zip file. In that # case, we can use that directly. return str(source)
return os.fsdecode(source / "__pip-runner__.py")
def _get_system_sitepackages() -> Set[str]: """Get system site packages
Usually from site.getsitepackages,
but fallback on `get_purelib()/get_platlib()` if unavailable
(e.g. in a virtualenv created by virtualenv<20)
Returns normalized set of strings. """ if hasattr(site, "getsitepackages"):
system_sites = site.getsitepackages() else: # virtualenv < 20 overwrites site.py without getsitepackages # fallback on get_purelib/get_platlib. # this is known to miss things, but shouldn't in the cases # where getsitepackages() has been removed (inside a virtualenv)
system_sites = [get_purelib(), get_platlib()] return {os.path.normcase(path) for path in system_sites}
class BuildEnvironment: """Creates and manages an isolated environment to install build deps"""
# First, drop system-sites related paths.
original_sys_path = sys.path[:]
known_paths = set() for path in {system_sites!r}:
site.addsitedir(path, known_paths=known_paths)
system_paths = set(
os.path.normcase(path) for path in sys.path[len(original_sys_path):]
)
original_sys_path = [
path for path in original_sys_path if os.path.normcase(path) notin system_paths
]
sys.path = original_sys_path
# Second, add lib directories. # ensuring .pth file are processed. for path in {lib_dirs!r}: assertnot path in sys.path
site.addsitedir(path) """
).format(system_sites=system_sites, lib_dirs=self._lib_dirs)
)
def __enter__(self) -> None:
self._save_env = {
name: os.environ.get(name, None) for name in ("PATH", "PYTHONNOUSERSITE", "PYTHONPATH")
}
path = self._bin_dirs[:]
old_path = self._save_env["PATH"] if old_path:
path.extend(old_path.split(os.pathsep))
index_urls = finder.index_urls if index_urls:
args.extend(["-i", index_urls[0]]) for extra_index in index_urls[1:]:
args.extend(["--extra-index-url", extra_index]) else:
args.append("--no-index") for link in finder.find_links:
args.extend(["--find-links", link])
for host in finder.trusted_hosts:
args.extend(["--trusted-host", host]) if finder.allow_all_prereleases:
args.append("--pre") if finder.prefer_binary:
args.append("--prefer-binary")
args.append("--")
args.extend(requirements)
extra_environ = {"_PIP_STANDALONE_CERT": where()} with open_spinner(f"Installing {kind}") as spinner:
call_subprocess(
args,
command_desc=f"pip subprocess to install {kind}",
spinner=spinner,
extra_environ=extra_environ,
)
class NoOpBuildEnvironment(BuildEnvironment): """A no-op drop-in replacement for BuildEnvironment"""
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.