Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 1 | # 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 Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 7 | from chromite.lib import commandline |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame] | 8 | from chromite.lib import metrics_lib |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 9 | from chromite.lib import portage_util |
Lizzy Presland | 1754b60 | 2022-06-01 23:47:35 +0000 | [diff] [blame^] | 10 | from chromite.utils import pformat |
| 11 | |
| 12 | |
| 13 | _PARTITION_NAMES = ['rootfs', 'stateful'] |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 14 | |
| 15 | |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 16 | def _get_parser(): |
| 17 | """Create an argument parser for this script.""" |
| 18 | parser = commandline.ArgumentParser(description=__doc__) |
Alex Klein | 8718c03 | 2022-04-19 14:06:03 -0600 | [diff] [blame] | 19 | 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 Presland | 1754b60 | 2022-06-01 23:47:35 +0000 | [diff] [blame^] | 30 | choices=_PARTITION_NAMES, |
Alex Klein | 8718c03 | 2022-04-19 14:06:03 -0600 | [diff] [blame] | 31 | 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 Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 38 | return parser |
| 39 | |
| 40 | |
| 41 | def 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 Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame] | 48 | timestamp = metrics_lib.current_milli_time() |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 49 | for package_cpv, size in package_sizes: |
| 50 | results[package_cpv] = size |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame] | 51 | metrics_lib.append_metrics_log( |
Alex Klein | 8718c03 | 2022-04-19 14:06:03 -0600 | [diff] [blame] | 52 | timestamp, |
| 53 | 'package_size.%s.%s.%s' % (image_type, partition_name, package_cpv), |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame] | 54 | metrics_lib.OP_GAUGE, |
Alex Klein | 8718c03 | 2022-04-19 14:06:03 -0600 | [diff] [blame] | 55 | arg=size) |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 56 | total_size += size |
| 57 | |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame] | 58 | metrics_lib.append_metrics_log( |
Alex Klein | 8718c03 | 2022-04-19 14:06:03 -0600 | [diff] [blame] | 59 | timestamp, |
| 60 | 'total_size.%s.%s' % (image_type, partition_name), |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame] | 61 | metrics_lib.OP_GAUGE, |
Alex Klein | 8718c03 | 2022-04-19 14:06:03 -0600 | [diff] [blame] | 62 | arg=total_size) |
Lizzy Presland | 1754b60 | 2022-06-01 23:47:35 +0000 | [diff] [blame^] | 63 | 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 Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 68 | |
| 69 | |
| 70 | def 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 Presland | 1754b60 | 2022-06-01 23:47:35 +0000 | [diff] [blame^] | 78 | 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 Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 85 | |
| 86 | if opts.packages: |
Alex Klein | 8718c03 | 2022-04-19 14:06:03 -0600 | [diff] [blame] | 87 | installed_packages = portage_util.GenerateInstalledPackages( |
| 88 | db, opts.root, opts.packages) |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 89 | 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 Presland | 1754b60 | 2022-06-01 23:47:35 +0000 | [diff] [blame^] | 95 | print(pformat.json(results)) |