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

f96a86
From aca0c9df4ef54f70a3fedb07f4faac463f88a331 Mon Sep 17 00:00:00 2001
f96a86
From: Karolina Surma <ksurma@redhat.com>
f96a86
Date: Mon, 10 May 2021 18:16:20 +0200
a80022
Subject: [PATCH] Prevent removing of the system packages installed under
a80022
 /usr/lib
a80022
a80022
when pip install -U is executed.
a80022
a80022
Resolves: rhbz#1550368
a80022
a80022
Co-Authored-By: Michal Cyprian <m.cyprian@gmail.com>
a80022
Co-Authored-By: Victor Stinner <vstinner@redhat.com>
a80022
Co-Authored-By: Petr Viktorin <pviktori@redhat.com>
f96a86
Co-Authored-By: Lumir Balhar <lbalhar@redhat.com>
f96a86
Co-Authored-By: Miro HronĨok <miro@hroncok.cz>
a80022
---
a80022
 src/pip/_internal/req/req_install.py               |  3 ++-
a80022
 src/pip/_internal/resolution/legacy/resolver.py    |  5 ++++-
a80022
 src/pip/_internal/resolution/resolvelib/factory.py | 10 ++++++++++
a80022
 src/pip/_internal/utils/misc.py                    | 11 +++++++++++
a80022
 4 files changed, 27 insertions(+), 2 deletions(-)
a80022
a80022
diff --git a/src/pip/_internal/req/req_install.py b/src/pip/_internal/req/req_install.py
f96a86
index 4c58cdb..3570e17 100644
a80022
--- a/src/pip/_internal/req/req_install.py
a80022
+++ b/src/pip/_internal/req/req_install.py
f96a86
@@ -43,6 +43,7 @@ from pip._internal.utils.misc import (
a80022
     ask_path_exists,
a80022
     backup_dir,
a80022
     display_path,
a80022
+    dist_in_install_path,
a80022
     dist_in_site_packages,
a80022
     dist_in_usersite,
a80022
     get_distribution,
f96a86
@@ -426,7 +427,7 @@ class InstallRequirement:
a80022
                         "lack sys.path precedence to {} in {}".format(
a80022
                             existing_dist.project_name, existing_dist.location)
a80022
                     )
a80022
-            else:
a80022
+            elif dist_in_install_path(existing_dist):
a80022
                 self.should_reinstall = True
a80022
         else:
a80022
             if self.editable:
a80022
diff --git a/src/pip/_internal/resolution/legacy/resolver.py b/src/pip/_internal/resolution/legacy/resolver.py
f96a86
index 4df8f7e..dda2292 100644
a80022
--- a/src/pip/_internal/resolution/legacy/resolver.py
a80022
+++ b/src/pip/_internal/resolution/legacy/resolver.py
f96a86
@@ -42,6 +42,7 @@ from pip._internal.resolution.base import BaseResolver, InstallRequirementProvid
a80022
 from pip._internal.utils.compatibility_tags import get_supported
a80022
 from pip._internal.utils.logging import indent_log
a80022
 from pip._internal.utils.misc import dist_in_usersite, normalize_version_info
a80022
+from pip._internal.utils.misc import dist_in_install_path
a80022
 from pip._internal.utils.packaging import check_requires_python, get_requires_python
a80022
 
f96a86
 logger = logging.getLogger(__name__)
