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 ##########################################################################/
38 def stripdump(trace, fifo):
39 dump = subprocess.Popen(
45 '--calls=' + options.calls,
48 stdout = subprocess.PIPE,
49 universal_newlines = True,
52 sed = subprocess.Popen(
56 '-e', r's/^[0-9]\+ //',
57 '-e', r's/hdc = \w\+/hdc/g',
60 stdout = open(fifo, 'wt'),
61 universal_newlines = True,
64 # XXX: Avoid a weird race condition
68 if platform.system() == 'Windows':
74 start_delete = '\33[9m\33[31m'
76 start_insert = '\33[32m'
81 fifodir = tempfile.mkdtemp()
84 for i in range(len(traces)):
86 fifo = os.path.join(fifodir, str(i))
87 stripdump(trace, fifo)
90 # TODO use difflib instead
91 if options.diff == 'diff':
94 '--speed-large-files',
95 '--old-line-format=' + start_delete + '%l' + end_delete + '\n',
96 '--new-line-format=' + start_insert + '%l' + end_insert + '\n',
98 elif options.diff == 'sdiff':
101 '--width=%u' % options.width,
102 '--speed-large-files',
104 elif options.diff == 'wdiff':
109 '--start-delete=' + start_delete,
110 '--end-delete=' + end_delete,
111 '--start-insert=' + start_insert,
112 '--end-insert=' + end_insert,
118 diff = subprocess.Popen(
120 stdout = subprocess.PIPE,
121 universal_newlines = True,
124 less = subprocess.Popen(
125 args = ['less', '-FRXn'],
132 shutil.rmtree(fifodir)
135 def which(executable):
136 '''Search for the executable on the PATH.'''
138 if platform.system() == 'Windows':
142 dirs = os.environ['PATH'].split(os.path.pathsep)
144 path = os.path.join(dir, executable)
146 if os.path.exists(path + ext):
154 return curses.tigetnum('cols')
162 default_diff = 'wdiff'
164 default_diff = 'sdiff'
166 default_diff = 'diff'
168 default_width = columns()
170 # Parse command line options
171 optparser = optparse.OptionParser(
172 usage='\n\t%prog [options] -- TRACE_FILE TRACE_FILE',
174 optparser.add_option(
175 '-a', '--apitrace', metavar='PROGRAM',
176 type='string', dest='apitrace', default='apitrace',
177 help='apitrace command [default: %default]')
178 optparser.add_option(
180 type="choice", choices=('diff', 'sdiff', 'wdiff'),
181 dest="diff", default=default_diff,
182 help="diff program: diff, sdiff, or wdiff [default: %default]")
183 optparser.add_option(
184 '-c', '--calls', metavar='CALLSET',
185 type="string", dest="calls", default='1-10000',
186 help="calls to compare [default: %default]")
187 optparser.add_option(
188 '-w', '--width', metavar='NUM',
189 type="string", dest="width", default=default_width,
190 help="columns [default: %default]")
193 (options, args) = optparser.parse_args(sys.argv[1:])
195 optparser.error("incorrect number of arguments")
200 if __name__ == '__main__':