Skip to content

Commit

Permalink
00251: Change user install location
Browse files Browse the repository at this point in the history
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 <encukou@gmail.com>
Co-authored-by: Miro Hrončok <miro@hroncok.cz>
Co-authored-by: Michal Cyprian <m.cyprian@gmail.com>
  • Loading branch information
4 people committed Oct 5, 2021
1 parent 3cebdae commit e54aa21
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
9 changes: 8 additions & 1 deletion Lib/site.py
Expand Up @@ -380,8 +380,15 @@ def getsitepackages(prefixes=None):
return sitepackages

def addsitepackages(known_paths, prefixes=None):
"""Add site-packages to sys.path"""
"""Add site-packages to sys.path
'/usr/local' is included in PREFIXES if RPM build is not detected
to make packages installed into this location visible.
"""
_trace("Processing global site-packages")
if ENABLE_USER_SITE and 'RPM_BUILD_ROOT' not in os.environ:
PREFIXES.insert(0, "/usr/local")
for sitedir in getsitepackages(prefixes):
if os.path.isdir(sitedir):
addsitedir(sitedir, known_paths)
Expand Down
19 changes: 19 additions & 0 deletions Lib/sysconfig.py
Expand Up @@ -58,6 +58,25 @@
},
}

# backup the original posix_prefix as rpm_prefix
# RPM packages use it and we need to be able to read it even when changed
_INSTALL_SCHEMES['rpm_prefix'] = _INSTALL_SCHEMES['posix_prefix']

if (not (hasattr(sys, 'real_prefix') or
sys.prefix != sys.base_prefix) and
'RPM_BUILD_ROOT' not in os.environ):
_INSTALL_SCHEMES['posix_prefix'] = {
'stdlib': '{installed_base}/{platlibdir}/python{py_version_short}',
'platstdlib': '{platbase}/{platlibdir}/python{py_version_short}',
'purelib': '{base}/local/lib/python{py_version_short}/site-packages',
'platlib': '{platbase}/local/{platlibdir}/python{py_version_short}/site-packages',
'include':
'{installed_base}/include/python{py_version_short}{abiflags}',
'platinclude':
'{installed_platbase}/include/python{py_version_short}{abiflags}',
'scripts': '{base}/local/bin',
'data': '{base}/local',
}

# NOTE: site.py has copy of this function.
# Sync it when modify this function.
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_sysconfig.py
Expand Up @@ -263,7 +263,7 @@ def test_get_config_h_filename(self):
self.assertTrue(os.path.isfile(config_h), config_h)

def test_get_scheme_names(self):
wanted = ['nt', 'posix_home', 'posix_prefix']
wanted = ['nt', 'posix_home', 'posix_prefix', 'rpm_prefix']
if HAS_USER_BASE:
wanted.extend(['nt_user', 'osx_framework_user', 'posix_user'])
self.assertEqual(get_scheme_names(), tuple(sorted(wanted)))
Expand All @@ -274,6 +274,8 @@ def test_symlink(self): # Issue 7880
cmd = "-c", "import sysconfig; print(sysconfig.get_platform())"
self.assertEqual(py.call_real(*cmd), py.call_link(*cmd))

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

0 comments on commit e54aa21

Please sign in to comment.