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