blob: a3c274c8f593e34750be07905031bc6628fba7f9 [file] [log] [blame]
José Fonseca299a1b32012-01-26 20:32:59 +00001#!/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
29Run as:
30
31 apitrace pickle foo.trace | python unpickle.py
32
33'''
34
35
José Fonseca447576d2012-01-27 14:27:13 +000036import optparse
José Fonseca299a1b32012-01-26 20:32:59 +000037import cPickle as pickle
38import sys
39import time
40
41
42def main():
José Fonseca447576d2012-01-27 14:27:13 +000043 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
José Fonsecac6977a72012-01-27 14:28:06 +000055 # Change stdin to binary mode
56 try:
57 import msvcrt
58 except ImportError:
59 pass
60 else:
61 import os
62 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
63
José Fonseca299a1b32012-01-26 20:32:59 +000064 calls = 0
65 startTime = time.time()
66 while True:
67 try:
68 call = pickle.load(sys.stdin)
69 except EOFError:
70 break
71 else:
72 callNo, functionName, args, ret = call
José Fonseca447576d2012-01-27 14:27:13 +000073 if not options.quiet:
74 sys.stdout.write('%u ' % callNo)
75 sys.stdout.write(functionName)
76 sys.stdout.write('(' + ', '.join(map(repr, args)) + ')')
77 if ret is not None:
78 sys.stdout.write(' = ')
79 sys.stdout.write(repr(ret))
80 sys.stdout.write('\n')
José Fonseca299a1b32012-01-26 20:32:59 +000081 calls += 1
82 stopTime = time.time()
83 duration = stopTime - startTime
José Fonseca447576d2012-01-27 14:27:13 +000084 sys.stderr.write('%u calls, %.03f secs, %u calls/sec\n' % (calls, duration, calls/duration))
José Fonseca299a1b32012-01-26 20:32:59 +000085
86
87if __name__ == '__main__':
88 main()