blob: f96c5fccb3d43cac1fec9fdc6e37b11de882352a [file] [log] [blame]
Alex Klein9ec04452020-05-18 15:08:24 -06001# -*- coding: utf-8 -*-
2# Copyright 2020 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Script to check if the package(s) have prebuilts.
7
8The script must be run inside the chroot. The output is a json dict mapping the
9package atoms to a boolean for whether a prebuilt exists.
10"""
11
12from __future__ import print_function
13
14import json
15import os
16
17from chromite.lib import commandline
18from chromite.lib import cros_build_lib
19from chromite.lib import osutils
20from chromite.lib import portage_util
21
22if cros_build_lib.IsInsideChroot():
23 from chromite.lib import depgraph
24
25
26def GetParser():
27 """Build the argument parser."""
28 parser = commandline.ArgumentParser(description=__doc__)
29
30 parser.add_argument(
31 '-b',
32 '--build-target',
Alex Klein952ceee2020-09-02 10:13:18 -060033 dest='build_target_name',
Alex Klein9ec04452020-05-18 15:08:24 -060034 help='The build target that is being checked.')
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 parser.add_argument(
41 'packages',
42 nargs='+',
43 help='The package atoms that are being checked.')
44
45 return parser
46
47
48def _ParseArguments(argv):
49 """Parse and validate arguments."""
50 parser = GetParser()
51 opts = parser.parse_args(argv)
52
53 if not os.path.exists(os.path.dirname(opts.output)):
54 parser.error('Path containing the output file does not exist.')
55
56 # Manually parse the packages as CPVs.
57 packages = []
58 for pkg in opts.packages:
59 cpv = portage_util.SplitCPV(pkg, strict=False)
60 if not cpv.category or not cpv.package:
61 parser.error('Invalid package atom: %s' % pkg)
62
63 packages.append(cpv)
64 opts.packages = packages
65
66 opts.Freeze()
67 return opts
68
69
70def main(argv):
71 opts = _ParseArguments(argv)
72 cros_build_lib.AssertInsideChroot()
73
Alex Klein952ceee2020-09-02 10:13:18 -060074 board = opts.build_target_name
Alex Klein9ec04452020-05-18 15:08:24 -060075 bests = {}
76 for cpv in opts.packages:
77 bests[cpv.cp] = portage_util.PortageqBestVisible(cpv.cp, board=board)
78
79 # Emerge args:
80 # g: use binpkgs (needed to find if we have one)
81 # u: update packages to latest version (want updates to invalidate binpkgs)
82 # D: deep -- consider full tree rather that just immediate deps
83 # (changes in dependencies and transitive deps can invalidate a binpkg)
84 # N: Packages with changed use flags should be considered
85 # (changes in dependencies and transitive deps can invalidate a binpkg)
86 # q: quiet (simplifies output)
87 # p: pretend (don't actually install it)
88 args = ['-guDNqp', '--with-bdeps=y', '--color=n']
89 if board:
90 args.append('--board=%s' % board)
91 args.extend('=%s' % best.cpf for best in bests.values())
92
93 generator = depgraph.DepGraphGenerator()
94 generator.Initialize(args)
95
96 results = {}
97 for atom, best in bests.items():
98 results[atom] = generator.HasPrebuilt(best.cpf)
99
100 osutils.WriteFile(opts.output, json.dumps(results))