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