Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 184612
1
#!/usr/bin/env python3
2
#
3
# Copyright 2013-2017 Jose Fonseca
4
#
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU Lesser General Public License as published
7
# by the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU Lesser General Public License for more details.
14
#
15
# You should have received a copy of the GNU Lesser General Public License
16
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
#
18
19
20
import difflib
21
import optparse
22
import os.path
23
import sys
24
import subprocess
25
import shutil
26
27
28
if sys.version_info[0] >= 3:
29
PYTHON_3 = True
30
else:
31
PYTHON_3 = False
32
33
34
formats = [
35
"axe",
36
"callgrind",
37
"hprof",
38
"json",
39
"oprofile",
40
"perf",
41
"prof",
42
"pstats",
43
"sleepy",
44
"sysprof",
45
"xperf",
46
]
47
48
49
def run(cmd):
50
sys.stderr.flush()
51
sys.stdout.write(' '.join(cmd) + '\n')
52
sys.stdout.flush()
53
p = subprocess.Popen(cmd)
54
try:
55
return p.wait()
56
except KeyboardInterrupt:
57
p.terminate()
58
raise
59
60
61
def diff(a, b):
62
if PYTHON_3:
63
a_lines = open(a, 'rt', encoding='UTF-8').readlines()
64
b_lines = open(b, 'rt', encoding='UTF-8').readlines()
65
else:
66
a_lines = open(a, 'rt').readlines()
67
b_lines = open(b, 'rt').readlines()
68
diff_lines = difflib.unified_diff(a_lines, b_lines, fromfile=a, tofile=b)
69
sys.stdout.write(''.join(diff_lines))
70
71
72
def main():
73
"""Main program."""
74
75
global formats
76
77
test_dir = os.path.dirname(os.path.abspath(__file__))
78
79
optparser = optparse.OptionParser(
80
usage="\n\t%prog [options] [format] ...")
81
optparser.add_option(
82
'-p', '--python', metavar='PATH',
83
type="string", dest="python", default=sys.executable,
84
help="path to python executable [default: %default]")
85
optparser.add_option(
86
'-g', '--gprof2dot', metavar='PATH',
87
type="string", dest="gprof2dot", default=os.path.join(test_dir, os.path.pardir, 'gprof2dot.py'),
88
help="path to gprof2dot.py script [default: %default]")
89
optparser.add_option(
90
'-f', '--force',
91
action="store_true",
92
dest="force", default=False,
93
help="force reference generation")
94
(options, args) = optparser.parse_args(sys.argv[1:])
95
96
if len(args):
97
formats = args
98
99
for format in formats:
100
test_subdir = os.path.join(test_dir, format)
101
for filename in os.listdir(test_subdir):
102
name, ext = os.path.splitext(filename)
103
if ext == '.' + format:
104
sys.stdout.write(filename + '\n')
105
106
profile = os.path.join(test_subdir, filename)
107
dot = os.path.join(test_subdir, name + '.dot')
108
png = os.path.join(test_subdir, name + '.png')
109
110
ref_dot = os.path.join(test_subdir, name + '.orig.dot')
111
ref_png = os.path.join(test_subdir, name + '.orig.png')
112
113
if run([ options.python, options.gprof2dot, '-f', format, '-o', dot, profile]) != 0:
114
continue
115
116
if run(['dot', '-Tpng', '-o', png, dot]) != 0:
117
continue
118
119
if options.force or not os.path.exists(ref_dot):
120
shutil.copy(dot, ref_dot)
121
shutil.copy(png, ref_png)
122
else:
123
diff(ref_dot, dot)
124
125
126
if __name__ == '__main__':
127
main()
128
129