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

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