1 # Parse Makefiles and Python Setup(.in) files.
6 # Extract variable definitions from a Makefile.
7 # Return a dictionary mapping names to values.
10 makevardef
= re
.compile('^([a-zA-Z0-9_]+)[ \t]*=(.*)')
12 def getmakevars(filename
):
20 line
= pendingline
+ line
24 if line
.endswith('\\\n'):
25 pendingline
= line
[:-2]
26 matchobj
= makevardef
.match(line
)
29 (name
, value
) = matchobj
.group(1, 2)
30 # Strip trailing comment
35 variables
[name
] = value
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.
46 setupvardef
= re
.compile('^([a-zA-Z0-9_]+)=(.*)')
48 def getsetupinfo(filename
):
57 line
= pendingline
+ line
65 if line
.endswith('\\\n'):
66 pendingline
= line
[:-2]
68 matchobj
= setupvardef
.match(line
)
70 (name
, value
) = matchobj
.group(1, 2)
71 variables
[name
] = value
.strip()
75 modules
[words
[0]] = words
[1:]
78 return modules
, variables
81 # Test the above functions.
87 print 'usage: python parsesetup.py Makefile*|Setup* ...'
89 for arg
in sys
.argv
[1:]:
90 base
= os
.path
.basename(arg
)
91 if base
[:8] == 'Makefile':
92 print 'Make style parsing:', arg
95 elif base
[:5] == 'Setup':
96 print 'Setup style parsing:', arg
97 m
, v
= getsetupinfo(arg
)
101 print arg
, 'is neither a Makefile nor a Setup file'
102 print '(name must begin with "Makefile" or "Setup")'
109 print "%-15s" % key
, str(value
)
111 if __name__
== '__main__':