Skip to content

Commit f1a88c1

Browse files
hroncokencukoumcyprianfrenzymadness
committed
00251: Change user install location
Set values of base and platbase in sysconfig from /usr to /usr/local when RPM build is not detected to make pip and similar tools install into separate location. Fedora Change: https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe Downstream only. We've tried to rework in Fedora 36/Python 3.10 to follow https://bugs.python.org/issue43976 but we have identified serious problems with that approach, see https://bugzilla.redhat.com/2026979 or https://bugzilla.redhat.com/2097183 pypa/distutils integration: pypa/distutils#70 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 3a83b17 commit f1a88c1

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

Lib/site.py

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

408408
def addsitepackages(known_paths, prefixes=None):
409-
"""Add site-packages to sys.path"""
409+
"""Add site-packages to sys.path
410+
411+
'/usr/local' is included in PREFIXES if RPM build is not detected
412+
to make packages installed into this location visible.
413+
414+
"""
410415
_trace("Processing global site-packages")
416+
if ENABLE_USER_SITE and 'RPM_BUILD_ROOT' not in os.environ:
417+
PREFIXES.insert(0, "/usr/local")
411418
for sitedir in getsitepackages(prefixes):
412419
if os.path.isdir(sitedir):
413420
addsitedir(sitedir, known_paths)

Lib/sysconfig/__init__.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@
106106
else:
107107
_INSTALL_SCHEMES['venv'] = _INSTALL_SCHEMES['posix_venv']
108108

109+
# For a brief period of time in the Fedora 36 life cycle,
110+
# this installation scheme existed and was documented in the release notes.
111+
# For backwards compatibility, we keep it here (at least on 3.10 and 3.11).
112+
_INSTALL_SCHEMES['rpm_prefix'] = _INSTALL_SCHEMES['posix_prefix']
113+
114+
109115
def _get_implementation():
110116
return 'Python'
111117

@@ -167,6 +173,19 @@ def joinuser(*args):
167173
},
168174
}
169175

176+
# This is used by distutils.command.install in the stdlib
177+
# as well as pypa/distutils (e.g. bundled in setuptools).
178+
# The self.prefix value is set to sys.prefix + /local/
179+
# if neither RPM build nor virtual environment is
180+
# detected to make distutils install packages
181+
# into the separate location.
182+
# https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe
183+
if (not (hasattr(sys, 'real_prefix') or
184+
sys.prefix != sys.base_prefix) and
185+
'RPM_BUILD_ROOT' not in os.environ):
186+
_prefix_addition = '/local'
187+
188+
170189
_SCHEME_KEYS = ('stdlib', 'platstdlib', 'purelib', 'platlib', 'include',
171190
'scripts', 'data')
172191

@@ -261,11 +280,40 @@ def _extend_dict(target_dict, other_dict):
261280
target_dict[key] = value
262281

263282

283+
_CONFIG_VARS_LOCAL = None
284+
285+
286+
def _config_vars_local():
287+
# This function returns the config vars with prefixes amended to /usr/local
288+
# https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe
289+
global _CONFIG_VARS_LOCAL
290+
if _CONFIG_VARS_LOCAL is None:
291+
_CONFIG_VARS_LOCAL = dict(get_config_vars())
292+
_CONFIG_VARS_LOCAL['base'] = '/usr/local'
293+
_CONFIG_VARS_LOCAL['platbase'] = '/usr/local'
294+
return _CONFIG_VARS_LOCAL
295+
296+
264297
def _expand_vars(scheme, vars):
265298
res = {}
266299
if vars is None:
267300
vars = {}
268-
_extend_dict(vars, get_config_vars())
301+
302+
# when we are not in a virtual environment or an RPM build
303+
# we change '/usr' to '/usr/local'
304+
# to avoid surprises, we explicitly check for the /usr/ prefix
305+
# Python virtual environments have different prefixes
306+
# we only do this for posix_prefix, not to mangle the venv scheme
307+
# posix_prefix is used by sudo pip install
308+
# we only change the defaults here, so explicit --prefix will take precedence
309+
# https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe
310+
if (scheme == 'posix_prefix' and
311+
_PREFIX == '/usr' and
312+
'RPM_BUILD_ROOT' not in os.environ):
313+
_extend_dict(vars, _config_vars_local())
314+
else:
315+
_extend_dict(vars, get_config_vars())
316+
269317
if os.name == 'nt':
270318
# On Windows we want to substitute 'lib' for schemes rather
271319
# than the native value (without modifying vars, in case it

Lib/test/test_sysconfig.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,19 @@ def test_get_path(self):
121121
for scheme in _INSTALL_SCHEMES:
122122
for name in _INSTALL_SCHEMES[scheme]:
123123
expected = _INSTALL_SCHEMES[scheme][name].format(**config_vars)
124+
tested = get_path(name, scheme)
125+
# https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe
126+
if tested.startswith('/usr/local'):
127+
# /usr/local should only be used in posix_prefix
128+
self.assertEqual(scheme, 'posix_prefix')
129+
# Fedora CI runs tests for venv and virtualenv that check for other prefixes
130+
self.assertEqual(sys.prefix, '/usr')
131+
# When building the RPM of Python, %check runs this with RPM_BUILD_ROOT set
132+
# Fedora CI runs this with RPM_BUILD_ROOT unset
133+
self.assertNotIn('RPM_BUILD_ROOT', os.environ)
134+
tested = tested.replace('/usr/local', '/usr')
124135
self.assertEqual(
125-
os.path.normpath(get_path(name, scheme)),
136+
os.path.normpath(tested),
126137
os.path.normpath(expected),
127138
)
128139

@@ -377,7 +388,7 @@ def test_get_config_h_filename(self):
377388
self.assertTrue(os.path.isfile(config_h), config_h)
378389

379390
def test_get_scheme_names(self):
380-
wanted = ['nt', 'posix_home', 'posix_prefix', 'posix_venv', 'nt_venv', 'venv']
391+
wanted = ['nt', 'posix_home', 'posix_prefix', 'posix_venv', 'nt_venv', 'venv', 'rpm_prefix']
381392
if HAS_USER_BASE:
382393
wanted.extend(['nt_user', 'osx_framework_user', 'posix_user'])
383394
self.assertEqual(get_scheme_names(), tuple(sorted(wanted)))
@@ -389,6 +400,8 @@ def test_symlink(self): # Issue 7880
389400
cmd = "-c", "import sysconfig; print(sysconfig.get_platform())"
390401
self.assertEqual(py.call_real(*cmd), py.call_link(*cmd))
391402

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

0 commit comments

Comments
 (0)