blob: f86fc391b0dd6d2eb3265120289db8e8b5c1aa8b [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
José Fonseca3cf1e9a2012-03-16 15:46:26 +000073 ref_dumper = Dumper(ref_trace, options.ref_calls)
74 src_dumper = Dumper(src_trace, options.src_calls)
José Fonsecaa65795f2012-02-18 18:15:18 +000075
José Fonseca3cf1e9a2012-03-16 15:46:26 +000076 # TODO use difflib instead
77 if options.diff == 'diff':
78 diff_args = [
79 'diff',
80 '--speed-large-files',
81 '--old-line-format=' + start_delete + '%l' + end_delete + '\n',
82 '--new-line-format=' + start_insert + '%l' + end_insert + '\n',
83 ]
84 elif options.diff == 'sdiff':
85 diff_args = [
86 'sdiff',
87 '--width=%u' % options.width,
88 '--speed-large-files',
89 ]
90 elif options.diff == 'wdiff':
91 diff_args = [
92 'wdiff',
93 #'--terminal',
94 '--avoid-wraps',
95 '--start-delete=' + start_delete,
96 '--end-delete=' + end_delete,
97 '--start-insert=' + start_insert,
98 '--end-insert=' + end_insert,
99 ]
100 else:
101 assert False
102 diff_args += [ref_dumper.output.name, src_dumper.output.name]
103
104 ref_dumper.dump.wait()
105 src_dumper.dump.wait()
106
107 less = None
108 if sys.stdout.isatty():
José Fonsecaa65795f2012-02-18 18:15:18 +0000109 less = subprocess.Popen(
110 args = ['less', '-FRXn'],
José Fonseca3cf1e9a2012-03-16 15:46:26 +0000111 stdin = subprocess.PIPE
José Fonsecaa65795f2012-02-18 18:15:18 +0000112 )
113
José Fonseca3cf1e9a2012-03-16 15:46:26 +0000114 diff_stdout = less.stdin
115 else:
116 diff_stdout = None
José Fonsecaa65795f2012-02-18 18:15:18 +0000117
José Fonseca3cf1e9a2012-03-16 15:46:26 +0000118 diff = subprocess.Popen(
119 args = diff_args,
120 stdout = diff_stdout,
121 universal_newlines = True,
122 )
123
124 diff.wait()
125
126 if less is not None:
José Fonsecabd750662012-05-08 09:03:05 +0100127 less.stdin.close()
José Fonseca3cf1e9a2012-03-16 15:46:26 +0000128 less.wait()
José Fonsecaa65795f2012-02-18 18:15:18 +0000129
130
José Fonsecac6998962012-03-16 09:56:25 +0000131def which(executable):
132 '''Search for the executable on the PATH.'''
133
134 if platform.system() == 'Windows':
135 exts = ['.exe']
136 else:
137 exts = ['']
138 dirs = os.environ['PATH'].split(os.path.pathsep)
139 for dir in dirs:
140 path = os.path.join(dir, executable)
141 for ext in exts:
142 if os.path.exists(path + ext):
143 return True
144 return False
145
146
José Fonsecaa65795f2012-02-18 18:15:18 +0000147def columns():
148 import curses
149 curses.setupterm()
150 return curses.tigetnum('cols')
151
152
153def main():
154 '''Main program.
155 '''
156
José Fonseca3cf1e9a2012-03-16 15:46:26 +0000157 # Determine default options
José Fonsecaa65795f2012-02-18 18:15:18 +0000158 default_width = columns()
159
160 # Parse command line options
161 optparser = optparse.OptionParser(
162 usage='\n\t%prog [options] -- TRACE_FILE TRACE_FILE',
163 version='%%prog')
164 optparser.add_option(
165 '-a', '--apitrace', metavar='PROGRAM',
166 type='string', dest='apitrace', default='apitrace',
167 help='apitrace command [default: %default]')
168 optparser.add_option(
José Fonsecac6998962012-03-16 09:56:25 +0000169 '-d', '--diff',
170 type="choice", choices=('diff', 'sdiff', 'wdiff'),
José Fonseca3cf1e9a2012-03-16 15:46:26 +0000171 dest="diff", default=None,
172 help="diff program: wdiff, sdiff, or diff [default: auto]")
José Fonsecac6998962012-03-16 09:56:25 +0000173 optparser.add_option(
José Fonsecaa65795f2012-02-18 18:15:18 +0000174 '-c', '--calls', metavar='CALLSET',
175 type="string", dest="calls", default='1-10000',
176 help="calls to compare [default: %default]")
177 optparser.add_option(
José Fonseca3cf1e9a2012-03-16 15:46:26 +0000178 '--ref-calls', metavar='CALLSET',
179 type="string", dest="ref_calls", default=None,
180 help="calls to compare from reference trace")
181 optparser.add_option(
182 '--src-calls', metavar='CALLSET',
183 type="string", dest="src_calls", default=None,
184 help="calls to compare from source trace")
185 optparser.add_option(
José Fonsecaa65795f2012-02-18 18:15:18 +0000186 '-w', '--width', metavar='NUM',
187 type="string", dest="width", default=default_width,
188 help="columns [default: %default]")
189
190 global options
191 (options, args) = optparser.parse_args(sys.argv[1:])
192 if len(args) != 2:
193 optparser.error("incorrect number of arguments")
194
José Fonseca3cf1e9a2012-03-16 15:46:26 +0000195 if options.diff is None:
196 if which('wdiff'):
197 options.diff = 'wdiff'
198 else:
199 sys.stderr.write('warning: wdiff not found\n')
200 if which('sdiff'):
201 options.diff = 'sdiff'
202 else:
203 sys.stderr.write('warning: sdiff not found\n')
204 options.diff = 'diff'
205
206 if options.ref_calls is None:
207 options.ref_calls = options.calls
208 if options.src_calls is None:
209 options.src_calls = options.calls
210
211 diff(*args)
José Fonsecaa65795f2012-02-18 18:15:18 +0000212
213
214if __name__ == '__main__':
215 main()