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