8000 00251: Change user install location · fedora-python/cpython@5dde033 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5dde033

Browse files
hroncokencukoumcyprianfrenzymadness
authored andcommitted
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 <encukou@gmail.com> Co-authored-by: Miro Hrončok <miro@hroncok.cz> Co-authored-by: Michal Cyprian <m.cyprian@gmail.com> Co-authored-by: Lumír Balhar <frenzy.madness@gmail.com>
1 parent 3ae9101 commit 5dde033

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
@@ -420,8 +420,15 @@ def getsitepackages(prefixes=None):
420420
return sitepackages
421421

422422
def addsitepackages(known_paths, prefixes=None):
423-
"""Add site-packages to sys.path"""
423+
"""Add site-packages to sys.path
424+
425+
'/usr/local' is included in PREFIXES if RPM build is not detected
426+
to make packages installed into this location visible.
427+
428+
"""
424429
_trace("Processing global site-packages")
430+
if ENABLE_USER_SITE and 'RPM_BUILD_ROOT' not in os.environ:
431+
PREFIXES.insert(0, "/usr/local")
425432
for sitedir in getsitepackages(prefixes):
426433
if os.path.isdir(sitedir):
427434
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

@@ -169,6 +175,19 @@ def joinuser(*args):
169175
},
170176
}
171177

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

@@ -268,11 +287,40 @@ def _extend_dict(target_dict, other_dict):
268287
target_dict[key] = value
269288

270289

290+
_CONFIG_VARS_LOCAL = None
291+
292+
293+
def _config_vars_local():
294+
# This function returns the config vars with prefixes amended to /usr/local
295+
# https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe
296+
global _CONFIG_VARS_LOCAL
297+
if _CONFIG_VARS_LOCAL is None:
298+
_CONFIG_VARS_LOCAL = dict(get_config_vars())
299+
_CONFIG_VARS_LOCAL['base'] = '/usr/local'
300+
_CONFIG_VARS_LOCAL['platbase'] = '/usr/local'
301+
return _CONFIG_VARS_LOCAL
302+
303+
271304
def _expand_vars(scheme, vars):
272305
res = {}
273306
if vars is None:
274307
vars = {}
275-
_extend_dict(vars, get_config_vars())
308+
309+
# when we are not in a virtual environment or an RPM build
310+
# we change '/usr' to '/usr/local'
311+
# to avoid surprises, we explicitly check for the /usr/ prefix
312+
# Python virtual environments have different prefixes
313+
# we only do this for posix_prefix, not to mangle the venv scheme
314+
# posix_prefix is used by sudo pip install
315+
# we only change the defaults here, so explicit --prefix will take precedence
316+
# https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe
317+
if (scheme == 'posix_prefix' and
318+
sys.prefix == '/usr' and
319+
'RPM_BUILD_ROOT' not in os.environ):
320+
_extend_dict(vars, _config_vars_local())
321+
else:
322+
_extend_dict(vars, get_config_vars())
323+
276324
if os.name == 'nt':
277325
# On Windows we want to substitute 'lib' for schemes rather
278326
# 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
@@ -131,8 +131,19 @@ def test_get_path(self):
131131
for scheme in _INSTALL_SCHEMES:
132132
for name in _INSTALL_SCHEMES[scheme]:
133133
expected = _INSTALL_SCHEMES[scheme][name].format(**config_vars)
134+
tested = get_path(name, scheme)
135+
# https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe
136+
if tested.startswith('/usr/local'):
137+
# /usr/local should only be used in posix_prefix
138+
self.assertEqual(scheme, 'posix_prefix')
139+
# Fedora CI runs tests for venv and virtualenv that check for other prefixes
140+
self.assertEqual(sys.prefix, '/usr')
141+
# When building the RPM of Python, %check runs this with RPM_BUILD_ROOT set
142+
# Fedora CI runs this with RPM_BUILD_ROOT unset
143+
self.assertNotIn('RPM_BUILD_ROOT', os.environ)
144+
tested = tested.replace('/usr/local', '/usr')
134145
self.assertEqual(
135-
os.path.normpath(get_path(name, scheme)),
146+
os.path.normpath(tested),
136147
os.path.normpath(expected),
137148
)
138149

@@ -387,7 +398,7 @@ def test_get_config_h_filename(self):
387398
self.assertTrue(os.path.isfile(config_h), config_h)
388399

389400
def test_get_scheme_names(self):
390-
wanted = ['nt', 'posix_home', 'posix_prefix', 'posix_venv', 'nt_venv', 'venv']
401+
wanted = ['nt', 'posix_home', 'posix_prefix', 'posix_venv', 'nt_venv', 'venv', 'rpm_prefix']
391402
if HAS_USER_BASE:
392403
wanted.extend(['nt_user', 'osx_framework_user', 'posix_user'])
393404
self.assertEqual(get_scheme_names(), tuple(sorted(wanted)))
@@ -399,6 +410,8 @@ def test_symlink(self): # Issue 7880
399410
cmd = "-c", "import sysconfig; print(sysconfig.get_platform())"
400411
self.assertEqual(py.call_real(*cmd), py.call_link(*cmd))
401412

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

0 commit comments

Comments
 (0)
0