Skip to content

Commit 284acf0

Browse files
frenzymadnessencukouhroncokmcyprian
committed
00251: Change user install location
Change the values of sysconfig's "posix_prefix" install scheme to /usr/local when RPM build or venv/virtualenv is not detected, to make pip, sysconfig and distutils install into an isolated location. The original values are saved as an additional "rpm_prefix" install scheme. The site module adds the /usr/local paths to sys.path when site packages are enabled and RPM build is not detected. Fedora Change: https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe Rewrote in Fedora 36+ to patch sysconfig instead of distutils, see https://discuss.python.org/t/pep-632-deprecate-distutils-module/5134/104 Downstream only for now, waiting for https://bugs.python.org/issue43976 Co-authored-by: Petr Viktorin <[email protected]> Co-authored-by: Miro Hrončok <[email protected]> Co-authored-by: Michal Cyprian <[email protected]> Co-authored-by: Lumír Balhar <[email protected]>
1 parent 7f376c3 commit 284acf0

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Lib/site.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,15 @@ def getsitepackages(prefixes=None):
377377
return sitepackages
378378

379379
def addsitepackages(known_paths, prefixes=None):
380-
"""Add site-packages to sys.path"""
380+
"""Add site-packages to sys.path
381+
382+
'/usr/local' is included in PREFIXES if RPM build is not detected
383+
to make packages installed into this location visible.
384+
385+
"""
381386
_trace("Processing global site-packages")
387+
if ENABLE_USER_SITE and 'RPM_BUILD_ROOT' not in os.environ:
388+
PREFIXES.insert(0, "/usr/local")
382389
for sitedir in getsitepackages(prefixes):
383390
if os.path.isdir(sitedir):
384391
addsitedir(sitedir, known_paths)

Lib/sysconfig.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,25 @@
103103
else:
104104
_INSTALL_SCHEMES['venv'] = _INSTALL_SCHEMES['posix_venv']
105105

106+
# backup the original posix_prefix as rpm_prefix
107+
# RPM packages use it and we need to be able to read it even when changed
108+
_INSTALL_SCHEMES['rpm_prefix'] = _INSTALL_SCHEMES['posix_prefix']
109+
110+
if (not (hasattr(sys, 'real_prefix') or
111+
sys.prefix != sys.base_prefix) and
112+
'RPM_BUILD_ROOT' not in os.environ):
113+
_INSTALL_SCHEMES['posix_prefix'] = {
114+
'stdlib': '{installed_base}/{platlibdir}/python{py_version_short}',
115+
'platstdlib': '{platbase}/{platlibdir}/python{py_version_short}',
116+
'purelib': '{base}/local/lib/python{py_version_short}/site-packages',
117+
'platlib': '{platbase}/local/{platlibdir}/python{py_version_short}/site-packages',
118+
'include':
119+
'{installed_base}/include/python{py_version_short}{abiflags}',
120+
'platinclude':
121+
'{installed_platbase}/include/python{py_version_short}{abiflags}',
122+
'scripts': '{base}/local/bin',
123+
'data': '{base}/local',
124+
}
106125

107126
# NOTE: site.py has copy of this function.
108127
# Sync it when modify this function.

Lib/test/test_sysconfig.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def test_get_config_h_filename(self):
333333
self.assertTrue(os.path.isfile(config_h), config_h)
334334

335335
def test_get_scheme_names(self):
336-
wanted = ['nt', 'posix_home', 'posix_prefix', 'posix_venv', 'nt_venv', 'venv']
336+
wanted = ['nt', 'posix_home', 'posix_prefix', 'posix_venv', 'nt_venv', 'venv', 'rpm_prefix']
337337
if HAS_USER_BASE:
338338
wanted.extend(['nt_user', 'osx_framework_user', 'posix_user'])
339339
self.assertEqual(get_scheme_names(), tuple(sorted(wanted)))
@@ -345,6 +345,8 @@ def test_symlink(self): # Issue 7880
345345
cmd = "-c", "import sysconfig; print(sysconfig.get_platform())"
346346
self.assertEqual(py.call_real(*cmd), py.call_link(*cmd))
347347

348+
@unittest.skipIf('RPM_BUILD_ROOT' not in os.environ,
349+
"Test doesn't expect Fedora's paths")
348350
def test_user_similar(self):
349351
# Issue #8759: make sure the posix scheme for the users
350352
# is similar to the global posix_prefix one

0 commit comments

Comments
 (0)