1 # Parse Makefiles and Python Setup(.in) files.
7 # Extract variable definitions from a Makefile.
8 # Return a dictionary mapping names to values.
11 makevardef
= regex
.compile('^\([a-zA-Z0-9_]+\)[ \t]*=\(.*\)')
13 def getmakevars(filename
):
21 if makevardef
.match(line
) < 0:
23 name
, value
= makevardef
.group(1, 2)
24 # Strip trailing comment
25 i
= string
.find(value
, '#')
28 value
= string
.strip(value
)
29 variables
[name
] = value
35 # Parse a Python Setup(.in) file.
36 # Return two dictionaries, the first mapping modules to their
37 # definitions, the second mapping variable names to their values.
40 setupvardef
= regex
.compile('^\([a-zA-Z0-9_]+\)=\(.*\)')
42 def getsetupinfo(filename
):
52 i
= string
.find(line
, '#')
55 if setupvardef
.match(line
) >= 0:
56 name
, value
= setupvardef
.group(1, 2)
57 variables
[name
] = string
.strip(value
)
59 words
= string
.split(line
)
61 modules
[words
[0]] = words
[1:]
64 return modules
, variables
67 # Test the above functions.
73 print 'usage: python parsesetup.py Makefile*|Setup* ...'
75 for arg
in sys
.argv
[1:]:
76 base
= os
.path
.basename(arg
)
77 if base
[:8] == 'Makefile':
78 print 'Make style parsing:', arg
81 elif base
[:5] == 'Setup':
82 print 'Setup style parsing:', arg
83 m
, v
= getsetupinfo(arg
)
87 print arg
, 'is neither a Makefile nor a Setup file'
88 print '(name must begin with "Makefile" or "Setup")'
95 print "%-15s" % key
, str(value
)
97 if __name__
== '__main__':