blob: 9cf9540db3ac49c0beb701a76c7019d44faa9945 [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
Fred Drakec1ee39a2000-03-09 15:54:52 +000016from errors import DistutilsPlatformError
Greg Warda0ca3f22000-02-02 00:05:14 +000017
Greg Ward1190ee31998-12-18 23:46:33 +000018
Greg Wardcf6bea32000-04-10 01:15:06 +000019PREFIX = os.path.normpath(sys.prefix)
20EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
Fred Drakec1ee39a2000-03-09 15:54:52 +000021
22
23def get_python_inc(plat_specific=0):
Greg Ward7d73b9e2000-03-09 03:16:05 +000024 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000025
26 If 'plat_specific' is false (the default), this is the path to the
27 non-platform-specific header files, i.e. Python.h and so on;
28 otherwise, this is the path to platform-specific header files
29 (namely config.h).
30
31 """
Greg Wardcf6bea32000-04-10 01:15:06 +000032 prefix = (plat_specific and EXEC_PREFIX or PREFIX)
Greg Ward7d73b9e2000-03-09 03:16:05 +000033 if os.name == "posix":
Greg Wardcf6bea32000-04-10 01:15:06 +000034 return os.path.join(prefix, "include", "python" + sys.version[:3])
Greg Ward7d73b9e2000-03-09 03:16:05 +000035 elif os.name == "nt":
Greg Wardcf6bea32000-04-10 01:15:06 +000036 return os.path.join(prefix, "Include") # include or Include?
Greg Ward7d73b9e2000-03-09 03:16:05 +000037 elif os.name == "mac":
Greg Wardcf6bea32000-04-10 01:15:06 +000038 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000039 else:
40 raise DistutilsPlatformError, \
41 ("I don't know where Python installs its C header files " +
42 "on platform '%s'") % os.name
43
44
Fred Drakec1ee39a2000-03-09 15:54:52 +000045def get_python_lib(plat_specific=0, standard_lib=0):
Greg Ward7d73b9e2000-03-09 03:16:05 +000046 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +000047 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +000048
Fred Drakec1ee39a2000-03-09 15:54:52 +000049 If 'plat_specific' is true, return the directory containing
50 platform-specific modules, i.e. any module from a non-pure-Python
51 module distribution; otherwise, return the platform-shared library
52 directory. If 'standard_lib' is true, return the directory
53 containing standard Python library modules; otherwise, return the
54 directory for site-specific modules.
55
56 """
Greg Wardcf6bea32000-04-10 01:15:06 +000057 prefix = (plat_specific and EXEC_PREFIX or PREFIX)
Greg Ward7d73b9e2000-03-09 03:16:05 +000058
59 if os.name == "posix":
Greg Wardcf6bea32000-04-10 01:15:06 +000060 libpython = os.path.join(prefix,
Fred Drakec1ee39a2000-03-09 15:54:52 +000061 "lib", "python" + sys.version[:3])
Greg Ward7d73b9e2000-03-09 03:16:05 +000062 if standard_lib:
63 return libpython
64 else:
Fred Drakec1ee39a2000-03-09 15:54:52 +000065 return os.path.join(libpython, "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +000066
67 elif os.name == "nt":
68 if standard_lib:
Greg Wardcf6bea32000-04-10 01:15:06 +000069 return os.path.join(PREFIX, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +000070 else:
Greg Wardcf6bea32000-04-10 01:15:06 +000071 return prefix
Greg Ward7d73b9e2000-03-09 03:16:05 +000072
73 elif os.name == "mac":
74 if platform_specific:
75 if standard_lib:
Greg Wardcf6bea32000-04-10 01:15:06 +000076 return os.path.join(EXEC_PREFIX, "Mac", "Plugins")
Greg Ward7d73b9e2000-03-09 03:16:05 +000077 else:
78 raise DistutilsPlatformError, \
79 "OK, where DO site-specific extensions go on the Mac?"
80 else:
81 if standard_lib:
Greg Wardcf6bea32000-04-10 01:15:06 +000082 return os.path.join(PREFIX, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +000083 else:
84 raise DistutilsPlatformError, \
85 "OK, where DO site-specific modules go on the Mac?"
86 else:
87 raise DistutilsPlatformError, \
88 ("I don't know where Python installs its library " +
89 "on platform '%s'") % os.name
90
Fred Drakec1ee39a2000-03-09 15:54:52 +000091# get_python_lib()
Greg Ward7d73b9e2000-03-09 03:16:05 +000092
93
Greg Ward9ddaaa11999-01-06 14:46:06 +000094def get_config_h_filename():
Fred Drake522af3a1999-01-06 16:28:34 +000095 """Return full pathname of installed config.h file."""
Fred Drakec1ee39a2000-03-09 15:54:52 +000096 inc_dir = get_python_inc(plat_specific=1)
97 return os.path.join(inc_dir, "config.h")
Greg Ward7d73b9e2000-03-09 03:16:05 +000098
Greg Ward1190ee31998-12-18 23:46:33 +000099
Greg Ward9ddaaa11999-01-06 14:46:06 +0000100def get_makefile_filename():
Fred Drake522af3a1999-01-06 16:28:34 +0000101 """Return full pathname of installed Makefile from the Python build."""
Fred Drakec1ee39a2000-03-09 15:54:52 +0000102 lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
103 return os.path.join(lib_dir, "config", "Makefile")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000104
Greg Ward1190ee31998-12-18 23:46:33 +0000105
Greg Ward9ddaaa11999-01-06 14:46:06 +0000106def parse_config_h(fp, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000107 """Parse a config.h-style file.
108
109 A dictionary containing name/value pairs is returned. If an
110 optional dictionary is passed in as the second argument, it is
111 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000112 """
Greg Ward9ddaaa11999-01-06 14:46:06 +0000113 if g is None:
114 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000115 define_rx = re.compile("#define ([A-Z][A-Z0-9_]+) (.*)\n")
116 undef_rx = re.compile("/[*] #undef ([A-Z][A-Z0-9_]+) [*]/\n")
Greg Ward9ddaaa11999-01-06 14:46:06 +0000117 #
Greg Ward1190ee31998-12-18 23:46:33 +0000118 while 1:
119 line = fp.readline()
120 if not line:
121 break
122 m = define_rx.match(line)
123 if m:
124 n, v = m.group(1, 2)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000125 try: v = string.atoi(v)
126 except ValueError: pass
127 g[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +0000128 else:
129 m = undef_rx.match(line)
130 if m:
131 g[m.group(1)] = 0
Greg Ward9ddaaa11999-01-06 14:46:06 +0000132 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000133
Greg Ward9ddaaa11999-01-06 14:46:06 +0000134def parse_makefile(fp, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000135 """Parse a Makefile-style file.
136
137 A dictionary containing name/value pairs is returned. If an
138 optional dictionary is passed in as the second argument, it is
139 used instead of a new dictionary.
140
Fred Drake522af3a1999-01-06 16:28:34 +0000141 """
Greg Ward9ddaaa11999-01-06 14:46:06 +0000142 if g is None:
143 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000144 variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)\n")
145 done = {}
146 notdone = {}
Greg Ward9ddaaa11999-01-06 14:46:06 +0000147 #
Greg Ward1190ee31998-12-18 23:46:33 +0000148 while 1:
149 line = fp.readline()
150 if not line:
151 break
152 m = variable_rx.match(line)
153 if m:
154 n, v = m.group(1, 2)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000155 v = string.strip(v)
Greg Ward1190ee31998-12-18 23:46:33 +0000156 if "$" in v:
157 notdone[n] = v
158 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000159 try: v = string.atoi(v)
160 except ValueError: pass
Greg Ward1190ee31998-12-18 23:46:33 +0000161 done[n] = v
162
163 # do variable interpolation here
164 findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
165 findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
166 while notdone:
167 for name in notdone.keys():
168 value = notdone[name]
169 m = findvar1_rx.search(value)
170 if not m:
171 m = findvar2_rx.search(value)
172 if m:
173 n = m.group(1)
174 if done.has_key(n):
175 after = value[m.end():]
176 value = value[:m.start()] + done[n] + after
177 if "$" in after:
178 notdone[name] = value
179 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000180 try: value = string.atoi(value)
181 except ValueError: pass
182 done[name] = string.strip(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000183 del notdone[name]
184 elif notdone.has_key(n):
185 # get it on a subsequent round
186 pass
187 else:
188 done[n] = ""
189 after = value[m.end():]
190 value = value[:m.start()] + after
191 if "$" in after:
192 notdone[name] = value
193 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000194 try: value = string.atoi(value)
195 except ValueError: pass
196 done[name] = string.strip(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000197 del notdone[name]
198 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000199 # bogus variable reference; just drop it since we can't deal
Greg Ward1190ee31998-12-18 23:46:33 +0000200 del notdone[name]
201
202 # save the results in the global dictionary
203 g.update(done)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000204 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000205
206
Greg Ward9ddaaa11999-01-06 14:46:06 +0000207def _init_posix():
Fred Drake522af3a1999-01-06 16:28:34 +0000208 """Initialize the module as appropriate for POSIX systems."""
Greg Ward9ddaaa11999-01-06 14:46:06 +0000209 g = globals()
210 # load the installed config.h:
211 parse_config_h(open(get_config_h_filename()), g)
Greg Warda0ca3f22000-02-02 00:05:14 +0000212 # load the installed Makefile:
Greg Ward9ddaaa11999-01-06 14:46:06 +0000213 parse_makefile(open(get_makefile_filename()), g)
214
215
Greg Ward4d74d731999-06-08 01:58:36 +0000216def _init_nt():
217 """Initialize the module as appropriate for NT"""
Fred Drake69e2c6e2000-02-08 15:55:42 +0000218 g = globals()
Greg Ward4d74d731999-06-08 01:58:36 +0000219 # load config.h, though I don't know how useful this is
Fred Drake69e2c6e2000-02-08 15:55:42 +0000220 parse_config_h(open(get_config_h_filename()), g)
Greg Ward4d74d731999-06-08 01:58:36 +0000221 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000222 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
223 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward4d74d731999-06-08 01:58:36 +0000224
Greg Ward32162e81999-08-29 18:22:13 +0000225 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000226 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward32162e81999-08-29 18:22:13 +0000227
Fred Drake69e2c6e2000-02-08 15:55:42 +0000228 g['SO'] = '.pyd'
Greg Wardcf6bea32000-04-10 01:15:06 +0000229 g['exec_prefix'] = EXEC_PREFIX
Greg Ward9ddaaa11999-01-06 14:46:06 +0000230
Fred Drake69e2c6e2000-02-08 15:55:42 +0000231
Greg Ward0eff87a2000-03-07 03:30:09 +0000232def _init_mac():
233 """Initialize the module as appropriate for Macintosh systems"""
234 g = globals()
235 # load the installed config.h (what if not installed? - still need to
236 # be able to install packages which don't require compilation)
237 parse_config_h(open(
Greg Wardcf6bea32000-04-10 01:15:06 +0000238 os.path.join(EXEC_PREFIX, "Mac", "Include", "config.h")), g)
Greg Ward0eff87a2000-03-07 03:30:09 +0000239 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000240 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
241 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward0eff87a2000-03-07 03:30:09 +0000242
243 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000244 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward0eff87a2000-03-07 03:30:09 +0000245
246 g['SO'] = '.ppc.slb'
Greg Wardcf6bea32000-04-10 01:15:06 +0000247 g['exec_prefix'] = EXEC_PREFIX
248 print sys.prefix, PREFIX
Greg Ward7d73b9e2000-03-09 03:16:05 +0000249
250 # XXX are these used anywhere?
Greg Wardcf6bea32000-04-10 01:15:06 +0000251 g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
252 g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
Greg Ward0eff87a2000-03-07 03:30:09 +0000253
254
Greg Ward9ddaaa11999-01-06 14:46:06 +0000255try:
256 exec "_init_" + os.name
257except NameError:
258 # not needed for this platform
259 pass
260else:
261 exec "_init_%s()" % os.name
262
Fred Drake69e2c6e2000-02-08 15:55:42 +0000263
Greg Ward1190ee31998-12-18 23:46:33 +0000264del _init_posix
Greg Ward4d74d731999-06-08 01:58:36 +0000265del _init_nt
Greg Ward0eff87a2000-03-07 03:30:09 +0000266del _init_mac