Fix an amazing number of typos & malformed sentences reported by Detlef
[python/dscho.git] / Lib / distutils / sysconfig.py
blobe71ae4668a1fd21bd38e101edb77935ab8273f96
1 """Provide access to Python's configuration information. The specific names
2 defined in the module depend heavily on the platform and configuration.
4 Written by: Fred L. Drake, Jr.
5 Email: <fdrake@acm.org>
6 Initial date: 17-Dec-1998
7 """
9 __version__ = "$Revision$"
11 import os
12 import re
13 import string
14 import sys
17 def get_config_h_filename():
18 """Return full pathname of installed config.h file."""
19 return os.path.join(sys.exec_prefix, "include", "python" + sys.version[:3],
20 "config.h")
22 def get_makefile_filename():
23 """Return full pathname of installed Makefile from the Python build."""
24 return os.path.join(sys.exec_prefix, "lib", "python" + sys.version[:3],
25 "config", "Makefile")
27 def parse_config_h(fp, g=None):
28 """Parse a config.h-style file. A dictionary containing name/value
29 pairs is returned. If an optional dictionary is passed in as the second
30 argument, it is used instead of a new dictionary.
31 """
32 if g is None:
33 g = {}
34 define_rx = re.compile("#define ([A-Z][A-Z0-9_]+) (.*)\n")
35 undef_rx = re.compile("/[*] #undef ([A-Z][A-Z0-9_]+) [*]/\n")
37 while 1:
38 line = fp.readline()
39 if not line:
40 break
41 m = define_rx.match(line)
42 if m:
43 n, v = m.group(1, 2)
44 try: v = string.atoi(v)
45 except ValueError: pass
46 g[n] = v
47 else:
48 m = undef_rx.match(line)
49 if m:
50 g[m.group(1)] = 0
51 return g
53 def parse_makefile(fp, g=None):
54 """Parse a Makefile-style file. A dictionary containing name/value
55 pairs is returned. If an optional dictionary is passed in as the second
56 argument, it is used instead of a new dictionary.
57 """
58 if g is None:
59 g = {}
60 variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)\n")
61 done = {}
62 notdone = {}
64 while 1:
65 line = fp.readline()
66 if not line:
67 break
68 m = variable_rx.match(line)
69 if m:
70 n, v = m.group(1, 2)
71 v = string.strip(v)
72 if "$" in v:
73 notdone[n] = v
74 else:
75 try: v = string.atoi(v)
76 except ValueError: pass
77 done[n] = v
79 # do variable interpolation here
80 findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
81 findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
82 while notdone:
83 for name in notdone.keys():
84 value = notdone[name]
85 m = findvar1_rx.search(value)
86 if not m:
87 m = findvar2_rx.search(value)
88 if m:
89 n = m.group(1)
90 if done.has_key(n):
91 after = value[m.end():]
92 value = value[:m.start()] + done[n] + after
93 if "$" in after:
94 notdone[name] = value
95 else:
96 try: value = string.atoi(value)
97 except ValueError: pass
98 done[name] = string.strip(value)
99 del notdone[name]
100 elif notdone.has_key(n):
101 # get it on a subsequent round
102 pass
103 else:
104 done[n] = ""
105 after = value[m.end():]
106 value = value[:m.start()] + after
107 if "$" in after:
108 notdone[name] = value
109 else:
110 try: value = string.atoi(value)
111 except ValueError: pass
112 done[name] = string.strip(value)
113 del notdone[name]
114 else:
115 # bogus variable reference; just drop it since we can't deal
116 del notdone[name]
118 # save the results in the global dictionary
119 g.update(done)
120 return g
123 def _init_posix():
124 """Initialize the module as appropriate for POSIX systems."""
125 g = globals()
126 # load the installed config.h:
127 parse_config_h(open(get_config_h_filename()), g)
128 # load the installed Makefile.pre.in:
129 parse_makefile(open(get_makefile_filename()), g)
133 try:
134 exec "_init_" + os.name
135 except NameError:
136 # not needed for this platform
137 pass
138 else:
139 exec "_init_%s()" % os.name
141 del _init_posix