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 | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 20 | def get_config_h_filename(): |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 21 | """Return full pathname of installed config.h file.""" |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 22 | if os.name == "nt": |
| 23 | return os.path.join(exec_prefix, "include", "config.h") |
| 24 | else: |
| 25 | return os.path.join(exec_prefix, |
| 26 | "include", "python" + sys.version[:3], |
| 27 | "config.h") |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 28 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 29 | def get_makefile_filename(): |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 30 | """Return full pathname of installed Makefile from the Python build.""" |
Greg Ward | a0ca3f2 | 2000-02-02 00:05:14 +0000 | [diff] [blame] | 31 | return os.path.join(exec_prefix, |
| 32 | "lib", "python" + sys.version[:3], |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 33 | "config", "Makefile") |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 34 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 35 | def parse_config_h(fp, g=None): |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 36 | """Parse a config.h-style file. A dictionary containing name/value |
| 37 | pairs is returned. If an optional dictionary is passed in as the second |
| 38 | argument, it is used instead of a new dictionary. |
| 39 | """ |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 40 | if g is None: |
| 41 | g = {} |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 42 | define_rx = re.compile("#define ([A-Z][A-Z0-9_]+) (.*)\n") |
| 43 | undef_rx = re.compile("/[*] #undef ([A-Z][A-Z0-9_]+) [*]/\n") |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 44 | # |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 45 | while 1: |
| 46 | line = fp.readline() |
| 47 | if not line: |
| 48 | break |
| 49 | m = define_rx.match(line) |
| 50 | if m: |
| 51 | n, v = m.group(1, 2) |
Greg Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 52 | try: v = string.atoi(v) |
| 53 | except ValueError: pass |
| 54 | g[n] = v |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 55 | else: |
| 56 | m = undef_rx.match(line) |
| 57 | if m: |
| 58 | g[m.group(1)] = 0 |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 59 | return g |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 60 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 61 | def parse_makefile(fp, g=None): |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 62 | """Parse a Makefile-style file. A dictionary containing name/value |
| 63 | pairs is returned. If an optional dictionary is passed in as the second |
| 64 | argument, it is used instead of a new dictionary. |
| 65 | """ |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 66 | if g is None: |
| 67 | g = {} |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 68 | variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)\n") |
| 69 | done = {} |
| 70 | notdone = {} |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 71 | # |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 72 | while 1: |
| 73 | line = fp.readline() |
| 74 | if not line: |
| 75 | break |
| 76 | m = variable_rx.match(line) |
| 77 | if m: |
| 78 | n, v = m.group(1, 2) |
Greg Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 79 | v = string.strip(v) |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 80 | if "$" in v: |
| 81 | notdone[n] = v |
| 82 | else: |
Greg Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 83 | try: v = string.atoi(v) |
| 84 | except ValueError: pass |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 85 | done[n] = v |
| 86 | |
| 87 | # do variable interpolation here |
| 88 | findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)") |
| 89 | findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}") |
| 90 | while notdone: |
| 91 | for name in notdone.keys(): |
| 92 | value = notdone[name] |
| 93 | m = findvar1_rx.search(value) |
| 94 | if not m: |
| 95 | m = findvar2_rx.search(value) |
| 96 | if m: |
| 97 | n = m.group(1) |
| 98 | if done.has_key(n): |
| 99 | after = value[m.end():] |
| 100 | value = value[:m.start()] + done[n] + after |
| 101 | if "$" in after: |
| 102 | notdone[name] = value |
| 103 | else: |
Greg Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 104 | try: value = string.atoi(value) |
| 105 | except ValueError: pass |
| 106 | done[name] = string.strip(value) |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 107 | del notdone[name] |
| 108 | elif notdone.has_key(n): |
| 109 | # get it on a subsequent round |
| 110 | pass |
| 111 | else: |
| 112 | done[n] = "" |
| 113 | after = value[m.end():] |
| 114 | value = value[:m.start()] + after |
| 115 | if "$" in after: |
| 116 | notdone[name] = value |
| 117 | else: |
Greg Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 118 | try: value = string.atoi(value) |
| 119 | except ValueError: pass |
| 120 | done[name] = string.strip(value) |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 121 | del notdone[name] |
| 122 | else: |
Greg Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 123 | # bogus variable reference; just drop it since we can't deal |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 124 | del notdone[name] |
| 125 | |
| 126 | # save the results in the global dictionary |
| 127 | g.update(done) |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 128 | return g |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 129 | |
| 130 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 131 | def _init_posix(): |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 132 | """Initialize the module as appropriate for POSIX systems.""" |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 133 | g = globals() |
| 134 | # load the installed config.h: |
| 135 | parse_config_h(open(get_config_h_filename()), g) |
Greg Ward | a0ca3f2 | 2000-02-02 00:05:14 +0000 | [diff] [blame] | 136 | # load the installed Makefile: |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 137 | parse_makefile(open(get_makefile_filename()), g) |
| 138 | |
| 139 | |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 140 | def _init_nt(): |
| 141 | """Initialize the module as appropriate for NT""" |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 142 | g = globals() |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 143 | # load config.h, though I don't know how useful this is |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 144 | parse_config_h(open(get_config_h_filename()), g) |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 145 | # set basic install directories |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 146 | g['LIBDEST'] = os.path.join(exec_prefix, "Lib") |
| 147 | g['BINLIBDEST'] = os.path.join(exec_prefix, "Lib") |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 148 | |
Greg Ward | 32162e8 | 1999-08-29 18:22:13 +0000 | [diff] [blame] | 149 | # XXX hmmm.. a normal install puts include files here |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 150 | g['INCLUDEPY'] = os.path.join(prefix, 'include') |
Greg Ward | 32162e8 | 1999-08-29 18:22:13 +0000 | [diff] [blame] | 151 | |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 152 | g['SO'] = '.pyd' |
Greg Ward | a0ca3f2 | 2000-02-02 00:05:14 +0000 | [diff] [blame] | 153 | g['exec_prefix'] = exec_prefix |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 154 | |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 155 | |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame^] | 156 | def _init_mac(): |
| 157 | """Initialize the module as appropriate for Macintosh systems""" |
| 158 | g = globals() |
| 159 | # load the installed config.h (what if not installed? - still need to |
| 160 | # be able to install packages which don't require compilation) |
| 161 | parse_config_h(open( |
| 162 | os.path.join(sys.exec_prefix, "Mac", "Include", "config.h")), g) |
| 163 | # set basic install directories |
| 164 | g['LIBDEST']=os.path.join(sys.exec_prefix, "Lib") |
| 165 | g['BINLIBDEST']= os.path.join(sys.exec_prefix, "Mac", "Plugins") |
| 166 | |
| 167 | # XXX hmmm.. a normal install puts include files here |
| 168 | g['INCLUDEPY'] = os.path.join (sys.prefix, 'Include' ) |
| 169 | |
| 170 | g['SO'] = '.ppc.slb' |
| 171 | g['exec_prefix'] = sys.exec_prefix |
| 172 | print sys.prefix |
| 173 | g['install_lib'] = os.path.join(sys.exec_prefix, "Lib") |
| 174 | g['install_platlib'] = os.path.join(sys.exec_prefix, "Mac", "Lib") |
| 175 | |
| 176 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 177 | try: |
| 178 | exec "_init_" + os.name |
| 179 | except NameError: |
| 180 | # not needed for this platform |
| 181 | pass |
| 182 | else: |
| 183 | exec "_init_%s()" % os.name |
| 184 | |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 185 | |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 186 | del _init_posix |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 187 | del _init_nt |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame^] | 188 | del _init_mac |