"""Maintain and use a protected copy of pip for use during development.

The protected copy of pip can be used to manipulate the environment while keeping a
potentially-non-functional installation of in-development pip in the development virtual
environment.

This allows for setting up the test environments and exercising the in-development code,
even when it is not functional-enough to install the packages for setting up the
environment that it is being used it.
"""

import os
import shutil
import subprocess
import sys
from glob import glob
from typing import List

VIRTUAL_ENV = os.environ["VIRTUAL_ENV"]
PROTECTED_PIP_DIR = os.path.join(VIRTUAL_ENV, "pip")


def _setup_protected_pip() -> None:
    # This setup happens before any development version of pip is installed in this
    # environment. So, at this point, the existing pip installation should be from a
    # stable release and can be safely used to create the protected copy.
    subprocess.check_call(
        [
            sys.executable,
            "-m",
            "pip",
            "--disable-pip-version-check",
            "install",
            "-t",
            PROTECTED_PIP_DIR,
            "pip",
        ]
    )
    # Make it impossible for pip (and other Python tooling) to discover this protected
    # installation of pip using the metadata, by deleting the metadata.
    shutil.rmtree(glob(os.path.join(PROTECTED_PIP_DIR, "pip-*.dist-info"))[0])


def main(args: List[str]) -> None:
    # If we don't have a protected pip, let's set it up.
    if not os.path.exists(PROTECTED_PIP_DIR):
        _setup_protected_pip()

    # Run Python, with the protected pip copy on PYTHONPATH.
    old_PYTHONPATH_entries = os.environ.get("PYTHONPATH", "").split(os.pathsep)
    new_PYTHONPATH = os.pathsep.join([PROTECTED_PIP_DIR] + old_PYTHONPATH_entries)

    subprocess.check_call(
        [sys.executable, "-m", "pip"] + args,
        env={"PYTHONPATH": new_PYTHONPATH},
    )


if __name__ == "__main__":
    main(sys.argv[1:])

@@ -1,477 +1,4 @@
-%22%22%22Maintain and use a protected copy of pip for use during development.%0A%0AThe protected copy of pip can be used to manipulate the environment while keeping a%0Apotentially-non-functional installation of in-development pip in the development virtual%0Aenvironment.%0A%0AThis allows for setting up the test environments and exercising the in-development code,%0Aeven when it is not functional-enough to install the packages for setting up the%0Aenvironment that it is being used it.%0A%22%22%22%0A%0A
 impo
@@ -133,25 +133,19 @@
 L_ENV%22%5D%0A
-PROTECTED
+TOX
 _PIP_DIR
@@ -190,283 +190,146 @@
 def 
-_setup_protected_pip() -%3E None:%0A    # This setup happens before any development version of pip is installed in this%0A    # environment. So, at this point, the existing pip installation should be from a%0A    # stable release and can be safely used to create the protected copy.%0A
+pip(args: List%5Bstr%5D) -%3E None:%0A    # First things first, get a recent (stable) version of pip.%0A    if not os.path.exists(TOX_PIP_DIR):%0A    
     
@@ -343,32 +343,36 @@
 ess.check_call(%0A
+    
         %5B%0A      
@@ -377,16 +377,20 @@
         
+    
 sys.exec
@@ -409,16 +409,20 @@
         
+    
 %22-m%22,%0A  
@@ -423,39 +423,47 @@
 m%22,%0A            
+   
+ 
 %22pip%22,%0A
+    
             %22--d
@@ -501,16 +501,20 @@
         
+    
 %22install
@@ -528,16 +528,20 @@
         
+  
+  
 %22-t%22,%0A  
@@ -550,25 +550,23 @@
         
-PROTECTED
+    TOX
 _PIP_DIR
@@ -567,16 +567,20 @@
 IP_DIR,%0A
+    
         
@@ -602,175 +602,28 @@
     
-%5D%0A
     
-)
+%5D
 %0A    
-# Make it impossible for pip (and other Python tooling) to discover this protected%0A    # installation of pip using the metadata, by deleting the metadata.%0A
+    )%0A    
     
@@ -654,25 +654,19 @@
 th.join(
-PROTECTED
+TOX
 _PIP_DIR
@@ -695,404 +695,246 @@
 0%5D)%0A
-%0A%0Adef main(args: List%5Bstr%5D) -%3E None:%0A    # If we don't have a protected pip, let's set it up.%0A    if not os.path.exists(PROTECTED_PIP_DIR):%0A        _setup_protected_pip()%0A%0A    # Run Python, with the protected pip copy on PYTHONPATH.%0A    old_PYTHONPATH_entries = os.environ.get(%22PYTHONPATH%22, %22%22).split(os.pathsep)%0A    new_PYTHONPATH = os.pathsep.join(%5BPROTECTED_PIP_DIR%5D + old_PYTHONPATH_entries)%0A
+    # And use that version.%0A    pypath_env = os.environ.get(%22PYTHONPATH%22)%0A    pypath = pypath_env.split(os.pathsep) if pypath_env is not None else %5B%5D%0A    pypath.insert(0, TOX_PIP_DIR)%0A    os.environ%5B%22PYTHONPATH%22%5D = os.pathsep.join(pypath)
 %0A   
@@ -956,25 +956,16 @@
 ck_call(
-%0A        
 %5Bsys.exe
@@ -996,58 +996,8 @@
 args
-,%0A        env=%7B%22PYTHONPATH%22: new_PYTHONPATH%7D,%0A    
 )%0A%0A%0A
@@ -1027,20 +1027,19 @@
 _%22:%0A    
-main
+pip
 (sys.arg

Revert "Add additional context to `protected_pip.py` about how it works"