|
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 |
| - |
12 | 1 | import os
|
13 | 2 | import shutil
|
14 | 3 | import subprocess
|
|
17 | 6 | from typing import List
|
18 | 7 |
|
19 | 8 | VIRTUAL_ENV = os.environ["VIRTUAL_ENV"]
|
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 |
| - ) |
| 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) |
57 | 34 |
|
58 | 35 |
|
59 | 36 | if __name__ == "__main__":
|
60 |
| - main(sys.argv[1:]) |
| 37 | + pip(sys.argv[1:]) |
0 commit comments