33import os
44import re
55import sys
6- import os
76from os .path import pardir , realpath
8- from configparser import RawConfigParser
97
108__all__ = [
119 'get_config_h_filename' ,
2119 'parse_config_h' ,
2220]
2321
24- # let's read the configuration file
25- # XXX _CONFIG_DIR will be set by the Makefile later
26- _CONFIG_DIR = os .path .normpath (os .path .dirname (__file__ ))
27- _CONFIG_FILE = os .path .join (_CONFIG_DIR , 'sysconfig.cfg' )
28- _SCHEMES = RawConfigParser (dict_type = dict ) # Faster than OrderedDict
29- _SCHEMES .read (_CONFIG_FILE )
30- _VAR_REPL = re .compile (r'\{([^{]*?)\}' )
31-
32-
33- def _expand_globals (config ):
34- if config .has_section ('globals' ):
35- globals = config .items ('globals' )
36- else :
37- globals = tuple ()
38-
39- sections = config .sections ()
40- for section in sections :
41- if section == 'globals' :
42- continue
43- for option , value in globals :
44- if config .has_option (section , option ):
45- continue
46- config .set (section , option , value )
47- config .remove_section ('globals' )
48-
49- # now expanding local variables defined in the cfg file
50- #
51- for section in config .sections ():
52- variables = dict (config .items (section ))
53-
54- def _replacer (matchobj ):
55- name = matchobj .group (1 )
56- if name in variables :
57- return variables [name ]
58- return matchobj .group (0 )
59-
60- for option , value in config .items (section ):
61- config .set (section , option , _VAR_REPL .sub (_replacer , value ))
62-
63- _expand_globals (_SCHEMES )
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+ },
55+ 'os2' : {
56+ 'stdlib' : '{installed_base}/Lib' ,
57+ 'platstdlib' : '{base}/Lib' ,
58+ 'purelib' : '{base}/Lib/site-packages' ,
59+ 'platlib' : '{base}/Lib/site-packages' ,
60+ 'include' : '{installed_base}/Include' ,
61+ 'platinclude' : '{installed_base}/Include' ,
62+ 'scripts' : '{base}/Scripts' ,
63+ 'data' : '{base}' ,
64+ },
65+ 'os2_home' : {
66+ 'stdlib' : '{userbase}/lib/python{py_version_short}' ,
67+ 'platstdlib' : '{userbase}/lib/python{py_version_short}' ,
68+ 'purelib' : '{userbase}/lib/python{py_version_short}/site-packages' ,
69+ 'platlib' : '{userbase}/lib/python{py_version_short}/site-packages' ,
70+ 'include' : '{userbase}/include/python{py_version_short}' ,
71+ 'scripts' : '{userbase}/bin' ,
72+ 'data' : '{userbase}' ,
73+ },
74+ 'nt_user' : {
75+ 'stdlib' : '{userbase}/Python{py_version_nodot}' ,
76+ 'platstdlib' : '{userbase}/Python{py_version_nodot}' ,
77+ 'purelib' : '{userbase}/Python{py_version_nodot}/site-packages' ,
78+ 'platlib' : '{userbase}/Python{py_version_nodot}/site-packages' ,
79+ 'include' : '{userbase}/Python{py_version_nodot}/Include' ,
80+ 'scripts' : '{userbase}/Scripts' ,
81+ 'data' : '{userbase}' ,
82+ },
83+ 'posix_user' : {
84+ 'stdlib' : '{userbase}/lib/python{py_version_short}' ,
85+ 'platstdlib' : '{userbase}/lib/python{py_version_short}' ,
86+ 'purelib' : '{userbase}/lib/python{py_version_short}/site-packages' ,
87+ 'platlib' : '{userbase}/lib/python{py_version_short}/site-packages' ,
88+ 'include' : '{userbase}/include/python{py_version_short}' ,
89+ 'scripts' : '{userbase}/bin' ,
90+ 'data' : '{userbase}' ,
91+ },
92+ 'osx_framework_user' : {
93+ 'stdlib' : '{userbase}/lib/python' ,
94+ 'platstdlib' : '{userbase}/lib/python' ,
95+ 'purelib' : '{userbase}/lib/python/site-packages' ,
96+ 'platlib' : '{userbase}/lib/python/site-packages' ,
97+ 'include' : '{userbase}/include' ,
98+ 'scripts' : '{userbase}/bin' ,
99+ 'data' : '{userbase}' ,
100+ },
101+ }
102+
103+ _SCHEME_KEYS = ('stdlib' , 'platstdlib' , 'purelib' , 'platlib' , 'include' ,
104+ 'scripts' , 'data' )
64105
65106 # FIXME don't rely on sys.version here, its format is an implementation detail
66107 # of CPython, use sys.version_info or sys.hexversion
@@ -118,25 +159,18 @@ def is_python_build(check_home=False):
118159
119160if _PYTHON_BUILD :
120161 for scheme in ('posix_prefix' , 'posix_home' ):
121- _SCHEMES .set (scheme , 'include' , '{srcdir}/Include' )
122- _SCHEMES .set (scheme , 'platinclude' , '{projectbase}/.' )
123-
162+ _INSTALL_SCHEMES [scheme ]['include' ] = '{srcdir}/Include'
163+ _INSTALL_SCHEMES [scheme ]['platinclude' ] = '{projectbase}/.'
124164
125- def _subst_vars (path , local_vars ):
126- """In the string `path`, replace tokens like {some.thing} with the
127- corresponding value from the map `local_vars`.
128-
129- If there is no corresponding value, leave the token unchanged.
130- """
131- def _replacer (matchobj ):
132- name = matchobj .group (1 )
133- if name in local_vars :
134- return local_vars [name ]
135- elif name in os .environ :
136- return os .environ [name ]
137- return matchobj .group (0 )
138- return _VAR_REPL .sub (_replacer , path )
139165
166+ def _subst_vars (s , local_vars ):
167+ try :
168+ return s .format (** local_vars )
169+ except KeyError :
170+ try :
171+ return s .format (** os .environ )
172+ except KeyError as var :
173+ raise AttributeError ('{%s}' % var )
140174
141175def _extend_dict (target_dict , other_dict ):
142176 target_keys = target_dict .keys ()
@@ -152,22 +186,13 @@ def _expand_vars(scheme, vars):
152186 vars = {}
153187 _extend_dict (vars , get_config_vars ())
154188
155- for key , value in _SCHEMES .items (scheme ):
189+ for key , value in _INSTALL_SCHEMES [ scheme ] .items ():
156190 if os .name in ('posix' , 'nt' ):
157191 value = os .path .expanduser (value )
158192 res [key ] = os .path .normpath (_subst_vars (value , vars ))
159193 return res
160194
161195
162- def format_value (value , vars ):
163- def _replacer (matchobj ):
164- name = matchobj .group (1 )
165- if name in vars :
166- return vars [name ]
167- return matchobj .group (0 )
168- return _VAR_REPL .sub (_replacer , value )
169-
170-
171196def _get_default_scheme ():
172197 if os .name == 'posix' :
173198 # the default scheme for posix is posix_prefix
@@ -435,13 +460,12 @@ def get_config_h_filename():
435460
436461def get_scheme_names ():
437462 """Return a tuple containing the schemes names."""
438- return tuple (sorted (_SCHEMES . sections () ))
463+ return tuple (sorted (_INSTALL_SCHEMES ))
439464
440465
441466def get_path_names ():
442467 """Return a tuple containing the paths names."""
443- # xxx see if we want a static list
444- return _SCHEMES .options ('posix_prefix' )
468+ return _SCHEME_KEYS
445469
446470
447471def get_paths (scheme = _get_default_scheme (), vars = None , expand = True ):
@@ -453,7 +477,7 @@ def get_paths(scheme=_get_default_scheme(), vars=None, expand=True):
453477 if expand :
454478 return _expand_vars (scheme , vars )
455479 else :
456- return dict ( _SCHEMES . items ( scheme ))
480+ return _INSTALL_SCHEMES [ scheme ]
457481
458482
459483def get_path (name , scheme = _get_default_scheme (), vars = None , expand = True ):
0 commit comments