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 |
| 12 | import os |
| 13 | |
| 14 | from chromite.lib import commandline |
| 15 | from chromite.lib import cros_build_lib |
| 16 | from chromite.lib import osutils |
| 17 | from chromite.lib import portage_util |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 18 | from chromite.lib.parser import package_info |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 19 | |
Mike Frysinger | 807d828 | 2022-04-28 22:45:17 -0400 | [diff] [blame] | 20 | |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 21 | if cros_build_lib.IsInsideChroot(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 22 | from chromite.lib import depgraph |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 23 | |
| 24 | |
| 25 | def GetParser(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 26 | """Build the argument parser.""" |
| 27 | parser = commandline.ArgumentParser(description=__doc__) |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 28 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 29 | parser.add_argument( |
| 30 | "-b", |
| 31 | "--build-target", |
| 32 | dest="build_target_name", |
| 33 | help="The build target that is being checked.", |
| 34 | ) |
| 35 | parser.add_argument( |
| 36 | "--output", |
| 37 | type="path", |
| 38 | required=True, |
| 39 | help="The file path where the result json should be stored.", |
| 40 | ) |
| 41 | parser.add_argument( |
| 42 | "packages", nargs="+", help="The package atoms that are being checked." |
| 43 | ) |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 44 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | return parser |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 46 | |
| 47 | |
| 48 | def _ParseArguments(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 49 | """Parse and validate arguments.""" |
| 50 | parser = GetParser() |
| 51 | opts = parser.parse_args(argv) |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 52 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 53 | if not os.path.exists(os.path.dirname(opts.output)): |
| 54 | parser.error("Path containing the output file does not exist.") |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 55 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 56 | # Manually parse the packages as CPVs. |
| 57 | packages = [] |
| 58 | for pkg in opts.packages: |
| 59 | cpv = package_info.parse(pkg) |
| 60 | if not cpv.atom: |
| 61 | parser.error("Invalid package atom: %s" % pkg) |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 62 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 63 | packages.append(cpv) |
| 64 | opts.packages = packages |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 65 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 66 | opts.Freeze() |
| 67 | return opts |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 68 | |
| 69 | |
| 70 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 71 | opts = _ParseArguments(argv) |
| 72 | cros_build_lib.AssertInsideChroot() |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 73 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 74 | board = opts.build_target_name |
| 75 | bests = {} |
| 76 | for cpv in opts.packages: |
| 77 | bests[cpv.atom] = portage_util.PortageqBestVisible( |
| 78 | cpv.atom, board=board |
| 79 | ) |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 80 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 81 | # Emerge args: |
| 82 | # g: use binpkgs (needed to find if we have one) |
| 83 | # u: update packages to latest version (want updates to invalidate binpkgs) |
| 84 | # D: deep -- consider full tree rather that just immediate deps |
| 85 | # (changes in dependencies and transitive deps can invalidate a binpkg) |
| 86 | # N: Packages with changed use flags should be considered |
| 87 | # (changes in dependencies and transitive deps can invalidate a binpkg) |
| 88 | # q: quiet (simplifies output) |
| 89 | # p: pretend (don't actually install it) |
| 90 | args = ["-guDNqp", "--with-bdeps=y", "--color=n"] |
| 91 | if board: |
| 92 | args.append("--board=%s" % board) |
| 93 | args.extend("=%s" % best.cpvr for best in bests.values()) |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 94 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 95 | generator = depgraph.DepGraphGenerator() |
| 96 | generator.Initialize(args) |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 97 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 98 | results = {} |
| 99 | for atom, best in bests.items(): |
| 100 | results[atom] = generator.HasPrebuilt(best.cpvr) |
Alex Klein | 9ec0445 | 2020-05-18 15:08:24 -0600 | [diff] [blame] | 101 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 102 | osutils.WriteFile(opts.output, json.dumps(results)) |