]> git.cworth.org Git - apitrace/blob - scripts/tracediff.py
Beginning of a Python reimplementation of tracediff script.
[apitrace] / scripts / tracediff.py
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 difflib
29 import optparse
30 import os.path
31 import re
32 import subprocess
33 import sys
34
35
36 call_re = re.compile('^([0-9]+) (\w+)\(')
37
38 ansi_re = re.compile('\x1b\[[0-9]{1,2}(;[0-9]{1,2}){0,2}m')
39
40
41 def ansi_strip(s):
42     # http://www.theeggeadventure.com/wikimedia/index.php/Linux_Tips#Use_sed_to_remove_ANSI_colors
43     return ansi_re.sub('', s)
44
45
46 ignored_function_names = set([
47     'glGetString',
48     'glXGetClientString',
49     'glXGetCurrentDisplay',
50     'glXGetProcAddress',
51     'glXGetProcAddressARB',
52     'wglGetProcAddress',
53 ])
54
55
56 def readtrace(trace):
57     p = subprocess.Popen([options.tracedump, trace], stdout=subprocess.PIPE)
58     lines = []
59     for line in p.stdout.readlines():
60         line = ansi_strip(line)
61         mo = call_re.match(line)
62         if mo:
63             function_name = mo.group(2)
64             if function_name in ignored_function_names:
65                 continue
66             lines.append(line[mo.start(2):])
67         else:
68             lines[-1] += line
69     p.wait()
70     return lines
71
72
73 def main():
74     global options
75
76     optparser = optparse.OptionParser(
77         usage='\n\t%prog <trace> <trace>',
78         version='%%prog')
79     optparser.add_option(
80         '-d', '--tracedump', metavar='PROGRAM',
81         type='string', dest='tracedump', default='tracedump',
82         help='tracedump command [default: %default]')
83
84     (options, args) = optparser.parse_args(sys.argv[1:])
85     if len(args) != 2:
86         optparser.error("incorrect number of arguments")
87
88     ref_lines = readtrace(args[0])
89     src_lines = readtrace(args[1])
90
91     diff = difflib.unified_diff(ref_lines, src_lines, n=3)
92     sys.stdout.writelines(diff)
93
94
95 if __name__ == '__main__':
96     main()