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

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