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