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