blob: 8eaf17dc356400cc25327f1a8dda048ca93569b0 [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 Ward1190ee31998-12-18 23:46:33 +000016
Greg Ward9ddaaa11999-01-06 14:46:06 +000017def get_config_h_filename():
Fred Drake522af3a1999-01-06 16:28:34 +000018 """Return full pathname of installed config.h file."""
Fred Drake6a1b53c1999-01-11 15:34:55 +000019 return os.path.join(sys.exec_prefix, "include", "python" + sys.version[:3],
20 "config.h")
Greg Ward1190ee31998-12-18 23:46:33 +000021
Greg Ward9ddaaa11999-01-06 14:46:06 +000022def get_makefile_filename():
Fred Drake522af3a1999-01-06 16:28:34 +000023 """Return full pathname of installed Makefile from the Python build."""
Greg Ward9ddaaa11999-01-06 14:46:06 +000024 return os.path.join(sys.exec_prefix, "lib", "python" + sys.version[:3],
25 "config", "Makefile")
Greg Ward1190ee31998-12-18 23:46:33 +000026
Greg Ward9ddaaa11999-01-06 14:46:06 +000027def parse_config_h(fp, g=None):
Fred Drake522af3a1999-01-06 16:28:34 +000028 """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 """
Greg Ward9ddaaa11999-01-06 14:46:06 +000032 if g is None:
33 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +000034 define_rx = re.compile("#define ([A-Z][A-Z0-9_]+) (.*)\n")
35 undef_rx = re.compile("/[*] #undef ([A-Z][A-Z0-9_]+) [*]/\n")
Greg Ward9ddaaa11999-01-06 14:46:06 +000036 #
Greg Ward1190ee31998-12-18 23:46:33 +000037 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)
Greg Ward3c8e54b1998-12-22 12:42:04 +000044 try: v = string.atoi(v)
45 except ValueError: pass
46 g[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +000047 else:
48 m = undef_rx.match(line)
49 if m:
50 g[m.group(1)] = 0
Greg Ward9ddaaa11999-01-06 14:46:06 +000051 return g
Greg Ward1190ee31998-12-18 23:46:33 +000052
Greg Ward9ddaaa11999-01-06 14:46:06 +000053def parse_makefile(fp, g=None):
Fred Drake522af3a1999-01-06 16:28:34 +000054 """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 """
Greg Ward9ddaaa11999-01-06 14:46:06 +000058 if g is None:
59 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +000060 variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)\n")
61 done = {}
62 notdone = {}
Greg Ward9ddaaa11999-01-06 14:46:06 +000063 #
Greg Ward1190ee31998-12-18 23:46:33 +000064 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)
Greg Ward3c8e54b1998-12-22 12:42:04 +000071 v = string.strip(v)
Greg Ward1190ee31998-12-18 23:46:33 +000072 if "$" in v:
73 notdone[n] = v
74 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +000075 try: v = string.atoi(v)
76 except ValueError: pass
Greg Ward1190ee31998-12-18 23:46:33 +000077 done[n] = v
78
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:
Greg Ward3c8e54b1998-12-22 12:42:04 +000096 try: value = string.atoi(value)
97 except ValueError: pass
98 done[name] = string.strip(value)
Greg Ward1190ee31998-12-18 23:46:33 +000099 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:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000110 try: value = string.atoi(value)
111 except ValueError: pass
112 done[name] = string.strip(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000113 del notdone[name]
114 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000115 # bogus variable reference; just drop it since we can't deal
Greg Ward1190ee31998-12-18 23:46:33 +0000116 del notdone[name]
117
118 # save the results in the global dictionary
119 g.update(done)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000120 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000121
122
Greg Ward9ddaaa11999-01-06 14:46:06 +0000123def _init_posix():
Fred Drake522af3a1999-01-06 16:28:34 +0000124 """Initialize the module as appropriate for POSIX systems."""
Greg Ward9ddaaa11999-01-06 14:46:06 +0000125 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)
130
131
Greg Ward4d74d731999-06-08 01:58:36 +0000132def _init_nt():
133 """Initialize the module as appropriate for NT"""
134 g=globals()
135 # load config.h, though I don't know how useful this is
136 parse_config_h(open(
137 os.path.join(sys.exec_prefix, "include", "config.h")), g)
138 # set basic install directories
139 g['LIBDEST']=os.path.join(sys.exec_prefix, "Lib")
Greg Ward32162e81999-08-29 18:22:13 +0000140 g['BINLIBDEST']= os.path.join(sys.exec_prefix, "Lib")
Greg Ward4d74d731999-06-08 01:58:36 +0000141
Greg Ward32162e81999-08-29 18:22:13 +0000142 # XXX hmmm.. a normal install puts include files here
143 g['INCLUDEPY'] = os.path.join (sys.prefix, 'include' )
144
145 g['SO'] = '.dll'
146 g['exec_prefix'] = sys.exec_prefix
Greg Ward9ddaaa11999-01-06 14:46:06 +0000147
148try:
149 exec "_init_" + os.name
150except NameError:
151 # not needed for this platform
152 pass
153else:
154 exec "_init_%s()" % os.name
155
Greg Ward1190ee31998-12-18 23:46:33 +0000156del _init_posix
Greg Ward4d74d731999-06-08 01:58:36 +0000157del _init_nt