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 | |
Greg Ward | a0ca3f2 | 2000-02-02 00:05:14 +0000 | [diff] [blame] | 16 | prefix = os.path.normpath (sys.prefix) |
| 17 | exec_prefix = os.path.normpath (sys.exec_prefix) |
| 18 | |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 19 | |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame^] | 20 | def 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 | |
| 40 | def 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 Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 86 | def get_config_h_filename(): |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 87 | """Return full pathname of installed config.h file.""" |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame^] | 88 | inc_dir = get_python_inc (plat_specific=1) |
| 89 | return os.path.join (inc_dir, "config.h") |
| 90 | |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 91 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 92 | def get_makefile_filename(): |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 93 | """Return full pathname of installed Makefile from the Python build.""" |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame^] | 94 | lib_dir = get_python_lib (plat_specific=1, standard_lib=1) |
| 95 | return os.path.join (lib_dir, "config", "Makefile") |
| 96 | |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 97 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 98 | def parse_config_h(fp, g=None): |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 99 | """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 Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 103 | if g is None: |
| 104 | g = {} |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 105 | define_rx = re.compile("#define ([A-Z][A-Z0-9_]+) (.*)\n") |
| 106 | undef_rx = re.compile("/[*] #undef ([A-Z][A-Z0-9_]+) [*]/\n") |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 107 | # |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 108 | 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 Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 115 | try: v = string.atoi(v) |
| 116 | except ValueError: pass |
| 117 | g[n] = v |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 118 | else: |
| 119 | m = undef_rx.match(line) |
| 120 | if m: |
| 121 | g[m.group(1)] = 0 |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 122 | return g |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 123 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 124 | def parse_makefile(fp, g=None): |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 125 | """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 Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 129 | if g is None: |
| 130 | g = {} |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 131 | variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)\n") |
| 132 | done = {} |
| 133 | notdone = {} |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 134 | # |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 135 | 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 Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 142 | v = string.strip(v) |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 143 | if "$" in v: |
| 144 | notdone[n] = v |
| 145 | else: |
Greg Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 146 | try: v = string.atoi(v) |
| 147 | except ValueError: pass |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 148 | 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 Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 167 | try: value = string.atoi(value) |
| 168 | except ValueError: pass |
| 169 | done[name] = string.strip(value) |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 170 | 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 Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 181 | try: value = string.atoi(value) |
| 182 | except ValueError: pass |
| 183 | done[name] = string.strip(value) |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 184 | del notdone[name] |
| 185 | else: |
Greg Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 186 | # bogus variable reference; just drop it since we can't deal |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 187 | del notdone[name] |
| 188 | |
| 189 | # save the results in the global dictionary |
| 190 | g.update(done) |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 191 | return g |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 192 | |
| 193 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 194 | def _init_posix(): |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 195 | """Initialize the module as appropriate for POSIX systems.""" |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 196 | g = globals() |
| 197 | # load the installed config.h: |
| 198 | parse_config_h(open(get_config_h_filename()), g) |
Greg Ward | a0ca3f2 | 2000-02-02 00:05:14 +0000 | [diff] [blame] | 199 | # load the installed Makefile: |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 200 | parse_makefile(open(get_makefile_filename()), g) |
| 201 | |
| 202 | |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 203 | def _init_nt(): |
| 204 | """Initialize the module as appropriate for NT""" |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 205 | g = globals() |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 206 | # load config.h, though I don't know how useful this is |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 207 | parse_config_h(open(get_config_h_filename()), g) |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 208 | # set basic install directories |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame^] | 209 | g['LIBDEST'] = get_python_lib (plat_specific=0, standard_lib=1) |
| 210 | g['BINLIBDEST'] = get_python_lib (plat_specific=1, standard_lib=1) |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 211 | |
Greg Ward | 32162e8 | 1999-08-29 18:22:13 +0000 | [diff] [blame] | 212 | # XXX hmmm.. a normal install puts include files here |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame^] | 213 | g['INCLUDEPY'] = get_python_inc (plat_specific=0) |
Greg Ward | 32162e8 | 1999-08-29 18:22:13 +0000 | [diff] [blame] | 214 | |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 215 | g['SO'] = '.pyd' |
Greg Ward | a0ca3f2 | 2000-02-02 00:05:14 +0000 | [diff] [blame] | 216 | g['exec_prefix'] = exec_prefix |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 217 | |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 218 | |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 219 | def _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 Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame^] | 227 | g['LIBDEST'] = get_python_lib (plat_specific=0, standard_lib=1) |
| 228 | g['BINLIBDEST'] = get_python_lib (plat_specific=1, standard_lib=1) |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 229 | |
| 230 | # XXX hmmm.. a normal install puts include files here |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame^] | 231 | g['INCLUDEPY'] = get_python_inc (plat_specific=0) |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 232 | |
| 233 | g['SO'] = '.ppc.slb' |
| 234 | g['exec_prefix'] = sys.exec_prefix |
| 235 | print sys.prefix |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame^] | 236 | |
| 237 | # XXX are these used anywhere? |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 238 | 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 Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 242 | try: |
| 243 | exec "_init_" + os.name |
| 244 | except NameError: |
| 245 | # not needed for this platform |
| 246 | pass |
| 247 | else: |
| 248 | exec "_init_%s()" % os.name |
| 249 | |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 250 | |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 251 | del _init_posix |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 252 | del _init_nt |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 253 | del _init_mac |