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

Skip to content

Commit ce02233

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 <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 0cc8128 commit ce02233

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

Lib/site.py

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

400400
def addsitepackages(known_paths, prefixes=None):
401-
"""Add site-packages to sys.path"""
401+
"""Add site-packages to sys.path
402+
403+
'/usr/local' is included in PREFIXES if RPM build is not detected
404+
to make packages installed into this location visible.
405+
406+
"""
402407
_trace("Processing global site-packages")
408+
if ENABLE_USER_SITE and 'RPM_BUILD_ROOT' not in os.environ:
409+
PREFIXES.insert(0, "/usr/local")
403410
for sitedir in getsitepackages(prefixes):
404411
if os.path.isdir(sitedir):
405412
addsitedir(sitedir, known_paths)

Lib/sysconfig.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@
104104
else:
105105
_INSTALL_SCHEMES['venv'] = _INSTALL_SCHEMES['posix_venv']
106106

107+
# For a brief period of time in the Fedora 36 life cycle,
108+
# this installation scheme existed and was documented in the release notes.
109+
# For backwards compatibility, we keep it here (at least on 3.10 and 3.11).
110+
_INSTALL_SCHEMES['rpm_prefix'] = _INSTALL_SCHEMES['posix_prefix']
111+
107112

108113
# NOTE: site.py has copy of this function.
109114
# Sync it when modify this function.
@@ -163,6 +168,19 @@ def joinuser(*args):
163168
},
164169
}
165170

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

@@ -268,11 +286,40 @@ def _extend_dict(target_dict, other_dict):
268286
target_dict[key] = value
269287

270288

289+
_CONFIG_VARS_LOCAL = None
290+
291+
292+
def _config_vars_local():
293+
# This function returns the config vars with prefixes amended to /usr/local
294+
# https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe
295+
global _CONFIG_VARS_LOCAL
296+
if _CONFIG_VARS_LOCAL is None:
297+
_CONFIG_VARS_LOCAL = dict(get_config_vars())
298+
_CONFIG_VARS_LOCAL['base'] = '/usr/local'
299+
_CONFIG_VARS_LOCAL['platbase'] = '/usr/local'
300+
return _CONFIG_VARS_LOCAL
301+
302+
271303
def _expand_vars(scheme, vars):
272304
res = {}
273305
if vars is None:
274306
vars = {}
275-
_extend_dict(vars, get_config_vars())
307+
308+
# when we are not in a virtual environment or an RPM build
309+
# we change '/usr' to '/usr/local'
310+
# to avoid surprises, we explicitly check for the /usr/ prefix
311+
# Python virtual environments have different prefixes
312+
# we only do this for posix_prefix, not to mangle the venv scheme
313+
# posix_prefix is used by sudo pip install
314+
# we only change the defaults here, so explicit --prefix will take precedence
315+
# https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe
316+
if (scheme == 'posix_prefix' and
317+
sys.prefix == '/usr' and
318+
'RPM_BUILD_ROOT' not in os.environ):
319+
_extend_dict(vars, _config_vars_local())
320+
else:
321+
_extend_dict(vars, get_config_vars())
322+
276323
if os.name == 'nt':
277324
# On Windows we want to substitute 'lib' for schemes rather
278325
# 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
@@ -119,8 +119,19 @@ def test_get_path(self):
119119
for scheme in _INSTALL_SCHEMES:
120120
for name in _INSTALL_SCHEMES[scheme]:
121121
expected = _INSTALL_SCHEMES[scheme][name].format(**config_vars)
122+
tested = get_path(name, scheme)
123+
# https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe
124+
if tested.startswith('/usr/local'):
125+
# /usr/local should only be used in posix_prefix
126+
self.assertEqual(scheme, 'posix_prefix')
127+
# Fedora CI runs tests for venv and virtualenv that check for other prefixes
128+
self.assertEqual(sys.prefix, '/usr')
129+
# When building the RPM of Python, %check runs this with RPM_BUILD_ROOT set
130+
# Fedora CI runs this with RPM_BUILD_ROOT unset
131+
self.assertNotIn('RPM_BUILD_ROOT', os.environ)
132+
tested = tested.replace('/usr/local', '/usr')
122133
self.assertEqual(
123-
os.path.normpath(get_path(name, scheme)),
134+
os.path.normpath(tested),
124135
os.path.normpath(expected),
125136
)
126137

@@ -353,7 +364,7 @@ def test_get_config_h_filename(self):
353364
self.assertTrue(os.path.isfile(config_h), config_h)
354365

355366
def test_get_scheme_names(self):
356-
wanted = ['nt', 'posix_home', 'posix_prefix', 'posix_venv', 'nt_venv', 'venv']
367+
wanted = ['nt', 'posix_home', 'posix_prefix', 'posix_venv', 'nt_venv', 'venv', 'rpm_prefix']
357368
if HAS_USER_BASE:
358369
wanted.extend(['nt_user', 'osx_framework_user', 'posix_user'])
359370
self.assertEqual(get_scheme_names(), tuple(sorted(wanted)))
@@ -365,6 +376,8 @@ def test_symlink(self): # Issue 7880
365376
cmd = "-c", "import sysconfig; print(sysconfig.get_platform())"
366377
self.assertEqual(py.call_real(*cmd), py.call_link(*cmd))
367378

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

0 commit comments

Comments
 (0)
0