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