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