[Python-checkins] cpython: Issue #19728: add private ensurepip._uninstall CLI

nick.coghlan python-checkins at python.org
Sat Nov 30 08:15:33 CET 2013


http://hg.python.org/cpython/rev/7dc4bf283857
changeset:   87649:7dc4bf283857
user:        Nick Coghlan <ncoghlan at gmail.com>
date:        Sat Nov 30 17:15:09 2013 +1000
summary:
  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)

files:
  Lib/ensurepip/__init__.py  |  26 ++++++++-
  Lib/test/test_ensurepip.py |  75 ++++++++++++++++++++++++++
  Lib/test/test_venv.py      |  66 +++++++++++++++++-----
  3 files changed, 149 insertions(+), 18 deletions(-)


diff --git a/Lib/ensurepip/__init__.py b/Lib/ensurepip/__init__.py
--- a/Lib/ensurepip/__init__.py
+++ b/Lib/ensurepip/__init__.py
@@ -20,9 +20,10 @@
 ]
 
 
-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 @@
             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)])
diff --git a/Lib/test/test_ensurepip.py b/Lib/test/test_ensurepip.py
--- a/Lib/test/test_ensurepip.py
+++ b/Lib/test/test_ensurepip.py
@@ -4,6 +4,8 @@
 import test.support
 import os
 import os.path
+import contextlib
+import sys
 
 
 class TestEnsurePipVersion(unittest.TestCase):
@@ -122,6 +124,79 @@
     def test_altinstall_default_pip_conflict(self):
         with self.assertRaises(ValueError):
             ensurepip.bootstrap(altinstall=True, default_pip=True)
+        self.run_pip.assert_not_called()
+
+ at contextlib.contextmanager
+def fake_pip(version=ensurepip._PIP_VERSION):
+    if version is None:
+        pip = None
+    else:
+        class FakePip():
+            __version__ = version
+        pip = FakePip()
+    sentinel = object()
+    orig_pip = sys.modules.get("pip", sentinel)
+    sys.modules["pip"] = pip
+    try:
+        yield pip
+    finally:
+        if orig_pip is sentinel:
+            del sys.modules["pip"]
+        else:
+            sys.modules["pip"] = orig_pip
+
+class TestUninstall(unittest.TestCase):
+
+    def setUp(self):
+        run_pip_patch = unittest.mock.patch("ensurepip._run_pip")
+        self.run_pip = run_pip_patch.start()
+        self.addCleanup(run_pip_patch.stop)
+
+    def test_uninstall_skipped_when_not_installed(self):
+        with fake_pip(None):
+            ensurepip._uninstall()
+        self.run_pip.assert_not_called()
+
+    def test_uninstall_fails_with_wrong_version(self):
+        with fake_pip("not a valid version"):
+            with self.assertRaises(RuntimeError):
+                ensurepip._uninstall()
+        self.run_pip.assert_not_called()
+
+
+    def test_uninstall(self):
+        with fake_pip():
+            ensurepip._uninstall()
+
+        self.run_pip.assert_called_once_with(
+            ["uninstall", "-y", "pip", "setuptools"]
+        )
+
+    def test_uninstall_with_verbosity_1(self):
+        with fake_pip():
+            ensurepip._uninstall(verbosity=1)
+
+        self.run_pip.assert_called_once_with(
+            ["uninstall", "-y", "-v", "pip", "setuptools"]
+        )
+
+    def test_uninstall_with_verbosity_2(self):
+        with fake_pip():
+            ensurepip._uninstall(verbosity=2)
+
+        self.run_pip.assert_called_once_with(
+            ["uninstall", "-y", "-vv", "pip", "setuptools"]
+        )
+
+    def test_uninstall_with_verbosity_3(self):
+        with fake_pip():
+            ensurepip._uninstall(verbosity=3)
+
+        self.run_pip.assert_called_once_with(
+            ["uninstall", "-y", "-vvv", "pip", "setuptools"]
+        )
+
+
 
 
 if __name__ == "__main__":
diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py
--- a/Lib/test/test_venv.py
+++ b/Lib/test/test_venv.py
@@ -14,6 +14,7 @@
 import tempfile
 from test.support import (captured_stdout, captured_stderr, run_unittest,
                           can_symlink, EnvironmentVarGuard)
+import textwrap
 import unittest
 import venv
 try:
