blob: 49e58eb919042b5f95265ad7a036e9bbff6770b6 [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 Ward7d73b9e2000-03-09 03:16:05 +000020def get_python_inc (plat_specific=0):
21 """Return the directory containing installed Python header files.
22 If 'plat_specific' is false (the default), this is the path to the
23 non-platform-specific header files, i.e. Python.h and so on;
24 otherwise, this is the path to platform-specific header files
25 (namely config.h)."""
26
27 the_prefix = (plat_specific and exec_prefix or prefix)
28 if os.name == "posix":
29 return os.path.join (the_prefix, "include", "python" + sys.version[:3])
30 elif os.name == "nt":
31 return os.path.join (the_prefix, "Include") # include or Include?
32 elif os.name == "mac":
33 return os.path.join (the_prefix, "Include")
34 else:
35 raise DistutilsPlatformError, \
36 ("I don't know where Python installs its C header files " +
37 "on platform '%s'") % os.name
38
39
40def get_python_lib (plat_specific=0, standard_lib=0):
41 """Return the directory containing the Python library (standard or
42 site additions). If 'plat_specific' is true, return the directory
43 containing platform-specific modules, i.e. any module from a
44 non-pure-Python module distribution; otherwise, return the
45 platform-shared library directory. If 'standard_lib' is true,
46 return the directory containing standard Python library modules;
47 otherwise, return the directory for site-specific modules."""
48
49 the_prefix = (plat_specific and exec_prefix or prefix)
50
51 if os.name == "posix":
52 libpython = os.path.join (the_prefix,
53 "lib", "python" + sys.version[:3])
54 if standard_lib:
55 return libpython
56 else:
57 return os.path.join (libpython, "site-packages")
58
59 elif os.name == "nt":
60 if standard_lib:
61 return os.path.join (the_prefix, "Lib")
62 else:
63 return the_prefix
64
65 elif os.name == "mac":
66 if platform_specific:
67 if standard_lib:
68 return os.path.join (exec_prefix, "Mac", "Plugins")
69 else:
70 raise DistutilsPlatformError, \
71 "OK, where DO site-specific extensions go on the Mac?"
72 else:
73 if standard_lib:
74 return os.path.join (prefix, "Lib")
75 else:
76 raise DistutilsPlatformError, \
77 "OK, where DO site-specific modules go on the Mac?"
78 else:
79 raise DistutilsPlatformError, \
80 ("I don't know where Python installs its library " +
81 "on platform '%s'") % os.name
82
83# get_python_lib ()
84
85
Greg Ward9ddaaa11999-01-06 14:46:06 +000086def get_config_h_filename():
Fred Drake522af3a1999-01-06 16:28:34 +000087 """Return full pathname of installed config.h file."""
Greg Ward7d73b9e2000-03-09 03:16:05 +000088 inc_dir = get_python_inc (plat_specific=1)
89 return os.path.join (inc_dir, "config.h")
90
Greg Ward1190ee31998-12-18 23:46:33 +000091
Greg Ward9ddaaa11999-01-06 14:46:06 +000092def get_makefile_filename():
Fred Drake522af3a1999-01-06 16:28:34 +000093 """Return full pathname of installed Makefile from the Python build."""
Greg Ward7d73b9e2000-03-09 03:16:05 +000094 lib_dir = get_python_lib (plat_specific=1, standard_lib=1)
95 return os.path.join (lib_dir, "config", "Makefile")
96
Greg Ward1190ee31998-12-18 23:46:33 +000097
Greg Ward9ddaaa11999-01-06 14:46:06 +000098def parse_config_h(fp, g=None):
Fred Drake522af3a1999-01-06 16:28:34 +000099 """Parse a config.h-style file. A dictionary containing name/value
100 pairs is returned. If an optional dictionary is passed in as the second
101 argument, it is used instead of a new dictionary.
102 """
Greg Ward9ddaaa11999-01-06 14:46:06 +0000103 if g is None:
104 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000105 define_rx = re.compile("#define ([A-Z][A-Z0-9_]+) (.*)\n")
106 undef_rx = re.compile("/[*] #undef ([A-Z][A-Z0-9_]+) [*]/\n")
Greg Ward9ddaaa11999-01-06 14:46:06 +0000107 #
Greg Ward1190ee31998-12-18 23:46:33 +0000108 while 1:
109 line = fp.readline()
110 if not line:
111 break
112 m = define_rx.match(line)
113 if m:
114 n, v = m.group(1, 2)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000115 try: v = string.atoi(v)
116 except ValueError: pass
117 g[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +0000118 else:
119 m = undef_rx.match(line)
120 if m:
121 g[m.group(1)] = 0
Greg Ward9ddaaa11999-01-06 14:46:06 +0000122 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000123
Greg Ward9ddaaa11999-01-06 14:46:06 +0000124def parse_makefile(fp, g=None):
Fred Drake522af3a1999-01-06 16:28:34 +0000125 """Parse a Makefile-style file. A dictionary containing name/value
126 pairs is returned. If an optional dictionary is passed in as the second
127 argument, it is used instead of a new dictionary.
128 """
Greg Ward9ddaaa11999-01-06 14:46:06 +0000129 if g is None:
130 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000131 variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)\n")
132 done = {}
133 notdone = {}
Greg Ward9ddaaa11999-01-06 14:46:06 +0000134 #
Greg Ward1190ee31998-12-18 23:46:33 +0000135 while 1:
136 line = fp.readline()
137 if not line:
138 break
139 m = variable_rx.match(line)
140 if m:
141 n, v = m.group(1, 2)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000142 v = string.strip(v)
Greg Ward1190ee31998-12-18 23:46:33 +0000143 if "$" in v:
144 notdone[n] = v
145 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000146 try: v = string.atoi(v)
147 except ValueError: pass
Greg Ward1190ee31998-12-18 23:46:33 +0000148 done[n] = v
149
150 # do variable interpolation here
151 findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
152 findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
153 while notdone:
154 for name in notdone.keys():
155 value = notdone[name]
156 m = findvar1_rx.search(value)
157 if not m:
158 m = findvar2_rx.search(value)
159 if m:
160 n = m.group(1)
161 if done.has_key(n):
162 after = value[m.end():]
163 value = value[:m.start()] + done[n] + after
164 if "$" in after:
165 notdone[name] = value
166 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000167 try: value = string.atoi(value)
168 except ValueError: pass
169 done[name] = string.strip(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000170 del notdone[name]
171 elif notdone.has_key(n):
172 # get it on a subsequent round
173 pass
174 else:
175 done[n] = ""
176 after = value[m.end():]
177 value = value[:m.start()] + after
178 if "$" in after:
179 notdone[name] = value
180 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000181 try: value = string.atoi(value)
182 except ValueError: pass
183 done[name] = string.strip(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000184 del notdone[name]
185 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000186 # bogus variable reference; just drop it since we can't deal
Greg Ward1190ee31998-12-18 23:46:33 +0000187 del notdone[name]
188
189 # save the results in the global dictionary
190 g.update(done)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000191 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000192
193
Greg Ward9ddaaa11999-01-06 14:46:06 +0000194def _init_posix():
Fred Drake522af3a1999-01-06 16:28:34 +0000195 """Initialize the module as appropriate for POSIX systems."""
Greg Ward9ddaaa11999-01-06 14:46:06 +0000196 g = globals()
197 # load the installed config.h:
198 parse_config_h(open(get_config_h_filename()), g)
Greg Warda0ca3f22000-02-02 00:05:14 +0000199 # load the installed Makefile:
Greg Ward9ddaaa11999-01-06 14:46:06 +0000200 parse_makefile(open(get_makefile_filename()), g)
201
202
Greg Ward4d74d731999-06-08 01:58:36 +0000203def _init_nt():
204 """Initialize the module as appropriate for NT"""
Fred Drake69e2c6e2000-02-08 15:55:42 +0000205 g = globals()
Greg Ward4d74d731999-06-08 01:58:36 +0000206 # load config.h, though I don't know how useful this is
Fred Drake69e2c6e2000-02-08 15:55:42 +0000207 parse_config_h(open(get_config_h_filename()), g)
Greg Ward4d74d731999-06-08 01:58:36 +0000208 # set basic install directories
Greg Ward7d73b9e2000-03-09 03:16:05 +0000209 g['LIBDEST'] = get_python_lib (plat_specific=0, standard_lib=1)
210 g['BINLIBDEST'] = get_python_lib (plat_specific=1, standard_lib=1)
Greg Ward4d74d731999-06-08 01:58:36 +0000211
Greg Ward32162e81999-08-29 18:22:13 +0000212 # XXX hmmm.. a normal install puts include files here
Greg Ward7d73b9e2000-03-09 03:16:05 +0000213 g['INCLUDEPY'] = get_python_inc (plat_specific=0)
Greg Ward32162e81999-08-29 18:22:13 +0000214
Fred Drake69e2c6e2000-02-08 15:55:42 +0000215 g['SO'] = '.pyd'
Greg Warda0ca3f22000-02-02 00:05:14 +0000216 g['exec_prefix'] = exec_prefix
Greg Ward9ddaaa11999-01-06 14:46:06 +0000217
Fred Drake69e2c6e2000-02-08 15:55:42 +0000218
Greg Ward0eff87a2000-03-07 03:30:09 +0000219def _init_mac():
220 """Initialize the module as appropriate for Macintosh systems"""
221 g = globals()
222 # load the installed config.h (what if not installed? - still need to
223 # be able to install packages which don't require compilation)
224 parse_config_h(open(
225 os.path.join(sys.exec_prefix, "Mac", "Include", "config.h")), g)
226 # set basic install directories
Greg Ward7d73b9e2000-03-09 03:16:05 +0000227 g['LIBDEST'] = get_python_lib (plat_specific=0, standard_lib=1)
228 g['BINLIBDEST'] = get_python_lib (plat_specific=1, standard_lib=1)
Greg Ward0eff87a2000-03-07 03:30:09 +0000229
230 # XXX hmmm.. a normal install puts include files here
Greg Ward7d73b9e2000-03-09 03:16:05 +0000231 g['INCLUDEPY'] = get_python_inc (plat_specific=0)
Greg Ward0eff87a2000-03-07 03:30:09 +0000232
233 g['SO'] = '.ppc.slb'
234 g['exec_prefix'] = sys.exec_prefix
235 print sys.prefix
Greg Ward7d73b9e2000-03-09 03:16:05 +0000236
237 # XXX are these used anywhere?
Greg Ward0eff87a2000-03-07 03:30:09 +0000238 g['install_lib'] = os.path.join(sys.exec_prefix, "Lib")
239 g['install_platlib'] = os.path.join(sys.exec_prefix, "Mac", "Lib")
240
241
Greg Ward9ddaaa11999-01-06 14:46:06 +0000242try:
243 exec "_init_" + os.name
244except NameError:
245 # not needed for this platform
246 pass
247else:
248 exec "_init_%s()" % os.name
249
Fred Drake69e2c6e2000-02-08 15:55:42 +0000250
Greg Ward1190ee31998-12-18 23:46:33 +0000251del _init_posix
Greg Ward4d74d731999-06-08 01:58:36 +0000252del _init_nt
Greg Ward0eff87a2000-03-07 03:30:09 +0000253del _init_mac