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