ยปCore Development>Code coverage>Tools/freeze/parsesetup.py

Python code coverage for Tools/freeze/parsesetup.py

#countcontent
1n/a# Parse Makefiles and Python Setup(.in) files.
2n/a
3n/aimport re
4n/a
5n/a
6n/a# Extract variable definitions from a Makefile.
7n/a# Return a dictionary mapping names to values.
8n/a# May raise IOError.
9n/a
10n/amakevardef = re.compile('^([a-zA-Z0-9_]+)[ \t]*=(.*)')
11n/a
12n/adef getmakevars(filename):
13n/a variables = {}
14n/a fp = open(filename)
15n/a pendingline = ""
16n/a try:
17n/a while 1:
18n/a line = fp.readline()
19n/a if pendingline:
20n/a line = pendingline + line
21n/a pendingline = ""
22n/a if not line:
23n/a break
24n/a if line.endswith('\\\n'):
25n/a pendingline = line[:-2]
26n/a matchobj = makevardef.match(line)
27n/a if not matchobj:
28n/a continue
29n/a (name, value) = matchobj.group(1, 2)
30n/a # Strip trailing comment
31n/a i = value.find('#')
32n/a if i >= 0:
33n/a value = value[:i]
34n/a value = value.strip()
35n/a variables[name] = value
36n/a finally:
37n/a fp.close()
38n/a return variables
39n/a
40n/a
41n/a# Parse a Python Setup(.in) file.
42n/a# Return two dictionaries, the first mapping modules to their
43n/a# definitions, the second mapping variable names to their values.
44n/a# May raise IOError.
45n/a
46n/asetupvardef = re.compile('^([a-zA-Z0-9_]+)=(.*)')
47n/a
48n/adef getsetupinfo(filename):
49n/a modules = {}
50n/a variables = {}
51n/a fp = open(filename)
52n/a pendingline = ""
53n/a try:
54n/a while 1:
55n/a line = fp.readline()
56n/a if pendingline:
57n/a line = pendingline + line
58n/a pendingline = ""
59n/a if not line:
60n/a break
61n/a # Strip comments
62n/a i = line.find('#')
63n/a if i >= 0:
64n/a line = line[:i]
65n/a if line.endswith('\\\n'):
66n/a pendingline = line[:-2]
67n/a continue
68n/a matchobj = setupvardef.match(line)
69n/a if matchobj:
70n/a (name, value) = matchobj.group(1, 2)
71n/a variables[name] = value.strip()
72n/a else:
73n/a words = line.split()
74n/a if words:
75n/a modules[words[0]] = words[1:]
76n/a finally:
77n/a fp.close()
78n/a return modules, variables
79n/a
80n/a
81n/a# Test the above functions.
82n/a
83n/adef test():
84n/a import sys
85n/a import os
86n/a if not sys.argv[1:]:
87n/a print('usage: python parsesetup.py Makefile*|Setup* ...')
88n/a sys.exit(2)
89n/a for arg in sys.argv[1:]:
90n/a base = os.path.basename(arg)
91n/a if base[:8] == 'Makefile':
92n/a print('Make style parsing:', arg)
93n/a v = getmakevars(arg)
94n/a prdict(v)
95n/a elif base[:5] == 'Setup':
96n/a print('Setup style parsing:', arg)
97n/a m, v = getsetupinfo(arg)
98n/a prdict(m)
99n/a prdict(v)
100n/a else:
101n/a print(arg, 'is neither a Makefile nor a Setup file')
102n/a print('(name must begin with "Makefile" or "Setup")')
103n/a
104n/adef prdict(d):
105n/a keys = sorted(d.keys())
106n/a for key in keys:
107n/a value = d[key]
108n/a print("%-15s" % key, str(value))
109n/a
110n/aif __name__ == '__main__':
111n/a test()