blob: 2b9123ea4def91747eaff47a61f176274cc704f9 [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):
13 variables = {}
14 fp = open(filename)
Martin v. Löwis2c91c812001-03-21 06:58:25 +000015 pendingline = ""
Guido van Rossum00ff4331994-10-03 16:33:08 +000016 try:
17 while 1:
18 line = fp.readline()
Martin v. Löwis2c91c812001-03-21 06:58:25 +000019 if pendingline:
20 line = pendingline + line
21 pendingline = ""
Guido van Rossum00ff4331994-10-03 16:33:08 +000022 if not line:
23 break
Martin v. Löwis2c91c812001-03-21 06:58:25 +000024 if line.endswith('\\\n'):
25 pendingline = line[:-2]
Eric S. Raymond1bb515b2001-03-18 11:27:58 +000026 matchobj = makevardef.match(line)
27 if not matchobj:
Guido van Rossum00ff4331994-10-03 16:33:08 +000028 continue
Eric S. Raymond1bb515b2001-03-18 11:27:58 +000029 (name, value) = matchobj.group(1, 2)
Guido van Rossum00ff4331994-10-03 16:33:08 +000030 # Strip trailing comment
Walter Dörwaldaaab30e2002-09-11 20:36:02 +000031 i = value.find('#')
Guido van Rossum00ff4331994-10-03 16:33:08 +000032 if i >= 0:
33 value = value[:i]
Walter Dörwaldaaab30e2002-09-11 20:36:02 +000034 value = value.strip()
Guido van Rossum00ff4331994-10-03 16:33:08 +000035 variables[name] = value
36 finally:
37 fp.close()
38 return variables
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. 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):
49 modules = {}
50 variables = {}
51 fp = open(filename)
Martin v. Löwis2c91c812001-03-21 06:58:25 +000052 pendingline = ""
Guido van Rossum00ff4331994-10-03 16:33:08 +000053 try:
54 while 1:
55 line = fp.readline()
Martin v. Löwis2c91c812001-03-21 06:58:25 +000056 if pendingline:
57 line = pendingline + line
58 pendingline = ""
Guido van Rossum00ff4331994-10-03 16:33:08 +000059 if not line:
60 break
61 # Strip comments
Walter Dörwaldaaab30e2002-09-11 20:36:02 +000062 i = line.find('#')
Guido van Rossum00ff4331994-10-03 16:33:08 +000063 if i >= 0:
64 line = line[:i]
Martin v. Löwis2c91c812001-03-21 06:58:25 +000065 if line.endswith('\\\n'):
66 pendingline = line[:-2]
67 continue
Eric S. Raymond1bb515b2001-03-18 11:27:58 +000068 matchobj = setupvardef.match(line)
69 if matchobj:
70 (name, value) = matchobj.group(1, 2)
Walter Dörwaldaaab30e2002-09-11 20:36:02 +000071 variables[name] = value.strip()
Guido van Rossum00ff4331994-10-03 16:33:08 +000072 else:
Walter Dörwaldaaab30e2002-09-11 20:36:02 +000073 words = line.split()
Guido van Rossum00ff4331994-10-03 16:33:08 +000074 if words:
75 modules[words[0]] = words[1:]
76 finally:
77 fp.close()
78 return modules, variables
79
80
81# Test the above functions.
82
83def test():
84 import sys
85 import os
86 if not sys.argv[1:]:
87 print 'usage: python parsesetup.py Makefile*|Setup* ...'
88 sys.exit(2)
89 for arg in sys.argv[1:]:
90 base = os.path.basename(arg)
91 if base[:8] == 'Makefile':
92 print 'Make style parsing:', arg
93 v = getmakevars(arg)
94 prdict(v)
95 elif base[:5] == 'Setup':
96 print 'Setup style parsing:', arg
97 m, v = getsetupinfo(arg)
98 prdict(m)
99 prdict(v)
100 else:
101 print arg, 'is neither a Makefile nor a Setup file'
102 print '(name must begin with "Makefile" or "Setup")'
103
104def prdict(d):
105 keys = d.keys()
106 keys.sort()
107 for key in keys:
108 value = d[key]
109 print "%-15s" % key, str(value)
110
111if __name__ == '__main__':
112 test()