José Fonseca | a65795f | 2012-02-18 18:15:18 +0000 | [diff] [blame] | 1 | #!/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 | |
| 28 | import platform |
| 29 | import optparse |
| 30 | import os |
| 31 | import shutil |
| 32 | import subprocess |
| 33 | import sys |
| 34 | import tempfile |
| 35 | |
| 36 | |
| 37 | def stripdump(trace, fifo): |
| 38 | dump = subprocess.Popen( |
| 39 | args = [ |
| 40 | options.apitrace, |
| 41 | 'dump', |
| 42 | '--color=never', |
| 43 | '--arg-names=no', |
| 44 | '--calls=' + options.calls, |
| 45 | trace |
| 46 | ], |
| 47 | stdout = subprocess.PIPE, |
| 48 | universal_newlines = True, |
| 49 | ) |
| 50 | |
| 51 | sed = subprocess.Popen( |
| 52 | args = [ |
| 53 | 'sed', |
| 54 | '-e', r's/\r$//g', |
| 55 | '-e', r's/^[0-9]\+ //', |
| 56 | '-e', r's/hdc = \w\+/hdc/g', |
| 57 | ], |
| 58 | stdin = dump.stdout, |
José Fonseca | ae789c4 | 2012-02-20 22:18:17 +0000 | [diff] [blame] | 59 | stdout = open(fifo, 'wt'), |
José Fonseca | a65795f | 2012-02-18 18:15:18 +0000 | [diff] [blame] | 60 | universal_newlines = True, |
| 61 | ) |
| 62 | |
| 63 | |
| 64 | def diff(traces): |
| 65 | fifodir = tempfile.mkdtemp() |
| 66 | try: |
| 67 | fifos = [] |
| 68 | for i in range(len(traces)): |
| 69 | trace = traces[i] |
| 70 | fifo = os.path.join(fifodir, str(i)) |
| 71 | stripdump(trace, fifo) |
| 72 | fifos.append(fifo) |
| 73 | |
| 74 | # TODO use difflib instead |
| 75 | sdiff = subprocess.Popen( |
| 76 | args = [ |
| 77 | 'sdiff', |
| 78 | '--width=%u' % options.width, |
| 79 | '--speed-large-files', |
| 80 | ] + fifos, |
José Fonseca | ae789c4 | 2012-02-20 22:18:17 +0000 | [diff] [blame] | 81 | stdout = subprocess.PIPE, |
José Fonseca | a65795f | 2012-02-18 18:15:18 +0000 | [diff] [blame] | 82 | universal_newlines = True, |
| 83 | ) |
| 84 | |
| 85 | less = subprocess.Popen( |
| 86 | args = ['less', '-FRXn'], |
| 87 | stdin = sdiff.stdout |
| 88 | ) |
| 89 | |
| 90 | less.wait() |
| 91 | |
| 92 | finally: |
| 93 | shutil.rmtree(fifodir) |
| 94 | |
| 95 | |
| 96 | def columns(): |
| 97 | import curses |
| 98 | curses.setupterm() |
| 99 | return curses.tigetnum('cols') |
| 100 | |
| 101 | |
| 102 | def main(): |
| 103 | '''Main program. |
| 104 | ''' |
| 105 | |
| 106 | default_width = columns() |
| 107 | |
| 108 | # Parse command line options |
| 109 | optparser = optparse.OptionParser( |
| 110 | usage='\n\t%prog [options] -- TRACE_FILE TRACE_FILE', |
| 111 | version='%%prog') |
| 112 | optparser.add_option( |
| 113 | '-a', '--apitrace', metavar='PROGRAM', |
| 114 | type='string', dest='apitrace', default='apitrace', |
| 115 | help='apitrace command [default: %default]') |
| 116 | optparser.add_option( |
| 117 | '-c', '--calls', metavar='CALLSET', |
| 118 | type="string", dest="calls", default='1-10000', |
| 119 | help="calls to compare [default: %default]") |
| 120 | optparser.add_option( |
| 121 | '-w', '--width', metavar='NUM', |
| 122 | type="string", dest="width", default=default_width, |
| 123 | help="columns [default: %default]") |
| 124 | |
| 125 | global options |
| 126 | (options, args) = optparser.parse_args(sys.argv[1:]) |
| 127 | if len(args) != 2: |
| 128 | optparser.error("incorrect number of arguments") |
| 129 | |
| 130 | diff(args) |
| 131 | |
| 132 | |
| 133 | if __name__ == '__main__': |
| 134 | main() |