Issue #19728: add private ensurepip._uninstall CLI

MvL would like to be able to preserve CPython's existing clean
uninstall behaviour on Windows before enabling the pip
installation option by default.

This private CLI means running "python -m ensurepip._uninstall"
will remove pip and setuptools before proceeding with the rest
of the uninstallation process.

If the version of pip differs from the one bootstrapped by
CPython, then the uninstallation helper will leave it alone
(just like any other pip installed packages)
This commit is contained in:
Nick Coghlan 2013-11-30 17:15:09 +10:00
parent 1b1b1789d0
commit fdf3a620a2
3 changed files with 150 additions and 19 deletions

View file

@ -20,9 +20,10 @@ _PROJECTS = [
]
def _run_pip(args, additional_paths):
def _run_pip(args, additional_paths=None):
# Add our bundled software to the sys.path so we can import it
sys.path = additional_paths + sys.path
if additional_paths is not None:
sys.path = additional_paths + sys.path
# Install the bundled software
import pip
@ -90,3 +91,24 @@ def bootstrap(*, root=None, upgrade=False, user=False,
args += ["-" + "v" * verbosity]
_run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
def _uninstall(*, verbosity=0):
"""Helper to support a clean default uninstall process on Windows"""
# Nothing to do if pip was never installed, or has been removed
try:
import pip
except ImportError:
return
# If the pip version doesn't match the bundled one, leave it alone
if pip.__version__ != _PIP_VERSION:
msg = ("ensurepip will only uninstall a matching pip "
"({!r} installed, {!r} bundled)")
raise RuntimeError(msg.format(pip.__version__, _PIP_VERSION))
# Construct the arguments to be passed to the pip command
args = ["uninstall", "-y"]
if verbosity:
args += ["-" + "v" * verbosity]
_run_pip(args + [p[0] for p in reversed(_PROJECTS)])