build_env.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. """Build Environment used for isolation during sdist building
  2. """
  3. import logging
  4. import os
  5. import pathlib
  6. import site
  7. import sys
  8. import textwrap
  9. from collections import OrderedDict
  10. from types import TracebackType
  11. from typing import TYPE_CHECKING, Iterable, List, Optional, Set, Tuple, Type, Union
  12. from pip._vendor.certifi import where
  13. from pip._vendor.packaging.requirements import Requirement
  14. from pip._vendor.packaging.version import Version
  15. from pip import __file__ as pip_location
  16. from pip._internal.cli.spinners import open_spinner
  17. from pip._internal.locations import get_platlib, get_purelib, get_scheme
  18. from pip._internal.metadata import get_default_environment, get_environment
  19. from pip._internal.utils.subprocess import call_subprocess
  20. from pip._internal.utils.temp_dir import TempDirectory, tempdir_kinds
  21. if TYPE_CHECKING:
  22. from pip._internal.index.package_finder import PackageFinder
  23. logger = logging.getLogger(__name__)
  24. def _dedup(a: str, b: str) -> Union[Tuple[str], Tuple[str, str]]:
  25. return (a, b) if a != b else (a,)
  26. class _Prefix:
  27. def __init__(self, path: str) -> None:
  28. self.path = path
  29. self.setup = False
  30. scheme = get_scheme("", prefix=path)
  31. self.bin_dir = scheme.scripts
  32. self.lib_dirs = _dedup(scheme.purelib, scheme.platlib)
  33. def get_runnable_pip() -> str:
  34. """Get a file to pass to a Python executable, to run the currently-running pip.
  35. This is used to run a pip subprocess, for installing requirements into the build
  36. environment.
  37. """
  38. source = pathlib.Path(pip_location).resolve().parent
  39. if not source.is_dir():
  40. # This would happen if someone is using pip from inside a zip file. In that
  41. # case, we can use that directly.
  42. return str(source)
  43. return os.fsdecode(source / "__pip-runner__.py")
  44. def _get_system_sitepackages() -> Set[str]:
  45. """Get system site packages
  46. Usually from site.getsitepackages,
  47. but fallback on `get_purelib()/get_platlib()` if unavailable
  48. (e.g. in a virtualenv created by virtualenv<20)
  49. Returns normalized set of strings.
  50. """
  51. if hasattr(site, "getsitepackages"):
  52. system_sites = site.getsitepackages()
  53. else:
  54. # virtualenv < 20 overwrites site.py without getsitepackages
  55. # fallback on get_purelib/get_platlib.
  56. # this is known to miss things, but shouldn't in the cases
  57. # where getsitepackages() has been removed (inside a virtualenv)
  58. system_sites = [get_purelib(), get_platlib()]
  59. return {os.path.normcase(path) for path in system_sites}
  60. class BuildEnvironment:
  61. """Creates and manages an isolated environment to install build deps"""
  62. def __init__(self) -> None:
  63. temp_dir = TempDirectory(kind=tempdir_kinds.BUILD_ENV, globally_managed=True)
  64. self._prefixes = OrderedDict(
  65. (name, _Prefix(os.path.join(temp_dir.path, name)))
  66. for name in ("normal", "overlay")
  67. )
  68. self._bin_dirs: List[str] = []
  69. self._lib_dirs: List[str] = []
  70. for prefix in reversed(list(self._prefixes.values())):
  71. self._bin_dirs.append(prefix.bin_dir)
  72. self._lib_dirs.extend(prefix.lib_dirs)
  73. # Customize site to:
  74. # - ensure .pth files are honored
  75. # - prevent access to system site packages
  76. system_sites = _get_system_sitepackages()
  77. self._site_dir = os.path.join(temp_dir.path, "site")
  78. if not os.path.exists(self._site_dir):
  79. os.mkdir(self._site_dir)
  80. with open(
  81. os.path.join(self._site_dir, "sitecustomize.py"), "w", encoding="utf-8"
  82. ) as fp:
  83. fp.write(
  84. textwrap.dedent(
  85. """
  86. import os, site, sys
  87. # First, drop system-sites related paths.
  88. original_sys_path = sys.path[:]
  89. known_paths = set()
  90. for path in {system_sites!r}:
  91. site.addsitedir(path, known_paths=known_paths)
  92. system_paths = set(
  93. os.path.normcase(path)
  94. for path in sys.path[len(original_sys_path):]
  95. )
  96. original_sys_path = [
  97. path for path in original_sys_path
  98. if os.path.normcase(path) not in system_paths
  99. ]
  100. sys.path = original_sys_path
  101. # Second, add lib directories.
  102. # ensuring .pth file are processed.
  103. for path in {lib_dirs!r}:
  104. assert not path in sys.path
  105. site.addsitedir(path)
  106. """
  107. ).format(system_sites=system_sites, lib_dirs=self._lib_dirs)
  108. )
  109. def __enter__(self) -> None:
  110. self._save_env = {
  111. name: os.environ.get(name, None)
  112. for name in ("PATH", "PYTHONNOUSERSITE", "PYTHONPATH")
  113. }
  114. path = self._bin_dirs[:]
  115. old_path = self._save_env["PATH"]
  116. if old_path:
  117. path.extend(old_path.split(os.pathsep))
  118. pythonpath = [self._site_dir]
  119. os.environ.update(
  120. {
  121. "PATH": os.pathsep.join(path),
  122. "PYTHONNOUSERSITE": "1",
  123. "PYTHONPATH": os.pathsep.join(pythonpath),
  124. }
  125. )
  126. def __exit__(
  127. self,
  128. exc_type: Optional[Type[BaseException]],
  129. exc_val: Optional[BaseException],
  130. exc_tb: Optional[TracebackType],
  131. ) -> None:
  132. for varname, old_value in self._save_env.items():
  133. if old_value is None:
  134. os.environ.pop(varname, None)
  135. else:
  136. os.environ[varname] = old_value
  137. def check_requirements(
  138. self, reqs: Iterable[str]
  139. ) -> Tuple[Set[Tuple[str, str]], Set[str]]:
  140. """Return 2 sets:
  141. - conflicting requirements: set of (installed, wanted) reqs tuples
  142. - missing requirements: set of reqs
  143. """
  144. missing = set()
  145. conflicting = set()
  146. if reqs:
  147. env = (
  148. get_environment(self._lib_dirs)
  149. if hasattr(self, "_lib_dirs")
  150. else get_default_environment()
  151. )
  152. for req_str in reqs:
  153. req = Requirement(req_str)
  154. # We're explicitly evaluating with an empty extra value, since build
  155. # environments are not provided any mechanism to select specific extras.
  156. if req.marker is not None and not req.marker.evaluate({"extra": ""}):
  157. continue
  158. dist = env.get_distribution(req.name)
  159. if not dist:
  160. missing.add(req_str)
  161. continue
  162. if isinstance(dist.version, Version):
  163. installed_req_str = f"{req.name}=={dist.version}"
  164. else:
  165. installed_req_str = f"{req.name}==={dist.version}"
  166. if not req.specifier.contains(dist.version, prereleases=True):
  167. conflicting.add((installed_req_str, req_str))
  168. # FIXME: Consider direct URL?
  169. return conflicting, missing
  170. def install_requirements(
  171. self,
  172. finder: "PackageFinder",
  173. requirements: Iterable[str],
  174. prefix_as_string: str,
  175. *,
  176. kind: str,
  177. ) -> None:
  178. prefix = self._prefixes[prefix_as_string]
  179. assert not prefix.setup
  180. prefix.setup = True
  181. if not requirements:
  182. return
  183. self._install_requirements(
  184. get_runnable_pip(),
  185. finder,
  186. requirements,
  187. prefix,
  188. kind=kind,
  189. )
  190. @staticmethod
  191. def _install_requirements(
  192. pip_runnable: str,
  193. finder: "PackageFinder",
  194. requirements: Iterable[str],
  195. prefix: _Prefix,
  196. *,
  197. kind: str,
  198. ) -> None:
  199. args: List[str] = [
  200. sys.executable,
  201. pip_runnable,
  202. "install",
  203. "--ignore-installed",
  204. "--no-user",
  205. "--prefix",
  206. prefix.path,
  207. "--no-warn-script-location",
  208. ]
  209. if logger.getEffectiveLevel() <= logging.DEBUG:
  210. args.append("-v")
  211. for format_control in ("no_binary", "only_binary"):
  212. formats = getattr(finder.format_control, format_control)
  213. args.extend(
  214. (
  215. "--" + format_control.replace("_", "-"),
  216. ",".join(sorted(formats or {":none:"})),
  217. )
  218. )
  219. index_urls = finder.index_urls
  220. if index_urls:
  221. args.extend(["-i", index_urls[0]])
  222. for extra_index in index_urls[1:]:
  223. args.extend(["--extra-index-url", extra_index])
  224. else:
  225. args.append("--no-index")
  226. for link in finder.find_links:
  227. args.extend(["--find-links", link])
  228. for host in finder.trusted_hosts:
  229. args.extend(["--trusted-host", host])
  230. if finder.allow_all_prereleases:
  231. args.append("--pre")
  232. if finder.prefer_binary:
  233. args.append("--prefer-binary")
  234. args.append("--")
  235. args.extend(requirements)
  236. extra_environ = {"_PIP_STANDALONE_CERT": where()}
  237. with open_spinner(f"Installing {kind}") as spinner:
  238. call_subprocess(
  239. args,
  240. command_desc=f"pip subprocess to install {kind}",
  241. spinner=spinner,
  242. extra_environ=extra_environ,
  243. )
  244. class NoOpBuildEnvironment(BuildEnvironment):
  245. """A no-op drop-in replacement for BuildEnvironment"""
  246. def __init__(self) -> None:
  247. pass
  248. def __enter__(self) -> None:
  249. pass
  250. def __exit__(
  251. self,
  252. exc_type: Optional[Type[BaseException]],
  253. exc_val: Optional[BaseException],
  254. exc_tb: Optional[TracebackType],
  255. ) -> None:
  256. pass
  257. def cleanup(self) -> None:
  258. pass
  259. def install_requirements(
  260. self,
  261. finder: "PackageFinder",
  262. requirements: Iterable[str],
  263. prefix_as_string: str,
  264. *,
  265. kind: str,
  266. ) -> None:
  267. raise NotImplementedError()