1#!/usr/bin/python
2
3# Copyright (C) 2013 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""summarize and compare the component sizes in installed-files.txt."""
18
19import sys
20
21bin_size1 = {}
22bin_size2 = {}
23bin_sizes = [bin_size1, bin_size2]
24
25file_sizes = {}
26
27def PrintUsage():
28  print "usage: " + sys.argv[0] + " filename [filename2]"
29  print ""
30  print "  Input file is installed-files.txt from the build output directory."
31  print "  When only one input file is given, it will generate module_0.csv."
32  print "  When two input files are given, in addition it will generate"
33  print "  module_1.csv and comparison.csv."
34  print ""
35  print "  The module_x.csv file shows the aggregated file size in each module"
36  print "  (eg bin, lib, app, ...)"
37  print "  The comparison.cvs file shows the individual file sizes side by side"
38  print "  from two different builds"
39  print ""
40  print "  These files can be uploaded to Google Doc for further processing."
41  sys.exit(1)
42
43def ParseFile(install_file, idx):
44  input_stream = open(install_file, 'r')
45  for line in input_stream:
46    # line = "25027208  /system/lib/libchromeview.so"
47    line = line.strip()
48
49    # size = "25027208", name = "/system/lib/libchromeview.so"
50    size, name = line.split()
51
52    # components = ["", "system", "lib", "libchromeview.so"]
53    components = name.split('/')
54
55    # module = "lib"
56    module = components[2]
57
58    # filename = libchromeview.so"
59    filename = components[-1]
60
61    # sum up the file sizes by module name
62    if module not in bin_sizes[idx]:
63      bin_sizes[idx][module] = int(size)
64    else:
65      bin_sizes[idx][module] += int(size)
66
67    # sometimes a file only exists on one build but not the other - use 0 as the
68    # default size.
69    if idx == 0:
70      file_sizes[name] = [module, size, 0]
71    else:
72      if name in file_sizes:
73        file_sizes[name][-1] = size
74      else:
75        file_sizes[name] = [module, 0, size]
76
77  input_stream.close()
78
79  # output the module csv file
80  output = open("module_%d.csv" % idx, 'w')
81  total = 0
82  for key in bin_sizes[idx]:
83    output.write("%s, %d\n" % (key, bin_sizes[idx][key]))
84  output.close()
85
86def main():
87  if len(sys.argv) < 2 or len(sys.argv) > 3:
88    PrintUsage()
89  # Parse the first installed-files.txt
90  ParseFile(sys.argv[1], 0)
91
92  # Parse the second installed-files.txt
93  if len(sys.argv) == 3:
94    ParseFile(sys.argv[2], 1)
95    # comparison.csv has the following columns:
96    # filename, module, size1, size2, size2-size1
97    # eg: /system/lib/libchromeview.so, lib, 25027208, 33278460, 8251252
98    output = open("comparison.csv", 'w')
99    for key in file_sizes:
100      output.write("%s, %s, %s, %s, %d\n" %
101                   (key, file_sizes[key][0], file_sizes[key][1],
102                    file_sizes[key][2],
103                    int(file_sizes[key][2]) - int(file_sizes[key][1])))
104    output.close()
105
106if __name__ == '__main__':
107  main()
108
109# vi: ts=2 sw=2
110