]> git.cworth.org Git - apitrace/blob - scripts/unpickle.py
efb860804389ad7e5ccfae0a89171673c7bc1798
[apitrace] / scripts / unpickle.py
1 #!/usr/bin/env python
2 ##########################################################################
3 #
4 # Copyright 2012 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 '''Sample program for apitrace pickle command.
28
29 Run as:
30
31    apitrace pickle foo.trace | python unpickle.py
32
33 '''
34
35
36 import optparse
37 import cPickle as pickle
38 import sys
39 import time
40
41
42 def main():
43     optparser = optparse.OptionParser(
44         usage="\n\tapitrace pickle trace. %prog [options]")
45     optparser.add_option(
46         '--quiet',
47         action="store_true", dest="quiet", default=False,
48         help="don't dump calls to stdout")
49
50     (options, args) = optparser.parse_args(sys.argv[1:])
51
52     if args:
53         optparser.error('unexpected arguments')
54
55     calls = 0
56     startTime = time.time()
57     while True:
58         try:
59             call = pickle.load(sys.stdin)
60         except EOFError:
61             break
62         else:
63             callNo, functionName, args, ret = call
64             if not options.quiet:
65                 sys.stdout.write('%u ' % callNo)
66                 sys.stdout.write(functionName)
67                 sys.stdout.write('(' + ', '.join(map(repr, args)) + ')')
68                 if ret is not None:
69                     sys.stdout.write(' = ')
70                     sys.stdout.write(repr(ret))
71                 sys.stdout.write('\n')
72             calls += 1
73     stopTime = time.time()
74     duration = stopTime - startTime
75     sys.stderr.write('%u calls, %.03f secs, %u calls/sec\n' % (calls, duration, calls/duration))
76
77
78 if __name__ == '__main__':
79     main()