|
58 | 58 | },
|
59 | 59 | }
|
60 | 60 |
|
| 61 | +# For a brief period of time in the Fedora 36 life cycle, |
| 62 | +# this installation scheme existed and was documented in the release notes. |
| 63 | +# For backwards compatibility, we keep it here (at least on 3.10 and 3.11). |
| 64 | +_INSTALL_SCHEMES['rpm_prefix'] = _INSTALL_SCHEMES['posix_prefix'] |
| 65 | +# Virtualenv >= 20.10.0 favors the "venv" scheme over the defaults when creating virtual environments. |
| 66 | +# See: https://github.com/pypa/virtualenv/commit/8da79db86d8a5c74d03667a40e64ff832076445e |
| 67 | +# See: https://bugs.python.org/issue45413 |
| 68 | +# "venv" should be the same as the posix_prefix for us, |
| 69 | +# so new virtual environments aren't created with paths like venv/local/bin/python. |
| 70 | +_INSTALL_SCHEMES['venv'] = _INSTALL_SCHEMES['posix_prefix'] |
61 | 71 |
|
62 | 72 | # NOTE: site.py has copy of this function.
|
63 | 73 | # Sync it when modify this function.
|
@@ -117,6 +127,19 @@ def joinuser(*args):
|
117 | 127 | },
|
118 | 128 | }
|
119 | 129 |
|
| 130 | +# This is used by distutils.command.install in the stdlib |
| 131 | +# as well as pypa/distutils (e.g. bundled in setuptools). |
| 132 | +# The self.prefix value is set to sys.prefix + /local/ |
| 133 | +# if neither RPM build nor virtual environment is |
| 134 | +# detected to make distutils install packages |
| 135 | +# into the separate location. |
| 136 | +# https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe |
| 137 | +if (not (hasattr(sys, 'real_prefix') or |
| 138 | + sys.prefix != sys.base_prefix) and |
| 139 | + 'RPM_BUILD_ROOT' not in os.environ): |
| 140 | + _prefix_addition = '/local' |
| 141 | + |
| 142 | + |
120 | 143 | _SCHEME_KEYS = ('stdlib', 'platstdlib', 'purelib', 'platlib', 'include',
|
121 | 144 | 'scripts', 'data')
|
122 | 145 |
|
@@ -211,11 +234,39 @@ def _extend_dict(target_dict, other_dict):
|
211 | 234 | target_dict[key] = value
|
212 | 235 |
|
213 | 236 |
|
| 237 | +_CONFIG_VARS_LOCAL = None |
| 238 | + |
| 239 | + |
| 240 | +def _config_vars_local(): |
| 241 | + # This function returns the config vars with prefixes amended to /usr/local |
| 242 | + # https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe |
| 243 | + global _CONFIG_VARS_LOCAL |
| 244 | + if _CONFIG_VARS_LOCAL is None: |
| 245 | + _CONFIG_VARS_LOCAL = dict(get_config_vars()) |
| 246 | + _CONFIG_VARS_LOCAL['base'] = '/usr/local' |
| 247 | + _CONFIG_VARS_LOCAL['platbase'] = '/usr/local' |
| 248 | + return _CONFIG_VARS_LOCAL |
| 249 | + |
| 250 | + |
214 | 251 | def _expand_vars(scheme, vars):
|
215 | 252 | res = {}
|
216 | 253 | if vars is None:
|
217 | 254 | vars = {}
|
218 |
| - _extend_dict(vars, get_config_vars()) |
| 255 | + |
| 256 | + # when we are not in a virtual environment or an RPM build |
| 257 | + # we change '/usr' to '/usr/local' |
| 258 | + # to avoid surprises, we explicitly check for the /usr/ prefix |
| 259 | + # Python virtual environments have different prefixes |
| 260 | + # we only do this for posix_prefix, not to mangle the venv scheme |
| 261 | + # posix_prefix is used by sudo pip install |
| 262 | + # we only change the defaults here, so explicit --prefix will take precedence |
| 263 | + # https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe |
| 264 | + if (scheme == 'posix_prefix' and |
| 265 | + _PREFIX == '/usr' and |
| 266 | + 'RPM_BUILD_ROOT' not in os.environ): |
| 267 | + _extend_dict(vars, _config_vars_local()) |
| 268 | + else: |
| 269 | + _extend_dict(vars, get_config_vars()) |
219 | 270 |
|
220 | 271 | for key, value in _INSTALL_SCHEMES[scheme].items():
|
221 | 272 | if os.name in ('posix', 'nt'):
|
|
0 commit comments