Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 1 | """Provide access to Python's configuration information. The specific names |
| 2 | defined in the module depend heavily on the platform and configuration. |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 3 | |
| 4 | Written by: Fred L. Drake, Jr. |
| 5 | Email: <fdrake@acm.org> |
| 6 | Initial date: 17-Dec-1998 |
| 7 | """ |
| 8 | |
| 9 | __version__ = "$Revision$" |
| 10 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 11 | import os |
| 12 | import re |
| 13 | import string |
| 14 | import sys |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 15 | |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 16 | from errors import DistutilsPlatformError |
Greg Ward | a0ca3f2 | 2000-02-02 00:05:14 +0000 | [diff] [blame] | 17 | |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 18 | |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 19 | prefix = os.path.normpath(sys.prefix) |
| 20 | exec_prefix = os.path.normpath(sys.exec_prefix) |
| 21 | |
| 22 | |
| 23 | def get_python_inc(plat_specific=0): |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 24 | """Return the directory containing installed Python header files. |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 25 | |
| 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 Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 32 | the_prefix = (plat_specific and exec_prefix or prefix) |
| 33 | if os.name == "posix": |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 34 | return os.path.join(the_prefix, "include", "python" + sys.version[:3]) |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 35 | elif os.name == "nt": |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 36 | return os.path.join(the_prefix, "Include") # include or Include? |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 37 | elif os.name == "mac": |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 38 | return os.path.join(the_prefix, "Include") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 39 | 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 Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 45 | def get_python_lib(plat_specific=0, standard_lib=0): |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 46 | """Return the directory containing the Python library (standard or |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 47 | site additions). |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 48 | |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 49 | 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 Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 57 | the_prefix = (plat_specific and exec_prefix or prefix) |
| 58 | |
| 59 | if os.name == "posix": |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 60 | libpython = os.path.join(the_prefix, |
| 61 | "lib", "python" + sys.version[:3]) |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 62 | if standard_lib: |
| 63 | return libpython |
| 64 | else: |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 65 | return os.path.join(libpython, "site-packages") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 66 | |
| 67 | elif os.name == "nt": |
| 68 | if standard_lib: |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 69 | return os.path.join(the_prefix, "Lib") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 70 | else: |
| 71 | return the_prefix |
| 72 | |
| 73 | elif os.name == "mac": |
| 74 | if platform_specific: |
| 75 | if standard_lib: |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 76 | return os.path.join(exec_prefix, "Mac", "Plugins") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 77 | else: |
| 78 | raise DistutilsPlatformError, \ |
| 79 | "OK, where DO site-specific extensions go on the Mac?" |
| 80 | else: |
| 81 | if standard_lib: |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 82 | return os.path.join(prefix, "Lib") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 83 | 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 Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 91 | # get_python_lib() |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 92 | |
| 93 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 94 | def get_config_h_filename(): |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 95 | """Return full pathname of installed config.h file.""" |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 96 | inc_dir = get_python_inc(plat_specific=1) |
| 97 | return os.path.join(inc_dir, "config.h") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 98 | |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 99 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 100 | def get_makefile_filename(): |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 101 | """Return full pathname of installed Makefile from the Python build.""" |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 102 | lib_dir = get_python_lib(plat_specific=1, standard_lib=1) |
| 103 | return os.path.join(lib_dir, "config", "Makefile") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 104 | |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 105 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 106 | def parse_config_h(fp, g=None): |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 107 | """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 Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 112 | """ |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 113 | if g is None: |
| 114 | g = {} |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 115 | define_rx = re.compile("#define ([A-Z][A-Z0-9_]+) (.*)\n") |
| 116 | undef_rx = re.compile("/[*] #undef ([A-Z][A-Z0-9_]+) [*]/\n") |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 117 | # |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 118 | 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 Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 125 | try: v = string.atoi(v) |
| 126 | except ValueError: pass |
| 127 | g[n] = v |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 128 | else: |
| 129 | m = undef_rx.match(line) |
| 130 | if m: |
| 131 | g[m.group(1)] = 0 |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 132 | return g |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 133 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 134 | def parse_makefile(fp, g=None): |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 135 | """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 Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 141 | """ |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 142 | if g is None: |
| 143 | g = {} |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 144 | variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)\n") |
| 145 | done = {} |
| 146 | notdone = {} |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 147 | # |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 148 | 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 Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 155 | v = string.strip(v) |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 156 | if "$" in v: |
| 157 | notdone[n] = v |
| 158 | else: |
Greg Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 159 | try: v = string.atoi(v) |
| 160 | except ValueError: pass |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 161 | 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 Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 180 | try: value = string.atoi(value) |
| 181 | except ValueError: pass |
| 182 | done[name] = string.strip(value) |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 183 | 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 Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 194 | try: value = string.atoi(value) |
| 195 | except ValueError: pass |
| 196 | done[name] = string.strip(value) |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 197 | del notdone[name] |
| 198 | else: |
Greg Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 199 | # bogus variable reference; just drop it since we can't deal |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 200 | del notdone[name] |
| 201 | |
| 202 | # save the results in the global dictionary |
| 203 | g.update(done) |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 204 | return g |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 205 | |
| 206 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 207 | def _init_posix(): |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 208 | """Initialize the module as appropriate for POSIX systems.""" |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 209 | g = globals() |
| 210 | # load the installed config.h: |
| 211 | parse_config_h(open(get_config_h_filename()), g) |
Greg Ward | a0ca3f2 | 2000-02-02 00:05:14 +0000 | [diff] [blame] | 212 | # load the installed Makefile: |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 213 | parse_makefile(open(get_makefile_filename()), g) |
| 214 | |
| 215 | |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 216 | def _init_nt(): |
| 217 | """Initialize the module as appropriate for NT""" |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 218 | g = globals() |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 219 | # load config.h, though I don't know how useful this is |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 220 | parse_config_h(open(get_config_h_filename()), g) |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 221 | # set basic install directories |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 222 | g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1) |
| 223 | g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1) |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 224 | |
Greg Ward | 32162e8 | 1999-08-29 18:22:13 +0000 | [diff] [blame] | 225 | # XXX hmmm.. a normal install puts include files here |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 226 | g['INCLUDEPY'] = get_python_inc(plat_specific=0) |
Greg Ward | 32162e8 | 1999-08-29 18:22:13 +0000 | [diff] [blame] | 227 | |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 228 | g['SO'] = '.pyd' |
Greg Ward | a0ca3f2 | 2000-02-02 00:05:14 +0000 | [diff] [blame] | 229 | g['exec_prefix'] = exec_prefix |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 230 | |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 231 | |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 232 | def _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( |
| 238 | os.path.join(sys.exec_prefix, "Mac", "Include", "config.h")), g) |
| 239 | # set basic install directories |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 240 | g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1) |
| 241 | g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1) |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 242 | |
| 243 | # XXX hmmm.. a normal install puts include files here |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 244 | g['INCLUDEPY'] = get_python_inc(plat_specific=0) |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 245 | |
| 246 | g['SO'] = '.ppc.slb' |
| 247 | g['exec_prefix'] = sys.exec_prefix |
| 248 | print sys.prefix |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 249 | |
| 250 | # XXX are these used anywhere? |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 251 | g['install_lib'] = os.path.join(sys.exec_prefix, "Lib") |
| 252 | g['install_platlib'] = os.path.join(sys.exec_prefix, "Mac", "Lib") |
| 253 | |
| 254 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 255 | try: |
| 256 | exec "_init_" + os.name |
| 257 | except NameError: |
| 258 | # not needed for this platform |
| 259 | pass |
| 260 | else: |
| 261 | exec "_init_%s()" % os.name |
| 262 | |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 263 | |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 264 | del _init_posix |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 265 | del _init_nt |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 266 | del _init_mac |