@@ -258,30 +259,31 @@
 @skipInVenv
 class EnsurePipTest(BaseTest):
     """Test venv module installation of pip."""
-
-    def test_no_pip_by_default(self):
-        shutil.rmtree(self.env_dir)
-        self.run_with_capture(venv.create, self.env_dir)
-        envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe)
+    def assert_pip_not_installed(self):
+        envpy = os.path.join(os.path.realpath(self.env_dir),
+                             self.bindir, self.exe)
         try_import = 'try:\n import pip\nexcept ImportError:\n print("OK")'
         cmd = [envpy, '-c', try_import]
         p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE)
         out, err = p.communicate()
-        self.assertEqual(err, b"")
-        self.assertEqual(out.strip(), b"OK")
+        # We force everything to text, so unittest gives the detailed diff
+        # if we get unexpected results
+        err = err.decode("latin-1") # Force to text, prevent decoding errors
+        self.assertEqual(err, "")
+        out = out.decode("latin-1") # Force to text, prevent decoding errors
+        self.assertEqual(out.strip(), "OK")
+
+
+    def test_no_pip_by_default(self):
+        shutil.rmtree(self.env_dir)
+        self.run_with_capture(venv.create, self.env_dir)
+        self.assert_pip_not_installed()
 
     def test_explicit_no_pip(self):
         shutil.rmtree(self.env_dir)
         self.run_with_capture(venv.create, self.env_dir, with_pip=False)
-        envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe)
-        try_import = 'try:\n import pip\nexcept ImportError:\n print("OK")'
-        cmd = [envpy, '-c', try_import]
-        p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
-                             stderr=subprocess.PIPE)
-        out, err = p.communicate()
-        self.assertEqual(err, b"")
-        self.assertEqual(out.strip(), b"OK")
+        self.assert_pip_not_installed()
 
     # Temporary skip for http://bugs.python.org/issue19744
     @unittest.skipIf(ssl is None, 'pip needs SSL support')
@@ -293,7 +295,8 @@
             # environment settings don't cause venv to fail.
             envvars["PYTHONWARNINGS"] = "e"
             # pip doesn't ignore environment variables when running in
-            # isolated mode, and we don't have an active virtualenv here
+            # isolated mode, and we don't have an active virtualenv here,
+            # we're relying on the native venv support in 3.3+
             # See http://bugs.python.org/issue19734 for details
             del envvars["PIP_REQUIRE_VIRTUALENV"]
             try:
@@ -304,6 +307,7 @@
                 details = exc.output.decode(errors="replace")
                 msg = "{}\n\n**Subprocess Output**\n{}".format(exc, details)
                 self.fail(msg)
+        # Ensure pip is available in the virtual environment
         envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe)
         cmd = [envpy, '-Im', 'pip', '--version']
         p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
@@ -319,6 +323,36 @@
         env_dir = os.fsencode(self.env_dir).decode("latin-1")
         self.assertIn(env_dir, out)
 
+        # http://bugs.python.org/issue19728
+        # Check the private uninstall command provided for the Windows
+        # installers works (at least in a virtual environment)
+        cmd = [envpy, '-Im', 'ensurepip._uninstall']
+        with EnvironmentVarGuard() as envvars:
+            # pip doesn't ignore environment variables when running in
+            # isolated mode, and we don't have an active virtualenv here,
+            # we're relying on the native venv support in 3.3+
+            # See http://bugs.python.org/issue19734 for details
+            del envvars["PIP_REQUIRE_VIRTUALENV"]
+            p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
+                                      stderr=subprocess.PIPE)
+            out, err = p.communicate()
+        # We force everything to text, so unittest gives the detailed diff
+        # if we get unexpected results
+        err = err.decode("latin-1") # Force to text, prevent decoding errors
+        self.assertEqual(err, "")
+        # Being really specific regarding the expected behaviour for the
+        # initial bundling phase in Python 3.4. If the output changes in
+        # future pip versions, this test can be relaxed a bit.
+        out = out.decode("latin-1") # Force to text, prevent decoding errors
+        expected_output = textwrap.dedent("""\
+                            Uninstalling pip:
+                              Successfully uninstalled pip
+                            Uninstalling setuptools:
+                              Successfully uninstalled setuptools
+                            """)
+        self.assertEqual(out, expected_output)
+        self.assert_pip_not_installed()
+
 
 def test_main():
     run_unittest(BasicTest, EnsurePipTest)

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list