Detect paths not to uninstall from via sysconfig's rpm_prefix install scheme

This commit is contained in:
Miro Hrončok 2021-09-23 14:56:56 +02:00
parent 27c255ce81
commit 8b7935f5a8
2 changed files with 29 additions and 13 deletions

View File

@ -22,7 +22,7 @@
Name: python-%{srcname}
Version: %{base_version}%{?prerel:~%{prerel}}
Release: 2%{?dist}
Release: 3%{?dist}
Summary: A tool for installing and managing Python packages
# We bundle a lot of libraries with pip, which itself is under MIT license.
@ -70,6 +70,7 @@ BuildRequires: python-wheel-wheel
# Prevent removing of the system packages installed under /usr/lib
# when pip install -U is executed.
# https://bugzilla.redhat.com/show_bug.cgi?id=1550368#c24
# Could be replaced with https://www.python.org/dev/peps/pep-0668/
Patch3: remove-existing-dist-only-if-path-conflicts.patch
# Use the system level root certificate instead of the one bundled in certifi
@ -203,6 +204,12 @@ Recommends: python%{python3_pkgversion}-setuptools
Provides: pip = %{version}-%{release}
Conflicts: python-pip < %{version}-%{release}
# The python3.10 version that added the rpm_prefix sysconfig install scheme
# This pip can also function with the previous version,
# but it would remove RPM-installed packages during sudo pip install --upgrade.
Requires: python3-libs >= 3.10.0-2
%{crypt_compat_recommends 3}
%description -n python%{python3_pkgversion}-%{srcname}
@ -391,6 +398,9 @@ pytest_k='not completion and
%{python_wheeldir}/%{python_wheelname}
%changelog
* Thu Sep 23 2021 Miro Hrončok <mhroncok@redhat.com> - 21.2.3-3
- Detect paths not to uninstall from via sysconfig's rpm_prefix install scheme
* Mon Aug 16 2021 Miro Hrončok <mhroncok@redhat.com> - 21.2.3-2
- Fix broken uninstallation by a bogus downstream patch

View File

@ -1,10 +1,13 @@
From aca0c9df4ef54f70a3fedb07f4faac463f88a331 Mon Sep 17 00:00:00 2001
From 03fe2f1a674d8a8dfbd7da5bef2a6ae123670092 Mon Sep 17 00:00:00 2001
From: Karolina Surma <ksurma@redhat.com>
Date: Mon, 10 May 2021 18:16:20 +0200
Subject: [PATCH] Prevent removing of the system packages installed under
/usr/lib
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
when pip install -U is executed.
when pip install --upgrade is executed.
Resolves: rhbz#1550368
@ -16,9 +19,9 @@ Co-Authored-By: Miro Hrončok <miro@hroncok.cz>
---
src/pip/_internal/req/req_install.py | 3 ++-
src/pip/_internal/resolution/legacy/resolver.py | 5 ++++-
src/pip/_internal/resolution/resolvelib/factory.py | 10 ++++++++++
src/pip/_internal/resolution/resolvelib/factory.py | 13 +++++++++++++
src/pip/_internal/utils/misc.py | 11 +++++++++++
4 files changed, 27 insertions(+), 2 deletions(-)
4 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/src/pip/_internal/req/req_install.py b/src/pip/_internal/req/req_install.py
index 4c58cdb..3570e17 100644
@ -65,14 +68,14 @@ index 4df8f7e..dda2292 100644
req.satisfied_by = None
diff --git a/src/pip/_internal/resolution/resolvelib/factory.py b/src/pip/_internal/resolution/resolvelib/factory.py
index e7fd344..555e657 100644
index e7fd344..73b83fb 100644
--- a/src/pip/_internal/resolution/resolvelib/factory.py
+++ b/src/pip/_internal/resolution/resolvelib/factory.py
@@ -1,6 +1,7 @@
import contextlib
import functools
import logging
+import sys
+import sysconfig
from typing import (
TYPE_CHECKING,
Dict,
@ -92,16 +95,19 @@ index e7fd344..555e657 100644
from pip._internal.utils.virtualenv import running_under_virtualenv
from .base import Candidate, CandidateVersion, Constraint, Requirement
@@ -525,6 +528,13 @@ class Factory:
@@ -525,6 +528,16 @@ class Factory:
if dist is None: # Not installed, no uninstallation required.
return None
+ # Prevent uninstalling packages from /usr
+ if dist_location(dist._dist) in (
+ get_scheme('', prefix=sys.base_prefix).purelib,
+ get_scheme('', prefix=sys.base_prefix).platlib,
+ ):
+ return None
+ try:
+ if dist_location(dist._dist) in (
+ sysconfig.get_path('purelib', scheme='rpm_prefix'),
+ sysconfig.get_path('platlib', scheme='rpm_prefix'),
+ ):
+ return None
+ except KeyError: # this Python doesn't have 'rpm_prefix' scheme yet
+ pass
+
# We're installing into global site. The current installation must
# be uninstalled, no matter it's in global or user site, because the