You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

build_env.py 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. """Build Environment used for isolation during sdist building
  2. """
  3. # The following comment should be removed at some point in the future.
  4. # mypy: strict-optional=False
  5. # mypy: disallow-untyped-defs=False
  6. import logging
  7. import os
  8. import sys
  9. import textwrap
  10. from collections import OrderedDict
  11. from distutils.sysconfig import get_python_lib
  12. from sysconfig import get_paths
  13. from pip._vendor.pkg_resources import Requirement, VersionConflict, WorkingSet
  14. from pip import __file__ as pip_location
  15. from pip._internal.utils.subprocess import call_subprocess
  16. from pip._internal.utils.temp_dir import TempDirectory
  17. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  18. from pip._internal.utils.ui import open_spinner
  19. if MYPY_CHECK_RUNNING:
  20. from typing import Tuple, Set, Iterable, Optional, List
  21. from pip._internal.index import PackageFinder
  22. logger = logging.getLogger(__name__)
  23. class _Prefix:
  24. def __init__(self, path):
  25. # type: (str) -> None
  26. self.path = path
  27. self.setup = False
  28. self.bin_dir = get_paths(
  29. 'nt' if os.name == 'nt' else 'posix_prefix',
  30. vars={'base': path, 'platbase': path}
  31. )['scripts']
  32. # Note: prefer distutils' sysconfig to get the
  33. # library paths so PyPy is correctly supported.
  34. purelib = get_python_lib(plat_specific=False, prefix=path)
  35. platlib = get_python_lib(plat_specific=True, prefix=path)
  36. if purelib == platlib:
  37. self.lib_dirs = [purelib]
  38. else:
  39. self.lib_dirs = [purelib, platlib]
  40. class BuildEnvironment(object):
  41. """Creates and manages an isolated environment to install build deps
  42. """
  43. def __init__(self):
  44. # type: () -> None
  45. self._temp_dir = TempDirectory(kind="build-env")
  46. self._prefixes = OrderedDict((
  47. (name, _Prefix(os.path.join(self._temp_dir.path, name)))
  48. for name in ('normal', 'overlay')
  49. ))
  50. self._bin_dirs = [] # type: List[str]
  51. self._lib_dirs = [] # type: List[str]
  52. for prefix in reversed(list(self._prefixes.values())):
  53. self._bin_dirs.append(prefix.bin_dir)
  54. self._lib_dirs.extend(prefix.lib_dirs)
  55. # Customize site to:
  56. # - ensure .pth files are honored
  57. # - prevent access to system site packages
  58. system_sites = {
  59. os.path.normcase(site) for site in (
  60. get_python_lib(plat_specific=False),
  61. get_python_lib(plat_specific=True),
  62. )
  63. }
  64. self._site_dir = os.path.join(self._temp_dir.path, 'site')
  65. if not os.path.exists(self._site_dir):
  66. os.mkdir(self._site_dir)
  67. with open(os.path.join(self._site_dir, 'sitecustomize.py'), 'w') as fp:
  68. fp.write(textwrap.dedent(
  69. '''
  70. import os, site, sys
  71. # First, drop system-sites related paths.
  72. original_sys_path = sys.path[:]
  73. known_paths = set()
  74. for path in {system_sites!r}:
  75. site.addsitedir(path, known_paths=known_paths)
  76. system_paths = set(
  77. os.path.normcase(path)
  78. for path in sys.path[len(original_sys_path):]
  79. )
  80. original_sys_path = [
  81. path for path in original_sys_path
  82. if os.path.normcase(path) not in system_paths
  83. ]
  84. sys.path = original_sys_path
  85. # Second, add lib directories.
  86. # ensuring .pth file are processed.
  87. for path in {lib_dirs!r}:
  88. assert not path in sys.path
  89. site.addsitedir(path)
  90. '''
  91. ).format(system_sites=system_sites, lib_dirs=self._lib_dirs))
  92. def __enter__(self):
  93. self._save_env = {
  94. name: os.environ.get(name, None)
  95. for name in ('PATH', 'PYTHONNOUSERSITE', 'PYTHONPATH')
  96. }
  97. path = self._bin_dirs[:]
  98. old_path = self._save_env['PATH']
  99. if old_path:
  100. path.extend(old_path.split(os.pathsep))
  101. pythonpath = [self._site_dir]
  102. os.environ.update({
  103. 'PATH': os.pathsep.join(path),
  104. 'PYTHONNOUSERSITE': '1',
  105. 'PYTHONPATH': os.pathsep.join(pythonpath),
  106. })
  107. def __exit__(self, exc_type, exc_val, exc_tb):
  108. for varname, old_value in self._save_env.items():
  109. if old_value is None:
  110. os.environ.pop(varname, None)
  111. else:
  112. os.environ[varname] = old_value
  113. def cleanup(self):
  114. # type: () -> None
  115. self._temp_dir.cleanup()
  116. def check_requirements(self, reqs):
  117. # type: (Iterable[str]) -> Tuple[Set[Tuple[str, str]], Set[str]]
  118. """Return 2 sets:
  119. - conflicting requirements: set of (installed, wanted) reqs tuples
  120. - missing requirements: set of reqs
  121. """
  122. missing = set()
  123. conflicting = set()
  124. if reqs:
  125. ws = WorkingSet(self._lib_dirs)
  126. for req in reqs:
  127. try:
  128. if ws.find(Requirement.parse(req)) is None:
  129. missing.add(req)
  130. except VersionConflict as e:
  131. conflicting.add((str(e.args[0].as_requirement()),
  132. str(e.args[1])))
  133. return conflicting, missing
  134. def install_requirements(
  135. self,
  136. finder, # type: PackageFinder
  137. requirements, # type: Iterable[str]
  138. prefix_as_string, # type: str
  139. message # type: Optional[str]
  140. ):
  141. # type: (...) -> None
  142. prefix = self._prefixes[prefix_as_string]
  143. assert not prefix.setup
  144. prefix.setup = True
  145. if not requirements:
  146. return
  147. args = [
  148. sys.executable, os.path.dirname(pip_location), 'install',
  149. '--ignore-installed', '--no-user', '--prefix', prefix.path,
  150. '--no-warn-script-location',
  151. ] # type: List[str]
  152. if logger.getEffectiveLevel() <= logging.DEBUG:
  153. args.append('-v')
  154. for format_control in ('no_binary', 'only_binary'):
  155. formats = getattr(finder.format_control, format_control)
  156. args.extend(('--' + format_control.replace('_', '-'),
  157. ','.join(sorted(formats or {':none:'}))))
  158. index_urls = finder.index_urls
  159. if index_urls:
  160. args.extend(['-i', index_urls[0]])
  161. for extra_index in index_urls[1:]:
  162. args.extend(['--extra-index-url', extra_index])
  163. else:
  164. args.append('--no-index')
  165. for link in finder.find_links:
  166. args.extend(['--find-links', link])
  167. for host in finder.trusted_hosts:
  168. args.extend(['--trusted-host', host])
  169. if finder.allow_all_prereleases:
  170. args.append('--pre')
  171. args.append('--')
  172. args.extend(requirements)
  173. with open_spinner(message) as spinner:
  174. call_subprocess(args, spinner=spinner)
  175. class NoOpBuildEnvironment(BuildEnvironment):
  176. """A no-op drop-in replacement for BuildEnvironment
  177. """
  178. def __init__(self):
  179. pass
  180. def __enter__(self):
  181. pass
  182. def __exit__(self, exc_type, exc_val, exc_tb):
  183. pass
  184. def cleanup(self):
  185. pass
  186. def install_requirements(self, finder, requirements, prefix, message):
  187. raise NotImplementedError()