blob: 26395879a10b8c54f3308f6a3a8eeab2e5506414 [file] [log] [blame]
José Fonsecaa65795f2012-02-18 18:15:18 +00001#!/usr/bin/env python
2##########################################################################
3#
4# Copyright 2011 Jose Fonseca
5# All Rights Reserved.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in
15# all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23# THE SOFTWARE.
24#
25##########################################################################/
26
27
28import platform
29import optparse
José Fonseca3cf1e9a2012-03-16 15:46:26 +000030import os.path
José Fonsecaa65795f2012-02-18 18:15:18 +000031import shutil
32import subprocess
33import sys
34import tempfile
35
36
José Fonseca3cf1e9a2012-03-16 15:46:26 +000037class Dumper:
38
39 def __init__(self, trace, calls):
40 self.output = tempfile.NamedTemporaryFile()
41
42 dump_args = [
José Fonsecaa65795f2012-02-18 18:15:18 +000043 options.apitrace,
44 'dump',
45 '--color=never',
José Fonseca3cf1e9a2012-03-16 15:46:26 +000046 '--call-nos=no',
José Fonsecaa65795f2012-02-18 18:15:18 +000047 '--arg-names=no',
José Fonseca3cf1e9a2012-03-16 15:46:26 +000048 '--calls=' + calls,
José Fonsecaa65795f2012-02-18 18:15:18 +000049 trace
José Fonseca3cf1e9a2012-03-16 15:46:26 +000050 ]
José Fonsecaa65795f2012-02-18 18:15:18 +000051
José Fonseca3cf1e9a2012-03-16 15:46:26 +000052 self.dump = subprocess.Popen(
53 args = dump_args,
54 stdout = self.output,
55 universal_newlines = True,
56 )
José Fonsecadf1a1812012-03-14 11:06:44 +000057
José Fonsecaa65795f2012-02-18 18:15:18 +000058
José Fonsecac6998962012-03-16 09:56:25 +000059if platform.system() == 'Windows':
60 start_delete = ''
61 end_delete = ''
62 start_insert = ''
63 end_insert = ''
64else:
65 start_delete = '\33[9m\33[31m'
66 end_delete = '\33[0m'
67 start_insert = '\33[32m'
68 end_insert = '\33[0m'
69
70
José Fonseca3cf1e9a2012-03-16 15:46:26 +000071def diff(ref_trace, src_trace):
José Fonsecaa65795f2012-02-18 18:15:18 +000072
Carl Worthf45f1e22012-08-13 13:24:03 -070073 isatty = sys.stdout.isatty()
74
José Fonseca3cf1e9a2012-03-16 15:46:26 +000075 ref_dumper = Dumper(ref_trace, options.ref_calls)
76 src_dumper = Dumper(src_trace, options.src_calls)
José Fonsecaa65795f2012-02-18 18:15:18 +000077
José Fonseca3cf1e9a2012-03-16 15:46:26 +000078 # TODO use difflib instead
79 if options.diff == 'diff':
80 diff_args = [
81 'diff',
82 '--speed-large-files',
Carl Worthf45f1e22012-08-13 13:24:03 -070083 ]
84 if isatty:
85 diff_args += [
José Fonseca3cf1e9a2012-03-16 15:46:26 +000086 '--old-line-format=' + start_delete + '%l' + end_delete + '\n',
87 '--new-line-format=' + start_insert + '%l' + end_insert + '\n',
88 ]
89 elif options.diff == 'sdiff':
90 diff_args = [
91 'sdiff',
92 '--width=%u' % options.width,
93 '--speed-large-files',
94 ]
95 elif options.diff == 'wdiff':
96 diff_args = [
97 'wdiff',
98 #'--terminal',
99 '--avoid-wraps',
Carl Worthf45f1e22012-08-13 13:24:03 -0700100 ]
101 if isatty:
102 diff_args += [
José Fonseca3cf1e9a2012-03-16 15:46:26 +0000103 '--start-delete=' + start_delete,
104 '--end-delete=' + end_delete,
105 '--start-insert=' + start_insert,
106 '--end-insert=' + end_insert,
107 ]
108 else:
109 assert False
110 diff_args += [ref_dumper.output.name, src_dumper.output.name]
111
112 ref_dumper.dump.wait()
113 src_dumper.dump.wait()
114
115 less = None
Carl Worthf45f1e22012-08-13 13:24:03 -0700116 if isatty:
José Fonsecaa65795f2012-02-18 18:15:18 +0000117 less = subprocess.Popen(
118 args = ['less', '-FRXn'],
José Fonseca3cf1e9a2012-03-16 15:46:26 +0000119 stdin = subprocess.PIPE
José Fonsecaa65795f2012-02-18 18:15:18 +0000120 )
121
José Fonseca3cf1e9a2012-03-16 15:46:26 +0000122 diff_stdout = less.stdin
123 else:
124 diff_stdout = None
José Fonsecaa65795f2012-02-18 18:15:18 +0000125
José Fonseca3cf1e9a2012-03-16 15:46:26 +0000126 diff = subprocess.Popen(
127 args = diff_args,
128 stdout = diff_stdout,
129 universal_newlines = True,
130 )
131
132 diff.wait()
133
134 if less is not None:
José Fonsecabd750662012-05-08 09:03:05 +0100135 less.stdin.close()
José Fonseca3cf1e9a2012-03-16 15:46:26 +0000136 less.wait()
José Fonsecaa65795f2012-02-18 18:15:18 +0000137
138
José Fonsecac6998962012-03-16 09:56:25 +0000139def which(executable):
140 '''Search for the executable on the PATH.'''
141
142 if platform.system() == 'Windows':
143 exts = ['.exe']
144 else:
145 exts = ['']
146 dirs = os.environ['PATH'].split(os.path.pathsep)
147 for dir in dirs:
148 path = os.path.join(dir, executable)
149 for ext in exts:
150 if os.path.exists(path + ext):
151 return True
152 return False
153
154
José Fonsecaa65795f2012-02-18 18:15:18 +0000155def columns():
156 import curses
157 curses.setupterm()
158 return curses.tigetnum('cols')
159
160
161def main():
162 '''Main program.
163 '''
164
José Fonseca3cf1e9a2012-03-16 15:46:26 +0000165 # Determine default options
José Fonsecaa65795f2012-02-18 18:15:18 +0000166 default_width = columns()
167
168 # Parse command line options
169 optparser = optparse.OptionParser(
170 usage='\n\t%prog [options] -- TRACE_FILE TRACE_FILE',
171 version='%%prog')
172 optparser.add_option(
173 '-a', '--apitrace', metavar='PROGRAM',
174 type='string', dest='apitrace', default='apitrace',
175 help='apitrace command [default: %default]')
176 optparser.add_option(
José Fonsecac6998962012-03-16 09:56:25 +0000177 '-d', '--diff',
178 type="choice", choices=('diff', 'sdiff', 'wdiff'),
José Fonseca3cf1e9a2012-03-16 15:46:26 +0000179 dest="diff", default=None,
180 help="diff program: wdiff, sdiff, or diff [default: auto]")
José Fonsecac6998962012-03-16 09:56:25 +0000181 optparser.add_option(
José Fonsecaa65795f2012-02-18 18:15:18 +0000182 '-c', '--calls', metavar='CALLSET',
183 type="string", dest="calls", default='1-10000',
184 help="calls to compare [default: %default]")
185 optparser.add_option(
José Fonseca3cf1e9a2012-03-16 15:46:26 +0000186 '--ref-calls', metavar='CALLSET',
187 type="string", dest="ref_calls", default=None,
188 help="calls to compare from reference trace")
189 optparser.add_option(
190 '--src-calls', metavar='CALLSET',
191 type="string", dest="src_calls", default=None,
192 help="calls to compare from source trace")
193 optparser.add_option(
José Fonsecaa65795f2012-02-18 18:15:18 +0000194 '-w', '--width', metavar='NUM',
José Fonseca3abdc572012-05-08 09:05:53 +0100195 type="int", dest="width", default=default_width,
José Fonsecaa65795f2012-02-18 18:15:18 +0000196 help="columns [default: %default]")
197
198 global options
199 (options, args) = optparser.parse_args(sys.argv[1:])
200 if len(args) != 2:
201 optparser.error("incorrect number of arguments")
202
José Fonseca3cf1e9a2012-03-16 15:46:26 +0000203 if options.diff is None:
204 if which('wdiff'):
205 options.diff = 'wdiff'
206 else:
207 sys.stderr.write('warning: wdiff not found\n')
208 if which('sdiff'):
209 options.diff = 'sdiff'
210 else:
211 sys.stderr.write('warning: sdiff not found\n')
212 options.diff = 'diff'
213
214 if options.ref_calls is None:
215 options.ref_calls = options.calls
216 if options.src_calls is None:
217 options.src_calls = options.calls
218
219 diff(*args)
José Fonsecaa65795f2012-02-18 18:15:18 +0000220
221
222if __name__ == '__main__':
223 main()