blob: 7a6b72ef770598f570b7ea1a4232147a504788bd [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 +00004import 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. Raymond1bb515b2001-03-18 11:27:58 +000011makevardef = re.compile('^([a-zA-Z0-9_]+)[ \t]*=(.*)')
Guido van Rossum00ff4331994-10-03 16:33:08 +000012
13def getmakevars(filename):
14 variables = {}
15 fp = open(filename)
Martin v. Löwis2c91c812001-03-21 06:58:25 +000016 pendingline = ""
Guido van Rossum00ff4331994-10-03 16:33:08 +000017 try:
18 while 1:
19 line = fp.readline()
Martin v. Löwis2c91c812001-03-21 06:58:25 +000020 if pendingline:
21 line = pendingline + line
22 pendingline = ""
Guido van Rossum00ff4331994-10-03 16:33:08 +000023 if not line:
24 break
Martin v. Löwis2c91c812001-03-21 06:58:25 +000025 if line.endswith('\\\n'):
26 pendingline = line[:-2]
Eric S. Raymond1bb515b2001-03-18 11:27:58 +000027 matchobj = makevardef.match(line)
28 if not matchobj:
Guido van Rossum00ff4331994-10-03 16:33:08 +000029 continue
Eric S. Raymond1bb515b2001-03-18 11:27:58 +000030 (name, value) = matchobj.group(1, 2)
Guido van Rossum00ff4331994-10-03 16:33:08 +000031 # 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. Raymond1bb515b2001-03-18 11:27:58 +000047setupvardef = re.compile('^([a-zA-Z0-9_]+)=(.*)')
Guido van Rossum00ff4331994-10-03 16:33:08 +000048
49def getsetupinfo(filename):
50 modules = {}
51 variables = {}
52 fp = open(filename)
Martin v. Löwis2c91c812001-03-21 06:58:25 +000053 pendingline = ""
Guido van Rossum00ff4331994-10-03 16:33:08 +000054 try:
55 while 1:
56 line = fp.readline()
Martin v. Löwis2c91c812001-03-21 06:58:25 +000057 if pendingline:
58 line = pendingline + line
59 pendingline = ""
Guido van Rossum00ff4331994-10-03 16:33:08 +000060 if not line:
61 break
62 # Strip comments
63 i = string.find(line, '#')
64 if i >= 0:
65 line = line[:i]
Martin v. Löwis2c91c812001-03-21 06:58:25 +000066 if line.endswith('\\\n'):
67 pendingline = line[:-2]
68 continue
Eric S. Raymond1bb515b2001-03-18 11:27:58 +000069 matchobj = setupvardef.match(line)
70 if matchobj:
71 (name, value) = matchobj.group(1, 2)
Guido van Rossum00ff4331994-10-03 16:33:08 +000072 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
84def 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
105def 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
112if __name__ == '__main__':
113 test()