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