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