python-pathvalidate: bump version to 0.14.1
[buildroot-gz.git] / support / scripts / size-stats-compare
blobe5a1ec3f94096967f4a38e2f96d2b7e0e61de94f
1 #!/usr/bin/env python
3 # Copyright (C) 2016 Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # TODO (improvements)
20 # - support K,M,G size suffixes for threshold
21 # - output CSV file in addition to stdout reporting
23 import csv
24 import argparse
25 import sys
27 def read_file_size_csv(inputf, detail=None):
28 """Extract package or file sizes from CSV file into size dictionary"""
29 sizes = {}
30 reader = csv.reader(inputf)
32 header = next(reader)
33 if (header[0] != 'File name' or header[1] != 'Package name' or
34 header[2] != 'File size' or header[3] != 'Package size'):
35 print(("Input file %s does not contain the expected header. Are you "
36 "sure this file corresponds to the file-size-stats.csv "
37 "file created by 'make graph-size'?") % inputf.name)
38 sys.exit(1)
40 for row in reader:
41 if detail:
42 sizes[row[0]] = int(row[2])
43 else:
44 sizes[row[1]] = int(row[3])
46 return sizes
48 def compare_sizes(old, new):
49 """Return delta/added/removed dictionaries based on two input size
50 dictionaries"""
51 delta = {}
52 oldkeys = set(old.keys())
53 newkeys = set(new.keys())
55 # packages/files in both
56 for entry in newkeys.intersection(oldkeys):
57 delta[entry] = ('', new[entry] - old[entry])
58 # packages/files only in new
59 for entry in newkeys.difference(oldkeys):
60 delta[entry] = ('added', new[entry])
61 # packages/files only in old
62 for entry in oldkeys.difference(newkeys):
63 delta[entry] = ('removed', -old[entry])
65 return delta
67 def print_results(result, threshold):
68 """Print the given result dictionary sorted by size, ignoring any entries
69 below or equal to threshold"""
71 from six import iteritems
72 list_result = list(iteritems(result))
73 # result is a dictionary: name -> (flag, size difference)
74 # list_result is a list of tuples: (name, (flag, size difference))
76 for entry in sorted(list_result, key=lambda entry: entry[1][1]):
77 if threshold is not None and abs(entry[1][1]) <= threshold:
78 continue
79 print('%12s %7s %s' % (entry[1][1], entry[1][0], entry[0]))
82 # main #########################################################################
84 description = """
85 Compare rootfs size between Buildroot compilations, for example after changing
86 configuration options or after switching to another Buildroot release.
88 This script compares the file-size-stats.csv file generated by 'make graph-size'
89 with the corresponding file from another Buildroot compilation.
90 The size differences can be reported per package or per file.
91 Size differences smaller or equal than a given threshold can be ignored.
92 """
94 parser = argparse.ArgumentParser(description=description,
95 formatter_class=argparse.RawDescriptionHelpFormatter)
97 parser.add_argument('-d', '--detail', action='store_true',
98 help='''report differences for individual files rather than
99 packages''')
100 parser.add_argument('-t', '--threshold', type=int,
101 help='''ignore size differences smaller or equal than this
102 value (bytes)''')
103 parser.add_argument('old_file_size_csv', type=argparse.FileType('r'),
104 metavar='old-file-size-stats.csv',
105 help="""old CSV file with file and package size statistics,
106 generated by 'make graph-size'""")
107 parser.add_argument('new_file_size_csv', type=argparse.FileType('r'),
108 metavar='new-file-size-stats.csv',
109 help='new CSV file with file and package size statistics')
110 args = parser.parse_args()
112 if args.detail:
113 keyword = 'file'
114 else:
115 keyword = 'package'
117 old_sizes = read_file_size_csv(args.old_file_size_csv, args.detail)
118 new_sizes = read_file_size_csv(args.new_file_size_csv, args.detail)
120 delta = compare_sizes(old_sizes, new_sizes)
122 print('Size difference per %s (bytes), threshold = %s' % (keyword, args.threshold))
123 print(80*'-')
124 print_results(delta, args.threshold)
125 print(80*'-')
126 print_results({'TOTAL': ('', sum(new_sizes.values()) - sum(old_sizes.values()))},
127 threshold=None)