1
1
"""Build Environment used for isolation during sdist building
2
2
"""
3
3
4
- import contextlib
5
4
import logging
6
5
import os
7
6
import pathlib
10
9
from collections import OrderedDict
11
10
from sysconfig import get_paths
12
11
from types import TracebackType
13
- from typing import TYPE_CHECKING , Generator , Iterable , List , Optional , Set , Tuple , Type
12
+ from typing import TYPE_CHECKING , Iterable , List , Optional , Set , Tuple , Type
14
13
15
14
from pip ._vendor .certifi import where
16
15
from pip ._vendor .packaging .requirements import Requirement
28
27
29
28
logger = logging .getLogger (__name__ )
30
29
31
- PIP_RUNNER = """
32
- import importlib.util
33
- import os
34
- import runpy
35
- import sys
36
-
37
-
38
- class PipImportRedirectingFinder:
39
-
40
- @classmethod
41
- def find_spec(cls, fullname, path=None, target=None):
42
- if not fullname.startswith("pip."):
43
- return None
44
-
45
- # Import pip from the current source directory
46
- location = os.path.join({source!r}, *fullname.split("."))
47
- return importlib.util.spec_from_file_location(fullname, location)
48
-
49
-
50
- sys.meta_path.insert(0, PipImportRedirectingFinder())
51
- runpy.run_module("pip", run_name="__main__")
52
- """
53
-
54
30
55
31
class _Prefix :
56
32
def __init__ (self , path : str ) -> None :
@@ -63,26 +39,20 @@ def __init__(self, path: str) -> None:
63
39
self .lib_dirs = get_prefixed_libs (path )
64
40
65
41
66
- @contextlib .contextmanager
67
- def _create_runnable_pip () -> Generator [str , None , None ]:
68
- """Create a "pip runner" file.
42
+ def _get_runnable_pip () -> str :
43
+ """Get a file to pass to a Python executable, to run the currently-running pip.
69
44
70
- The runner file ensures that import for pip happens using the currently-running pip.
71
- It will be used to install requirements into the build environment.
45
+ This is used to run a pip subprocess, for installing requirements into the build
46
+ environment.
72
47
"""
73
48
source = pathlib .Path (pip_location ).resolve ().parent
74
49
75
- # Return the current instance if `source` is not a directory. It likely
76
- # means that this copy of pip is already standalone.
77
50
if not source .is_dir ():
78
- yield str (source )
79
- return
51
+ # This would happen if someone is using pip from inside a zip file. In that
52
+ # case, we can use that directly.
53
+ return str (source )
80
54
81
- with TempDirectory (kind = "standalone-pip" ) as tmp_dir :
82
- pip_runner = os .path .join (tmp_dir .path , "__pip-runner__.py" )
83
- with open (pip_runner , "w" , encoding = "utf8" ) as f :
84
- f .write (PIP_RUNNER .format (source = os .fsdecode (source )))
85
- yield pip_runner
55
+ return os .fsdecode (source / "__pip-runner__.py" )
86
56
87
57
88
58
class BuildEnvironment :
@@ -223,15 +193,13 @@ def install_requirements(
223
193
prefix .setup = True
224
194
if not requirements :
225
195
return
226
- with contextlib .ExitStack () as ctx :
227
- pip_runnable = ctx .enter_context (_create_runnable_pip ())
228
- self ._install_requirements (
229
- pip_runnable ,
230
- finder ,
231
- requirements ,
232
- prefix ,
233
- kind = kind ,
234
- )
196
+ self ._install_requirements (
197
+ _get_runnable_pip (),
198
+ finder ,
199
+ requirements ,
200
+ prefix ,
201
+ kind = kind ,
202
+ )
235
203
236
204
@staticmethod
237
205
def _install_requirements (
0 commit comments