blob: 434e5feafa7ac68034ec7941c1ed692e8bcb5b81 [file] [log] [blame]
Alex Klein9ec04452020-05-18 15:08:24 -06001# 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
7The script must be run inside the chroot. The output is a json dict mapping the
8package atoms to a boolean for whether a prebuilt exists.
9"""
10
Alex Klein9ec04452020-05-18 15:08:24 -060011import json
12import os
13
14from chromite.lib import commandline
15from chromite.lib import cros_build_lib
16from chromite.lib import osutils
17from chromite.lib import portage_util
Alex Klein18a60af2020-06-11 12:08:47 -060018from chromite.lib.parser import package_info
Alex Klein9ec04452020-05-18 15:08:24 -060019
20if cros_build_lib.IsInsideChroot():
21 from chromite.lib import depgraph
22
23
24def GetParser():
25 """Build the argument parser."""
26 parser = commandline.ArgumentParser(description=__doc__)
27
28 parser.add_argument(
29 '-b',
30 '--build-target',
Alex Klein952ceee2020-09-02 10:13:18 -060031 dest='build_target_name',
Alex Klein9ec04452020-05-18 15:08:24 -060032 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
46def _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 Klein18a60af2020-06-11 12:08:47 -060057 cpv = package_info.parse(pkg)
58 if not cpv.atom:
Alex Klein9ec04452020-05-18 15:08:24 -060059 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
68def main(argv):
69 opts = _ParseArguments(argv)
70 cros_build_lib.AssertInsideChroot()
71
Alex Klein952ceee2020-09-02 10:13:18 -060072 board = opts.build_target_name
Alex Klein9ec04452020-05-18 15:08:24 -060073 bests = {}
74 for cpv in opts.packages:
Alex Klein18a60af2020-06-11 12:08:47 -060075 bests[cpv.atom] = portage_util.PortageqBestVisible(cpv.atom, board=board)
Alex Klein9ec04452020-05-18 15:08:24 -060076
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)
Alex Klein5caab872021-09-10 11:44:37 -060089 args.extend('=%s' % best.cpvr for best in bests.values())
Alex Klein9ec04452020-05-18 15:08:24 -060090
91 generator = depgraph.DepGraphGenerator()
92 generator.Initialize(args)
93
94 results = {}
95 for atom, best in bests.items():
Alex Klein5caab872021-09-10 11:44:37 -060096 results[atom] = generator.HasPrebuilt(best.cpvr)
Alex Klein9ec04452020-05-18 15:08:24 -060097
98 osutils.WriteFile(opts.output, json.dumps(results))