1ee682
From 8c58a99221415ca7c3d5ce50dcffefa14e421928 Mon Sep 17 00:00:00 2001
1ee682
From: Tomas Orsava <torsava@redhat.com>
1ee682
Date: Tue, 12 Nov 2019 17:24:20 +0100
1ee682
Subject: [PATCH] Subject: Prevent removing of the system packages installed
1ee682
 under /usr/lib
875274
1ee682
when pip install -U is executed.
1ee682
1ee682
Resolves: rhbz#1550368
1ee682
1ee682
Co-Authored-By: Michal Cyprian <m.cyprian@gmail.com>
1ee682
Co-Authored-By: Victor Stinner <vstinner@redhat.com>
1ee682
---
1ee682
 src/pip/_internal/legacy_resolve.py  |  5 ++++-
1ee682
 src/pip/_internal/req/req_install.py |  3 ++-
1ee682
 src/pip/_internal/utils/misc.py      | 11 +++++++++++
1ee682
 3 files changed, 17 insertions(+), 2 deletions(-)
875274
875274
diff --git a/src/pip/_internal/legacy_resolve.py b/src/pip/_internal/legacy_resolve.py
1ee682
index c24158f..bd92287 100644
875274
--- a/src/pip/_internal/legacy_resolve.py
875274
+++ b/src/pip/_internal/legacy_resolve.py
1ee682
@@ -30,6 +30,7 @@ from pip._internal.exceptions import (
1ee682
 )
875274
 from pip._internal.utils.logging import indent_log
875274
 from pip._internal.utils.misc import (
1ee682
+    dist_in_install_path,
1ee682
     dist_in_usersite,
1ee682
     ensure_dir,
1ee682
     normalize_version_info,
1ee682
@@ -224,7 +225,9 @@ class Resolver(object):
875274
         """
875274
         # Don't uninstall the conflict if doing a user install and the
875274
         # conflict is not a user install.
875274
-        if not self.use_user_site or dist_in_usersite(req.satisfied_by):
875274
+        if ((not self.use_user_site
875274
+                or dist_in_usersite(req.satisfied_by))
875274
+                and dist_in_install_path(req.satisfied_by)):
875274
             req.conflicts_with = req.satisfied_by
875274
         req.satisfied_by = None
875274
 
875274
diff --git a/src/pip/_internal/req/req_install.py b/src/pip/_internal/req/req_install.py
1ee682
index 5a8c0dc..f80ba87 100644
875274
--- a/src/pip/_internal/req/req_install.py
875274
+++ b/src/pip/_internal/req/req_install.py
1ee682
@@ -39,6 +39,7 @@ from pip._internal.utils.misc import (
1ee682
     ask_path_exists,
1ee682
     backup_dir,
1ee682
     display_path,
1ee682
+    dist_in_install_path,
1ee682
     dist_in_site_packages,
1ee682
     dist_in_usersite,
1ee682
     ensure_dir,
1ee682
@@ -461,7 +462,7 @@ class InstallRequirement(object):
875274
                         "lack sys.path precedence to %s in %s" %
875274
                         (existing_dist.project_name, existing_dist.location)
875274
                     )
875274
-            else:
875274
+            elif dist_in_install_path(existing_dist):
875274
                 self.conflicts_with = existing_dist
875274
         return True
875274
 
875274
diff --git a/src/pip/_internal/utils/misc.py b/src/pip/_internal/utils/misc.py
1ee682
index b848263..5b75fed 100644
875274
--- a/src/pip/_internal/utils/misc.py
875274
+++ b/src/pip/_internal/utils/misc.py
1ee682
@@ -28,6 +28,7 @@ from pip._vendor.six.moves.urllib.parse import unquote as urllib_unquote
875274
 from pip import __version__
1ee682
 from pip._internal.exceptions import CommandError
1ee682
 from pip._internal.locations import (
1ee682
+    distutils_scheme,
1ee682
     get_major_minor_version,
1ee682
     site_packages,
1ee682
     user_site,
1ee682
@@ -389,6 +390,16 @@ def dist_in_site_packages(dist):
1ee682
     return dist_location(dist).startswith(normalize_path(site_packages))
875274
 
875274
 
875274
+def dist_in_install_path(dist):
875274
+    """
875274
+    Return True if given Distribution is installed in
875274
+    path matching distutils_scheme layout.
875274
+    """
875274
+    norm_path = normalize_path(dist_location(dist))
875274
+    return norm_path.startswith(normalize_path(
875274
+        distutils_scheme("")['purelib'].split('python')[0]))
875274
+
875274
+
875274
 def dist_is_editable(dist):
875274
     # type: (Distribution) -> bool
875274
     """
1ee682
-- 
1ee682
2.20.1
1ee682