1 | n/a | """Access to Python's configuration information.""" |
---|
2 | n/a | |
---|
3 | n/a | import os |
---|
4 | n/a | import sys |
---|
5 | n/a | from os.path import pardir, realpath |
---|
6 | n/a | |
---|
7 | n/a | __all__ = [ |
---|
8 | n/a | 'get_config_h_filename', |
---|
9 | n/a | 'get_config_var', |
---|
10 | n/a | 'get_config_vars', |
---|
11 | n/a | 'get_makefile_filename', |
---|
12 | n/a | 'get_path', |
---|
13 | n/a | 'get_path_names', |
---|
14 | n/a | 'get_paths', |
---|
15 | n/a | 'get_platform', |
---|
16 | n/a | 'get_python_version', |
---|
17 | n/a | 'get_scheme_names', |
---|
18 | n/a | 'parse_config_h', |
---|
19 | n/a | ] |
---|
20 | n/a | |
---|
21 | n/a | _INSTALL_SCHEMES = { |
---|
22 | n/a | 'posix_prefix': { |
---|
23 | n/a | 'stdlib': '{installed_base}/lib/python{py_version_short}', |
---|
24 | n/a | 'platstdlib': '{platbase}/lib/python{py_version_short}', |
---|
25 | n/a | 'purelib': '{base}/lib/python{py_version_short}/site-packages', |
---|
26 | n/a | 'platlib': '{platbase}/lib/python{py_version_short}/site-packages', |
---|
27 | n/a | 'include': |
---|
28 | n/a | '{installed_base}/include/python{py_version_short}{abiflags}', |
---|
29 | n/a | 'platinclude': |
---|
30 | n/a | '{installed_platbase}/include/python{py_version_short}{abiflags}', |
---|
31 | n/a | 'scripts': '{base}/bin', |
---|
32 | n/a | 'data': '{base}', |
---|
33 | n/a | }, |
---|
34 | n/a | 'posix_home': { |
---|
35 | n/a | 'stdlib': '{installed_base}/lib/python', |
---|
36 | n/a | 'platstdlib': '{base}/lib/python', |
---|
37 | n/a | 'purelib': '{base}/lib/python', |
---|
38 | n/a | 'platlib': '{base}/lib/python', |
---|
39 | n/a | 'include': '{installed_base}/include/python', |
---|
40 | n/a | 'platinclude': '{installed_base}/include/python', |
---|
41 | n/a | 'scripts': '{base}/bin', |
---|
42 | n/a | 'data': '{base}', |
---|
43 | n/a | }, |
---|
44 | n/a | 'nt': { |
---|
45 | n/a | 'stdlib': '{installed_base}/Lib', |
---|
46 | n/a | 'platstdlib': '{base}/Lib', |
---|
47 | n/a | 'purelib': '{base}/Lib/site-packages', |
---|
48 | n/a | 'platlib': '{base}/Lib/site-packages', |
---|
49 | n/a | 'include': '{installed_base}/Include', |
---|
50 | n/a | 'platinclude': '{installed_base}/Include', |
---|
51 | n/a | 'scripts': '{base}/Scripts', |
---|
52 | n/a | 'data': '{base}', |
---|
53 | n/a | }, |
---|
54 | n/a | 'nt_user': { |
---|
55 | n/a | 'stdlib': '{userbase}/Python{py_version_nodot}', |
---|
56 | n/a | 'platstdlib': '{userbase}/Python{py_version_nodot}', |
---|
57 | n/a | 'purelib': '{userbase}/Python{py_version_nodot}/site-packages', |
---|
58 | n/a | 'platlib': '{userbase}/Python{py_version_nodot}/site-packages', |
---|
59 | n/a | 'include': '{userbase}/Python{py_version_nodot}/Include', |
---|
60 | n/a | 'scripts': '{userbase}/Python{py_version_nodot}/Scripts', |
---|
61 | n/a | 'data': '{userbase}', |
---|
62 | n/a | }, |
---|
63 | n/a | 'posix_user': { |
---|
64 | n/a | 'stdlib': '{userbase}/lib/python{py_version_short}', |
---|
65 | n/a | 'platstdlib': '{userbase}/lib/python{py_version_short}', |
---|
66 | n/a | 'purelib': '{userbase}/lib/python{py_version_short}/site-packages', |
---|
67 | n/a | 'platlib': '{userbase}/lib/python{py_version_short}/site-packages', |
---|
68 | n/a | 'include': '{userbase}/include/python{py_version_short}', |
---|
69 | n/a | 'scripts': '{userbase}/bin', |
---|
70 | n/a | 'data': '{userbase}', |
---|
71 | n/a | }, |
---|
72 | n/a | 'osx_framework_user': { |
---|
73 | n/a | 'stdlib': '{userbase}/lib/python', |
---|
74 | n/a | 'platstdlib': '{userbase}/lib/python', |
---|
75 | n/a | 'purelib': '{userbase}/lib/python/site-packages', |
---|
76 | n/a | 'platlib': '{userbase}/lib/python/site-packages', |
---|
77 | n/a | 'include': '{userbase}/include', |
---|
78 | n/a | 'scripts': '{userbase}/bin', |
---|
79 | n/a | 'data': '{userbase}', |
---|
80 | n/a | }, |
---|
81 | n/a | } |
---|
82 | n/a | |
---|
83 | n/a | _SCHEME_KEYS = ('stdlib', 'platstdlib', 'purelib', 'platlib', 'include', |
---|
84 | n/a | 'scripts', 'data') |
---|
85 | n/a | |
---|
86 | n/a | # FIXME don't rely on sys.version here, its format is an implementation detail |
---|
87 | n/a | # of CPython, use sys.version_info or sys.hexversion |
---|
88 | n/a | _PY_VERSION = sys.version.split()[0] |
---|
89 | n/a | _PY_VERSION_SHORT = '%d.%d' % sys.version_info[:2] |
---|
90 | n/a | _PY_VERSION_SHORT_NO_DOT = '%d%d' % sys.version_info[:2] |
---|
91 | n/a | _PREFIX = os.path.normpath(sys.prefix) |
---|
92 | n/a | _BASE_PREFIX = os.path.normpath(sys.base_prefix) |
---|
93 | n/a | _EXEC_PREFIX = os.path.normpath(sys.exec_prefix) |
---|
94 | n/a | _BASE_EXEC_PREFIX = os.path.normpath(sys.base_exec_prefix) |
---|
95 | n/a | _CONFIG_VARS = None |
---|
96 | n/a | _USER_BASE = None |
---|
97 | n/a | |
---|
98 | n/a | |
---|
99 | n/a | def _safe_realpath(path): |
---|
100 | n/a | try: |
---|
101 | n/a | return realpath(path) |
---|
102 | n/a | except OSError: |
---|
103 | n/a | return path |
---|
104 | n/a | |
---|
105 | n/a | if sys.executable: |
---|
106 | n/a | _PROJECT_BASE = os.path.dirname(_safe_realpath(sys.executable)) |
---|
107 | n/a | else: |
---|
108 | n/a | # sys.executable can be empty if argv[0] has been changed and Python is |
---|
109 | n/a | # unable to retrieve the real program name |
---|
110 | n/a | _PROJECT_BASE = _safe_realpath(os.getcwd()) |
---|
111 | n/a | |
---|
112 | n/a | if (os.name == 'nt' and |
---|
113 | n/a | _PROJECT_BASE.lower().endswith(('\\pcbuild\\win32', '\\pcbuild\\amd64'))): |
---|
114 | n/a | _PROJECT_BASE = _safe_realpath(os.path.join(_PROJECT_BASE, pardir, pardir)) |
---|
115 | n/a | |
---|
116 | n/a | # set for cross builds |
---|
117 | n/a | if "_PYTHON_PROJECT_BASE" in os.environ: |
---|
118 | n/a | _PROJECT_BASE = _safe_realpath(os.environ["_PYTHON_PROJECT_BASE"]) |
---|
119 | n/a | |
---|
120 | n/a | def _is_python_source_dir(d): |
---|
121 | n/a | for fn in ("Setup.dist", "Setup.local"): |
---|
122 | n/a | if os.path.isfile(os.path.join(d, "Modules", fn)): |
---|
123 | n/a | return True |
---|
124 | n/a | return False |
---|
125 | n/a | |
---|
126 | n/a | _sys_home = getattr(sys, '_home', None) |
---|
127 | n/a | if (_sys_home and os.name == 'nt' and |
---|
128 | n/a | _sys_home.lower().endswith(('\\pcbuild\\win32', '\\pcbuild\\amd64'))): |
---|
129 | n/a | _sys_home = os.path.dirname(os.path.dirname(_sys_home)) |
---|
130 | n/a | def is_python_build(check_home=False): |
---|
131 | n/a | if check_home and _sys_home: |
---|
132 | n/a | return _is_python_source_dir(_sys_home) |
---|
133 | n/a | return _is_python_source_dir(_PROJECT_BASE) |
---|
134 | n/a | |
---|
135 | n/a | _PYTHON_BUILD = is_python_build(True) |
---|
136 | n/a | |
---|
137 | n/a | if _PYTHON_BUILD: |
---|
138 | n/a | for scheme in ('posix_prefix', 'posix_home'): |
---|
139 | n/a | _INSTALL_SCHEMES[scheme]['include'] = '{srcdir}/Include' |
---|
140 | n/a | _INSTALL_SCHEMES[scheme]['platinclude'] = '{projectbase}/.' |
---|
141 | n/a | |
---|
142 | n/a | |
---|
143 | n/a | def _subst_vars(s, local_vars): |
---|
144 | n/a | try: |
---|
145 | n/a | return s.format(**local_vars) |
---|
146 | n/a | except KeyError: |
---|
147 | n/a | try: |
---|
148 | n/a | return s.format(**os.environ) |
---|
149 | n/a | except KeyError as var: |
---|
150 | n/a | raise AttributeError('{%s}' % var) |
---|
151 | n/a | |
---|
152 | n/a | def _extend_dict(target_dict, other_dict): |
---|
153 | n/a | target_keys = target_dict.keys() |
---|
154 | n/a | for key, value in other_dict.items(): |
---|
155 | n/a | if key in target_keys: |
---|
156 | n/a | continue |
---|
157 | n/a | target_dict[key] = value |
---|
158 | n/a | |
---|
159 | n/a | |
---|
160 | n/a | def _expand_vars(scheme, vars): |
---|
161 | n/a | res = {} |
---|
162 | n/a | if vars is None: |
---|
163 | n/a | vars = {} |
---|
164 | n/a | _extend_dict(vars, get_config_vars()) |
---|
165 | n/a | |
---|
166 | n/a | for key, value in _INSTALL_SCHEMES[scheme].items(): |
---|
167 | n/a | if os.name in ('posix', 'nt'): |
---|
168 | n/a | value = os.path.expanduser(value) |
---|
169 | n/a | res[key] = os.path.normpath(_subst_vars(value, vars)) |
---|
170 | n/a | return res |
---|
171 | n/a | |
---|
172 | n/a | |
---|
173 | n/a | def _get_default_scheme(): |
---|
174 | n/a | if os.name == 'posix': |
---|
175 | n/a | # the default scheme for posix is posix_prefix |
---|
176 | n/a | return 'posix_prefix' |
---|
177 | n/a | return os.name |
---|
178 | n/a | |
---|
179 | n/a | |
---|
180 | n/a | def _getuserbase(): |
---|
181 | n/a | env_base = os.environ.get("PYTHONUSERBASE", None) |
---|
182 | n/a | |
---|
183 | n/a | def joinuser(*args): |
---|
184 | n/a | return os.path.expanduser(os.path.join(*args)) |
---|
185 | n/a | |
---|
186 | n/a | if os.name == "nt": |
---|
187 | n/a | base = os.environ.get("APPDATA") or "~" |
---|
188 | n/a | if env_base: |
---|
189 | n/a | return env_base |
---|
190 | n/a | else: |
---|
191 | n/a | return joinuser(base, "Python") |
---|
192 | n/a | |
---|
193 | n/a | if sys.platform == "darwin": |
---|
194 | n/a | framework = get_config_var("PYTHONFRAMEWORK") |
---|
195 | n/a | if framework: |
---|
196 | n/a | if env_base: |
---|
197 | n/a | return env_base |
---|
198 | n/a | else: |
---|
199 | n/a | return joinuser("~", "Library", framework, "%d.%d" % |
---|
200 | n/a | sys.version_info[:2]) |
---|
201 | n/a | |
---|
202 | n/a | if env_base: |
---|
203 | n/a | return env_base |
---|
204 | n/a | else: |
---|
205 | n/a | return joinuser("~", ".local") |
---|
206 | n/a | |
---|
207 | n/a | |
---|
208 | n/a | def _parse_makefile(filename, vars=None): |
---|
209 | n/a | """Parse a Makefile-style file. |
---|
210 | n/a | |
---|
211 | n/a | A dictionary containing name/value pairs is returned. If an |
---|
212 | n/a | optional dictionary is passed in as the second argument, it is |
---|
213 | n/a | used instead of a new dictionary. |
---|
214 | n/a | """ |
---|
215 | n/a | # Regexes needed for parsing Makefile (and similar syntaxes, |
---|
216 | n/a | # like old-style Setup files). |
---|
217 | n/a | import re |
---|
218 | n/a | _variable_rx = re.compile(r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)") |
---|
219 | n/a | _findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)") |
---|
220 | n/a | _findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}") |
---|
221 | n/a | |
---|
222 | n/a | if vars is None: |
---|
223 | n/a | vars = {} |
---|
224 | n/a | done = {} |
---|
225 | n/a | notdone = {} |
---|
226 | n/a | |
---|
227 | n/a | with open(filename, errors="surrogateescape") as f: |
---|
228 | n/a | lines = f.readlines() |
---|
229 | n/a | |
---|
230 | n/a | for line in lines: |
---|
231 | n/a | if line.startswith('#') or line.strip() == '': |
---|
232 | n/a | continue |
---|
233 | n/a | m = _variable_rx.match(line) |
---|
234 | n/a | if m: |
---|
235 | n/a | n, v = m.group(1, 2) |
---|
236 | n/a | v = v.strip() |
---|
237 | n/a | # `$$' is a literal `$' in make |
---|
238 | n/a | tmpv = v.replace('$$', '') |
---|
239 | n/a | |
---|
240 | n/a | if "$" in tmpv: |
---|
241 | n/a | notdone[n] = v |
---|
242 | n/a | else: |
---|
243 | n/a | try: |
---|
244 | n/a | v = int(v) |
---|
245 | n/a | except ValueError: |
---|
246 | n/a | # insert literal `$' |
---|
247 | n/a | done[n] = v.replace('$$', '$') |
---|
248 | n/a | else: |
---|
249 | n/a | done[n] = v |
---|
250 | n/a | |
---|
251 | n/a | # do variable interpolation here |
---|
252 | n/a | variables = list(notdone.keys()) |
---|
253 | n/a | |
---|
254 | n/a | # Variables with a 'PY_' prefix in the makefile. These need to |
---|
255 | n/a | # be made available without that prefix through sysconfig. |
---|
256 | n/a | # Special care is needed to ensure that variable expansion works, even |
---|
257 | n/a | # if the expansion uses the name without a prefix. |
---|
258 | n/a | renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS') |
---|
259 | n/a | |
---|
260 | n/a | while len(variables) > 0: |
---|
261 | n/a | for name in tuple(variables): |
---|
262 | n/a | value = notdone[name] |
---|
263 | n/a | m1 = _findvar1_rx.search(value) |
---|
264 | n/a | m2 = _findvar2_rx.search(value) |
---|
265 | n/a | if m1 and m2: |
---|
266 | n/a | m = m1 if m1.start() < m2.start() else m2 |
---|
267 | n/a | else: |
---|
268 | n/a | m = m1 if m1 else m2 |
---|
269 | n/a | if m is not None: |
---|
270 | n/a | n = m.group(1) |
---|
271 | n/a | found = True |
---|
272 | n/a | if n in done: |
---|
273 | n/a | item = str(done[n]) |
---|
274 | n/a | elif n in notdone: |
---|
275 | n/a | # get it on a subsequent round |
---|
276 | n/a | found = False |
---|
277 | n/a | elif n in os.environ: |
---|
278 | n/a | # do it like make: fall back to environment |
---|
279 | n/a | item = os.environ[n] |
---|
280 | n/a | |
---|
281 | n/a | elif n in renamed_variables: |
---|
282 | n/a | if (name.startswith('PY_') and |
---|
283 | n/a | name[3:] in renamed_variables): |
---|
284 | n/a | item = "" |
---|
285 | n/a | |
---|
286 | n/a | elif 'PY_' + n in notdone: |
---|
287 | n/a | found = False |
---|
288 | n/a | |
---|
289 | n/a | else: |
---|
290 | n/a | item = str(done['PY_' + n]) |
---|
291 | n/a | |
---|
292 | n/a | else: |
---|
293 | n/a | done[n] = item = "" |
---|
294 | n/a | |
---|
295 | n/a | if found: |
---|
296 | n/a | after = value[m.end():] |
---|
297 | n/a | value = value[:m.start()] + item + after |
---|
298 | n/a | if "$" in after: |
---|
299 | n/a | notdone[name] = value |
---|
300 | n/a | else: |
---|
301 | n/a | try: |
---|
302 | n/a | value = int(value) |
---|
303 | n/a | except ValueError: |
---|
304 | n/a | done[name] = value.strip() |
---|
305 | n/a | else: |
---|
306 | n/a | done[name] = value |
---|
307 | n/a | variables.remove(name) |
---|
308 | n/a | |
---|
309 | n/a | if name.startswith('PY_') \ |
---|
310 | n/a | and name[3:] in renamed_variables: |
---|
311 | n/a | |
---|
312 | n/a | name = name[3:] |
---|
313 | n/a | if name not in done: |
---|
314 | n/a | done[name] = value |
---|
315 | n/a | |
---|
316 | n/a | else: |
---|
317 | n/a | # bogus variable reference (e.g. "prefix=$/opt/python"); |
---|
318 | n/a | # just drop it since we can't deal |
---|
319 | n/a | done[name] = value |
---|
320 | n/a | variables.remove(name) |
---|
321 | n/a | |
---|
322 | n/a | # strip spurious spaces |
---|
323 | n/a | for k, v in done.items(): |
---|
324 | n/a | if isinstance(v, str): |
---|
325 | n/a | done[k] = v.strip() |
---|
326 | n/a | |
---|
327 | n/a | # save the results in the global dictionary |
---|
328 | n/a | vars.update(done) |
---|
329 | n/a | return vars |
---|
330 | n/a | |
---|
331 | n/a | |
---|
332 | n/a | def get_makefile_filename(): |
---|
333 | n/a | """Return the path of the Makefile.""" |
---|
334 | n/a | if _PYTHON_BUILD: |
---|
335 | n/a | return os.path.join(_sys_home or _PROJECT_BASE, "Makefile") |
---|
336 | n/a | if hasattr(sys, 'abiflags'): |
---|
337 | n/a | config_dir_name = 'config-%s%s' % (_PY_VERSION_SHORT, sys.abiflags) |
---|
338 | n/a | else: |
---|
339 | n/a | config_dir_name = 'config' |
---|
340 | n/a | if hasattr(sys.implementation, '_multiarch'): |
---|
341 | n/a | config_dir_name += '-%s' % sys.implementation._multiarch |
---|
342 | n/a | return os.path.join(get_path('stdlib'), config_dir_name, 'Makefile') |
---|
343 | n/a | |
---|
344 | n/a | |
---|
345 | n/a | def _get_sysconfigdata_name(): |
---|
346 | n/a | return os.environ.get('_PYTHON_SYSCONFIGDATA_NAME', |
---|
347 | n/a | '_sysconfigdata_{abi}_{platform}_{multiarch}'.format( |
---|
348 | n/a | abi=sys.abiflags, |
---|
349 | n/a | platform=sys.platform, |
---|
350 | n/a | multiarch=getattr(sys.implementation, '_multiarch', ''), |
---|
351 | n/a | )) |
---|
352 | n/a | |
---|
353 | n/a | |
---|
354 | n/a | def _generate_posix_vars(): |
---|
355 | n/a | """Generate the Python module containing build-time variables.""" |
---|
356 | n/a | import pprint |
---|
357 | n/a | vars = {} |
---|
358 | n/a | # load the installed Makefile: |
---|
359 | n/a | makefile = get_makefile_filename() |
---|
360 | n/a | try: |
---|
361 | n/a | _parse_makefile(makefile, vars) |
---|
362 | n/a | except OSError as e: |
---|
363 | n/a | msg = "invalid Python installation: unable to open %s" % makefile |
---|
364 | n/a | if hasattr(e, "strerror"): |
---|
365 | n/a | msg = msg + " (%s)" % e.strerror |
---|
366 | n/a | raise OSError(msg) |
---|
367 | n/a | # load the installed pyconfig.h: |
---|
368 | n/a | config_h = get_config_h_filename() |
---|
369 | n/a | try: |
---|
370 | n/a | with open(config_h) as f: |
---|
371 | n/a | parse_config_h(f, vars) |
---|
372 | n/a | except OSError as e: |
---|
373 | n/a | msg = "invalid Python installation: unable to open %s" % config_h |
---|
374 | n/a | if hasattr(e, "strerror"): |
---|
375 | n/a | msg = msg + " (%s)" % e.strerror |
---|
376 | n/a | raise OSError(msg) |
---|
377 | n/a | # On AIX, there are wrong paths to the linker scripts in the Makefile |
---|
378 | n/a | # -- these paths are relative to the Python source, but when installed |
---|
379 | n/a | # the scripts are in another directory. |
---|
380 | n/a | if _PYTHON_BUILD: |
---|
381 | n/a | vars['BLDSHARED'] = vars['LDSHARED'] |
---|
382 | n/a | |
---|
383 | n/a | # There's a chicken-and-egg situation on OS X with regards to the |
---|
384 | n/a | # _sysconfigdata module after the changes introduced by #15298: |
---|
385 | n/a | # get_config_vars() is called by get_platform() as part of the |
---|
386 | n/a | # `make pybuilddir.txt` target -- which is a precursor to the |
---|
387 | n/a | # _sysconfigdata.py module being constructed. Unfortunately, |
---|
388 | n/a | # get_config_vars() eventually calls _init_posix(), which attempts |
---|
389 | n/a | # to import _sysconfigdata, which we won't have built yet. In order |
---|
390 | n/a | # for _init_posix() to work, if we're on Darwin, just mock up the |
---|
391 | n/a | # _sysconfigdata module manually and populate it with the build vars. |
---|
392 | n/a | # This is more than sufficient for ensuring the subsequent call to |
---|
393 | n/a | # get_platform() succeeds. |
---|
394 | n/a | name = _get_sysconfigdata_name() |
---|
395 | n/a | if 'darwin' in sys.platform: |
---|
396 | n/a | import types |
---|
397 | n/a | module = types.ModuleType(name) |
---|
398 | n/a | module.build_time_vars = vars |
---|
399 | n/a | sys.modules[name] = module |
---|
400 | n/a | |
---|
401 | n/a | pybuilddir = 'build/lib.%s-%s' % (get_platform(), _PY_VERSION_SHORT) |
---|
402 | n/a | if hasattr(sys, "gettotalrefcount"): |
---|
403 | n/a | pybuilddir += '-pydebug' |
---|
404 | n/a | os.makedirs(pybuilddir, exist_ok=True) |
---|
405 | n/a | destfile = os.path.join(pybuilddir, name + '.py') |
---|
406 | n/a | |
---|
407 | n/a | with open(destfile, 'w', encoding='utf8') as f: |
---|
408 | n/a | f.write('# system configuration generated and used by' |
---|
409 | n/a | ' the sysconfig module\n') |
---|
410 | n/a | f.write('build_time_vars = ') |
---|
411 | n/a | pprint.pprint(vars, stream=f) |
---|
412 | n/a | |
---|
413 | n/a | # Create file used for sys.path fixup -- see Modules/getpath.c |
---|
414 | n/a | with open('pybuilddir.txt', 'w', encoding='ascii') as f: |
---|
415 | n/a | f.write(pybuilddir) |
---|
416 | n/a | |
---|
417 | n/a | def _init_posix(vars): |
---|
418 | n/a | """Initialize the module as appropriate for POSIX systems.""" |
---|
419 | n/a | # _sysconfigdata is generated at build time, see _generate_posix_vars() |
---|
420 | n/a | name = _get_sysconfigdata_name() |
---|
421 | n/a | _temp = __import__(name, globals(), locals(), ['build_time_vars'], 0) |
---|
422 | n/a | build_time_vars = _temp.build_time_vars |
---|
423 | n/a | vars.update(build_time_vars) |
---|
424 | n/a | |
---|
425 | n/a | def _init_non_posix(vars): |
---|
426 | n/a | """Initialize the module as appropriate for NT""" |
---|
427 | n/a | # set basic install directories |
---|
428 | n/a | vars['LIBDEST'] = get_path('stdlib') |
---|
429 | n/a | vars['BINLIBDEST'] = get_path('platstdlib') |
---|
430 | n/a | vars['INCLUDEPY'] = get_path('include') |
---|
431 | n/a | vars['EXT_SUFFIX'] = '.pyd' |
---|
432 | n/a | vars['EXE'] = '.exe' |
---|
433 | n/a | vars['VERSION'] = _PY_VERSION_SHORT_NO_DOT |
---|
434 | n/a | vars['BINDIR'] = os.path.dirname(_safe_realpath(sys.executable)) |
---|
435 | n/a | |
---|
436 | n/a | # |
---|
437 | n/a | # public APIs |
---|
438 | n/a | # |
---|
439 | n/a | |
---|
440 | n/a | |
---|
441 | n/a | def parse_config_h(fp, vars=None): |
---|
442 | n/a | """Parse a config.h-style file. |
---|
443 | n/a | |
---|
444 | n/a | A dictionary containing name/value pairs is returned. If an |
---|
445 | n/a | optional dictionary is passed in as the second argument, it is |
---|
446 | n/a | used instead of a new dictionary. |
---|
447 | n/a | """ |
---|
448 | n/a | if vars is None: |
---|
449 | n/a | vars = {} |
---|
450 | n/a | import re |
---|
451 | n/a | define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n") |
---|
452 | n/a | undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n") |
---|
453 | n/a | |
---|
454 | n/a | while True: |
---|
455 | n/a | line = fp.readline() |
---|
456 | n/a | if not line: |
---|
457 | n/a | break |
---|
458 | n/a | m = define_rx.match(line) |
---|
459 | n/a | if m: |
---|
460 | n/a | n, v = m.group(1, 2) |
---|
461 | n/a | try: |
---|
462 | n/a | v = int(v) |
---|
463 | n/a | except ValueError: |
---|
464 | n/a | pass |
---|
465 | n/a | vars[n] = v |
---|
466 | n/a | else: |
---|
467 | n/a | m = undef_rx.match(line) |
---|
468 | n/a | if m: |
---|
469 | n/a | vars[m.group(1)] = 0 |
---|
470 | n/a | return vars |
---|
471 | n/a | |
---|
472 | n/a | |
---|
473 | n/a | def get_config_h_filename(): |
---|
474 | n/a | """Return the path of pyconfig.h.""" |
---|
475 | n/a | if _PYTHON_BUILD: |
---|
476 | n/a | if os.name == "nt": |
---|
477 | n/a | inc_dir = os.path.join(_sys_home or _PROJECT_BASE, "PC") |
---|
478 | n/a | else: |
---|
479 | n/a | inc_dir = _sys_home or _PROJECT_BASE |
---|
480 | n/a | else: |
---|
481 | n/a | inc_dir = get_path('platinclude') |
---|
482 | n/a | return os.path.join(inc_dir, 'pyconfig.h') |
---|
483 | n/a | |
---|
484 | n/a | |
---|
485 | n/a | def get_scheme_names(): |
---|
486 | n/a | """Return a tuple containing the schemes names.""" |
---|
487 | n/a | return tuple(sorted(_INSTALL_SCHEMES)) |
---|
488 | n/a | |
---|
489 | n/a | |
---|
490 | n/a | def get_path_names(): |
---|
491 | n/a | """Return a tuple containing the paths names.""" |
---|
492 | n/a | return _SCHEME_KEYS |
---|
493 | n/a | |
---|
494 | n/a | |
---|
495 | n/a | def get_paths(scheme=_get_default_scheme(), vars=None, expand=True): |
---|
496 | n/a | """Return a mapping containing an install scheme. |
---|
497 | n/a | |
---|
498 | n/a | ``scheme`` is the install scheme name. If not provided, it will |
---|
499 | n/a | return the default scheme for the current platform. |
---|
500 | n/a | """ |
---|
501 | n/a | if expand: |
---|
502 | n/a | return _expand_vars(scheme, vars) |
---|
503 | n/a | else: |
---|
504 | n/a | return _INSTALL_SCHEMES[scheme] |
---|
505 | n/a | |
---|
506 | n/a | |
---|
507 | n/a | def get_path(name, scheme=_get_default_scheme(), vars=None, expand=True): |
---|
508 | n/a | """Return a path corresponding to the scheme. |
---|
509 | n/a | |
---|
510 | n/a | ``scheme`` is the install scheme name. |
---|
511 | n/a | """ |
---|
512 | n/a | return get_paths(scheme, vars, expand)[name] |
---|
513 | n/a | |
---|
514 | n/a | |
---|
515 | n/a | def get_config_vars(*args): |
---|
516 | n/a | """With no arguments, return a dictionary of all configuration |
---|
517 | n/a | variables relevant for the current platform. |
---|
518 | n/a | |
---|
519 | n/a | On Unix, this means every variable defined in Python's installed Makefile; |
---|
520 | n/a | On Windows it's a much smaller set. |
---|
521 | n/a | |
---|
522 | n/a | With arguments, return a list of values that result from looking up |
---|
523 | n/a | each argument in the configuration variable dictionary. |
---|
524 | n/a | """ |
---|
525 | n/a | global _CONFIG_VARS |
---|
526 | n/a | if _CONFIG_VARS is None: |
---|
527 | n/a | _CONFIG_VARS = {} |
---|
528 | n/a | # Normalized versions of prefix and exec_prefix are handy to have; |
---|
529 | n/a | # in fact, these are the standard versions used most places in the |
---|
530 | n/a | # Distutils. |
---|
531 | n/a | _CONFIG_VARS['prefix'] = _PREFIX |
---|
532 | n/a | _CONFIG_VARS['exec_prefix'] = _EXEC_PREFIX |
---|
533 | n/a | _CONFIG_VARS['py_version'] = _PY_VERSION |
---|
534 | n/a | _CONFIG_VARS['py_version_short'] = _PY_VERSION_SHORT |
---|
535 | n/a | _CONFIG_VARS['py_version_nodot'] = _PY_VERSION_SHORT_NO_DOT |
---|
536 | n/a | _CONFIG_VARS['installed_base'] = _BASE_PREFIX |
---|
537 | n/a | _CONFIG_VARS['base'] = _PREFIX |
---|
538 | n/a | _CONFIG_VARS['installed_platbase'] = _BASE_EXEC_PREFIX |
---|
539 | n/a | _CONFIG_VARS['platbase'] = _EXEC_PREFIX |
---|
540 | n/a | _CONFIG_VARS['projectbase'] = _PROJECT_BASE |
---|
541 | n/a | try: |
---|
542 | n/a | _CONFIG_VARS['abiflags'] = sys.abiflags |
---|
543 | n/a | except AttributeError: |
---|
544 | n/a | # sys.abiflags may not be defined on all platforms. |
---|
545 | n/a | _CONFIG_VARS['abiflags'] = '' |
---|
546 | n/a | |
---|
547 | n/a | if os.name == 'nt': |
---|
548 | n/a | _init_non_posix(_CONFIG_VARS) |
---|
549 | n/a | if os.name == 'posix': |
---|
550 | n/a | _init_posix(_CONFIG_VARS) |
---|
551 | n/a | # For backward compatibility, see issue19555 |
---|
552 | n/a | SO = _CONFIG_VARS.get('EXT_SUFFIX') |
---|
553 | n/a | if SO is not None: |
---|
554 | n/a | _CONFIG_VARS['SO'] = SO |
---|
555 | n/a | # Setting 'userbase' is done below the call to the |
---|
556 | n/a | # init function to enable using 'get_config_var' in |
---|
557 | n/a | # the init-function. |
---|
558 | n/a | _CONFIG_VARS['userbase'] = _getuserbase() |
---|
559 | n/a | |
---|
560 | n/a | # Always convert srcdir to an absolute path |
---|
561 | n/a | srcdir = _CONFIG_VARS.get('srcdir', _PROJECT_BASE) |
---|
562 | n/a | if os.name == 'posix': |
---|
563 | n/a | if _PYTHON_BUILD: |
---|
564 | n/a | # If srcdir is a relative path (typically '.' or '..') |
---|
565 | n/a | # then it should be interpreted relative to the directory |
---|
566 | n/a | # containing Makefile. |
---|
567 | n/a | base = os.path.dirname(get_makefile_filename()) |
---|
568 | n/a | srcdir = os.path.join(base, srcdir) |
---|
569 | n/a | else: |
---|
570 | n/a | # srcdir is not meaningful since the installation is |
---|
571 | n/a | # spread about the filesystem. We choose the |
---|
572 | n/a | # directory containing the Makefile since we know it |
---|
573 | n/a | # exists. |
---|
574 | n/a | srcdir = os.path.dirname(get_makefile_filename()) |
---|
575 | n/a | _CONFIG_VARS['srcdir'] = _safe_realpath(srcdir) |
---|
576 | n/a | |
---|
577 | n/a | # OS X platforms require special customization to handle |
---|
578 | n/a | # multi-architecture, multi-os-version installers |
---|
579 | n/a | if sys.platform == 'darwin': |
---|
580 | n/a | import _osx_support |
---|
581 | n/a | _osx_support.customize_config_vars(_CONFIG_VARS) |
---|
582 | n/a | |
---|
583 | n/a | if args: |
---|
584 | n/a | vals = [] |
---|
585 | n/a | for name in args: |
---|
586 | n/a | vals.append(_CONFIG_VARS.get(name)) |
---|
587 | n/a | return vals |
---|
588 | n/a | else: |
---|
589 | n/a | return _CONFIG_VARS |
---|
590 | n/a | |
---|
591 | n/a | |
---|
592 | n/a | def get_config_var(name): |
---|
593 | n/a | """Return the value of a single variable using the dictionary returned by |
---|
594 | n/a | 'get_config_vars()'. |
---|
595 | n/a | |
---|
596 | n/a | Equivalent to get_config_vars().get(name) |
---|
597 | n/a | """ |
---|
598 | n/a | if name == 'SO': |
---|
599 | n/a | import warnings |
---|
600 | n/a | warnings.warn('SO is deprecated, use EXT_SUFFIX', DeprecationWarning, 2) |
---|
601 | n/a | return get_config_vars().get(name) |
---|
602 | n/a | |
---|
603 | n/a | |
---|
604 | n/a | def get_platform(): |
---|
605 | n/a | """Return a string that identifies the current platform. |
---|
606 | n/a | |
---|
607 | n/a | This is used mainly to distinguish platform-specific build directories and |
---|
608 | n/a | platform-specific built distributions. Typically includes the OS name |
---|
609 | n/a | and version and the architecture (as supplied by 'os.uname()'), |
---|
610 | n/a | although the exact information included depends on the OS; eg. for IRIX |
---|
611 | n/a | the architecture isn't particularly important (IRIX only runs on SGI |
---|
612 | n/a | hardware), but for Linux the kernel version isn't particularly |
---|
613 | n/a | important. |
---|
614 | n/a | |
---|
615 | n/a | Examples of returned values: |
---|
616 | n/a | linux-i586 |
---|
617 | n/a | linux-alpha (?) |
---|
618 | n/a | solaris-2.6-sun4u |
---|
619 | n/a | irix-5.3 |
---|
620 | n/a | irix64-6.2 |
---|
621 | n/a | |
---|
622 | n/a | Windows will return one of: |
---|
623 | n/a | win-amd64 (64bit Windows on AMD64 (aka x86_64, Intel64, EM64T, etc) |
---|
624 | n/a | win-ia64 (64bit Windows on Itanium) |
---|
625 | n/a | win32 (all others - specifically, sys.platform is returned) |
---|
626 | n/a | |
---|
627 | n/a | For other non-POSIX platforms, currently just returns 'sys.platform'. |
---|
628 | n/a | """ |
---|
629 | n/a | if os.name == 'nt': |
---|
630 | n/a | # sniff sys.version for architecture. |
---|
631 | n/a | prefix = " bit (" |
---|
632 | n/a | i = sys.version.find(prefix) |
---|
633 | n/a | if i == -1: |
---|
634 | n/a | return sys.platform |
---|
635 | n/a | j = sys.version.find(")", i) |
---|
636 | n/a | look = sys.version[i+len(prefix):j].lower() |
---|
637 | n/a | if look == 'amd64': |
---|
638 | n/a | return 'win-amd64' |
---|
639 | n/a | if look == 'itanium': |
---|
640 | n/a | return 'win-ia64' |
---|
641 | n/a | return sys.platform |
---|
642 | n/a | |
---|
643 | n/a | if os.name != "posix" or not hasattr(os, 'uname'): |
---|
644 | n/a | # XXX what about the architecture? NT is Intel or Alpha |
---|
645 | n/a | return sys.platform |
---|
646 | n/a | |
---|
647 | n/a | # Set for cross builds explicitly |
---|
648 | n/a | if "_PYTHON_HOST_PLATFORM" in os.environ: |
---|
649 | n/a | return os.environ["_PYTHON_HOST_PLATFORM"] |
---|
650 | n/a | |
---|
651 | n/a | # Try to distinguish various flavours of Unix |
---|
652 | n/a | osname, host, release, version, machine = os.uname() |
---|
653 | n/a | |
---|
654 | n/a | # Convert the OS name to lowercase, remove '/' characters |
---|
655 | n/a | # (to accommodate BSD/OS), and translate spaces (for "Power Macintosh") |
---|
656 | n/a | osname = osname.lower().replace('/', '') |
---|
657 | n/a | machine = machine.replace(' ', '_') |
---|
658 | n/a | machine = machine.replace('/', '-') |
---|
659 | n/a | |
---|
660 | n/a | if osname[:5] == "linux": |
---|
661 | n/a | # At least on Linux/Intel, 'machine' is the processor -- |
---|
662 | n/a | # i386, etc. |
---|
663 | n/a | # XXX what about Alpha, SPARC, etc? |
---|
664 | n/a | return "%s-%s" % (osname, machine) |
---|
665 | n/a | elif osname[:5] == "sunos": |
---|
666 | n/a | if release[0] >= "5": # SunOS 5 == Solaris 2 |
---|
667 | n/a | osname = "solaris" |
---|
668 | n/a | release = "%d.%s" % (int(release[0]) - 3, release[2:]) |
---|
669 | n/a | # We can't use "platform.architecture()[0]" because a |
---|
670 | n/a | # bootstrap problem. We use a dict to get an error |
---|
671 | n/a | # if some suspicious happens. |
---|
672 | n/a | bitness = {2147483647:"32bit", 9223372036854775807:"64bit"} |
---|
673 | n/a | machine += ".%s" % bitness[sys.maxsize] |
---|
674 | n/a | # fall through to standard osname-release-machine representation |
---|
675 | n/a | elif osname[:4] == "irix": # could be "irix64"! |
---|
676 | n/a | return "%s-%s" % (osname, release) |
---|
677 | n/a | elif osname[:3] == "aix": |
---|
678 | n/a | return "%s-%s.%s" % (osname, version, release) |
---|
679 | n/a | elif osname[:6] == "cygwin": |
---|
680 | n/a | osname = "cygwin" |
---|
681 | n/a | import re |
---|
682 | n/a | rel_re = re.compile(r'[\d.]+') |
---|
683 | n/a | m = rel_re.match(release) |
---|
684 | n/a | if m: |
---|
685 | n/a | release = m.group() |
---|
686 | n/a | elif osname[:6] == "darwin": |
---|
687 | n/a | import _osx_support |
---|
688 | n/a | osname, release, machine = _osx_support.get_platform_osx( |
---|
689 | n/a | get_config_vars(), |
---|
690 | n/a | osname, release, machine) |
---|
691 | n/a | |
---|
692 | n/a | return "%s-%s-%s" % (osname, release, machine) |
---|
693 | n/a | |
---|
694 | n/a | |
---|
695 | n/a | def get_python_version(): |
---|
696 | n/a | return _PY_VERSION_SHORT |
---|
697 | n/a | |
---|
698 | n/a | |
---|
699 | n/a | def _print_dict(title, data): |
---|
700 | n/a | for index, (key, value) in enumerate(sorted(data.items())): |
---|
701 | n/a | if index == 0: |
---|
702 | n/a | print('%s: ' % (title)) |
---|
703 | n/a | print('\t%s = "%s"' % (key, value)) |
---|
704 | n/a | |
---|
705 | n/a | |
---|
706 | n/a | def _main(): |
---|
707 | n/a | """Display all information sysconfig detains.""" |
---|
708 | n/a | if '--generate-posix-vars' in sys.argv: |
---|
709 | n/a | _generate_posix_vars() |
---|
710 | n/a | return |
---|
711 | n/a | print('Platform: "%s"' % get_platform()) |
---|
712 | n/a | print('Python version: "%s"' % get_python_version()) |
---|
713 | n/a | print('Current installation scheme: "%s"' % _get_default_scheme()) |
---|
714 | n/a | print() |
---|
715 | n/a | _print_dict('Paths', get_paths()) |
---|
716 | n/a | print() |
---|
717 | n/a | _print_dict('Variables', get_config_vars()) |
---|
718 | n/a | |
---|
719 | n/a | |
---|
720 | n/a | if __name__ == '__main__': |
---|
721 | n/a | _main() |
---|