blob: f961612c926ef6d5ae16b058dd9df06caaa6da86 [file] [log] [blame]
Will Bradley333d22c2019-09-11 15:04:59 -06001# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""The Package Size Reporting CLI entry point."""
6
Will Bradley333d22c2019-09-11 15:04:59 -06007from chromite.lib import commandline
Alex Kleinaef41942022-04-19 14:13:17 -06008from chromite.lib import metrics_lib
Will Bradley333d22c2019-09-11 15:04:59 -06009from chromite.lib import portage_util
Lizzy Presland1754b602022-06-01 23:47:35 +000010from chromite.utils import pformat
11
12
13_PARTITION_NAMES = ['rootfs', 'stateful']
Will Bradley333d22c2019-09-11 15:04:59 -060014
15
Will Bradley333d22c2019-09-11 15:04:59 -060016def _get_parser():
17 """Create an argument parser for this script."""
18 parser = commandline.ArgumentParser(description=__doc__)
Alex Klein8718c032022-04-19 14:06:03 -060019 parser.add_argument(
20 '--root',
21 required=True,
22 type='path',
23 help='Specify the rootfs to investigate.')
24 parser.add_argument(
25 '--image-type',
26 help='Specify the type of image being investigated. '
27 'e.g. [base, dev, test]')
28 parser.add_argument(
29 '--partition-name',
Lizzy Presland1754b602022-06-01 23:47:35 +000030 choices=_PARTITION_NAMES,
Alex Klein8718c032022-04-19 14:06:03 -060031 help='Specify the partition name. '
32 'e.g. [rootfs, stateful]')
33 parser.add_argument(
34 'packages',
35 nargs='*',
36 help='Names of packages to investigate. Must be '
37 'specified as category/package-version.')
Will Bradley333d22c2019-09-11 15:04:59 -060038 return parser
39
40
41def generate_package_size_report(db, root, image_type, partition_name,
42 installed_packages):
43 """Collect package sizes and generate a report."""
44 results = {}
45 total_size = 0
46 package_sizes = portage_util.GeneratePackageSizes(db, root,
47 installed_packages)
Alex Kleinaef41942022-04-19 14:13:17 -060048 timestamp = metrics_lib.current_milli_time()
Will Bradley333d22c2019-09-11 15:04:59 -060049 for package_cpv, size in package_sizes:
50 results[package_cpv] = size
Alex Kleinaef41942022-04-19 14:13:17 -060051 metrics_lib.append_metrics_log(
Alex Klein8718c032022-04-19 14:06:03 -060052 timestamp,
53 'package_size.%s.%s.%s' % (image_type, partition_name, package_cpv),
Alex Kleinaef41942022-04-19 14:13:17 -060054 metrics_lib.OP_GAUGE,
Alex Klein8718c032022-04-19 14:06:03 -060055 arg=size)
Will Bradley333d22c2019-09-11 15:04:59 -060056 total_size += size
57
Alex Kleinaef41942022-04-19 14:13:17 -060058 metrics_lib.append_metrics_log(
Alex Klein8718c032022-04-19 14:06:03 -060059 timestamp,
60 'total_size.%s.%s' % (image_type, partition_name),
Alex Kleinaef41942022-04-19 14:13:17 -060061 metrics_lib.OP_GAUGE,
Alex Klein8718c032022-04-19 14:06:03 -060062 arg=total_size)
Lizzy Presland1754b602022-06-01 23:47:35 +000063 return {'root': root,
64 'package_sizes': results,
65 'total_size': total_size,
66 'package_database_path': db.db_path,
67 'package_install_path': db.package_install_path}
Will Bradley333d22c2019-09-11 15:04:59 -060068
69
70def main(argv):
71 """Find and report approximate size info for a particular built package."""
72 commandline.RunInsideChroot()
73
74 parser = _get_parser()
75 opts = parser.parse_args(argv)
76 opts.Freeze()
77
Lizzy Presland1754b602022-06-01 23:47:35 +000078 vdb = package_install_path = ''
79 if opts.partition_name == 'stateful':
80 vdb = 'var_overlay/db/pkg'
81 package_install_path = 'dev_image'
82 db = portage_util.PortageDB(root=opts.root,
83 vdb=vdb,
84 package_install_path=package_install_path)
Will Bradley333d22c2019-09-11 15:04:59 -060085
86 if opts.packages:
Alex Klein8718c032022-04-19 14:06:03 -060087 installed_packages = portage_util.GenerateInstalledPackages(
88 db, opts.root, opts.packages)
Will Bradley333d22c2019-09-11 15:04:59 -060089 else:
90 installed_packages = db.InstalledPackages()
91
92 results = generate_package_size_report(db, opts.root, opts.image_type,
93 opts.partition_name,
94 installed_packages)
Lizzy Presland1754b602022-06-01 23:47:35 +000095 print(pformat.json(results))