Skip to content

Commit 341e8a9

Browse files
bpo-41282: (PEP 632) Load install schemes from sysconfig (GH-24549)
With this patch, `distutils.command.install.INSTALL_SCHEMES` are loaded from `sysconfig._INSTALL_SCHEMES`. The distutils module is deprecated and will be removed in 3.12 (PEP 632). This change makes the `sysconfig._INSTALL_SCHEMES` the single point of truth for install schemes while keeping `distutils.command.install.INSTALL_SCHEMES` exactly the same. If we, during the transition to the sysconfig, change something, this makes sure that it also propagates to distutils until the module gets removed. Moreover, as discussed [on Discourse], Linux distros need to patch distutils/sysconfig to make sure the packages will land in proper locations. This patch makes it easier because it leaves only one location where install schemes are defined which is much easier to patch/adjust. [on Discourse]: https://discuss.python.org/t/pep-632-deprecate-distutils-module/5134 The implementation is slightly different than the plan but I think it's the easiest way how to do it and it also makes the downstream patch simple, flexible and easy to maintain. It's also necessary to implement this before setuptools starts bundling the distutils module so the default install schemes stay in the standard library. The removed code from sysconfig does not seem to have any negative effect because, honestly, it seems that nothing actually uses the install schemes from sysconfig at all. There were many big changes in these modules where they were trying to include packaging in stdlib and then reverted that. Also, the test of distutils install command does not count with the different locations which is good evidence that the reason to have this piece of code is no longer valid. https://bugs.python.org/issue41282
1 parent def9193 commit 341e8a9

File tree

3 files changed

+43
-38
lines changed

3 files changed

+43
-38
lines changed

Lib/distutils/command/install.py

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
Implements the Distutils 'install' command."""
44

55
import sys
6+
import sysconfig
67
import os
8+
import re
79

810
from distutils import log
911
from distutils.core import Command
@@ -20,33 +22,45 @@
2022

2123
HAS_USER_SITE = (USER_SITE is not None)
2224

23-
WINDOWS_SCHEME = {
24-
'purelib': '$base/Lib/site-packages',
25-
'platlib': '$base/Lib/site-packages',
26-
'headers': '$base/Include/$dist_name',
27-
'scripts': '$base/Scripts',
28-
'data' : '$base',
29-
}
30-
31-
INSTALL_SCHEMES = {
32-
'unix_prefix': {
33-
'purelib': '$base/lib/python$py_version_short/site-packages',
34-
'platlib': '$platbase/$platlibdir/python$py_version_short/site-packages',
35-
'headers': '$base/include/python$py_version_short$abiflags/$dist_name',
36-
'scripts': '$base/bin',
37-
'data' : '$base',
38-
},
39-
'unix_home': {
40-
'purelib': '$base/lib/python',
41-
'platlib': '$base/$platlibdir/python',
42-
'headers': '$base/include/python/$dist_name',
43-
'scripts': '$base/bin',
44-
'data' : '$base',
45-
},
46-
'nt': WINDOWS_SCHEME,
47-
}
48-
49-
# user site schemes
25+
# The keys to an installation scheme; if any new types of files are to be
26+
# installed, be sure to add an entry to every scheme in
27+
# sysconfig._INSTALL_SCHEMES, and to SCHEME_KEYS here.
28+
SCHEME_KEYS = ('purelib', 'platlib', 'headers', 'scripts', 'data')
29+
30+
# The following code provides backward-compatible INSTALL_SCHEMES
31+
# while making the sysconfig module the single point of truth.
32+
# This makes it easier for OS distributions where they need to
33+
# alter locations for packages installations in a single place.
34+
# Note that this module is depracated (PEP 632); all consumers
35+
# of this information should switch to using sysconfig directly.
36+
INSTALL_SCHEMES = {"unix_prefix": {}, "unix_home": {}, "nt": {}}
37+
38+
# Copy from sysconfig._INSTALL_SCHEMES
39+
for key in SCHEME_KEYS:
40+
sys_key = key
41+
if key == "headers":
42+
sys_key = "include"
43+
INSTALL_SCHEMES["unix_prefix"][key] = sysconfig._INSTALL_SCHEMES["posix_prefix"][sys_key]
44+
INSTALL_SCHEMES["unix_home"][key] = sysconfig._INSTALL_SCHEMES["posix_home"][sys_key]
45+
INSTALL_SCHEMES["nt"][key] = sysconfig._INSTALL_SCHEMES["nt"][sys_key]
46+
47+
# Transformation to different template format
48+
for main_key in INSTALL_SCHEMES:
49+
for key, value in INSTALL_SCHEMES[main_key].items():
50+
# Change all ocurences of {variable} to $variable
51+
value = re.sub(r"\{(.+?)\}", r"$\g<1>", value)
52+
value = value.replace("$installed_base", "$base")
53+
value = value.replace("$py_version_nodot_plat", "$py_version_nodot")
54+
if key == "headers":
55+
value += "/$dist_name"
56+
if sys.version_info >= (3, 9) and key == "platlib":
57+
# platlibdir is available since 3.9: bpo-1294959
58+
value = value.replace("/lib/", "/$platlibdir/")
59+
INSTALL_SCHEMES[main_key][key] = value
60+
61+
# The following part of INSTALL_SCHEMES has a different definition
62+
# than the one in sysconfig, but because both depend on the site module,
63+
# the outcomes should be the same.
5064
if HAS_USER_SITE:
5165
INSTALL_SCHEMES['nt_user'] = {
5266
'purelib': '$usersite',
@@ -65,11 +79,6 @@
6579
'data' : '$userbase',
6680
}
6781

68-
# The keys to an installation scheme; if any new types of files are to be
69-
# installed, be sure to add an entry to every installation scheme above,
70-
# and to SCHEME_KEYS here.
71-
SCHEME_KEYS = ('purelib', 'platlib', 'headers', 'scripts', 'data')
72-
7382

7483
class install(Command):
7584

Lib/sysconfig.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,6 @@ def is_python_build(check_home=False):
176176

177177
_PYTHON_BUILD = is_python_build(True)
178178

179-
if _PYTHON_BUILD:
180-
for scheme in ('posix_prefix', 'posix_home'):
181-
_INSTALL_SCHEMES[scheme]['include'] = '{srcdir}/Include'
182-
_INSTALL_SCHEMES[scheme]['platinclude'] = '{projectbase}/.'
183-
184-
185179
def _subst_vars(s, local_vars):
186180
try:
187181
return s.format(**local_vars)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Install schemes in :mod:`distutils.command.install` are now loaded from
2+
:mod:`sysconfig`.

0 commit comments

Comments
 (0)