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

db7918
From 2c3f3a590ddfc151a456b44a5f96f0f603d178e9 Mon Sep 17 00:00:00 2001
db7918
From: Lumir Balhar <lbalhar@redhat.com>
db7918
Date: Wed, 16 Feb 2022 08:36:21 +0100
db7918
Subject: [PATCH] Prevent removing of the system packages installed under
db7918
 /usr/lib when pip install --upgrade is executed.
db7918
MIME-Version: 1.0
db7918
Content-Type: text/plain; charset=UTF-8
db7918
Content-Transfer-Encoding: 8bit
db7918
db7918
Resolves: rhbz#1550368
db7918
db7918
Co-Authored-By: Michal Cyprian <m.cyprian@gmail.com>
db7918
Co-Authored-By: Victor Stinner <vstinner@redhat.com>
db7918
Co-Authored-By: Petr Viktorin <pviktori@redhat.com>
db7918
Co-Authored-By: Lumir Balhar <lbalhar@redhat.com>
db7918
Co-Authored-By: Miro HronĨok <miro@hroncok.cz>
db7918
Co-Authored-By: Karolina Surma <ksurma@redhat.com>
db7918
---
db7918
 src/pip/_internal/metadata/base.py                 | 12 +++++++++++-
db7918
 src/pip/_internal/req/req_install.py               |  2 +-
db7918
 src/pip/_internal/resolution/legacy/resolver.py    |  4 +++-
db7918
 src/pip/_internal/resolution/resolvelib/factory.py | 12 ++++++++++++
db7918
 4 files changed, 27 insertions(+), 3 deletions(-)
db7918
db7918
diff --git a/src/pip/_internal/metadata/base.py b/src/pip/_internal/metadata/base.py
db7918
index 151fd6d..f9109cd 100644
db7918
--- a/src/pip/_internal/metadata/base.py
db7918
+++ b/src/pip/_internal/metadata/base.py
db7918
@@ -28,7 +28,7 @@ from pip._vendor.packaging.utils import NormalizedName
db7918
 from pip._vendor.packaging.version import LegacyVersion, Version
db7918
 
db7918
 from pip._internal.exceptions import NoneMetadataError
db7918
-from pip._internal.locations import site_packages, user_site
db7918
+from pip._internal.locations import get_scheme, site_packages, user_site
db7918
 from pip._internal.models.direct_url import (
db7918
     DIRECT_URL_METADATA_NAME,
db7918
     DirectUrl,
db7918
@@ -560,6 +560,16 @@ class BaseDistribution(Protocol):
db7918
             for extra in self._iter_egg_info_extras():
db7918
                 metadata["Provides-Extra"] = extra
db7918
 
db7918
+    @property
db7918
+    def in_install_path(self) -> bool:
db7918
+        """
db7918
+        Return True if given Distribution is installed in
db7918
+        path matching distutils_scheme layout.
db7918
+        """
db7918
+        norm_path = normalize_path(self.installed_location)
db7918
+        return norm_path.startswith(normalize_path(
db7918
+            get_scheme("").purelib.split('python')[0]))
db7918
+
db7918
 
db7918
 class BaseEnvironment:
db7918
     """An environment containing distributions to introspect."""
db7918
diff --git a/src/pip/_internal/req/req_install.py b/src/pip/_internal/req/req_install.py
db7918
index a1e376c..ed7facf 100644
db7918
--- a/src/pip/_internal/req/req_install.py
db7918
+++ b/src/pip/_internal/req/req_install.py
db7918
@@ -416,7 +416,7 @@ class InstallRequirement:
db7918
                         f"lack sys.path precedence to {existing_dist.raw_name} "
db7918
                         f"in {existing_dist.location}"
db7918
                     )
db7918
-            else:
db7918
+            elif existing_dist.in_install_path:
db7918
                 self.should_reinstall = True
db7918
         else:
db7918
             if self.editable:
db7918
diff --git a/src/pip/_internal/resolution/legacy/resolver.py b/src/pip/_internal/resolution/legacy/resolver.py
db7918
index fb49d41..040f2c1 100644
db7918
--- a/src/pip/_internal/resolution/legacy/resolver.py
db7918
+++ b/src/pip/_internal/resolution/legacy/resolver.py
db7918
@@ -325,7 +325,9 @@ class Resolver(BaseResolver):
db7918
         """
db7918
         # Don't uninstall the conflict if doing a user install and the
db7918
         # conflict is not a user install.
db7918
-        if not self.use_user_site or req.satisfied_by.in_usersite:
db7918
+        if ((not self.use_user_site
db7918
+                or req.satisfied_by.in_usersite)
db7918
+                and req.satisfied_by.in_install_path):
db7918
             req.should_reinstall = True
db7918
         req.satisfied_by = None
db7918
 
db7918
diff --git a/src/pip/_internal/resolution/resolvelib/factory.py b/src/pip/_internal/resolution/resolvelib/factory.py
db7918
index a4c24b5..e7e2da9 100644
db7918
--- a/src/pip/_internal/resolution/resolvelib/factory.py
db7918
+++ b/src/pip/_internal/resolution/resolvelib/factory.py
db7918
@@ -1,6 +1,8 @@
db7918
 import contextlib
db7918
 import functools
db7918
 import logging
db7918
+import sys
db7918
+import sysconfig
db7918
 from typing import (
db7918
     TYPE_CHECKING,
db7918
     Dict,
db7918
@@ -549,6 +551,16 @@ class Factory:
db7918
         if dist is None:  # Not installed, no uninstallation required.
db7918
             return None
db7918
 
db7918
+        # Prevent uninstalling packages from /usr
db7918
+        try:
db7918
+            if dist.installed_location in (
db7918
+                    sysconfig.get_path('purelib', scheme='posix_prefix', vars={'base': sys.base_prefix}),
db7918
+                    sysconfig.get_path('platlib', scheme='posix_prefix', vars={'platbase': sys.base_prefix}),
db7918
+            ):
db7918
+                return None
db7918
+        except KeyError:  # this Python doesn't have 'rpm_prefix' scheme yet
db7918
+            pass
db7918
+
db7918
         # We're installing into global site. The current installation must
db7918
         # be uninstalled, no matter it's in global or user site, because the
db7918
         # user site installation has precedence over global.
db7918
-- 
db7918
2.35.3
db7918