bloaty_merger.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # 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. Merges a list of .csv files from Bloaty into a protobuf. It takes the list as
  16. a first argument and the output as second. For instance:
  17. $ bloaty_merger binary_sizes.lst binary_sizes.pb
  18. """
  19. import argparse
  20. import csv
  21. import ninja_rsp
  22. import file_sections_pb2
  23. BLOATY_EXTENSION = ".bloaty.csv"
  24. def parse_csv(path):
  25. """Parses a Bloaty-generated CSV file into a protobuf.
  26. Args:
  27. path: The filepath to the CSV file, relative to $ANDROID_TOP.
  28. Returns:
  29. A file_sections_pb2.File if the file was found; None otherwise.
  30. """
  31. file_proto = None
  32. with open(path, newline='') as csv_file:
  33. file_proto = file_sections_pb2.File()
  34. if path.endswith(BLOATY_EXTENSION):
  35. file_proto.path = path[:-len(BLOATY_EXTENSION)]
  36. section_reader = csv.DictReader(csv_file)
  37. for row in section_reader:
  38. section = file_proto.sections.add()
  39. section.name = row["sections"]
  40. section.vm_size = int(row["vmsize"])
  41. section.file_size = int(row["filesize"])
  42. return file_proto
  43. def create_file_size_metrics(input_list, output_proto):
  44. """Creates a FileSizeMetrics proto from a list of CSV files.
  45. Args:
  46. input_list: The path to the file which contains the list of CSV files. Each
  47. filepath is separated by a space.
  48. output_proto: The path for the output protobuf.
  49. """
  50. metrics = file_sections_pb2.FileSizeMetrics()
  51. reader = ninja_rsp.NinjaRspFileReader(input_list)
  52. for csv_path in reader:
  53. file_proto = parse_csv(csv_path)
  54. if file_proto:
  55. metrics.files.append(file_proto)
  56. with open(output_proto, "wb") as output:
  57. output.write(metrics.SerializeToString())
  58. def main():
  59. parser = argparse.ArgumentParser()
  60. parser.add_argument("input_list_file", help="List of bloaty csv files.")
  61. parser.add_argument("output_proto", help="Output proto.")
  62. args = parser.parse_args()
  63. create_file_size_metrics(args.input_list_file, args.output_proto)
  64. if __name__ == '__main__':
  65. main()