1#!/usr/bin/env python3 2 3# Copyright (C) 2016 Thomas De Schampheleire <thomas.de.schampheleire@gmail.com> 4 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. 9# 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. 14# 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 18 19# TODO (improvements) 20# - support K,M,G size suffixes for threshold 21# - output CSV file in addition to stdout reporting 22 23import csv 24import argparse 25import sys 26 27 28def read_file_size_csv(inputf, detail=None): 29 """Extract package or file sizes from CSV file into size dictionary""" 30 sizes = {} 31 reader = csv.reader(inputf) 32 33 header = next(reader) 34 if header[0] != 'File name' or header[1] != 'Package name' or \ 35 header[2] != 'File size' or header[3] != 'Package size': 36 print(("Input file %s does not contain the expected header. Are you " 37 "sure this file corresponds to the file-size-stats.csv " 38 "file created by 'make graph-size'?") % inputf.name) 39 sys.exit(1) 40 41 for row in reader: 42 if detail: 43 sizes[(row[0], row[1])] = int(row[2]) 44 else: 45 sizes[(None, row[1])] = int(row[3]) 46 47 return sizes 48 49 50def compare_sizes(old, new): 51 """Return delta/added/removed dictionaries based on two input size 52 dictionaries""" 53 delta = {} 54 oldkeys = set(old.keys()) 55 newkeys = set(new.keys()) 56 57 # packages/files in both 58 for entry in newkeys.intersection(oldkeys): 59 delta[entry] = ('', new[entry] - old[entry]) 60 # packages/files only in new 61 for entry in newkeys.difference(oldkeys): 62 delta[entry] = ('added', new[entry]) 63 # packages/files only in old 64 for entry in oldkeys.difference(newkeys): 65 delta[entry] = ('removed', -old[entry]) 66 67 return delta 68 69 70def print_results(result, threshold): 71 """Print the given result dictionary sorted by size, ignoring any entries 72 below or equal to threshold""" 73 74 from six import iteritems 75 list_result = list(iteritems(result)) 76 # result is a dictionary: (filename, pkgname) -> (flag, size difference) 77 # list_result is a list of tuples: ((filename, pkgname), (flag, size difference)) 78 # filename may be None if no detail is requested. 79 80 maxpkgname = max(len(pkgname) for filename, pkgname in result) 81 82 for entry in sorted(list_result, key=lambda entry: entry[1][1]): 83 data = dict( 84 filename=entry[0][0], 85 pkgname=entry[0][1], 86 action=entry[1][0], 87 size=entry[1][1], 88 maxpkgname=maxpkgname, 89 ) 90 91 if threshold is not None and abs(data['size']) <= threshold: 92 continue 93 if data['filename']: 94 print('{size:12d} {action:7s} {pkgname:{maxpkgname}s} {filename}'.format(**data)) 95 else: 96 print('{size:12d} {action:7s} {pkgname}'.format(**data)) 97 98 99# main ######################################################################### 100 101description = """ 102Compare rootfs size between Buildroot compilations, for example after changing 103configuration options or after switching to another Buildroot release. 104 105This script compares the file-size-stats.csv file generated by 'make graph-size' 106with the corresponding file from another Buildroot compilation. 107The size differences can be reported per package or per file. 108Size differences smaller or equal than a given threshold can be ignored. 109""" 110 111parser = argparse.ArgumentParser(description=description, 112 formatter_class=argparse.RawDescriptionHelpFormatter) 113 114parser.add_argument('-d', '--detail', action='store_true', 115 help='''report differences for individual files rather than 116 packages''') 117parser.add_argument('-t', '--threshold', type=int, 118 help='''ignore size differences smaller or equal than this 119 value (bytes)''') 120parser.add_argument('old_file_size_csv', type=argparse.FileType('r'), 121 metavar='old-file-size-stats.csv', 122 help="""old CSV file with file and package size statistics, 123 generated by 'make graph-size'""") 124parser.add_argument('new_file_size_csv', type=argparse.FileType('r'), 125 metavar='new-file-size-stats.csv', 126 help='new CSV file with file and package size statistics') 127args = parser.parse_args() 128 129if args.detail: 130 keyword = 'file' 131else: 132 keyword = 'package' 133 134old_sizes = read_file_size_csv(args.old_file_size_csv, args.detail) 135new_sizes = read_file_size_csv(args.new_file_size_csv, args.detail) 136 137delta = compare_sizes(old_sizes, new_sizes) 138 139print('Size difference per %s (bytes), threshold = %s' % (keyword, args.threshold)) 140print(80*'-') 141print_results(delta, args.threshold) 142print(80*'-') 143print_results({(None, 'TOTAL'): ('', sum(new_sizes.values()) - sum(old_sizes.values()))}, 144 threshold=None) 145