Skip to content

Commit 84ee57d

Browse files
committed
Add additional context to protected_pip.py about how it works
This should make it easier to understand what this file is doing, and when it is useful.
1 parent 1a2d2a1 commit 84ee57d

File tree

1 file changed

+49
-26
lines changed

1 file changed

+49
-26
lines changed

tools/protected_pip.py

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
"""Maintain and use a protected copy of pip for use during development.
2+
3+
The protected copy of pip can be used to manipulate the environment while keeping a
4+
potentially-non-functional installation of in-development pip in the development virtual
5+
environment.
6+
7+
This allows for setting up the test environments and exercising the in-development code,
8+
even when it is not functional-enough to install the packages for setting up the
9+
environment that it is being used it.
10+
"""
11+
112
import os
213
import shutil
314
import subprocess
@@ -6,32 +17,44 @@
617
from typing import List
718

819
VIRTUAL_ENV = os.environ["VIRTUAL_ENV"]
9-
TOX_PIP_DIR = os.path.join(VIRTUAL_ENV, "pip")
10-
11-
12-
def pip(args: List[str]) -> None:
13-
# First things first, get a recent (stable) version of pip.
14-
if not os.path.exists(TOX_PIP_DIR):
15-
subprocess.check_call(
16-
[
17-
sys.executable,
18-
"-m",
19-
"pip",
20-
"--disable-pip-version-check",
21-
"install",
22-
"-t",
23-
TOX_PIP_DIR,
24-
"pip",
25-
]
26-
)
27-
shutil.rmtree(glob(os.path.join(TOX_PIP_DIR, "pip-*.dist-info"))[0])
28-
# And use that version.
29-
pypath_env = os.environ.get("PYTHONPATH")
30-
pypath = pypath_env.split(os.pathsep) if pypath_env is not None else []
31-
pypath.insert(0, TOX_PIP_DIR)
32-
os.environ["PYTHONPATH"] = os.pathsep.join(pypath)
33-
subprocess.check_call([sys.executable, "-m", "pip"] + args)
20+
PROTECTED_PIP_DIR = os.path.join(VIRTUAL_ENV, "pip")
21+
22+
23+
def _setup_protected_pip() -> None:
24+
# This setup happens before any development version of pip is installed in this
25+
# environment. So, at this point, the existing pip installation should be from a
26+
# stable release and can be safely used to create the protected copy.
27+
subprocess.check_call(
28+
[
29+
sys.executable,
30+
"-m",
31+
"pip",
32+
"--disable-pip-version-check",
33+
"install",
34+
"-t",
35+
PROTECTED_PIP_DIR,
36+
"pip",
37+
]
38+
)
39+
# Make it impossible for pip (and other Python tooling) to discover this protected
40+
# installation of pip using the metadata, by deleting the metadata.
41+
shutil.rmtree(glob(os.path.join(PROTECTED_PIP_DIR, "pip-*.dist-info"))[0])
42+
43+
44+
def main(args: List[str]) -> None:
45+
# If we don't have a protected pip, let's set it up.
46+
if not os.path.exists(PROTECTED_PIP_DIR):
47+
_setup_protected_pip()
48+
49+
# Run Python, with the protected pip copy on PYTHONPATH.
50+
old_PYTHONPATH_entries = os.environ.get("PYTHONPATH", "").split(os.pathsep)
51+
new_PYTHONPATH = os.pathsep.join([PROTECTED_PIP_DIR] + old_PYTHONPATH_entries)
52+
53+
subprocess.check_call(
54+
[sys.executable, "-m", "pip"] + args,
55+
env={"PYTHONPATH": new_PYTHONPATH},
56+
)
3457

3558

3659
if __name__ == "__main__":
37-
pip(sys.argv[1:])
60+
main(sys.argv[1:])

0 commit comments

Comments
 (0)