blob: 158847614dcc018c7ae6c51d71b48caff065034c [file] [log] [blame]
Guido van Rossum00ff4331994-10-03 16:33:08 +00001# Parse Makefiles and Python Setup(.in) files.
2
Eric S. Raymond1bb515b2001-03-18 11:27:58 +00003import re
Guido van Rossum00ff4331994-10-03 16:33:08 +00004
5
6# Extract variable definitions from a Makefile.
7# Return a dictionary mapping names to values.
8# May raise IOError.
9
Eric S. Raymond1bb515b2001-03-18 11:27:58 +000010makevardef = re.compile('^([a-zA-Z0-9_]+)[ \t]*=(.*)')
Guido van Rossum00ff4331994-10-03 16:33:08 +000011
12def getmakevars(filename):
Tim Peters182b5ac2004-07-18 06:16:08 +000013 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 Rossum00ff4331994-10-03 16:33:08 +000039
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. Raymond1bb515b2001-03-18 11:27:58 +000046setupvardef = re.compile('^([a-zA-Z0-9_]+)=(.*)')
Guido van Rossum00ff4331994-10-03 16:33:08 +000047
48def getsetupinfo(filename):
Tim Peters182b5ac2004-07-18 06:16:08 +000049 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 Rossum00ff4331994-10-03 16:33:08 +000079
80
81# Test the above functions.
82
83def test():
Tim Peters182b5ac2004-07-18 06:16:08 +000084 import sys
85 import os
86 if not sys.argv[1:]:
Guido van Rossum96bf7e82007-02-09 23:27:01 +000087 print('usage: python parsesetup.py Makefile*|Setup* ...')
Tim Peters182b5ac2004-07-18 06:16:08 +000088 sys.exit(2)
89 for arg in sys.argv[1:]:
90 base = os.path.basename(arg)
91 if base[:8] == 'Makefile':
Guido van Rossum96bf7e82007-02-09 23:27:01 +000092 print('Make style parsing:', arg)
Tim Peters182b5ac2004-07-18 06:16:08 +000093 v = getmakevars(arg)
94 prdict(v)
95 elif base[:5] == 'Setup':
Guido van Rossum96bf7e82007-02-09 23:27:01 +000096 print('Setup style parsing:', arg)
Tim Peters182b5ac2004-07-18 06:16:08 +000097 m, v = getsetupinfo(arg)
98 prdict(m)
99 prdict(v)
100 else:
Guido van Rossum96bf7e82007-02-09 23:27:01 +0000101 print(arg, 'is neither a Makefile nor a Setup file')
102 print('(name must begin with "Makefile" or "Setup")')
Guido van Rossum00ff4331994-10-03 16:33:08 +0000103
104def prdict(d):
Guido van Rossum53970392007-06-12 00:28:30 +0000105 keys = sorted(d.keys())
Tim Peters182b5ac2004-07-18 06:16:08 +0000106 for key in keys:
107 value = d[key]
Guido van Rossum96bf7e82007-02-09 23:27:01 +0000108 print("%-15s" % key, str(value))
Guido van Rossum00ff4331994-10-03 16:33:08 +0000109
110if __name__ == '__main__':
Tim Peters182b5ac2004-07-18 06:16:08 +0000111 test()