Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 1 | # Parse Makefiles and Python Setup(.in) files. |
| 2 | |
Eric S. Raymond | 1bb515b | 2001-03-18 11:27:58 +0000 | [diff] [blame] | 3 | import re |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 4 | import string |
| 5 | |
| 6 | |
| 7 | # Extract variable definitions from a Makefile. |
| 8 | # Return a dictionary mapping names to values. |
| 9 | # May raise IOError. |
| 10 | |
Eric S. Raymond | 1bb515b | 2001-03-18 11:27:58 +0000 | [diff] [blame] | 11 | makevardef = re.compile('^([a-zA-Z0-9_]+)[ \t]*=(.*)') |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 12 | |
| 13 | def getmakevars(filename): |
| 14 | variables = {} |
| 15 | fp = open(filename) |
Martin v. Löwis | 2c91c81 | 2001-03-21 06:58:25 +0000 | [diff] [blame] | 16 | pendingline = "" |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 17 | try: |
| 18 | while 1: |
| 19 | line = fp.readline() |
Martin v. Löwis | 2c91c81 | 2001-03-21 06:58:25 +0000 | [diff] [blame] | 20 | if pendingline: |
| 21 | line = pendingline + line |
| 22 | pendingline = "" |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 23 | if not line: |
| 24 | break |
Martin v. Löwis | 2c91c81 | 2001-03-21 06:58:25 +0000 | [diff] [blame] | 25 | if line.endswith('\\\n'): |
| 26 | pendingline = line[:-2] |
Eric S. Raymond | 1bb515b | 2001-03-18 11:27:58 +0000 | [diff] [blame] | 27 | matchobj = makevardef.match(line) |
| 28 | if not matchobj: |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 29 | continue |
Eric S. Raymond | 1bb515b | 2001-03-18 11:27:58 +0000 | [diff] [blame] | 30 | (name, value) = matchobj.group(1, 2) |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 31 | # Strip trailing comment |
| 32 | i = string.find(value, '#') |
| 33 | if i >= 0: |
| 34 | value = value[:i] |
| 35 | value = string.strip(value) |
| 36 | variables[name] = value |
| 37 | finally: |
| 38 | fp.close() |
| 39 | return variables |
| 40 | |
| 41 | |
| 42 | # Parse a Python Setup(.in) file. |
| 43 | # Return two dictionaries, the first mapping modules to their |
| 44 | # definitions, the second mapping variable names to their values. |
| 45 | # May raise IOError. |
| 46 | |
Eric S. Raymond | 1bb515b | 2001-03-18 11:27:58 +0000 | [diff] [blame] | 47 | setupvardef = re.compile('^([a-zA-Z0-9_]+)=(.*)') |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 48 | |
| 49 | def getsetupinfo(filename): |
| 50 | modules = {} |
| 51 | variables = {} |
| 52 | fp = open(filename) |
Martin v. Löwis | 2c91c81 | 2001-03-21 06:58:25 +0000 | [diff] [blame] | 53 | pendingline = "" |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 54 | try: |
| 55 | while 1: |
| 56 | line = fp.readline() |
Martin v. Löwis | 2c91c81 | 2001-03-21 06:58:25 +0000 | [diff] [blame] | 57 | if pendingline: |
| 58 | line = pendingline + line |
| 59 | pendingline = "" |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 60 | if not line: |
| 61 | break |
| 62 | # Strip comments |
| 63 | i = string.find(line, '#') |
| 64 | if i >= 0: |
| 65 | line = line[:i] |
Martin v. Löwis | 2c91c81 | 2001-03-21 06:58:25 +0000 | [diff] [blame] | 66 | if line.endswith('\\\n'): |
| 67 | pendingline = line[:-2] |
| 68 | continue |
Eric S. Raymond | 1bb515b | 2001-03-18 11:27:58 +0000 | [diff] [blame] | 69 | matchobj = setupvardef.match(line) |
| 70 | if matchobj: |
| 71 | (name, value) = matchobj.group(1, 2) |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 72 | variables[name] = string.strip(value) |
| 73 | else: |
| 74 | words = string.split(line) |
| 75 | if words: |
| 76 | modules[words[0]] = words[1:] |
| 77 | finally: |
| 78 | fp.close() |
| 79 | return modules, variables |
| 80 | |
| 81 | |
| 82 | # Test the above functions. |
| 83 | |
| 84 | def test(): |
| 85 | import sys |
| 86 | import os |
| 87 | if not sys.argv[1:]: |
| 88 | print 'usage: python parsesetup.py Makefile*|Setup* ...' |
| 89 | sys.exit(2) |
| 90 | for arg in sys.argv[1:]: |
| 91 | base = os.path.basename(arg) |
| 92 | if base[:8] == 'Makefile': |
| 93 | print 'Make style parsing:', arg |
| 94 | v = getmakevars(arg) |
| 95 | prdict(v) |
| 96 | elif base[:5] == 'Setup': |
| 97 | print 'Setup style parsing:', arg |
| 98 | m, v = getsetupinfo(arg) |
| 99 | prdict(m) |
| 100 | prdict(v) |
| 101 | else: |
| 102 | print arg, 'is neither a Makefile nor a Setup file' |
| 103 | print '(name must begin with "Makefile" or "Setup")' |
| 104 | |
| 105 | def prdict(d): |
| 106 | keys = d.keys() |
| 107 | keys.sort() |
| 108 | for key in keys: |
| 109 | value = d[key] |
| 110 | print "%-15s" % key, str(value) |
| 111 | |
| 112 | if __name__ == '__main__': |
| 113 | test() |