Debian Codegraph @

lib-python/3/_distutils_system_mod.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 """
2 Apply Debian-specific patches to distutils commands.
3
4 Extracts the customized behavior from patches as reported
5 in pypa/distutils#2 and applies those customizations (except
6 for scheme definitions) to those commands.
7
8 Place this module somewhere in sys.path to take effect.
9 """
10
11 import os
12 import sys
13 import sysconfig
14
15 import distutils.sysconfig
16 import distutils.command.install as orig_install
17 import distutils.command.install_egg_info as orig_install_egg_info
18 from distutils.command.install_egg_info import (
19 to_filename,
20 safe_name,
21 safe_version,
22 )
23 from distutils.errors import DistutilsOptionError
24
25
26 class install(orig_install.install):
27 user_options = list(orig_install.install.user_options) + [
28 ('install-layout=', None,
29 "installation layout to choose (known values: deb, unix)"),
30 ]
31
32 def initialize_options(self):
33 super().initialize_options()
34 self.prefix_option = None
35 self.install_layout = None
36
37 def select_scheme(self, name):
38 if name == "posix_prefix":
39 if self.install_layout:
40 if self.install_layout.lower() in ['deb']:
41 name = "deb_system"
42 elif self.install_layout.lower() in ['unix']:
43 name = "posix_prefix"
44 else:
45 raise DistutilsOptionError(
46 "unknown value for --install-layout")
47 elif ((self.prefix_option and
48 os.path.normpath(self.prefix) != '/usr/local')
49 or is_virtual_environment()):
50 name = "posix_prefix"
51 else:
52 if os.path.normpath(self.prefix) == '/usr/local':
53 self.prefix = self.exec_prefix = '/usr'
54 self.install_base = self.install_platbase = '/usr'
55 name = "posix_local"
56 super().select_scheme(name)
57
58 def finalize_unix(self):
59 self.prefix_option = self.prefix
60 super().finalize_unix()
61
62
63 class install_egg_info(orig_install_egg_info.install_egg_info):
64 user_options = list(orig_install_egg_info.install_egg_info.user_options) + [
65 ('install-layout', None, "custom installation layout"),
66 ]
67
68 def initialize_options(self):
69 super().initialize_options()
70 self.prefix_option = None
71 self.install_layout = None
72
73 def finalize_options(self):
74 self.set_undefined_options('install',('install_layout','install_layout'))
75 self.set_undefined_options('install',('prefix_option','prefix_option'))
76 super().finalize_options()
77
78 @property
79 def basename(self):
80 if self.install_layout:
81 if not self.install_layout.lower() in ['deb', 'unix']:
82 raise DistutilsOptionError(
83 "unknown value for --install-layout")
84 no_pyver = (self.install_layout.lower() == 'deb')
85 elif self.prefix_option:
86 no_pyver = False
87 else:
88 no_pyver = True
89 if no_pyver:
90 basename = "%s-%s.egg-info" % (
91 to_filename(safe_name(self.distribution.get_name())),
92 to_filename(safe_version(self.distribution.get_version()))
93 )
94 else:
95 basename = "%s-%s-py%d.%d.egg-info" % (
96 to_filename(safe_name(self.distribution.get_name())),
97 to_filename(safe_version(self.distribution.get_version())),
98 *sys.version_info[:2]
99 )
100 return basename
101
102
103 def is_virtual_environment():
104 return sys.base_prefix != sys.prefix or hasattr(sys, "real_prefix")
105
106
107 def _posix_lib(standard_lib, libpython, early_prefix, prefix):
108 is_default_prefix = not early_prefix or os.path.normpath(early_prefix) in ('/usr', '/usr/local')
109 if standard_lib:
110 return libpython
111 elif is_default_prefix and not is_virtual_environment():
112 return os.path.join(prefix, "lib", "python3", "dist-packages")
113 else:
114 return os.path.join(libpython, "site-packages")
115
116
117 def _inject_headers(name, scheme):
118 """
119 Given a scheme name and the resolved scheme,
120 if the scheme does not include headers, resolve
121 the fallback scheme for the name and use headers
122 from it. pypa/distutils#88
123
124 headers: module headers install location (posix_local is /local/ prefixed)
125 include: cpython headers (Python.h)
126 See also: bpo-44445
127 """
128 if 'headers' not in scheme:
129 if name == 'posix_prefix':
130 headers = scheme['include']
131 else:
132 headers = orig_install.INSTALL_SCHEMES['posix_prefix']['headers']
133 if name == 'posix_local' and '/local/' not in headers:
134 headers = headers.replace('/include/', '/local/include/')
135 scheme['headers'] = headers
136 return scheme
137
138
139 def load_schemes_wrapper(_load_schemes):
140 """
141 Implement the _inject_headers modification, above, but before
142 _inject_headers() was introduced, upstream. So, slower and messier.
143 """
144 def wrapped_load_schemes():
145 schemes = _load_schemes()
146 for name, scheme in schemes.items():
147 _inject_headers(name, scheme)
148 return schemes
149 return wrapped_load_schemes
150
151
152 def add_debian_schemes(schemes):
153 """
154 Ensure that the custom schemes we refer to above are present in schemes.
155 """
156 for name in ('posix_prefix', 'posix_local', 'deb_system'):
157 if name not in schemes:
158 scheme = sysconfig.get_paths(name, expand=False)
159 schemes[name] = _inject_headers(name, scheme)
160
161
162 def apply_customizations():
163 orig_install.install = install
164 orig_install_egg_info.install_egg_info = install_egg_info
165 distutils.sysconfig._posix_lib = _posix_lib
166
167 if hasattr(orig_install, '_inject_headers'):
168 # setuptools-bundled distutils >= 60.0.5
169 orig_install._inject_headers = _inject_headers
170 elif hasattr(orig_install, '_load_schemes'):
171 # setuptools-bundled distutils >= 59.2.0
172 orig_install._load_schemes = load_schemes_wrapper(orig_install._load_schemes)
173 else:
174 # older version with only statically defined schemes
175 # this includes the version bundled with Python 3.10 that has our
176 # schemes already included
177 add_debian_schemes(orig_install.INSTALL_SCHEMES)
178
179
180 apply_customizations()