Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2020 The ChromiumOS Authors |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Script to check if the package(s) have prebuilts. |
| 6 | |
| 7 | The script must be run inside the chroot. The output is a json dict mapping the |
| 8 | package atoms to a boolean for whether a prebuilt exists. |
| 9 | """ |
| 10 | |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 11 | import json |
Mike Frysinger | 065bb3d | 2023-06-21 14:25:15 -0400 | [diff] [blame] | 12 | import logging |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 13 | import os |
| 14 | |
| 15 | from chromite.lib import commandline |
| 16 | from chromite.lib import cros_build_lib |
| 17 | from chromite.lib import osutils |
| 18 | from chromite.lib import portage_util |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 19 | from chromite.lib.parser import package_info |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 20 | |
Mike Frysinger | 807d828 | 2022-04-28 22:45:17 -0400 | [diff] [blame] | 21 | |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 22 | if cros_build_lib.IsInsideChroot(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 23 | from chromite.lib import depgraph |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 24 | |
| 25 | |
| 26 | def GetParser(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 27 | """Build the argument parser.""" |
| 28 | parser = commandline.ArgumentParser(description=__doc__) |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 29 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 30 | parser.add_argument( |
| 31 | "-b", |
| 32 | "--build-target", |
| 33 | dest="build_target_name", |
| 34 | help="The build target that is being checked.", |
| 35 | ) |
| 36 | parser.add_argument( |
| 37 | "--output", |
| 38 | type="path", |
| 39 | required=True, |
| 40 | help="The file path where the result json should be stored.", |
| 41 | ) |
| 42 | parser.add_argument( |
| 43 | "packages", nargs="+", help="The package atoms that are being checked." |
| 44 | ) |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 45 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 46 | return parser |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 47 | |
| 48 | |
| 49 | def _ParseArguments(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 50 | """Parse and validate arguments.""" |
| 51 | parser = GetParser() |
| 52 | opts = parser.parse_args(argv) |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 53 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 54 | if not os.path.exists(os.path.dirname(opts.output)): |
| 55 | parser.error("Path containing the output file does not exist.") |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 56 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 57 | # Manually parse the packages as CPVs. |
| 58 | packages = [] |
| 59 | for pkg in opts.packages: |
| 60 | cpv = package_info.parse(pkg) |
| 61 | if not cpv.atom: |
| 62 | parser.error("Invalid package atom: %s" % pkg) |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 63 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 64 | packages.append(cpv) |
| 65 | opts.packages = packages |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 66 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 67 | opts.Freeze() |
| 68 | return opts |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 69 | |
| 70 | |
| 71 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 72 | opts = _ParseArguments(argv) |
| 73 | cros_build_lib.AssertInsideChroot() |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 74 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 75 | board = opts.build_target_name |
Mike Frysinger | fbb9899 | 2023-06-21 14:34:54 -0400 | [diff] [blame] | 76 | results = {} |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 77 | bests = {} |
| 78 | for cpv in opts.packages: |
Mike Frysinger | fbb9899 | 2023-06-21 14:34:54 -0400 | [diff] [blame] | 79 | try: |
| 80 | bests[cpv.atom] = portage_util.PortageqBestVisible( |
| 81 | cpv.atom, board=board |
| 82 | ) |
| 83 | logging.debug( |
| 84 | "Resolved %s best visible to %s", cpv.atom, bests[cpv.atom] |
| 85 | ) |
| 86 | except portage_util.NoVisiblePackageError: |
| 87 | results[cpv.atom] = False |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 88 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 89 | # Emerge args: |
| 90 | # g: use binpkgs (needed to find if we have one) |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 91 | # u: update packages to the latest version (want updates to invalidate |
| 92 | # binpkgs) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 93 | # D: deep -- consider full tree rather that just immediate deps |
| 94 | # (changes in dependencies and transitive deps can invalidate a binpkg) |
| 95 | # N: Packages with changed use flags should be considered |
| 96 | # (changes in dependencies and transitive deps can invalidate a binpkg) |
| 97 | # q: quiet (simplifies output) |
| 98 | # p: pretend (don't actually install it) |
Mike Frysinger | fbb9899 | 2023-06-21 14:34:54 -0400 | [diff] [blame] | 99 | if bests: |
Mike Frysinger | a22e21d | 2023-06-21 14:50:23 -0400 | [diff] [blame^] | 100 | args = ["-guDNqp", "--with-bdeps=y"] |
Mike Frysinger | fbb9899 | 2023-06-21 14:34:54 -0400 | [diff] [blame] | 101 | if board: |
| 102 | args.append("--board=%s" % board) |
| 103 | args.extend("=%s" % best.cpvr for best in bests.values()) |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 104 | |
Mike Frysinger | fbb9899 | 2023-06-21 14:34:54 -0400 | [diff] [blame] | 105 | generator = depgraph.DepGraphGenerator() |
| 106 | logging.debug( |
| 107 | "Initializing depgraph with: %s", cros_build_lib.CmdToStr(args) |
| 108 | ) |
| 109 | generator.Initialize(args) |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 110 | |
Mike Frysinger | fbb9899 | 2023-06-21 14:34:54 -0400 | [diff] [blame] | 111 | for atom, best in bests.items(): |
| 112 | results[atom] = generator.HasPrebuilt(best.cpvr) |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 113 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 114 | osutils.WriteFile(opts.output, json.dumps(results)) |