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