blob: e291aec21e6d31bb412f5703ccae3bf38c0c16e9 [file] [log] [blame]
Fred Drake522af3a1999-01-06 16:28:34 +00001"""Provide access to Python's configuration information. The specific names
2defined in the module depend heavily on the platform and configuration.
Greg Ward1190ee31998-12-18 23:46:33 +00003
4Written by: Fred L. Drake, Jr.
5Email: <fdrake@acm.org>
6Initial date: 17-Dec-1998
7"""
8
9__version__ = "$Revision$"
10
Greg Ward9ddaaa11999-01-06 14:46:06 +000011import os
12import re
13import string
14import sys
Greg Ward1190ee31998-12-18 23:46:33 +000015
Greg Warda0ca3f22000-02-02 00:05:14 +000016prefix = os.path.normpath (sys.prefix)
17exec_prefix = os.path.normpath (sys.exec_prefix)
18
Greg Ward1190ee31998-12-18 23:46:33 +000019
Greg Ward9ddaaa11999-01-06 14:46:06 +000020def get_config_h_filename():
Fred Drake522af3a1999-01-06 16:28:34 +000021 """Return full pathname of installed config.h file."""
Fred Drake69e2c6e2000-02-08 15:55:42 +000022 if os.name == "nt":
23 return os.path.join(exec_prefix, "include", "config.h")
24 else:
25 return os.path.join(exec_prefix,
26 "include", "python" + sys.version[:3],
27 "config.h")
Greg Ward1190ee31998-12-18 23:46:33 +000028
Greg Ward9ddaaa11999-01-06 14:46:06 +000029def get_makefile_filename():
Fred Drake522af3a1999-01-06 16:28:34 +000030 """Return full pathname of installed Makefile from the Python build."""
Greg Warda0ca3f22000-02-02 00:05:14 +000031 return os.path.join(exec_prefix,
32 "lib", "python" + sys.version[:3],
Greg Ward9ddaaa11999-01-06 14:46:06 +000033 "config", "Makefile")
Greg Ward1190ee31998-12-18 23:46:33 +000034
Greg Ward9ddaaa11999-01-06 14:46:06 +000035def parse_config_h(fp, g=None):
Fred Drake522af3a1999-01-06 16:28:34 +000036 """Parse a config.h-style file. A dictionary containing name/value
37 pairs is returned. If an optional dictionary is passed in as the second
38 argument, it is used instead of a new dictionary.
39 """
Greg Ward9ddaaa11999-01-06 14:46:06 +000040 if g is None:
41 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +000042 define_rx = re.compile("#define ([A-Z][A-Z0-9_]+) (.*)\n")
43 undef_rx = re.compile("/[*] #undef ([A-Z][A-Z0-9_]+) [*]/\n")
Greg Ward9ddaaa11999-01-06 14:46:06 +000044 #
Greg Ward1190ee31998-12-18 23:46:33 +000045 while 1:
46 line = fp.readline()
47 if not line:
48 break
49 m = define_rx.match(line)
50 if m:
51 n, v = m.group(1, 2)
Greg Ward3c8e54b1998-12-22 12:42:04 +000052 try: v = string.atoi(v)
53 except ValueError: pass
54 g[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +000055 else:
56 m = undef_rx.match(line)
57 if m:
58 g[m.group(1)] = 0
Greg Ward9ddaaa11999-01-06 14:46:06 +000059 return g
Greg Ward1190ee31998-12-18 23:46:33 +000060
Greg Ward9ddaaa11999-01-06 14:46:06 +000061def parse_makefile(fp, g=None):
Fred Drake522af3a1999-01-06 16:28:34 +000062 """Parse a Makefile-style file. A dictionary containing name/value
63 pairs is returned. If an optional dictionary is passed in as the second
64 argument, it is used instead of a new dictionary.
65 """
Greg Ward9ddaaa11999-01-06 14:46:06 +000066 if g is None:
67 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +000068 variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)\n")
69 done = {}
70 notdone = {}
Greg Ward9ddaaa11999-01-06 14:46:06 +000071 #
Greg Ward1190ee31998-12-18 23:46:33 +000072 while 1:
73 line = fp.readline()
74 if not line:
75 break
76 m = variable_rx.match(line)
77 if m:
78 n, v = m.group(1, 2)
Greg Ward3c8e54b1998-12-22 12:42:04 +000079 v = string.strip(v)
Greg Ward1190ee31998-12-18 23:46:33 +000080 if "$" in v:
81 notdone[n] = v
82 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +000083 try: v = string.atoi(v)
84 except ValueError: pass
Greg Ward1190ee31998-12-18 23:46:33 +000085 done[n] = v
86
87 # do variable interpolation here
88 findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
89 findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
90 while notdone:
91 for name in notdone.keys():
92 value = notdone[name]
93 m = findvar1_rx.search(value)
94 if not m:
95 m = findvar2_rx.search(value)
96 if m:
97 n = m.group(1)
98 if done.has_key(n):
99 after = value[m.end():]
100 value = value[:m.start()] + done[n] + after
101 if "$" in after:
102 notdone[name] = value
103 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000104 try: value = string.atoi(value)
105 except ValueError: pass
106 done[name] = string.strip(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000107 del notdone[name]
108 elif notdone.has_key(n):
109 # get it on a subsequent round
110 pass
111 else:
112 done[n] = ""
113 after = value[m.end():]
114 value = value[:m.start()] + after
115 if "$" in after:
116 notdone[name] = value
117 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000118 try: value = string.atoi(value)
119 except ValueError: pass
120 done[name] = string.strip(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000121 del notdone[name]
122 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000123 # bogus variable reference; just drop it since we can't deal
Greg Ward1190ee31998-12-18 23:46:33 +0000124 del notdone[name]
125
126 # save the results in the global dictionary
127 g.update(done)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000128 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000129
130
Greg Ward9ddaaa11999-01-06 14:46:06 +0000131def _init_posix():
Fred Drake522af3a1999-01-06 16:28:34 +0000132 """Initialize the module as appropriate for POSIX systems."""
Greg Ward9ddaaa11999-01-06 14:46:06 +0000133 g = globals()
134 # load the installed config.h:
135 parse_config_h(open(get_config_h_filename()), g)
Greg Warda0ca3f22000-02-02 00:05:14 +0000136 # load the installed Makefile:
Greg Ward9ddaaa11999-01-06 14:46:06 +0000137 parse_makefile(open(get_makefile_filename()), g)
138
139
Greg Ward4d74d731999-06-08 01:58:36 +0000140def _init_nt():
141 """Initialize the module as appropriate for NT"""
Fred Drake69e2c6e2000-02-08 15:55:42 +0000142 g = globals()
Greg Ward4d74d731999-06-08 01:58:36 +0000143 # load config.h, though I don't know how useful this is
Fred Drake69e2c6e2000-02-08 15:55:42 +0000144 parse_config_h(open(get_config_h_filename()), g)
Greg Ward4d74d731999-06-08 01:58:36 +0000145 # set basic install directories
Fred Drake69e2c6e2000-02-08 15:55:42 +0000146 g['LIBDEST'] = os.path.join(exec_prefix, "Lib")
147 g['BINLIBDEST'] = os.path.join(exec_prefix, "Lib")
Greg Ward4d74d731999-06-08 01:58:36 +0000148
Greg Ward32162e81999-08-29 18:22:13 +0000149 # XXX hmmm.. a normal install puts include files here
Fred Drake69e2c6e2000-02-08 15:55:42 +0000150 g['INCLUDEPY'] = os.path.join(prefix, 'include')
Greg Ward32162e81999-08-29 18:22:13 +0000151
Fred Drake69e2c6e2000-02-08 15:55:42 +0000152 g['SO'] = '.pyd'
Greg Warda0ca3f22000-02-02 00:05:14 +0000153 g['exec_prefix'] = exec_prefix
Greg Ward9ddaaa11999-01-06 14:46:06 +0000154
Fred Drake69e2c6e2000-02-08 15:55:42 +0000155
Greg Ward9ddaaa11999-01-06 14:46:06 +0000156try:
157 exec "_init_" + os.name
158except NameError:
159 # not needed for this platform
160 pass
161else:
162 exec "_init_%s()" % os.name
163
Fred Drake69e2c6e2000-02-08 15:55:42 +0000164
Greg Ward1190ee31998-12-18 23:46:33 +0000165del _init_posix
Greg Ward4d74d731999-06-08 01:58:36 +0000166del _init_nt