ยปCore Development>Code coverage>Tools/scripts/gprof2html.py

Python code coverage for Tools/scripts/gprof2html.py

#countcontent
1n/a#! /usr/bin/env python3
2n/a
3n/a"""Transform gprof(1) output into useful HTML."""
4n/a
5n/aimport html
6n/aimport os
7n/aimport re
8n/aimport sys
9n/aimport webbrowser
10n/a
11n/aheader = """\
12n/a<html>
13n/a<head>
14n/a <title>gprof output (%s)</title>
15n/a</head>
16n/a<body>
17n/a<pre>
18n/a"""
19n/a
20n/atrailer = """\
21n/a</pre>
22n/a</body>
23n/a</html>
24n/a"""
25n/a
26n/adef add_escapes(filename):
27n/a with open(filename) as fp:
28n/a for line in fp:
29n/a yield html.escape(line)
30n/a
31n/a
32n/adef main():
33n/a filename = "gprof.out"
34n/a if sys.argv[1:]:
35n/a filename = sys.argv[1]
36n/a outputfilename = filename + ".html"
37n/a input = add_escapes(filename)
38n/a output = open(outputfilename, "w")
39n/a output.write(header % filename)
40n/a for line in input:
41n/a output.write(line)
42n/a if line.startswith(" time"):
43n/a break
44n/a labels = {}
45n/a for line in input:
46n/a m = re.match(r"(.* )(\w+)\n", line)
47n/a if not m:
48n/a output.write(line)
49n/a break
50n/a stuff, fname = m.group(1, 2)
51n/a labels[fname] = fname
52n/a output.write('%s<a name="flat:%s" href="#call:%s">%s</a>\n' %
53n/a (stuff, fname, fname, fname))
54n/a for line in input:
55n/a output.write(line)
56n/a if line.startswith("index % time"):
57n/a break
58n/a for line in input:
59n/a m = re.match(r"(.* )(\w+)(( &lt;cycle.*&gt;)? \[\d+\])\n", line)
60n/a if not m:
61n/a output.write(line)
62n/a if line.startswith("Index by function name"):
63n/a break
64n/a continue
65n/a prefix, fname, suffix = m.group(1, 2, 3)
66n/a if fname not in labels:
67n/a output.write(line)
68n/a continue
69n/a if line.startswith("["):
70n/a output.write('%s<a name="call:%s" href="#flat:%s">%s</a>%s\n' %
71n/a (prefix, fname, fname, fname, suffix))
72n/a else:
73n/a output.write('%s<a href="#call:%s">%s</a>%s\n' %
74n/a (prefix, fname, fname, suffix))
75n/a for line in input:
76n/a for part in re.findall(r"(\w+(?:\.c)?|\W+)", line):
77n/a if part in labels:
78n/a part = '<a href="#call:%s">%s</a>' % (part, part)
79n/a output.write(part)
80n/a output.write(trailer)
81n/a output.close()
82n/a webbrowser.open("file:" + os.path.abspath(outputfilename))
83n/a
84n/aif __name__ == '__main__':
85n/a main()