Skip to content

Commit bafbbb0

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 <[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 f6650f9 commit bafbbb0

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
@@ -387,8 +387,15 @@ def getsitepackages(prefixes=None):
387387
return sitepackages
388388

389389
def addsitepackages(known_paths, prefixes=None):
390-
"""Add site-packages to sys.path"""
390+
"""Add site-packages to sys.path
391+
392+
'/usr/local' is included in PREFIXES if RPM build is not detected
393+
to make packages installed into this location visible.
394+
395+
"""
391396
_trace("Processing global site-packages")
397+
if ENABLE_USER_SITE and 'RPM_BUILD_ROOT' not in os.environ:
398+
PREFIXES.insert(0, "/usr/local")
392399
for sitedir in getsitepackages(prefixes):
393400
if os.path.isdir(sitedir):
394401
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

@@ -263,11 +281,40 @@ def _extend_dict(target_dict, other_dict):
263281
target_dict[key] = value
264282

265283

284+
_CONFIG_VARS_LOCAL = None
285+
286+
287+
def _config_vars_local():
288+
# This function returns the config vars with prefixes amended to /usr/local
289+
# https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe
290+
global _CONFIG_VARS_LOCAL
291+
if _CONFIG_VARS_LOCAL is None:
292+
_CONFIG_VARS_LOCAL = dict(get_config_vars())
293+
_CONFIG_VARS_LOCAL['base'] = '/usr/local'
294+
_CONFIG_VARS_LOCAL['platbase'] = '/usr/local'
295+
return _CONFIG_VARS_LOCAL
296+
297+
266298
def _expand_vars(scheme, vars):
267299
res = {}
268300
if vars is None:
269301
vars = {}
270-
_extend_dict(vars, get_config_vars())
302+
303+
# when we are not in a virtual environment or an RPM build
304+
# we change '/usr' to '/usr/local'
305+
# to avoid surprises, we explicitly check for the /usr/ prefix
306+
# Python virtual environments have different prefixes
307+
# we only do this for posix_prefix, not to mangle the venv scheme
308+
# posix_prefix is used by sudo pip install
309+
# we only change the defaults here, so explicit --prefix will take precedence
310+
# https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe
311+
if (scheme == 'posix_prefix' and
312+
_PREFIX == '/usr' and
313+
'RPM_BUILD_ROOT' not in os.environ):
314+
_extend_dict(vars, _config_vars_local())
315+
else:
316+
_extend_dict(vars, get_config_vars())
317+
271318
if os.name == 'nt':
272319
# On Windows we want to substitute 'lib' for schemes rather
273320
# 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
@@ -110,8 +110,19 @@ def test_get_path(self):
110110
for scheme in _INSTALL_SCHEMES:
111111
for name in _INSTALL_SCHEMES[scheme]:
112112
expected = _INSTALL_SCHEMES[scheme][name].format(**config_vars)
113+
tested = get_path(name, scheme)
114+
# https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe
115+
if tested.startswith('/usr/local'):
116+
# /usr/local should only be used in posix_prefix
117+
self.assertEqual(scheme, 'posix_prefix')
118+
# Fedora CI runs tests for venv and virtualenv that check for other prefixes
119+
self.assertEqual(sys.prefix, '/usr')
120+
# When building the RPM of Python, %check runs this with RPM_BUILD_ROOT set
121+
# Fedora CI runs this with RPM_BUILD_ROOT unset
122+
self.assertNotIn('RPM_BUILD_ROOT', os.environ)
123+
tested = tested.replace('/usr/local', '/usr')
113124
self.assertEqual(
114-
os.path.normpath(get_path(name, scheme)),
125+
os.path.normpath(tested),
115126
os.path.normpath(expected),
116127
)
117128

@@ -344,7 +355,7 @@ def test_get_config_h_filename(self):
344355
self.assertTrue(os.path.isfile(config_h), config_h)
345356

346357
def test_get_scheme_names(self):
347-
wanted = ['nt', 'posix_home', 'posix_prefix', 'posix_venv', 'nt_venv', 'venv']
358+
wanted = ['nt', 'posix_home', 'posix_prefix', 'posix_venv', 'nt_venv', 'venv', 'rpm_prefix']
348359
if HAS_USER_BASE:
349360
wanted.extend(['nt_user', 'osx_framework_user', 'posix_user'])
350361
self.assertEqual(get_scheme_names(), tuple(sorted(wanted)))
@@ -356,6 +367,8 @@ def test_symlink(self): # Issue 7880
356367
cmd = "-c", "import sysconfig; print(sysconfig.get_platform())"
357368
self.assertEqual(py.call_real(*cmd), py.call_link(*cmd))
358369

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

0 commit comments

Comments
 (0)