blob: 0e40cbc3501f971f165dc2cde32f6ed3fdb94dd4 [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."""
Greg Warda0ca3f22000-02-02 00:05:14 +000022 return os.path.join(exec_prefix,
23 "include", "python" + sys.version[:3],
Fred Drake6a1b53c1999-01-11 15:34:55 +000024 "config.h")
Greg Ward1190ee31998-12-18 23:46:33 +000025
Greg Ward9ddaaa11999-01-06 14:46:06 +000026def get_makefile_filename():
Fred Drake522af3a1999-01-06 16:28:34 +000027 """Return full pathname of installed Makefile from the Python build."""
Greg Warda0ca3f22000-02-02 00:05:14 +000028 return os.path.join(exec_prefix,
29 "lib", "python" + sys.version[:3],
Greg Ward9ddaaa11999-01-06 14:46:06 +000030 "config", "Makefile")
Greg Ward1190ee31998-12-18 23:46:33 +000031
Greg Ward9ddaaa11999-01-06 14:46:06 +000032def parse_config_h(fp, g=None):
Fred Drake522af3a1999-01-06 16:28:34 +000033 """Parse a config.h-style file. A dictionary containing name/value
34 pairs is returned. If an optional dictionary is passed in as the second
35 argument, it is used instead of a new dictionary.
36 """
Greg Ward9ddaaa11999-01-06 14:46:06 +000037 if g is None:
38 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +000039 define_rx = re.compile("#define ([A-Z][A-Z0-9_]+) (.*)\n")
40 undef_rx = re.compile("/[*] #undef ([A-Z][A-Z0-9_]+) [*]/\n")
Greg Ward9ddaaa11999-01-06 14:46:06 +000041 #
Greg Ward1190ee31998-12-18 23:46:33 +000042 while 1:
43 line = fp.readline()
44 if not line:
45 break
46 m = define_rx.match(line)
47 if m:
48 n, v = m.group(1, 2)
Greg Ward3c8e54b1998-12-22 12:42:04 +000049 try: v = string.atoi(v)
50 except ValueError: pass
51 g[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +000052 else:
53 m = undef_rx.match(line)
54 if m:
55 g[m.group(1)] = 0
Greg Ward9ddaaa11999-01-06 14:46:06 +000056 return g
Greg Ward1190ee31998-12-18 23:46:33 +000057
Greg Ward9ddaaa11999-01-06 14:46:06 +000058def parse_makefile(fp, g=None):
Fred Drake522af3a1999-01-06 16:28:34 +000059 """Parse a Makefile-style file. A dictionary containing name/value
60 pairs is returned. If an optional dictionary is passed in as the second
61 argument, it is used instead of a new dictionary.
62 """
Greg Ward9ddaaa11999-01-06 14:46:06 +000063 if g is None:
64 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +000065 variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)\n")
66 done = {}
67 notdone = {}
Greg Ward9ddaaa11999-01-06 14:46:06 +000068 #
Greg Ward1190ee31998-12-18 23:46:33 +000069 while 1:
70 line = fp.readline()
71 if not line:
72 break
73 m = variable_rx.match(line)
74 if m:
75 n, v = m.group(1, 2)
Greg Ward3c8e54b1998-12-22 12:42:04 +000076 v = string.strip(v)
Greg Ward1190ee31998-12-18 23:46:33 +000077 if "$" in v:
78 notdone[n] = v
79 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +000080 try: v = string.atoi(v)
81 except ValueError: pass
Greg Ward1190ee31998-12-18 23:46:33 +000082 done[n] = v
83
84 # do variable interpolation here
85 findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
86 findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
87 while notdone:
88 for name in notdone.keys():
89 value = notdone[name]
90 m = findvar1_rx.search(value)
91 if not m:
92 m = findvar2_rx.search(value)
93 if m:
94 n = m.group(1)
95 if done.has_key(n):
96 after = value[m.end():]
97 value = value[:m.start()] + done[n] + after
98 if "$" in after:
99 notdone[name] = value
100 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000101 try: value = string.atoi(value)
102 except ValueError: pass
103 done[name] = string.strip(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000104 del notdone[name]
105 elif notdone.has_key(n):
106 # get it on a subsequent round
107 pass
108 else:
109 done[n] = ""
110 after = value[m.end():]
111 value = value[:m.start()] + after
112 if "$" in after:
113 notdone[name] = value
114 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000115 try: value = string.atoi(value)
116 except ValueError: pass
117 done[name] = string.strip(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000118 del notdone[name]
119 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000120 # bogus variable reference; just drop it since we can't deal
Greg Ward1190ee31998-12-18 23:46:33 +0000121 del notdone[name]
122
123 # save the results in the global dictionary
124 g.update(done)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000125 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000126
127
Greg Ward9ddaaa11999-01-06 14:46:06 +0000128def _init_posix():
Fred Drake522af3a1999-01-06 16:28:34 +0000129 """Initialize the module as appropriate for POSIX systems."""
Greg Ward9ddaaa11999-01-06 14:46:06 +0000130 g = globals()
131 # load the installed config.h:
132 parse_config_h(open(get_config_h_filename()), g)
Greg Warda0ca3f22000-02-02 00:05:14 +0000133 # load the installed Makefile:
Greg Ward9ddaaa11999-01-06 14:46:06 +0000134 parse_makefile(open(get_makefile_filename()), g)
135
136
Greg Ward4d74d731999-06-08 01:58:36 +0000137def _init_nt():
138 """Initialize the module as appropriate for NT"""
139 g=globals()
140 # load config.h, though I don't know how useful this is
141 parse_config_h(open(
Greg Warda0ca3f22000-02-02 00:05:14 +0000142 os.path.join(exec_prefix, "include", "config.h")), g)
Greg Ward4d74d731999-06-08 01:58:36 +0000143 # set basic install directories
Greg Warda0ca3f22000-02-02 00:05:14 +0000144 g['LIBDEST']=os.path.join(exec_prefix, "Lib")
145 g['BINLIBDEST']= os.path.join(exec_prefix, "Lib")
Greg Ward4d74d731999-06-08 01:58:36 +0000146
Greg Ward32162e81999-08-29 18:22:13 +0000147 # XXX hmmm.. a normal install puts include files here
Greg Warda0ca3f22000-02-02 00:05:14 +0000148 g['INCLUDEPY'] = os.path.join (prefix, 'include' )
Greg Ward32162e81999-08-29 18:22:13 +0000149
150 g['SO'] = '.dll'
Greg Warda0ca3f22000-02-02 00:05:14 +0000151 g['exec_prefix'] = exec_prefix
Greg Ward9ddaaa11999-01-06 14:46:06 +0000152
153try:
154 exec "_init_" + os.name
155except NameError:
156 # not needed for this platform
157 pass
158else:
159 exec "_init_%s()" % os.name
160
Greg Ward1190ee31998-12-18 23:46:33 +0000161del _init_posix
Greg Ward4d74d731999-06-08 01:58:36 +0000162del _init_nt