Blame SOURCES/remove-existing-dist-only-if-path-conflicts.patch

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