2 ##########################################################################
4 # Copyright 2011 Jose Fonseca
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:
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
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
25 ##########################################################################/
39 def __init__(self, trace, calls):
40 self.output = tempfile.NamedTemporaryFile()
52 self.dump = subprocess.Popen(
55 universal_newlines = True,
59 if platform.system() == 'Windows':
65 start_delete = '\33[9m\33[31m'
67 start_insert = '\33[32m'
71 def diff(ref_trace, src_trace):
73 isatty = sys.stdout.isatty()
75 ref_dumper = Dumper(ref_trace, options.ref_calls)
76 src_dumper = Dumper(src_trace, options.src_calls)
78 # TODO use difflib instead
79 if options.diff == 'diff':
82 '--speed-large-files',
86 '--old-line-format=' + start_delete + '%l' + end_delete + '\n',
87 '--new-line-format=' + start_insert + '%l' + end_insert + '\n',
89 elif options.diff == 'sdiff':
92 '--width=%u' % options.width,
93 '--speed-large-files',
95 elif options.diff == 'wdiff':
103 '--start-delete=' + start_delete,
104 '--end-delete=' + end_delete,
105 '--start-insert=' + start_insert,
106 '--end-insert=' + end_insert,
110 diff_args += [ref_dumper.output.name, src_dumper.output.name]
112 ref_dumper.dump.wait()
113 src_dumper.dump.wait()
117 less = subprocess.Popen(
118 args = ['less', '-FRXn'],
119 stdin = subprocess.PIPE
122 diff_stdout = less.stdin
126 diff = subprocess.Popen(
128 stdout = diff_stdout,
129 universal_newlines = True,
139 def which(executable):
140 '''Search for the executable on the PATH.'''
142 if platform.system() == 'Windows':
146 dirs = os.environ['PATH'].split(os.path.pathsep)
148 path = os.path.join(dir, executable)
150 if os.path.exists(path + ext):
158 return curses.tigetnum('cols')
165 # Determine default options
166 default_width = columns()
168 # Parse command line options
169 optparser = optparse.OptionParser(
170 usage='\n\t%prog [options] -- TRACE_FILE TRACE_FILE',
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(
178 type="choice", choices=('diff', 'sdiff', 'wdiff'),
179 dest="diff", default=None,
180 help="diff program: wdiff, sdiff, or diff [default: auto]")
181 optparser.add_option(
182 '-c', '--calls', metavar='CALLSET',
183 type="string", dest="calls", default='1-10000',
184 help="calls to compare [default: %default]")
185 optparser.add_option(
186 '--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(
194 '-w', '--width', metavar='NUM',
195 type="int", dest="width", default=default_width,
196 help="columns [default: %default]")
199 (options, args) = optparser.parse_args(sys.argv[1:])
201 optparser.error("incorrect number of arguments")
203 if options.diff is None:
205 options.diff = 'wdiff'
207 sys.stderr.write('warning: wdiff not found\n')
209 options.diff = 'sdiff'
211 sys.stderr.write('warning: sdiff not found\n')
212 options.diff = 'diff'
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
222 if __name__ == '__main__':