f96a86
@@ -194,7 +195,9 @@ class Resolver(BaseResolver):
a80022
         """
a80022
         # Don't uninstall the conflict if doing a user install and the
a80022
         # conflict is not a user install.
a80022
-        if not self.use_user_site or dist_in_usersite(req.satisfied_by):
a80022
+        if ((not self.use_user_site
a80022
+                or dist_in_usersite(req.satisfied_by))
a80022
+                and dist_in_install_path(req.satisfied_by)):
a80022
             req.should_reinstall = True
a80022
         req.satisfied_by = None
a80022
 
a80022
diff --git a/src/pip/_internal/resolution/resolvelib/factory.py b/src/pip/_internal/resolution/resolvelib/factory.py
f96a86
index e7fd344..555e657 100644
a80022
--- a/src/pip/_internal/resolution/resolvelib/factory.py
a80022
+++ b/src/pip/_internal/resolution/resolvelib/factory.py
f96a86
@@ -1,6 +1,7 @@
f96a86
 import contextlib
a80022
 import functools
a80022
 import logging
a80022
+import sys
f96a86
 from typing import (
f96a86
     TYPE_CHECKING,
f96a86
     Dict,
f96a86
@@ -34,6 +35,7 @@ from pip._internal.exceptions import (
f96a86
     UnsupportedWheel,
a80022
 )
f96a86
 from pip._internal.index.package_finder import PackageFinder
f96a86
+from pip._internal.locations import get_scheme
f96a86
 from pip._internal.metadata import BaseDistribution, get_default_environment
f96a86
 from pip._internal.models.link import Link
f96a86
 from pip._internal.models.wheel import Wheel
f96a86
@@ -46,6 +48,7 @@ from pip._internal.req.req_install import (
f96a86
 from pip._internal.resolution.base import InstallRequirementProvider
f96a86
 from pip._internal.utils.compatibility_tags import get_supported
f96a86
 from pip._internal.utils.hashes import Hashes
f96a86
+from pip._internal.utils.misc import dist_location
a80022
 from pip._internal.utils.virtualenv import running_under_virtualenv
a80022
 
f96a86
 from .base import Candidate, CandidateVersion, Constraint, Requirement
f96a86
@@ -525,6 +528,13 @@ class Factory:
a80022
         if dist is None:  # Not installed, no uninstallation required.
a80022
             return None
a80022
 
a80022
+        # Prevent uninstalling packages from /usr
f96a86
+        if dist_location(dist._dist) in (
f96a86
+                get_scheme('', prefix=sys.base_prefix).purelib,
f96a86
+                get_scheme('', prefix=sys.base_prefix).platlib,
a80022
+            ):
a80022
+            return None
a80022
+
a80022
         # We're installing into global site. The current installation must
a80022
         # be uninstalled, no matter it's in global or user site, because the
a80022
         # user site installation has precedence over global.
a80022
diff --git a/src/pip/_internal/utils/misc.py b/src/pip/_internal/utils/misc.py
f96a86
index 99ebea3..5901687 100644
a80022
--- a/src/pip/_internal/utils/misc.py
a80022
+++ b/src/pip/_internal/utils/misc.py
f96a86
@@ -40,6 +40,7 @@ from pip._vendor.tenacity import retry, stop_after_delay, wait_fixed
a80022
 from pip import __version__
a80022
 from pip._internal.exceptions import CommandError
a80022
 from pip._internal.locations import get_major_minor_version, site_packages, user_site
f96a86
+from pip._internal.locations import get_scheme
a80022
 from pip._internal.utils.compat import WINDOWS, stdlib_pkgs
a80022
 from pip._internal.utils.virtualenv import (
f96a86
     running_under_virtualenv,
f96a86
@@ -382,6 +383,16 @@ def dist_in_site_packages(dist):
a80022
     return dist_location(dist).startswith(normalize_path(site_packages))
a80022
 
a80022
 
a80022
+def dist_in_install_path(dist):
a80022
+    """
a80022
+    Return True if given Distribution is installed in
a80022
+    path matching distutils_scheme layout.
a80022
+    """
a80022
+    norm_path = normalize_path(dist_location(dist))
a80022
+    return norm_path.startswith(normalize_path(
f96a86
+        get_scheme("").purelib.split('python')[0]))
a80022
+
a80022
+
a80022
 def dist_is_editable(dist):
a80022
     # type: (Distribution) -> bool
a80022
     """
a80022
-- 
f96a86
2.32.0
a80022