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