blob: c873fb844dd0e2370eec26d21bd154eb5b070ef7 [file] [log] [blame]
ThiƩbaud Weksteen713db482021-02-10 14:03:27 +01001# Copyright 2021 Google Inc. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Bloaty CSV Merger
15
16Merges a list of .csv files from Bloaty into a protobuf. It takes the list as
17a first argument and the output as second. For instance:
18
19 $ bloaty_merger binary_sizes.lst binary_sizes.pb
20
21"""
22
23import argparse
24import csv
25
26import ninja_rsp
27
28import file_sections_pb2
29
30BLOATY_EXTENSION = ".bloaty.csv"
31
32def parse_csv(path):
33 """Parses a Bloaty-generated CSV file into a protobuf.
34
35 Args:
36 path: The filepath to the CSV file, relative to $ANDROID_TOP.
37
38 Returns:
39 A file_sections_pb2.File if the file was found; None otherwise.
40 """
41 file_proto = None
42 with open(path, newline='') as csv_file:
43 file_proto = file_sections_pb2.File()
44 if path.endswith(BLOATY_EXTENSION):
45 file_proto.path = path[:-len(BLOATY_EXTENSION)]
46 section_reader = csv.DictReader(csv_file)
47 for row in section_reader:
48 section = file_proto.sections.add()
49 section.name = row["sections"]
50 section.vm_size = int(row["vmsize"])
51 section.file_size = int(row["filesize"])
52 return file_proto
53
54def create_file_size_metrics(input_list, output_proto):
55 """Creates a FileSizeMetrics proto from a list of CSV files.
56
57 Args:
58 input_list: The path to the file which contains the list of CSV files. Each
59 filepath is separated by a space.
60 output_proto: The path for the output protobuf.
61 """
62 metrics = file_sections_pb2.FileSizeMetrics()
63 reader = ninja_rsp.NinjaRspFileReader(input_list)
64 for csv_path in reader:
65 file_proto = parse_csv(csv_path)
66 if file_proto:
67 metrics.files.append(file_proto)
68 with open(output_proto, "wb") as output:
69 output.write(metrics.SerializeToString())
70
71def main():
72 parser = argparse.ArgumentParser()
73 parser.add_argument("input_list_file", help="List of bloaty csv files.")
74 parser.add_argument("output_proto", help="Output proto.")
75 args = parser.parse_args()
76 create_file_size_metrics(args.input_list_file, args.output_proto)
77
78if __name__ == '__main__':
79 main()