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