blob: 6aab2b19f8f657b3c5a3f2f22a72f6ba383f5663 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Will Bradley333d22c2019-09-11 15:04:59 -06002# 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
Alex Klein1699fab2022-09-08 08:46:06 -060013_PARTITION_NAMES = ["rootfs", "stateful"]
Will Bradley333d22c2019-09-11 15:04:59 -060014
15
Will Bradley333d22c2019-09-11 15:04:59 -060016def _get_parser():
Alex Klein1699fab2022-09-08 08:46:06 -060017 """Create an argument parser for this script."""
18 parser = commandline.ArgumentParser(description=__doc__)
19 parser.add_argument(
20 "--root",
21 required=True,
22 type="path",
23 help="Specify the rootfs to investigate.",
24 )
25 parser.add_argument(
26 "--image-type",
27 help="Specify the type of image being investigated. "
28 "e.g. [base, dev, test]",
29 )
30 parser.add_argument(
31 "--partition-name",
32 choices=_PARTITION_NAMES,
33 help="Specify the partition name. " "e.g. [rootfs, stateful]",
34 )
35 parser.add_argument(
36 "packages",
37 nargs="*",
38 help="Names of packages to investigate. Must be "
39 "specified as category/package-version.",
40 )
41 return parser
Will Bradley333d22c2019-09-11 15:04:59 -060042
43
Alex Klein1699fab2022-09-08 08:46:06 -060044def generate_package_size_report(
45 db, root, image_type, partition_name, installed_packages
46):
47 """Collect package sizes and generate a report."""
48 results = {}
49 total_size = 0
50 package_sizes = portage_util.GeneratePackageSizes(
51 db, root, installed_packages
52 )
53 timestamp = metrics_lib.current_milli_time()
54 for package_cpv, size in package_sizes:
55 results[package_cpv] = size
56 metrics_lib.append_metrics_log(
57 timestamp,
58 "package_size.%s.%s.%s" % (image_type, partition_name, package_cpv),
59 metrics_lib.OP_GAUGE,
60 arg=size,
61 )
62 total_size += size
63
Alex Kleinaef41942022-04-19 14:13:17 -060064 metrics_lib.append_metrics_log(
Alex Klein8718c032022-04-19 14:06:03 -060065 timestamp,
Alex Klein1699fab2022-09-08 08:46:06 -060066 "total_size.%s.%s" % (image_type, partition_name),
Alex Kleinaef41942022-04-19 14:13:17 -060067 metrics_lib.OP_GAUGE,
Alex Klein1699fab2022-09-08 08:46:06 -060068 arg=total_size,
69 )
70 return {
71 "root": root,
72 "package_sizes": results,
73 "total_size": total_size,
74 "package_database_path": db.db_path,
75 "package_install_path": db.package_install_path,
76 }
Will Bradley333d22c2019-09-11 15:04:59 -060077
78
79def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060080 """Find and report approximate size info for a particular built package."""
81 commandline.RunInsideChroot()
Will Bradley333d22c2019-09-11 15:04:59 -060082
Alex Klein1699fab2022-09-08 08:46:06 -060083 parser = _get_parser()
84 opts = parser.parse_args(argv)
85 opts.Freeze()
Will Bradley333d22c2019-09-11 15:04:59 -060086
Alex Klein1699fab2022-09-08 08:46:06 -060087 vdb = package_install_path = ""
88 if opts.partition_name == "stateful":
89 vdb = "var_overlay/db/pkg"
90 package_install_path = "dev_image"
91 db = portage_util.PortageDB(
92 root=opts.root, vdb=vdb, package_install_path=package_install_path
93 )
Will Bradley333d22c2019-09-11 15:04:59 -060094
Alex Klein1699fab2022-09-08 08:46:06 -060095 if opts.packages:
96 installed_packages = portage_util.GenerateInstalledPackages(
97 db, opts.root, opts.packages
98 )
99 else:
100 installed_packages = db.InstalledPackages()
Will Bradley333d22c2019-09-11 15:04:59 -0600101
Alex Klein1699fab2022-09-08 08:46:06 -0600102 results = generate_package_size_report(
103 db, opts.root, opts.image_type, opts.partition_name, installed_packages
104 )
105 print(pformat.json(results))