]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #19728: add private ensurepip._uninstall CLI
authorNick Coghlan <ncoghlan@gmail.com>
Sat, 30 Nov 2013 07:15:09 +0000 (17:15 +1000)
committerNick Coghlan <ncoghlan@gmail.com>
Sat, 30 Nov 2013 07:15:09 +0000 (17:15 +1000)
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)

Lib/ensurepip/__init__.py
Lib/test/test_ensurepip.py
Lib/test/test_venv.py

index 1a644da6349afec34996b1cd7c943a0fa1dd2ddd..63013aec3db2b0a4e8def396bb19a74422b07add 100644 (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)])
index abf00fd1884712bc746a4e56d88e48e128614ab9..c119327d4250bc74c939e6adec32078b0d81b0ea 100644 (file)
@@ -4,6 +4,8 @@ import ensurepip
 import test.support
 import os
 import os.path
+import contextlib
+import sys
 
 
 class TestEnsurePipVersion(unittest.TestCase):
@@ -122,6 +124,79 @@ class TestBootstrap(unittest.TestCase):
     def test_altinstall_default_pip_conflict(self):
         with self.assertRaises(ValueError):
             ensurepip.bootstrap(altinstall=True, default_pip=True)
+        self.run_pip.assert_not_called()
+
+@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__":
index a92f4929da175a8420fa74ae546de1c69938e778..c15610b25c7bc0f385ff68d55a16f66845e13278 100644 (file)
@@ -14,6 +14,7 @@ import sys
 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 @@ class BasicTest(BaseTest):
 @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 @@ class EnsurePipTest(BaseTest):
             # 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 @@ class EnsurePipTest(BaseTest):
                 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 @@ class EnsurePipTest(BaseTest):
         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)