Fred Drake | 70b014d | 2001-07-18 18:39:56 +0000 | [diff] [blame] | 1 | """Provide access to Python's configuration information. The specific |
| 2 | configuration variables available depend heavily on the platform and |
| 3 | configuration. The values may be retrieved using |
| 4 | get_config_var(name), and the list of variables is available via |
| 5 | get_config_vars().keys(). Additional convenience functions are also |
| 6 | available. |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 7 | |
| 8 | Written by: Fred L. Drake, Jr. |
| 9 | Email: <fdrake@acm.org> |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 10 | |
| 11 | **This module has been moved out of Distutils and will be removed from |
Tarek Ziadé | bd79768 | 2010-02-02 23:16:13 +0000 | [diff] [blame] | 12 | Python in the next version (3.3)** |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 13 | """ |
| 14 | |
Greg Ward | 82d71ca | 2000-06-03 00:44:30 +0000 | [diff] [blame] | 15 | __revision__ = "$Id$" |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 63236cf | 2007-05-25 18:39:29 +0000 | [diff] [blame] | 17 | import io |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 18 | import os |
| 19 | import re |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 20 | from warnings import warn |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 21 | |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 22 | from distutils.errors import DistutilsPlatformError |
Greg Ward | a0ca3f2 | 2000-02-02 00:05:14 +0000 | [diff] [blame] | 23 | |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 24 | # importing sysconfig from Lib |
| 25 | # to avoid this module to shadow it |
| 26 | _sysconfig = __import__('sysconfig') |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 27 | |
Tarek Ziadé | 8b441d0 | 2010-01-29 11:46:31 +0000 | [diff] [blame] | 28 | # names defined here to keep backward compatibility |
| 29 | # for APIs that were relocated |
| 30 | get_python_version = _sysconfig.get_python_version |
| 31 | get_config_h_filename = _sysconfig.get_config_h_filename |
| 32 | parse_config_h = _sysconfig.parse_config_h |
| 33 | get_config_vars = _sysconfig.get_config_vars |
| 34 | get_config_var = _sysconfig.get_config_var |
| 35 | from distutils.ccompiler import customize_compiler |
| 36 | |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 37 | _DEPRECATION_MSG = ("distutils.sysconfig.%s is deprecated. " |
| 38 | "Use the APIs provided by the sysconfig module instead") |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 39 | |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 40 | def _get_project_base(): |
| 41 | return _sysconfig._PROJECT_BASE |
| 42 | |
| 43 | project_base = _get_project_base() |
| 44 | |
| 45 | class _DeprecatedBool(int): |
| 46 | def __nonzero__(self): |
| 47 | warn(_DEPRECATION_MSG % 'get_python_version', DeprecationWarning) |
| 48 | return super(_DeprecatedBool, self).__nonzero__() |
| 49 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 50 | def _python_build(): |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 51 | return _DeprecatedBool(_sysconfig.is_python_build()) |
| 52 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 53 | python_build = _python_build() |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame] | 54 | |
Greg Ward | d38e6f7 | 2000-04-10 01:17:49 +0000 | [diff] [blame] | 55 | def get_python_inc(plat_specific=0, prefix=None): |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 56 | """This function is deprecated. |
| 57 | |
| 58 | Return the directory containing installed Python header files. |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 59 | |
| 60 | If 'plat_specific' is false (the default), this is the path to the |
| 61 | non-platform-specific header files, i.e. Python.h and so on; |
| 62 | otherwise, this is the path to platform-specific header files |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 63 | (namely pyconfig.h). |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 64 | |
Greg Ward | d38e6f7 | 2000-04-10 01:17:49 +0000 | [diff] [blame] | 65 | If 'prefix' is supplied, use it instead of sys.prefix or |
| 66 | sys.exec_prefix -- i.e., ignore 'plat_specific'. |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 67 | """ |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 68 | warn(_DEPRECATION_MSG % 'get_python_inc', DeprecationWarning) |
| 69 | get_path = _sysconfig.get_path |
| 70 | |
| 71 | if prefix is not None: |
| 72 | vars = {'base': prefix} |
| 73 | return get_path('include', vars=vars) |
| 74 | |
| 75 | if not plat_specific: |
| 76 | return get_path('include') |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 77 | else: |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 78 | return get_path('platinclude') |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 79 | |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 80 | def get_python_lib(plat_specific=False, standard_lib=False, prefix=None): |
| 81 | """This function is deprecated. |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 82 | |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 83 | Return the directory containing the Python library (standard or |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 84 | site additions). |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 85 | |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 86 | If 'plat_specific' is true, return the directory containing |
| 87 | platform-specific modules, i.e. any module from a non-pure-Python |
| 88 | module distribution; otherwise, return the platform-shared library |
| 89 | directory. If 'standard_lib' is true, return the directory |
| 90 | containing standard Python library modules; otherwise, return the |
| 91 | directory for site-specific modules. |
| 92 | |
Greg Ward | d38e6f7 | 2000-04-10 01:17:49 +0000 | [diff] [blame] | 93 | If 'prefix' is supplied, use it instead of sys.prefix or |
| 94 | sys.exec_prefix -- i.e., ignore 'plat_specific'. |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 95 | """ |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 96 | warn(_DEPRECATION_MSG % 'get_python_lib', DeprecationWarning) |
| 97 | vars = {} |
| 98 | get_path = _sysconfig.get_path |
| 99 | if prefix is not None: |
Greg Ward | dc9fe8a | 2000-08-02 01:49:40 +0000 | [diff] [blame] | 100 | if plat_specific: |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 101 | vars['platbase'] = prefix |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 102 | else: |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 103 | vars['base'] = prefix |
| 104 | if standard_lib: |
| 105 | if plat_specific: |
| 106 | return get_path('platstdlib', vars=vars) |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 107 | else: |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 108 | return get_path('stdlib', vars=vars) |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 109 | else: |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 110 | if plat_specific: |
| 111 | return get_path('platlib', vars=vars) |
Andrew M. Kuchling | 29c8623 | 2002-11-04 19:53:24 +0000 | [diff] [blame] | 112 | else: |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 113 | return get_path('purelib', vars=vars) |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 114 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 115 | def get_makefile_filename(): |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 116 | """This function is deprecated. |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 117 | |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 118 | Return full pathname of installed Makefile from the Python build. |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 119 | """ |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 120 | |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 121 | warn(_DEPRECATION_MSG % 'get_makefile_filename', DeprecationWarning) |
| 122 | return _sysconfig._get_makefile_filename() |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 123 | |
| 124 | # Regexes needed for parsing Makefile (and similar syntaxes, |
| 125 | # like old-style Setup files). |
| 126 | _variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)") |
| 127 | _findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)") |
| 128 | _findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}") |
| 129 | |
Greg Ward | 3fff8d2 | 2000-09-15 00:03:13 +0000 | [diff] [blame] | 130 | def parse_makefile(fn, g=None): |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 131 | """This function is deprecated. |
| 132 | |
| 133 | Parse a Makefile-style file. |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 134 | |
| 135 | A dictionary containing name/value pairs is returned. If an |
| 136 | optional dictionary is passed in as the second argument, it is |
| 137 | used instead of a new dictionary. |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 138 | """ |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 139 | warn(_DEPRECATION_MSG % 'parse_makefile', DeprecationWarning) |
| 140 | return _sysconfig._parse_makefile(fn, g) |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 141 | |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 142 | def expand_makefile_vars(s, vars): |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 143 | """This function is deprecated. |
| 144 | |
| 145 | Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 146 | 'string' according to 'vars' (a dictionary mapping variable names to |
| 147 | values). Variables not present in 'vars' are silently expanded to the |
| 148 | empty string. The variable values in 'vars' should not contain further |
| 149 | variable expansions; if 'vars' is the output of 'parse_makefile()', |
| 150 | you're fine. Returns a variable-expanded version of 's'. |
| 151 | """ |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 152 | warn('this function will be removed in then next version of Python', |
| 153 | DeprecationWarning) |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 154 | |
| 155 | # This algorithm does multiple expansion, so if vars['foo'] contains |
| 156 | # "${bar}", it will expand ${foo} to ${bar}, and then expand |
| 157 | # ${bar}... and so forth. This is fine as long as 'vars' comes from |
| 158 | # 'parse_makefile()', which takes care of such expansions eagerly, |
| 159 | # according to make's variable expansion semantics. |
| 160 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 161 | while True: |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 162 | m = _findvar1_rx.search(s) or _findvar2_rx.search(s) |
| 163 | if m: |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 164 | (beg, end) = m.span() |
| 165 | s = s[0:beg] + vars.get(m.group(1)) + s[end:] |
| 166 | else: |
| 167 | break |
| 168 | return s |