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