Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / third_party / npapi / npspy / analyze_streams.py
1 # A script for analyzing the output of NPSPY and merging data about streams.\r
2 \r
3 import sys\r
4 \r
5 \r
6 def ReadFile(filename, flags='rb'):\r
7   """Returns the contents of a file."""\r
8   file = open(filename, flags)\r
9   result = file.read()\r
10   file.close()\r
11   return result\r
12 \r
13 \r
14 def WriteFile(filename, contents):\r
15   """Overwrites the file with the given contents."""\r
16   file = open(filename, 'w')\r
17   file.write(contents)\r
18   file.close()  \r
19   \r
20 \r
21 # sample line: 'NPP_NewStream(0x645c898, 0x56ba900("application/x-shockwave-flash"), 0x64bb3b0 (http://weeklyad.target.com/target/flash/target/target.swf?ver=090326), TRUE, NP_NORMAL)'\r
22 class Stream:\r
23   def __init__(self, line):\r
24     split = line.split(', ')\r
25     \r
26     self.mime_type = split[1].split('"')[1]\r
27     self.url = split[2].split(' ')[1].strip('()')\r
28     self.seekable = split[3]\r
29     self.type = split[4].strip(')')\r
30     self.size = 0\r
31     self.status = ''\r
32     try:\r
33       self.address = split[2].split(' ')[0]\r
34     except:\r
35       print 'parsing error on ' + line\r
36       self.address = ''\r
37 \r
38     if self.type != 'NP_NORMAL':\r
39       print 'line got unexpected type: ' + line\r
40 \r
41     \r
42 def main(argv=None):\r
43   if argv is None:\r
44     argv = sys.argv\r
45     \r
46   streams = []\r
47 \r
48   if len(argv) != 2:\r
49     print 'need filename'\r
50     return\r
51   file = ReadFile(argv[1])\r
52   for line in file.splitlines():\r
53     if line.startswith('NPP_NewStream('):\r
54       if line.count('(') < 3:\r
55         print 'unknown format for line: ' + line\r
56         continue\r
57 \r
58       s = Stream(line)\r
59       streams.append(s)\r
60     elif line.startswith('NPP_Write('):\r
61       # sample: NPP_Write(0x645c898, 0x64bb3b0, 0, 16384, 0x56c1000("CW")))\r
62       split = line.split(', ')\r
63       address = split[1]\r
64       start = int(split[2])\r
65       size = int(split[3])\r
66       found = False\r
67       for stream in streams:\r
68         if stream.address == address:\r
69           if stream.size != start:\r
70             print 'error: starting at wrong place for write ' + stream.url + ' ' + str(stream.size) + ' ' + str(start)\r
71           stream.size += size\r
72           found = True\r
73           break\r
74           \r
75       if not found:\r
76         print "couldn't find stream to match NPP_Write " + line\r
77     elif line.startswith('NPP_DestroyStream('):\r
78       # sample: NPP_DestroyStream(0x645c898, 0x64bb3b0, NPRES_DONE)\r
79       split = line.split(', ')\r
80       address = split[1]\r
81       status = split[2].strip(')')\r
82       found = False\r
83       for stream in streams:\r
84         if stream.address == address:\r
85           stream.status = status\r
86           stream.address = ''  # address can be reused\r
87           found = True\r
88           break\r
89           \r
90       if not found:\r
91         print "couldn't find stream to match NPP_DestroyStream " + line\r
92 \r
93 \r
94   output = []\r
95   for stream in streams:\r
96     if stream.status != 'NPRES_DONE':\r
97       print 'error: no NPP_DestroyStream with success for ' + stream.url + ' ' + stream.status + '.'\r
98     output.append(', '.join([stream.url, stream.mime_type, str(stream.size), stream.seekable]))\r
99   output_file = argv[1].replace('.', '_analyzed.')\r
100   \r
101   WriteFile(output_file, '\n'.join(output))\r
102   \r
103   \r
104 if __name__ == "__main__":\r
105   sys.exit(main())\r