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

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