6d97c1
From 854fd7296bb9306d46ba3cc8bb7c6f18a7960ed6 Mon Sep 17 00:00:00 2001
6d97c1
From: Tomas Hrnciar <thrnciar@redhat.com>
6d97c1
Date: Sun, 26 Apr 2020 21:19:03 +0200
6d97c1
Subject: [PATCH] Prevent removing of the system packages installed under
6d97c1
 /usr/lib
6d97c1
6d97c1
when pip install -U is executed.
6d97c1
6d97c1
Resolves: rhbz#1550368
6d97c1
6d97c1
Co-Authored-By: Michal Cyprian <m.cyprian@gmail.com>
6d97c1
Co-Authored-By: Victor Stinner <vstinner@redhat.com>
6d97c1
---
6d97c1
 src/pip/_internal/req/req_install.py            |  3 ++-
6d97c1
 src/pip/_internal/resolution/legacy/resolver.py |  5 ++++-
6d97c1
 src/pip/_internal/utils/misc.py                 | 11 +++++++++++
6d97c1
 3 files changed, 17 insertions(+), 2 deletions(-)
6d97c1
6d97c1
diff --git a/src/pip/_internal/req/req_install.py b/src/pip/_internal/req/req_install.py
6d97c1
index 4759f4a..2e76e35 100644
6d97c1
--- a/src/pip/_internal/req/req_install.py
6d97c1
+++ b/src/pip/_internal/req/req_install.py
6d97c1
@@ -39,6 +39,7 @@ from pip._internal.utils.misc import (
6d97c1
     ask_path_exists,
6d97c1
     backup_dir,
6d97c1
     display_path,
6d97c1
+    dist_in_install_path,
6d97c1
     dist_in_site_packages,
6d97c1
     dist_in_usersite,
6d97c1
     get_distribution,
6d97c1
@@ -446,7 +447,7 @@ class InstallRequirement(object):
6d97c1
                         "lack sys.path precedence to {} in {}".format(
6d97c1
                             existing_dist.project_name, existing_dist.location)
6d97c1
                     )
6d97c1
-            else:
6d97c1
+            elif dist_in_install_path(existing_dist):
6d97c1
                 self.should_reinstall = True
6d97c1
         else:
6d97c1
             if self.editable:
6d97c1
diff --git a/src/pip/_internal/resolution/legacy/resolver.py b/src/pip/_internal/resolution/legacy/resolver.py
6d97c1
index c9b4c66..ff361d8 100644
6d97c1
--- a/src/pip/_internal/resolution/legacy/resolver.py
6d97c1
+++ b/src/pip/_internal/resolution/legacy/resolver.py
6d97c1
@@ -34,6 +34,7 @@ from pip._internal.resolution.base import BaseResolver
6d97c1
 from pip._internal.utils.compatibility_tags import get_supported
6d97c1
 from pip._internal.utils.logging import indent_log
6d97c1
 from pip._internal.utils.misc import dist_in_usersite, normalize_version_info
6d97c1
+from pip._internal.utils.misc import dist_in_install_path
6d97c1
 from pip._internal.utils.packaging import (
6d97c1
     check_requires_python,
6d97c1
     get_requires_python,
6d97c1
@@ -207,7 +208,9 @@ class Resolver(BaseResolver):
6d97c1
         """
6d97c1
         # Don't uninstall the conflict if doing a user install and the
6d97c1
         # conflict is not a user install.
6d97c1
-        if not self.use_user_site or dist_in_usersite(req.satisfied_by):
6d97c1
+        if ((not self.use_user_site
6d97c1
+                or dist_in_usersite(req.satisfied_by))
6d97c1
+                and dist_in_install_path(req.satisfied_by)):
6d97c1
             req.should_reinstall = True
6d97c1
         req.satisfied_by = None
6d97c1
 
6d97c1
diff --git a/src/pip/_internal/utils/misc.py b/src/pip/_internal/utils/misc.py
6d97c1
index 24a7455..5fd48d3 100644
6d97c1
--- a/src/pip/_internal/utils/misc.py
6d97c1
+++ b/src/pip/_internal/utils/misc.py
6d97c1
@@ -31,6 +31,7 @@ from pip._vendor.six.moves.urllib.parse import unquote as urllib_unquote
6d97c1
 from pip import __version__
6d97c1
 from pip._internal.exceptions import CommandError
6d97c1
 from pip._internal.locations import (
6d97c1
+    distutils_scheme,
6d97c1
     get_major_minor_version,
6d97c1
     site_packages,
6d97c1
     user_site,
6d97c1
@@ -403,6 +404,16 @@ def dist_in_site_packages(dist):
6d97c1
     return dist_location(dist).startswith(normalize_path(site_packages))
6d97c1
 
6d97c1
 
6d97c1
+def dist_in_install_path(dist):
6d97c1
+    """
6d97c1
+    Return True if given Distribution is installed in
6d97c1
+    path matching distutils_scheme layout.
6d97c1
+    """
6d97c1
+    norm_path = normalize_path(dist_location(dist))
6d97c1
+    return norm_path.startswith(normalize_path(
6d97c1
+        distutils_scheme("")['purelib'].split('python')[0]))
6d97c1
+
6d97c1
+
6d97c1
 def dist_is_editable(dist):
6d97c1
     # type: (Distribution) -> bool
6d97c1
     """
6d97c1
-- 
6d97c1
2.25.4
6d97c1