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