Skip to content

Commit 98bca70

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. Set values of prefix and exec_prefix in distutils install command to /usr/local if executable is /usr/bin/python* and RPM build is not detected to make distutils and pypa/distutils 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 52351e7 commit 98bca70

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

Lib/distutils/command/install.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ class install(Command):
159159

160160
negative_opt = {'no-compile' : 'compile'}
161161

162+
# Allow Fedora to add components to the prefix
163+
_prefix_addition = getattr(sysconfig, '_prefix_addition', '')
162164

163165
def initialize_options(self):
164166
"""Initializes options."""
@@ -441,8 +443,10 @@ def finalize_unix(self):
441443
raise DistutilsOptionError(
442444
"must not supply exec-prefix without prefix")
443445

444-
self.prefix = os.path.normpath(sys.prefix)
445-
self.exec_prefix = os.path.normpath(sys.exec_prefix)
446+
self.prefix = (
447+
os.path.normpath(sys.prefix) + self._prefix_addition)
448+
self.exec_prefix = (
449+
os.path.normpath(sys.exec_prefix) + self._prefix_addition)
446450

447451
else:
448452
if self.exec_prefix is None:

Lib/site.py

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

379379
def addsitepackages(known_paths, prefixes=None):
380-
"""Add site-packages to sys.path"""
380+
"""Add site-packages to sys.path
381+
382+
'/usr/local' is included in PREFIXES if RPM build is not detected
383+
to make packages installed into this location visible.
384+
385+
"""
381386
_trace("Processing global site-packages")
387+
if ENABLE_USER_SITE and 'RPM_BUILD_ROOT' not in os.environ:
388+
PREFIXES.insert(0, "/usr/local")
382389
for sitedir in getsitepackages(prefixes):
383390
if os.path.isdir(sitedir):
384391
addsitedir(sitedir, known_paths)

Lib/sysconfig.py

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

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

107112
# NOTE: site.py has copy of this function.
108113
# Sync it when modify this function.
@@ -162,6 +167,19 @@ def joinuser(*args):
162167
},
163168
}
164169

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

@@ -258,11 +276,40 @@ def _extend_dict(target_dict, other_dict):
258276
target_dict[key] = value
259277

260278

279+
_CONFIG_VARS_LOCAL = None
280+
281+
282+
def _config_vars_local():
283+
# This function returns the config vars with prefixes amended to /usr/local
284+
# https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe
285+
global _CONFIG_VARS_LOCAL
286+
if _CONFIG_VARS_LOCAL is None:
287+
_CONFIG_VARS_LOCAL = dict(get_config_vars())
288+
_CONFIG_VARS_LOCAL['base'] = '/usr/local'
289+
_CONFIG_VARS_LOCAL['platbase'] = '/usr/local'
290+
return _CONFIG_VARS_LOCAL
291+
292+
261293
def _expand_vars(scheme, vars):
262294
res = {}
263295
if vars is None:
264296
vars = {}
265-
_extend_dict(vars, get_config_vars())
297+
298+
# when we are not in a virtual environment or an RPM build
299+
# we change '/usr' to '/usr/local'
300+
# to avoid surprises, we explicitly check for the /usr/ prefix
301+
# Python virtual environments have different prefixes
302+
# we only do this for posix_prefix, not to mangle the venv scheme
303+
# posix_prefix is used by sudo pip install
304+
# we only change the defaults here, so explicit --prefix will take precedence
305+
# https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe
306+
if (scheme == 'posix_prefix' and
307+
_PREFIX == '/usr' and
308+
'RPM_BUILD_ROOT' not in os.environ):
309+
_extend_dict(vars, _config_vars_local())
310+
else:
311+
_extend_dict(vars, get_config_vars())
312+
266313
if os.name == 'nt':
267314
# On Windows we want to substitute 'lib' for schemes rather
268315
# 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
@@ -111,8 +111,19 @@ def test_get_path(self):
111111
for scheme in _INSTALL_SCHEMES:
112112
for name in _INSTALL_SCHEMES[scheme]:
113113
expected = _INSTALL_SCHEMES[scheme][name].format(**config_vars)
114+
tested = get_path(name, scheme)
115+
# https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe
116+
if tested.startswith('/usr/local'):
117+
# /usr/local should only be used in posix_prefix
118+
self.assertEqual(scheme, 'posix_prefix')
119+
# Fedora CI runs tests for venv and virtualenv that check for other prefixes
120+
self.assertEqual(sys.prefix, '/usr')
121+
# When building the RPM of Python, %check runs this with RPM_BUILD_ROOT set
122+
# Fedora CI runs this with RPM_BUILD_ROOT unset
123+
self.assertNotIn('RPM_BUILD_ROOT', os.environ)
124+
tested = tested.replace('/usr/local', '/usr')
114125
self.assertEqual(
115-
os.path.normpath(get_path(name, scheme)),
126+
os.path.normpath(tested),
116127
os.path.normpath(expected),
117128
)
118129

@@ -336,7 +347,7 @@ def test_get_config_h_filename(self):
336347
self.assertTrue(os.path.isfile(config_h), config_h)
337348

338349
def test_get_scheme_names(self):
339-
wanted = ['nt', 'posix_home', 'posix_prefix', 'posix_venv', 'nt_venv', 'venv']
350+
wanted = ['nt', 'posix_home', 'posix_prefix', 'posix_venv', 'nt_venv', 'venv', 'rpm_prefix']
340351
if HAS_USER_BASE:
341352
wanted.extend(['nt_user', 'osx_framework_user', 'posix_user'])
342353
self.assertEqual(get_scheme_names(), tuple(sorted(wanted)))
@@ -348,6 +359,8 @@ def test_symlink(self): # Issue 7880
348359
cmd = "-c", "import sysconfig; print(sysconfig.get_platform())"
349360
self.assertEqual(py.call_real(*cmd), py.call_link(*cmd))
350361

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

0 commit comments

Comments
 (0)