build_env.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. """Build Environment used for isolation during sdist building
  2. """
  3. import contextlib
  4. import logging
  5. import os
  6. import pathlib
  7. import sys
  8. import textwrap
  9. import zipfile
  10. from collections import OrderedDict
  11. from sysconfig import get_paths
  12. from types import TracebackType
  13. from typing import TYPE_CHECKING, Iterable, Iterator, List, Optional, Set, Tuple, Type
  14. from pip._vendor.certifi import where
  15. from pip._vendor.packaging.requirements import Requirement
  16. from pip._vendor.packaging.version import Version
  17. from pip import __file__ as pip_location
  18. from pip._internal.cli.spinners import open_spinner
  19. from pip._internal.locations import get_platlib, get_prefixed_libs, get_purelib
  20. from pip._internal.metadata import get_environment
  21. from pip._internal.utils.subprocess import call_subprocess
  22. from pip._internal.utils.temp_dir import TempDirectory, tempdir_kinds
  23. if TYPE_CHECKING:
  24. from pip._internal.index.package_finder import PackageFinder
  25. logger = logging.getLogger(__name__)
  26. class _Prefix:
  27. def __init__(self, path: str) -> None:
  28. self.path = path
  29. self.setup = False
  30. self.bin_dir = get_paths(
  31. "nt" if os.name == "nt" else "posix_prefix",
  32. vars={"base": path, "platbase": path},
  33. )["scripts"]
  34. self.lib_dirs = get_prefixed_libs(path)
  35. @contextlib.contextmanager
  36. def _create_standalone_pip() -> Iterator[str]:
  37. """Create a "standalone pip" zip file.
  38. The zip file's content is identical to the currently-running pip.
  39. It will be used to install requirements into the build environment.
  40. """
  41. source = pathlib.Path(pip_location).resolve().parent
  42. # Return the current instance if `source` is not a directory. We can't build
  43. # a zip from this, and it likely means the instance is already standalone.
  44. if not source.is_dir():
  45. yield str(source)
  46. return
  47. with TempDirectory(kind="standalone-pip") as tmp_dir:
  48. pip_zip = os.path.join(tmp_dir.path, "__env_pip__.zip")
  49. kwargs = {}
  50. if sys.version_info >= (3, 8):
  51. kwargs["strict_timestamps"] = False
  52. with zipfile.ZipFile(pip_zip, "w", **kwargs) as zf:
  53. for child in source.rglob("*"):
  54. zf.write(child, child.relative_to(source.parent).as_posix())
  55. yield os.path.join(pip_zip, "pip")
  56. class BuildEnvironment:
  57. """Creates and manages an isolated environment to install build deps"""
  58. def __init__(self) -> None:
  59. temp_dir = TempDirectory(kind=tempdir_kinds.BUILD_ENV, globally_managed=True)
  60. self._prefixes = OrderedDict(
  61. (name, _Prefix(os.path.join(temp_dir.path, name)))
  62. for name in ("normal", "overlay")
  63. )
  64. self._bin_dirs: List[str] = []
  65. self._lib_dirs: List[str] = []
  66. for prefix in reversed(list(self._prefixes.values())):
  67. self._bin_dirs.append(prefix.bin_dir)
  68. self._lib_dirs.extend(prefix.lib_dirs)
  69. # Customize site to:
  70. # - ensure .pth files are honored
  71. # - prevent access to system site packages
  72. system_sites = {
  73. os.path.normcase(site) for site in (get_purelib(), get_platlib())
  74. }
  75. self._site_dir = os.path.join(temp_dir.path, "site")
  76. if not os.path.exists(self._site_dir):
  77. os.mkdir(self._site_dir)
  78. with open(os.path.join(self._site_dir, "sitecustomize.py"), "w") as fp:
  79. fp.write(
  80. textwrap.dedent(
  81. """
  82. import os, site, sys
  83. # First, drop system-sites related paths.
  84. original_sys_path = sys.path[:]
  85. known_paths = set()
  86. for path in {system_sites!r}:
  87. site.addsitedir(path, known_paths=known_paths)
  88. system_paths = set(
  89. os.path.normcase(path)
  90. for path in sys.path[len(original_sys_path):]
  91. )
  92. original_sys_path = [
  93. path for path in original_sys_path
  94. if os.path.normcase(path) not in system_paths
  95. ]
  96. sys.path = original_sys_path
  97. # Second, add lib directories.
  98. # ensuring .pth file are processed.
  99. for path in {lib_dirs!r}:
  100. assert not path in sys.path
  101. site.addsitedir(path)
  102. """
  103. ).format(system_sites=system_sites, lib_dirs=self._lib_dirs)
  104. )
  105. def __enter__(self) -> None:
  106. self._save_env = {
  107. name: os.environ.get(name, None)
  108. for name in ("PATH", "PYTHONNOUSERSITE", "PYTHONPATH")
  109. }
  110. path = self._bin_dirs[:]
  111. old_path = self._save_env["PATH"]
  112. if old_path:
  113. path.extend(old_path.split(os.pathsep))
  114. pythonpath = [self._site_dir]
  115. os.environ.update(
  116. {
  117. "PATH": os.pathsep.join(path),
  118. "PYTHONNOUSERSITE": "1",
  119. "PYTHONPATH": os.pathsep.join(pythonpath),
  120. }
  121. )
  122. def __exit__(
  123. self,
  124. exc_type: Optional[Type[BaseException]],
  125. exc_val: Optional[BaseException],
  126. exc_tb: Optional[TracebackType],
  127. ) -> None:
  128. for varname, old_value in self._save_env.items():
  129. if old_value is None:
  130. os.environ.pop(varname, None)
  131. else:
  132. os.environ[varname] = old_value
  133. def check_requirements(
  134. self, reqs: Iterable[str]
  135. ) -> Tuple[Set[Tuple[str, str]], Set[str]]:
  136. """Return 2 sets:
  137. - conflicting requirements: set of (installed, wanted) reqs tuples
  138. - missing requirements: set of reqs
  139. """
  140. missing = set()
  141. conflicting = set()
  142. if reqs:
  143. env = get_environment(self._lib_dirs)
  144. for req_str in reqs:
  145. req = Requirement(req_str)
  146. dist = env.get_distribution(req.name)
  147. if not dist:
  148. missing.add(req_str)
  149. continue
  150. if isinstance(dist.version, Version):
  151. installed_req_str = f"{req.name}=={dist.version}"
  152. else:
  153. installed_req_str = f"{req.name}==={dist.version}"
  154. if dist.version not in req.specifier:
  155. conflicting.add((installed_req_str, req_str))
  156. # FIXME: Consider direct URL?
  157. return conflicting, missing
  158. def install_requirements(
  159. self,
  160. finder: "PackageFinder",
  161. requirements: Iterable[str],
  162. prefix_as_string: str,
  163. message: str,
  164. ) -> None:
  165. prefix = self._prefixes[prefix_as_string]
  166. assert not prefix.setup
  167. prefix.setup = True
  168. if not requirements:
  169. return
  170. with contextlib.ExitStack() as ctx:
  171. # TODO: Remove this block when dropping 3.6 support. Python 3.6
  172. # lacks importlib.resources and pep517 has issues loading files in
  173. # a zip, so we fallback to the "old" method by adding the current
  174. # pip directory to the child process's sys.path.
  175. if sys.version_info < (3, 7):
  176. pip_runnable = os.path.dirname(pip_location)
  177. else:
  178. pip_runnable = ctx.enter_context(_create_standalone_pip())
  179. self._install_requirements(
  180. pip_runnable,
  181. finder,
  182. requirements,
  183. prefix,
  184. message,
  185. )
  186. @staticmethod
  187. def _install_requirements(
  188. pip_runnable: str,
  189. finder: "PackageFinder",
  190. requirements: Iterable[str],
  191. prefix: _Prefix,
  192. message: str,
  193. ) -> None:
  194. args: List[str] = [
  195. sys.executable,
  196. pip_runnable,
  197. "install",
  198. "--ignore-installed",
  199. "--no-user",
  200. "--prefix",
  201. prefix.path,
  202. "--no-warn-script-location",
  203. ]
  204. if logger.getEffectiveLevel() <= logging.DEBUG:
  205. args.append("-v")
  206. for format_control in ("no_binary", "only_binary"):
  207. formats = getattr(finder.format_control, format_control)
  208. args.extend(
  209. (
  210. "--" + format_control.replace("_", "-"),
  211. ",".join(sorted(formats or {":none:"})),
  212. )
  213. )
  214. index_urls = finder.index_urls
  215. if index_urls:
  216. args.extend(["-i", index_urls[0]])
  217. for extra_index in index_urls[1:]:
  218. args.extend(["--extra-index-url", extra_index])
  219. else:
  220. args.append("--no-index")
  221. for link in finder.find_links:
  222. args.extend(["--find-links", link])
  223. for host in finder.trusted_hosts:
  224. args.extend(["--trusted-host", host])
  225. if finder.allow_all_prereleases:
  226. args.append("--pre")
  227. if finder.prefer_binary:
  228. args.append("--prefer-binary")
  229. args.append("--")
  230. args.extend(requirements)
  231. extra_environ = {"_PIP_STANDALONE_CERT": where()}
  232. with open_spinner(message) as spinner:
  233. call_subprocess(args, spinner=spinner, extra_environ=extra_environ)
  234. class NoOpBuildEnvironment(BuildEnvironment):
  235. """A no-op drop-in replacement for BuildEnvironment"""
  236. def __init__(self) -> None:
  237. pass
  238. def __enter__(self) -> None:
  239. pass
  240. def __exit__(
  241. self,
  242. exc_type: Optional[Type[BaseException]],
  243. exc_val: Optional[BaseException],
  244. exc_tb: Optional[TracebackType],
  245. ) -> None:
  246. pass
  247. def cleanup(self) -> None:
  248. pass
  249. def install_requirements(
  250. self,
  251. finder: "PackageFinder",
  252. requirements: Iterable[str],
  253. prefix_as_string: str,
  254. message: str,
  255. ) -> None:
  256. raise NotImplementedError()