blob: 77e9ffbe88c64a07929293f6e6c53c6bd70019fc [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Alex Kleineb77ffa2019-05-28 14:47:44 -06002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Package utility functionality."""
6
Yaakov Shaul730814a2019-09-10 13:58:25 -06007import collections
Trent Apted593c0742023-05-05 03:50:20 +00008
9# TODO(b/236161656): Fix.
10# pylint: disable-next=no-name-in-module,import-error
Ben Reiche779cf42020-12-15 03:21:31 +000011from distutils.version import LooseVersion
Yaakov Shaulcb1cfc32019-09-16 13:51:19 -060012import fileinput
Alex Klein87531182019-08-12 15:23:37 -060013import functools
Yaakov Shaul395ae832019-09-09 14:45:32 -060014import json
Chris McDonaldf7c03d42021-07-21 11:54:26 -060015import logging
Evan Hernandezb51f1522019-08-15 11:29:40 -060016import os
Michael Mortensenb70e8a82019-10-10 18:43:41 -060017import re
Yaakov Shaulcb1cfc32019-09-16 13:51:19 -060018import sys
Alex Klein68a28712021-11-08 11:08:30 -070019from typing import Iterable, List, NamedTuple, Optional, TYPE_CHECKING, Union
Alex Klein87531182019-08-12 15:23:37 -060020
Mike Frysinger2c024062021-05-22 15:43:22 -040021from chromite.third_party.google.protobuf import json_format
Yaakov Shaul730814a2019-09-10 13:58:25 -060022
Andrew Lamb2bde9e42019-11-04 13:24:09 -070023from chromite.api.gen.config import replication_config_pb2
Ram Chandrasekar60f69f32022-06-03 22:49:30 +000024from chromite.lib import chromeos_version
Alex Kleineb77ffa2019-05-28 14:47:44 -060025from chromite.lib import constants
Evan Hernandezb51f1522019-08-15 11:29:40 -060026from chromite.lib import cros_build_lib
Alex Kleineb77ffa2019-05-28 14:47:44 -060027from chromite.lib import git
Michael Mortensende716a12020-05-15 11:27:00 -060028from chromite.lib import image_lib
Michael Mortensenb70e8a82019-10-10 18:43:41 -060029from chromite.lib import osutils
Alex Kleineb77ffa2019-05-28 14:47:44 -060030from chromite.lib import portage_util
Andrew Lamb2bde9e42019-11-04 13:24:09 -070031from chromite.lib import replication_lib
Alex Kleind6195b62019-08-06 16:01:16 -060032from chromite.lib import uprev_lib
Alex Klein18a60af2020-06-11 12:08:47 -060033from chromite.lib.parser import package_info
Shao-Chuan Lee05e51142021-11-24 12:27:37 +090034from chromite.service import android
Alex Kleineb77ffa2019-05-28 14:47:44 -060035
Mike Frysinger68796b52019-08-25 00:04:27 -040036
Alex Klein5caab872021-09-10 11:44:37 -060037if TYPE_CHECKING:
Alex Klein1699fab2022-09-08 08:46:06 -060038 from chromite.lib import build_target_lib
39 from chromite.lib import chroot_lib
Chris McDonaldf7c03d42021-07-21 11:54:26 -060040
Alex Klein36b117f2019-09-30 15:13:46 -060041if cros_build_lib.IsInsideChroot():
Alex Klein1699fab2022-09-08 08:46:06 -060042 from chromite.lib import depgraph
43 from chromite.service import dependency
Alex Klein36b117f2019-09-30 15:13:46 -060044
Alex Klein87531182019-08-12 15:23:37 -060045# Registered handlers for uprevving versioned packages.
46_UPREV_FUNCS = {}
47
Alex Kleineb77ffa2019-05-28 14:47:44 -060048
49class Error(Exception):
Alex Klein1699fab2022-09-08 08:46:06 -060050 """Module's base error class."""
Alex Kleineb77ffa2019-05-28 14:47:44 -060051
52
Alex Klein4de25e82019-08-05 15:58:39 -060053class UnknownPackageError(Error):
Alex Klein1699fab2022-09-08 08:46:06 -060054 """Uprev attempted for a package without a registered handler."""
Alex Klein4de25e82019-08-05 15:58:39 -060055
56
Alex Kleineb77ffa2019-05-28 14:47:44 -060057class UprevError(Error):
Alex Klein1699fab2022-09-08 08:46:06 -060058 """An error occurred while uprevving packages."""
Alex Kleineb77ffa2019-05-28 14:47:44 -060059
60
Michael Mortensenb70e8a82019-10-10 18:43:41 -060061class NoAndroidVersionError(Error):
Alex Klein1699fab2022-09-08 08:46:06 -060062 """An error occurred while trying to determine the android version."""
Michael Mortensenb70e8a82019-10-10 18:43:41 -060063
64
65class NoAndroidBranchError(Error):
Alex Klein1699fab2022-09-08 08:46:06 -060066 """An error occurred while trying to determine the android branch."""
Michael Mortensenb70e8a82019-10-10 18:43:41 -060067
68
69class NoAndroidTargetError(Error):
Alex Klein1699fab2022-09-08 08:46:06 -060070 """An error occurred while trying to determine the android target."""
Michael Mortensenb70e8a82019-10-10 18:43:41 -060071
72
Lizzy Presland0b978e62022-09-09 16:55:29 +000073class KernelVersionError(Error):
74 """An error occurred while trying to determine the kernel version."""
75
76
Alex Klein4de25e82019-08-05 15:58:39 -060077class AndroidIsPinnedUprevError(UprevError):
Alex Klein1699fab2022-09-08 08:46:06 -060078 """Raised when we try to uprev while Android is pinned."""
Alex Klein4de25e82019-08-05 15:58:39 -060079
Alex Klein1699fab2022-09-08 08:46:06 -060080 def __init__(self, new_android_atom):
81 """Initialize a AndroidIsPinnedUprevError.
Alex Klein4de25e82019-08-05 15:58:39 -060082
Alex Klein1699fab2022-09-08 08:46:06 -060083 Args:
Alex Klein348e7692022-10-13 17:03:37 -060084 new_android_atom: The Android atom that we failed to uprev to, due
85 to Android being pinned.
Alex Klein1699fab2022-09-08 08:46:06 -060086 """
87 assert new_android_atom
88 msg = (
89 "Failed up uprev to Android version %s as Android was pinned."
90 % new_android_atom
91 )
92 super().__init__(msg)
93 self.new_android_atom = new_android_atom
Alex Klein87531182019-08-12 15:23:37 -060094
95
Andrew Lamb9563a152019-12-04 11:42:18 -070096class GeneratedCrosConfigFilesError(Error):
Alex Klein1699fab2022-09-08 08:46:06 -060097 """Error when cros_config_schema does not produce expected files"""
Andrew Lamb9563a152019-12-04 11:42:18 -070098
Alex Klein1699fab2022-09-08 08:46:06 -060099 def __init__(self, expected_files, found_files):
100 msg = "Expected to find generated C files: %s. Actually found: %s" % (
101 expected_files,
102 found_files,
103 )
104 super().__init__(msg)
Andrew Lamb9563a152019-12-04 11:42:18 -0700105
Alex Klein7a3a7dd2020-01-08 16:44:38 -0700106
Alex Klein1699fab2022-09-08 08:46:06 -0600107NeedsChromeSourceResult = collections.namedtuple(
108 "NeedsChromeSourceResult",
109 (
110 "needs_chrome_source",
111 "builds_chrome",
112 "packages",
113 "missing_chrome_prebuilt",
114 "missing_follower_prebuilt",
115 "local_uprev",
116 ),
117)
Alex Klein6becabc2020-09-11 14:03:05 -0600118
119
Yaakov Shaulcb1cfc32019-09-16 13:51:19 -0600120def patch_ebuild_vars(ebuild_path, variables):
Alex Klein1699fab2022-09-08 08:46:06 -0600121 """Updates variables in ebuild.
Yaakov Shaulcb1cfc32019-09-16 13:51:19 -0600122
Alex Klein1699fab2022-09-08 08:46:06 -0600123 Use this function rather than portage_util.EBuild.UpdateEBuild when you
124 want to preserve the variable position and quotes within the ebuild.
Yaakov Shaulcb1cfc32019-09-16 13:51:19 -0600125
Alex Klein1699fab2022-09-08 08:46:06 -0600126 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600127 ebuild_path: The path of the ebuild.
128 variables: Dictionary of variables to update in ebuild.
Alex Klein1699fab2022-09-08 08:46:06 -0600129 """
130 try:
131 for line in fileinput.input(ebuild_path, inplace=1):
132 for var, value in variables.items():
133 line = re.sub(rf"\b{var}=\S+", f'{var}="{value}"', line)
134 sys.stdout.write(line)
135 finally:
136 fileinput.close()
Yaakov Shaulcb1cfc32019-09-16 13:51:19 -0600137
138
Alex Klein87531182019-08-12 15:23:37 -0600139def uprevs_versioned_package(package):
Alex Klein1699fab2022-09-08 08:46:06 -0600140 """Decorator to register package uprev handlers."""
141 assert package
Alex Klein87531182019-08-12 15:23:37 -0600142
Alex Klein1699fab2022-09-08 08:46:06 -0600143 def register(func):
144 """Registers |func| as a handler for |package|."""
145 _UPREV_FUNCS[package] = func
Alex Klein87531182019-08-12 15:23:37 -0600146
Alex Klein1699fab2022-09-08 08:46:06 -0600147 @functools.wraps(func)
148 def pass_through(*args, **kwargs):
149 return func(*args, **kwargs)
Alex Klein87531182019-08-12 15:23:37 -0600150
Alex Klein1699fab2022-09-08 08:46:06 -0600151 return pass_through
Alex Klein87531182019-08-12 15:23:37 -0600152
Alex Klein1699fab2022-09-08 08:46:06 -0600153 return register
Alex Klein87531182019-08-12 15:23:37 -0600154
155
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900156class UprevAndroidResult(NamedTuple):
Alex Klein1699fab2022-09-08 08:46:06 -0600157 """Results of an Android uprev."""
158
159 revved: bool
160 android_atom: str = None
161 modified_files: List[str] = None
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900162
163
164def uprev_android(
165 android_package: str,
Alex Klein1699fab2022-09-08 08:46:06 -0600166 chroot: "chroot_lib.Chroot",
167 build_targets: Optional[List["build_target_lib.BuildTarget"]] = None,
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900168 android_build_branch: Optional[str] = None,
169 android_version: Optional[str] = None,
Alex Klein1699fab2022-09-08 08:46:06 -0600170 skip_commit: bool = False,
171) -> UprevAndroidResult:
172 """Performs an Android uprev by calling cros_mark_android_as_stable.
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900173
Alex Klein1699fab2022-09-08 08:46:06 -0600174 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600175 android_package: The Android package to uprev.
176 chroot: The chroot to enter.
177 build_targets: List of build targets to cleanup after uprev.
178 android_build_branch: Override the default Android branch corresponding
179 to the package.
180 android_version: Uprev to the particular version. By default the latest
181 available version is used.
182 skip_commit: Whether to skip committing the change after a successful
183 uprev.
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900184
Alex Klein1699fab2022-09-08 08:46:06 -0600185 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600186 The uprev result containing:
187 revved: Whether an uprev happened.
188 android_atom: If revved, the portage atom for the revved Android
189 ebuild.
190 modified_files: If revved, list of files being modified.
Alex Klein1699fab2022-09-08 08:46:06 -0600191 """
192 command = [
193 "cros_mark_android_as_stable",
194 f"--android_package={android_package}",
195 ]
196 if build_targets:
197 command.append(f'--boards={":".join(bt.name for bt in build_targets)}')
198 if android_build_branch:
199 command.append(f"--android_build_branch={android_build_branch}")
200 if android_version:
201 command.append(f"--force_version={android_version}")
202 if skip_commit:
203 command.append("--skip_commit")
Alex Klein4de25e82019-08-05 15:58:39 -0600204
Brian Norris3a585d22023-07-19 16:26:11 -0700205 result = chroot.run(command, stdout=True, encoding="utf-8")
Alex Klein4de25e82019-08-05 15:58:39 -0600206
Alex Klein1699fab2022-09-08 08:46:06 -0600207 # cros_mark_android_as_stable prints the uprev result to stdout as JSON in a
208 # single line. We only take the last line from stdout to make sure no junk
Alex Kleinfee86da2023-01-20 18:40:06 -0700209 # output is included (e.g. messages from bashrc scripts that run upon
210 # entering the chroot.)
Alex Klein1699fab2022-09-08 08:46:06 -0600211 output = json.loads(result.stdout.strip().splitlines()[-1])
Shao-Chuan Leedea458f2021-11-25 23:46:53 +0900212
Alex Klein1699fab2022-09-08 08:46:06 -0600213 if not output["revved"]:
214 logging.info("Found nothing to rev.")
215 return UprevAndroidResult(revved=False)
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900216
Alex Klein1699fab2022-09-08 08:46:06 -0600217 android_atom = output["android_atom"]
Alex Klein4de25e82019-08-05 15:58:39 -0600218
Alex Klein1699fab2022-09-08 08:46:06 -0600219 for target in build_targets or []:
220 # Sanity check: We should always be able to merge the version of
221 # Android we just unmasked.
222 command = [f"emerge-{target.name}", "-p", "--quiet", f"={android_atom}"]
223 try:
Brian Norris3a585d22023-07-19 16:26:11 -0700224 chroot.run(command)
Alex Klein1699fab2022-09-08 08:46:06 -0600225 except cros_build_lib.RunCommandError:
226 logging.error(
Trent Aptedb18e36f2023-05-10 15:06:38 +1000227 "Cannot emerge-%s =%s\nIs Android pinned to an older version?",
Alex Klein1699fab2022-09-08 08:46:06 -0600228 target,
229 android_atom,
230 )
231 raise AndroidIsPinnedUprevError(android_atom)
Alex Klein4de25e82019-08-05 15:58:39 -0600232
Alex Klein1699fab2022-09-08 08:46:06 -0600233 return UprevAndroidResult(
234 revved=True,
235 android_atom=android_atom,
236 modified_files=output["modified_files"],
237 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900238
239
Alex Klein1699fab2022-09-08 08:46:06 -0600240def uprev_android_lkgb(
241 android_package: str,
242 build_targets: List["build_target_lib.BuildTarget"],
243 chroot: "chroot_lib.Chroot",
244) -> uprev_lib.UprevVersionedPackageResult:
245 """Uprevs an Android package to the version specified in the LKGB file.
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900246
Alex Klein1699fab2022-09-08 08:46:06 -0600247 This is the PUpr handler for Android packages, triggered whenever the
248 corresponding LKGB file is being updated.
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900249
Alex Kleinfee86da2023-01-20 18:40:06 -0700250 PUpr for Android does not test the uprev change in CQ; instead we run
251 separate jobs to test new Android versions, and we write the latest vetted
252 version to the LKGB file. Find the design at go/android-uprev-recipes.
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900253
Alex Klein1699fab2022-09-08 08:46:06 -0600254 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600255 android_package: The Android package to uprev.
256 build_targets: List of build targets to cleanup after uprev.
257 chroot: The chroot to enter.
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900258
Alex Klein1699fab2022-09-08 08:46:06 -0600259 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600260 An uprev_lib.UprevVersionedPackageResult containing the new version and
261 a list of modified files.
Alex Klein1699fab2022-09-08 08:46:06 -0600262 """
263 android_package_dir = android.GetAndroidPackageDir(android_package)
Shao-Chuan Lee293389c2023-01-20 17:44:35 +0900264 lkgb = android.ReadLKGB(android_package_dir)
265 android_version = lkgb["build_id"]
266 android_branch = lkgb.get("branch", None)
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900267
Alex Klein1699fab2022-09-08 08:46:06 -0600268 result = uprev_lib.UprevVersionedPackageResult()
269 uprev_result = uprev_android(
270 android_package,
271 chroot,
272 build_targets=build_targets,
Shao-Chuan Lee293389c2023-01-20 17:44:35 +0900273 android_build_branch=android_branch,
Alex Klein1699fab2022-09-08 08:46:06 -0600274 android_version=android_version,
275 skip_commit=True,
276 )
277 if not uprev_result.revved:
278 return result
279
Alex Kleinfee86da2023-01-20 18:40:06 -0700280 # cros_mark_android_as_stable returns paths relative to
281 # |android.OVERLAY_DIR|.
Alex Klein1699fab2022-09-08 08:46:06 -0600282 result.add_result(
283 android_version,
284 [
285 os.path.join(android.OVERLAY_DIR, f)
286 for f in uprev_result.modified_files
287 ],
288 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900289 return result
290
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900291
292def define_uprev_android_lkgb_handlers():
Alex Klein1699fab2022-09-08 08:46:06 -0600293 """Dynamically define uprev handlers for each Android package"""
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900294
Alex Klein1699fab2022-09-08 08:46:06 -0600295 def define_handler(android_package):
296 """Defines the uprev handler for an Android package."""
297 full_package_name = "chromeos-base/" + android_package
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900298
Alex Klein1699fab2022-09-08 08:46:06 -0600299 @uprevs_versioned_package(full_package_name)
300 def _handler(build_targets, _refs, chroot):
301 return uprev_android_lkgb(android_package, build_targets, chroot)
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900302
Shao-Chuan Leeca2cbcc2022-11-02 08:28:31 +0900303 for android_package in android.GetAllAndroidPackages():
Alex Klein1699fab2022-09-08 08:46:06 -0600304 define_handler(android_package)
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900305
306
307define_uprev_android_lkgb_handlers()
Alex Klein4de25e82019-08-05 15:58:39 -0600308
309
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700310def uprev_build_targets(
Alex Klein1699fab2022-09-08 08:46:06 -0600311 build_targets: Optional[List["build_target_lib.BuildTarget"]],
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700312 overlay_type: str,
Alex Klein1699fab2022-09-08 08:46:06 -0600313 chroot: "chroot_lib.Chroot" = None,
314 output_dir: Optional[str] = None,
315):
316 """Uprev the set provided build targets, or all if not specified.
Alex Kleineb77ffa2019-05-28 14:47:44 -0600317
Alex Klein1699fab2022-09-08 08:46:06 -0600318 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600319 build_targets: The build targets whose overlays should be uprevved,
320 empty or None for all.
321 overlay_type: One of the valid overlay types except None (see
322 constants.VALID_OVERLAYS).
323 chroot: The chroot to clean, if desired.
324 output_dir: The path to optionally dump result files.
Alex Klein1699fab2022-09-08 08:46:06 -0600325 """
326 # Need a valid overlay, but exclude None.
327 assert overlay_type and overlay_type in constants.VALID_OVERLAYS
Alex Kleineb77ffa2019-05-28 14:47:44 -0600328
Alex Klein1699fab2022-09-08 08:46:06 -0600329 if build_targets:
330 overlays = portage_util.FindOverlaysForBoards(
331 overlay_type, boards=[t.name for t in build_targets]
332 )
333 else:
334 overlays = portage_util.FindOverlays(overlay_type)
Alex Kleineb77ffa2019-05-28 14:47:44 -0600335
Alex Klein1699fab2022-09-08 08:46:06 -0600336 return uprev_overlays(
337 overlays,
338 build_targets=build_targets,
339 chroot=chroot,
340 output_dir=output_dir,
341 )
Alex Kleineb77ffa2019-05-28 14:47:44 -0600342
343
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700344def uprev_overlays(
345 overlays: List[str],
Alex Klein1699fab2022-09-08 08:46:06 -0600346 build_targets: Optional[List["build_target_lib.BuildTarget"]] = None,
347 chroot: Optional["chroot_lib.Chroot"] = None,
348 output_dir: Optional[str] = None,
349) -> List[str]:
350 """Uprev the given overlays.
Alex Kleineb77ffa2019-05-28 14:47:44 -0600351
Alex Klein1699fab2022-09-08 08:46:06 -0600352 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600353 overlays: The list of overlay paths.
354 build_targets: The build targets to clean in |chroot|, if desired. No
355 effect unless |chroot| is provided.
356 chroot: The chroot to clean, if desired.
357 output_dir: The path to optionally dump result files.
Alex Kleineb77ffa2019-05-28 14:47:44 -0600358
Alex Klein1699fab2022-09-08 08:46:06 -0600359 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600360 The paths to all the modified ebuild files. This includes the new files
361 that were added (i.e. the new versions) and all the removed files
Alex Klein1699fab2022-09-08 08:46:06 -0600362 (i.e. the old versions).
363 """
364 assert overlays
Alex Kleineb77ffa2019-05-28 14:47:44 -0600365
Alex Klein1699fab2022-09-08 08:46:06 -0600366 manifest = git.ManifestCheckout.Cached(constants.SOURCE_ROOT)
Alex Kleineb77ffa2019-05-28 14:47:44 -0600367
Alex Klein1699fab2022-09-08 08:46:06 -0600368 uprev_manager = uprev_lib.UprevOverlayManager(
369 overlays,
370 manifest,
371 build_targets=build_targets,
372 chroot=chroot,
373 output_dir=output_dir,
374 )
375 uprev_manager.uprev()
Alex Kleineb77ffa2019-05-28 14:47:44 -0600376
Alex Klein1699fab2022-09-08 08:46:06 -0600377 return uprev_manager.modified_ebuilds, uprev_manager.revved_packages
Alex Kleineb77ffa2019-05-28 14:47:44 -0600378
379
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700380def uprev_versioned_package(
Alex Kleind3b84042023-05-19 14:43:59 -0600381 package: package_info.PackageInfo,
Alex Klein1699fab2022-09-08 08:46:06 -0600382 build_targets: List["build_target_lib.BuildTarget"],
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700383 refs: List[uprev_lib.GitRef],
Alex Klein1699fab2022-09-08 08:46:06 -0600384 chroot: "chroot_lib.Chroot",
385) -> "uprev_lib.UprevVersionedPackageResult":
386 """Call registered uprev handler function for the package.
Alex Klein87531182019-08-12 15:23:37 -0600387
Alex Klein1699fab2022-09-08 08:46:06 -0600388 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600389 package: The package being uprevved.
390 build_targets: The build targets to clean on a successful uprev.
391 refs:
392 chroot: The chroot to enter for cleaning.
Alex Klein87531182019-08-12 15:23:37 -0600393
Alex Klein1699fab2022-09-08 08:46:06 -0600394 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600395 The result.
Alex Klein1699fab2022-09-08 08:46:06 -0600396 """
397 assert package
Alex Klein87531182019-08-12 15:23:37 -0600398
Alex Klein1699fab2022-09-08 08:46:06 -0600399 if package.cp not in _UPREV_FUNCS:
400 raise UnknownPackageError(
401 'Package "%s" does not have a registered handler.' % package.cp
402 )
Alex Klein87531182019-08-12 15:23:37 -0600403
Alex Klein1699fab2022-09-08 08:46:06 -0600404 return _UPREV_FUNCS[package.cp](build_targets, refs, chroot)
Alex Klein87531182019-08-12 15:23:37 -0600405
406
Alex Klein1699fab2022-09-08 08:46:06 -0600407@uprevs_versioned_package("media-libs/virglrenderer")
Navil Perezf57ba872020-06-04 22:38:37 +0000408def uprev_virglrenderer(_build_targets, refs, _chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600409 """Updates virglrenderer ebuilds.
Navil Perezf57ba872020-06-04 22:38:37 +0000410
Alex Klein1699fab2022-09-08 08:46:06 -0600411 See: uprev_versioned_package.
Navil Perezf57ba872020-06-04 22:38:37 +0000412
Alex Klein1699fab2022-09-08 08:46:06 -0600413 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600414 UprevVersionedPackageResult: The result of updating virglrenderer
Trent Aptedb18e36f2023-05-10 15:06:38 +1000415 ebuilds.
Alex Klein1699fab2022-09-08 08:46:06 -0600416 """
417 overlay = os.path.join(
418 constants.SOURCE_ROOT, constants.CHROMIUMOS_OVERLAY_DIR
419 )
420 repo_path = os.path.join(
421 constants.SOURCE_ROOT, "src", "third_party", "virglrenderer"
422 )
423 manifest = git.ManifestCheckout.Cached(repo_path)
Navil Perezf57ba872020-06-04 22:38:37 +0000424
Alex Klein1699fab2022-09-08 08:46:06 -0600425 uprev_manager = uprev_lib.UprevOverlayManager([overlay], manifest)
426 # TODO(crbug.com/1066242): Ebuilds for virglrenderer are currently
427 # denylisted. Do not force uprevs after builder is stable and ebuilds are no
428 # longer denylisted.
429 uprev_manager.uprev(package_list=["media-libs/virglrenderer"], force=True)
Navil Perezf57ba872020-06-04 22:38:37 +0000430
Alex Klein1699fab2022-09-08 08:46:06 -0600431 updated_files = uprev_manager.modified_ebuilds
432 result = uprev_lib.UprevVersionedPackageResult()
433 result.add_result(refs[-1].revision, updated_files)
434 return result
Navil Perezf57ba872020-06-04 22:38:37 +0000435
Alex Klein1699fab2022-09-08 08:46:06 -0600436
Matthew Lam59ca37d2022-10-24 18:11:06 +0000437@uprevs_versioned_package("x11-apps/igt-gpu-tools")
438def uprev_igt_gpu_tools(_build_targets, refs, _chroot):
439 """Updates igt-gpu-tools ebuilds.
440
441 See: uprev_versioned_package.
442
443 Returns:
Alex Kleinfee86da2023-01-20 18:40:06 -0700444 UprevVersionedPackageResult: The result of updating igt-gpu-tools
Trent Aptedb18e36f2023-05-10 15:06:38 +1000445 ebuilds.
Matthew Lam59ca37d2022-10-24 18:11:06 +0000446 """
447 overlay = os.path.join(
448 constants.SOURCE_ROOT, constants.CHROMIUMOS_OVERLAY_DIR
449 )
450 repo_path = os.path.join(
451 constants.SOURCE_ROOT, "src", "third_party", "igt-gpu-tools"
452 )
453 manifest = git.ManifestCheckout.Cached(repo_path)
454
455 uprev_manager = uprev_lib.UprevOverlayManager([overlay], manifest)
456 uprev_manager.uprev(package_list=["x11-apps/igt-gpu-tools"], force=True)
457
458 updated_files = uprev_manager.modified_ebuilds
459 result = uprev_lib.UprevVersionedPackageResult()
460 result.add_result(refs[-1].revision, updated_files)
461 return result
462
463
Alex Klein1699fab2022-09-08 08:46:06 -0600464@uprevs_versioned_package("chromeos-base/drivefs")
Jose Magana03b5a842020-08-19 12:52:59 +1000465def uprev_drivefs(_build_targets, refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600466 """Updates drivefs ebuilds.
Jose Magana03b5a842020-08-19 12:52:59 +1000467
Alex Klein1699fab2022-09-08 08:46:06 -0600468 DriveFS versions follow the tag format of refs/tags/drivefs_1.2.3.
469 See: uprev_versioned_package.
Jose Magana03b5a842020-08-19 12:52:59 +1000470
Alex Klein1699fab2022-09-08 08:46:06 -0600471 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600472 UprevVersionedPackageResult: The result of updating drivefs ebuilds.
Alex Klein1699fab2022-09-08 08:46:06 -0600473 """
Jose Magana03b5a842020-08-19 12:52:59 +1000474
Alex Klein1699fab2022-09-08 08:46:06 -0600475 DRIVEFS_PATH_PREFIX = "src/private-overlays/chromeos-overlay/chromeos-base"
476 result = uprev_lib.UprevVersionedPackageResult()
477 all_changed_files = []
Jose Magana03b5a842020-08-19 12:52:59 +1000478
Alex Klein1699fab2022-09-08 08:46:06 -0600479 DRIVEFS_REFS_PREFIX = "refs/tags/drivefs_"
480 drivefs_version = _get_latest_version_from_refs(DRIVEFS_REFS_PREFIX, refs)
481 if not drivefs_version:
482 # No valid DriveFS version is identified.
483 return result
484
485 logging.debug("DriveFS version determined from refs: %s", drivefs_version)
486
487 # Attempt to uprev drivefs package.
488 pkg_path = os.path.join(DRIVEFS_PATH_PREFIX, "drivefs")
489 uprev_result = uprev_lib.uprev_workon_ebuild_to_version(
490 pkg_path, drivefs_version, chroot, allow_downrev=False
491 )
492
493 if not uprev_result:
494 return result
495 all_changed_files.extend(uprev_result.changed_files)
496 result.add_result(drivefs_version, all_changed_files)
497
Ben Reich4f3fa1b2020-12-19 08:21:26 +0000498 return result
Jose Magana03b5a842020-08-19 12:52:59 +1000499
Jose Magana03b5a842020-08-19 12:52:59 +1000500
Alex Klein1699fab2022-09-08 08:46:06 -0600501@uprevs_versioned_package("chromeos-base/perfetto")
Chinglin Yufa728552023-04-13 03:12:04 +0000502@uprevs_versioned_package("dev-go/perfetto-protos")
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800503def uprev_perfetto(_build_targets, refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600504 """Updates Perfetto ebuilds.
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800505
Alex Klein1699fab2022-09-08 08:46:06 -0600506 Perfetto versions follow the tag format of refs/tags/v1.2.
507 See: uprev_versioned_package.
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800508
Alex Klein1699fab2022-09-08 08:46:06 -0600509 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600510 UprevVersionedPackageResult: The result of updating Perfetto ebuilds.
Alex Klein1699fab2022-09-08 08:46:06 -0600511 """
512 result = uprev_lib.UprevVersionedPackageResult()
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800513
Alex Klein1699fab2022-09-08 08:46:06 -0600514 PERFETTO_REFS_PREFIX = "refs/tags/v"
Chinglin Yufa728552023-04-13 03:12:04 +0000515
516 perfetto_ebuilds = ["chromeos-base/perfetto", "dev-go/perfetto-protos"]
517 perfetto_paths = [
518 os.path.join(constants.CHROMIUMOS_OVERLAY_DIR, e)
519 for e in perfetto_ebuilds
520 ]
521 # chromeos-base/perfetto is the primary ebuild.
522 primary_ebuild_path = perfetto_paths[0]
Chinglin Yuad12a512022-10-07 17:26:12 +0800523
524 # Decide the version number to uprev to:
525 # * If |refs| contains refs/tags/v*, get the latest from them.
Alex Klein1699fab2022-09-08 08:46:06 -0600526 perfetto_version = _get_latest_version_from_refs(PERFETTO_REFS_PREFIX, refs)
Chinglin Yuad12a512022-10-07 17:26:12 +0800527 # * Or if |refs| contains only the latest trunk revisions, use the current
528 # stable ebuild version for a revision bump.
529 if refs and not perfetto_version:
Chinglin Yufa728552023-04-13 03:12:04 +0000530 perfetto_version = uprev_lib.get_stable_ebuild_version(
531 primary_ebuild_path
532 )
Chinglin Yuad12a512022-10-07 17:26:12 +0800533
Alex Klein1699fab2022-09-08 08:46:06 -0600534 if not perfetto_version:
535 # No valid Perfetto version is identified.
536 return result
537
Chinglin Yufa728552023-04-13 03:12:04 +0000538 for path in perfetto_paths:
539 # Attempt to uprev perfetto package.
540 # |perfetto_version| is only used in determining the ebuild version. The
541 # package is always updated to the latest HEAD.
542 uprev_result = uprev_lib.uprev_workon_ebuild_to_version(
543 path,
544 perfetto_version,
545 chroot,
546 allow_downrev=False,
547 # Use default ref="HEAD"
548 )
Alex Klein1699fab2022-09-08 08:46:06 -0600549
Chinglin Yufa728552023-04-13 03:12:04 +0000550 if not uprev_result:
551 return result
Alex Klein1699fab2022-09-08 08:46:06 -0600552
Chinglin Yufa728552023-04-13 03:12:04 +0000553 # Include short git sha hash in the uprev commit message.
554 # Use 9 digits to match the short hash length in `perfetto --version`.
555 short_revision = refs[-1].revision[0:9]
556 version_and_rev = f"{perfetto_version}-{short_revision}"
557 result.add_result(version_and_rev, uprev_result.changed_files)
Alex Klein1699fab2022-09-08 08:46:06 -0600558
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800559 return result
560
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800561
Denis Nikitin63613e32022-09-09 22:26:50 -0700562class AfdoMetadata(NamedTuple):
563 """Data class holding AFDO metadata."""
564
565 var_name: str
566 path: str
567
568
Alex Klein1699fab2022-09-08 08:46:06 -0600569@uprevs_versioned_package("afdo/kernel-profiles")
Brian Norrisf0c68572023-07-12 17:32:20 -0700570def uprev_kernel_afdo(_build_targets, _refs, chroot: "chroot_lib.Chroot"):
Alex Klein1699fab2022-09-08 08:46:06 -0600571 """Updates kernel ebuilds with versions from kernel_afdo.json.
Yaakov Shaul395ae832019-09-09 14:45:32 -0600572
Alex Klein1699fab2022-09-08 08:46:06 -0600573 See: uprev_versioned_package.
Yaakov Shaul1eafe832019-09-10 16:50:26 -0600574
Alex Klein1699fab2022-09-08 08:46:06 -0600575 Raises:
Alex Klein348e7692022-10-13 17:03:37 -0600576 EbuildManifestError: When ebuild manifest does not complete
577 successfully.
578 JSONDecodeError: When json is malformed.
Alex Klein1699fab2022-09-08 08:46:06 -0600579 """
Denis Nikitin63613e32022-09-09 22:26:50 -0700580 metadata_dir = os.path.join(
Alex Klein1699fab2022-09-08 08:46:06 -0600581 constants.SOURCE_ROOT,
582 "src",
583 "third_party",
584 "toolchain-utils",
585 "afdo_metadata",
Denis Nikitin63613e32022-09-09 22:26:50 -0700586 )
587 metadata_files = (
588 AfdoMetadata(
589 var_name="AFDO_PROFILE_VERSION",
590 path=os.path.join(metadata_dir, "kernel_afdo.json"),
591 ),
592 AfdoMetadata(
593 var_name="ARM_AFDO_PROFILE_VERSION",
594 path=os.path.join(metadata_dir, "kernel_arm_afdo.json"),
595 ),
Alex Klein1699fab2022-09-08 08:46:06 -0600596 )
Yaakov Shaul395ae832019-09-09 14:45:32 -0600597
Alex Klein1699fab2022-09-08 08:46:06 -0600598 result = uprev_lib.UprevVersionedPackageResult()
Denis Nikitin63613e32022-09-09 22:26:50 -0700599 for metadata in metadata_files:
Mike Frysinger81a98062023-02-24 15:42:04 -0500600 with open(metadata.path, "r", encoding="utf-8") as f:
Denis Nikitin63613e32022-09-09 22:26:50 -0700601 versions = json.load(f)
Yaakov Shaul1eafe832019-09-10 16:50:26 -0600602
Denis Nikitin63613e32022-09-09 22:26:50 -0700603 for kernel_pkg, version_info in versions.items():
604 path = os.path.join(
605 constants.CHROMIUMOS_OVERLAY_DIR, "sys-kernel", kernel_pkg
606 )
607 ebuild_path = os.path.join(
608 constants.SOURCE_ROOT, path, f"{kernel_pkg}-9999.ebuild"
609 )
610 chroot_ebuild_path = os.path.join(
611 constants.CHROOT_SOURCE_ROOT, path, f"{kernel_pkg}-9999.ebuild"
612 )
613 afdo_profile_version = version_info["name"]
614 patch_ebuild_vars(
615 ebuild_path, {metadata.var_name: afdo_profile_version}
Alex Klein1699fab2022-09-08 08:46:06 -0600616 )
Yaakov Shaul1eafe832019-09-10 16:50:26 -0600617
Denis Nikitin63613e32022-09-09 22:26:50 -0700618 try:
619 cmd = ["ebuild", chroot_ebuild_path, "manifest", "--force"]
Brian Norris3a585d22023-07-19 16:26:11 -0700620 chroot.run(cmd)
Denis Nikitin63613e32022-09-09 22:26:50 -0700621 except cros_build_lib.RunCommandError as e:
622 raise uprev_lib.EbuildManifestError(
623 "Error encountered when regenerating the manifest for "
624 f"ebuild: {chroot_ebuild_path}\n{e}",
625 e,
626 )
Yaakov Shaul1eafe832019-09-10 16:50:26 -0600627
Denis Nikitin63613e32022-09-09 22:26:50 -0700628 manifest_path = os.path.join(
629 constants.SOURCE_ROOT, path, "Manifest"
630 )
631 result.add_result(
632 afdo_profile_version, [ebuild_path, manifest_path]
633 )
Yaakov Shaul730814a2019-09-10 13:58:25 -0600634
Alex Klein1699fab2022-09-08 08:46:06 -0600635 return result
Yaakov Shaul395ae832019-09-09 14:45:32 -0600636
637
Alex Klein1699fab2022-09-08 08:46:06 -0600638@uprevs_versioned_package("chromeos-base/termina-dlc")
639@uprevs_versioned_package("chromeos-base/termina-tools-dlc")
Maciek Swiech6b12f662022-01-25 16:51:19 +0000640def uprev_termina_dlcs(_build_targets, _refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600641 """Updates shared termina-dlc and termina-tools-dlc ebuilds.
Maciek Swiech6b12f662022-01-25 16:51:19 +0000642
Alex Klein1699fab2022-09-08 08:46:06 -0600643 termina-dlc - chromeos-base/termina-dlc
644 termina-tools-dlc - chromeos-base/termina-tools-dlc
Trent Beginaf51f1b2020-03-09 17:35:31 -0600645
Alex Klein1699fab2022-09-08 08:46:06 -0600646 See: uprev_versioned_package.
647 """
648 termina_dlc_pkg = "termina-dlc"
649 termina_dlc_pkg_path = os.path.join(
650 constants.CHROMIUMOS_OVERLAY_DIR, "chromeos-base", termina_dlc_pkg
651 )
652 tools_dlc_pkg = "termina-tools-dlc"
653 tools_dlc_pkg_path = os.path.join(
654 constants.CHROMIUMOS_OVERLAY_DIR, "chromeos-base", tools_dlc_pkg
655 )
Patrick Meiring5897add2020-09-16 16:30:17 +1000656
Alex Klein1699fab2022-09-08 08:46:06 -0600657 # termina-dlc and termina-tools-dlc are pinned to the same version.
658 version_pin_src_path = _get_version_pin_src_path(termina_dlc_pkg_path)
659 version_no_rev = osutils.ReadFile(version_pin_src_path).strip()
Patrick Meiring5897add2020-09-16 16:30:17 +1000660
Alex Klein1699fab2022-09-08 08:46:06 -0600661 result = uprev_lib.uprev_ebuild_from_pin(
662 termina_dlc_pkg_path, version_no_rev, chroot
663 )
664 result += uprev_lib.uprev_ebuild_from_pin(
665 tools_dlc_pkg_path, version_no_rev, chroot
666 )
Patrick Meiring5897add2020-09-16 16:30:17 +1000667
Alex Klein1699fab2022-09-08 08:46:06 -0600668 return result
Patrick Meiring5897add2020-09-16 16:30:17 +1000669
Alex Klein1699fab2022-09-08 08:46:06 -0600670
671@uprevs_versioned_package("chromeos-base/chromeos-lacros")
Julio Hurtadof1befec2021-05-05 21:34:26 +0000672def uprev_lacros(_build_targets, refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600673 """Updates lacros ebuilds.
Julio Hurtadof1befec2021-05-05 21:34:26 +0000674
Alex Klein1699fab2022-09-08 08:46:06 -0600675 Version to uprev to is gathered from the QA qualified version tracking file
Alex Kleinfee86da2023-01-20 18:40:06 -0700676 stored in chromium/src/chrome/LACROS_QA_QUALIFIED_VERSION. Uprev is
677 triggered on modification of this file across all chromium/src branches.
Julio Hurtadof1befec2021-05-05 21:34:26 +0000678
Alex Klein1699fab2022-09-08 08:46:06 -0600679 See: uprev_versioned_package.
680 """
681 result = uprev_lib.UprevVersionedPackageResult()
682 path = os.path.join(
683 constants.CHROMIUMOS_OVERLAY_DIR, "chromeos-base", "chromeos-lacros"
684 )
685 lacros_version = refs[0].revision
686 uprev_result = uprev_lib.uprev_workon_ebuild_to_version(
687 path, lacros_version, chroot, allow_downrev=False
688 )
Julio Hurtadoa994e002021-07-07 17:57:45 +0000689
Alex Klein1699fab2022-09-08 08:46:06 -0600690 if not uprev_result:
691 return result
692
693 result.add_result(lacros_version, uprev_result.changed_files)
Julio Hurtadoa994e002021-07-07 17:57:45 +0000694 return result
695
Julio Hurtadof1befec2021-05-05 21:34:26 +0000696
Alex Klein1699fab2022-09-08 08:46:06 -0600697@uprevs_versioned_package("chromeos-base/chromeos-lacros-parallel")
Julio Hurtado870ed322021-12-03 18:22:40 +0000698def uprev_lacros_in_parallel(
Alex Klein1699fab2022-09-08 08:46:06 -0600699 _build_targets: Optional[List["build_target_lib.BuildTarget"]],
Julio Hurtado870ed322021-12-03 18:22:40 +0000700 refs: List[uprev_lib.GitRef],
Alex Klein1699fab2022-09-08 08:46:06 -0600701 chroot: "chroot_lib.Chroot",
702) -> "uprev_lib.UprevVersionedPackageResult":
703 """Updates lacros ebuilds in parallel with ash-chrome.
Julio Hurtado870ed322021-12-03 18:22:40 +0000704
Alex Kleinfee86da2023-01-20 18:40:06 -0700705 This handler is going to be used temporarily while lacros transitions to
706 being uprevved atomically with ash-chrome. Unlike a standalone lacros uprev,
707 this handler will not need to look at the QA qualified file. Rather, it will
Alex Klein1699fab2022-09-08 08:46:06 -0600708 function identical to ash-chrome using git tags.
Julio Hurtado870ed322021-12-03 18:22:40 +0000709
Alex Klein1699fab2022-09-08 08:46:06 -0600710 See: uprev_versioned_package.
Julio Hurtado870ed322021-12-03 18:22:40 +0000711
Alex Klein1699fab2022-09-08 08:46:06 -0600712 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600713 UprevVersionedPackageResult: The result.
Alex Klein1699fab2022-09-08 08:46:06 -0600714 """
715 result = uprev_lib.UprevVersionedPackageResult()
716 path = os.path.join(
717 constants.CHROMIUMOS_OVERLAY_DIR, "chromeos-base", "chromeos-lacros"
718 )
719 lacros_version = uprev_lib.get_version_from_refs(refs)
720 uprev_result = uprev_lib.uprev_workon_ebuild_to_version(
721 path, lacros_version, chroot, allow_downrev=False
722 )
Julio Hurtado870ed322021-12-03 18:22:40 +0000723
Alex Klein1699fab2022-09-08 08:46:06 -0600724 if not uprev_result:
725 return result
726
727 result.add_result(lacros_version, uprev_result.changed_files)
Julio Hurtado870ed322021-12-03 18:22:40 +0000728 return result
729
Julio Hurtado870ed322021-12-03 18:22:40 +0000730
Alex Klein1699fab2022-09-08 08:46:06 -0600731@uprevs_versioned_package("app-emulation/parallels-desktop")
Patrick Meiring5897add2020-09-16 16:30:17 +1000732def uprev_parallels_desktop(_build_targets, _refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600733 """Updates Parallels Desktop ebuild - app-emulation/parallels-desktop.
Patrick Meiring5897add2020-09-16 16:30:17 +1000734
Alex Klein1699fab2022-09-08 08:46:06 -0600735 See: uprev_versioned_package
Patrick Meiring5897add2020-09-16 16:30:17 +1000736
Alex Klein1699fab2022-09-08 08:46:06 -0600737 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600738 UprevVersionedPackageResult: The result.
Alex Klein1699fab2022-09-08 08:46:06 -0600739 """
740 package = "parallels-desktop"
741 package_path = os.path.join(
742 constants.CHROMEOS_PARTNER_OVERLAY_DIR, "app-emulation", package
743 )
744 version_pin_src_path = _get_version_pin_src_path(package_path)
Patrick Meiring5897add2020-09-16 16:30:17 +1000745
Alex Klein1699fab2022-09-08 08:46:06 -0600746 # Expect a JSON blob like the following:
747 # {
748 # "version": "1.2.3",
749 # "test_image": { "url": "...", "size": 12345678,
750 # "sha256sum": "<32 bytes of hexadecimal>" }
751 # }
Mike Frysinger81a98062023-02-24 15:42:04 -0500752 with open(version_pin_src_path, "r", encoding="utf-8") as f:
Alex Klein1699fab2022-09-08 08:46:06 -0600753 pinned = json.load(f)
Patrick Meiring5897add2020-09-16 16:30:17 +1000754
Alex Klein1699fab2022-09-08 08:46:06 -0600755 if "version" not in pinned or "test_image" not in pinned:
756 raise UprevError(
Trent Aptedb18e36f2023-05-10 15:06:38 +1000757 "VERSION-PIN for %s missing version and/or test_image field"
758 % package
Alex Klein1699fab2022-09-08 08:46:06 -0600759 )
Patrick Meiring5897add2020-09-16 16:30:17 +1000760
Alex Klein1699fab2022-09-08 08:46:06 -0600761 version = pinned["version"]
762 if not isinstance(version, str):
763 raise UprevError("version in VERSION-PIN for %s not a string" % package)
Patrick Meiring5897add2020-09-16 16:30:17 +1000764
Alex Klein1699fab2022-09-08 08:46:06 -0600765 # Update the ebuild.
766 result = uprev_lib.uprev_ebuild_from_pin(package_path, version, chroot)
Patrick Meiring5897add2020-09-16 16:30:17 +1000767
Alex Klein1699fab2022-09-08 08:46:06 -0600768 # Update the VM image used for testing.
769 test_image_path = (
Timothy Loh0cdf15d2023-06-19 17:54:22 +1000770 "src/platform/tast-tests-private/"
Dmitry Torokhov257eba12023-05-30 13:42:48 -0700771 "src/go.chromium.org/tast-tests-private/crosint/"
Alex Klein1699fab2022-09-08 08:46:06 -0600772 "local/bundles/crosint/pita/data/"
773 "pluginvm_image.zip.external"
774 )
775 test_image_src_path = os.path.join(constants.SOURCE_ROOT, test_image_path)
Mike Frysinger81a98062023-02-24 15:42:04 -0500776 with open(test_image_src_path, "w", encoding="utf-8") as f:
Alex Klein1699fab2022-09-08 08:46:06 -0600777 json.dump(pinned["test_image"], f, indent=2)
778 result.add_result(version, [test_image_src_path])
Patrick Meiring5897add2020-09-16 16:30:17 +1000779
Alex Klein1699fab2022-09-08 08:46:06 -0600780 return result
Trent Beginaf51f1b2020-03-09 17:35:31 -0600781
782
Alex Klein1699fab2022-09-08 08:46:06 -0600783@uprevs_versioned_package("chromeos-base/chromeos-dtc-vm")
Trent Beginaf51f1b2020-03-09 17:35:31 -0600784def uprev_sludge(_build_targets, _refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600785 """Updates sludge VM - chromeos-base/chromeos-dtc-vm.
Trent Begin315d9d92019-12-03 21:55:53 -0700786
Alex Klein1699fab2022-09-08 08:46:06 -0600787 See: uprev_versioned_package.
788 """
789 package = "chromeos-dtc-vm"
790 package_path = os.path.join(
791 "src",
792 "private-overlays",
793 "project-wilco-private",
794 "chromeos-base",
795 package,
796 )
797 version_pin_src_path = _get_version_pin_src_path(package_path)
798 version_no_rev = osutils.ReadFile(version_pin_src_path).strip()
Trent Begin315d9d92019-12-03 21:55:53 -0700799
Alex Klein1699fab2022-09-08 08:46:06 -0600800 return uprev_lib.uprev_ebuild_from_pin(package_path, version_no_rev, chroot)
Trent Begin315d9d92019-12-03 21:55:53 -0700801
802
Alex Klein1699fab2022-09-08 08:46:06 -0600803@uprevs_versioned_package("chromeos-base/borealis-dlc")
David Riley8513c1f2021-10-14 17:07:41 -0700804def uprev_borealis_dlc(_build_targets, _refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600805 """Updates shared borealis-dlc ebuild - chromeos-base/borealis-dlc.
David Riley8513c1f2021-10-14 17:07:41 -0700806
Alex Klein1699fab2022-09-08 08:46:06 -0600807 See: uprev_versioned_package.
808 """
809 package_path = os.path.join(
810 "src",
811 "private-overlays",
812 "chromeos-partner-overlay",
813 "chromeos-base",
814 "borealis-dlc",
815 )
David Riley8513c1f2021-10-14 17:07:41 -0700816
Alex Klein1699fab2022-09-08 08:46:06 -0600817 version_pin_src_path = _get_version_pin_src_path(package_path)
818 version_no_rev = osutils.ReadFile(version_pin_src_path).strip()
David Riley8513c1f2021-10-14 17:07:41 -0700819
Alex Klein1699fab2022-09-08 08:46:06 -0600820 return uprev_lib.uprev_ebuild_from_pin(package_path, version_no_rev, chroot)
David Riley8513c1f2021-10-14 17:07:41 -0700821
822
Po-Hsien Wang92fb1e52023-04-27 11:35:04 -0700823@uprevs_versioned_package("chromeos-base/borealis-dlc-nvidia")
824def uprev_borealis_dlc_nvidia(_build_targets, _refs, chroot):
Trent Apted380d1c62023-05-16 09:19:31 +1000825 """Updates shared chromeos-base/borealis-dlc-nvidia ebuild.
Po-Hsien Wang92fb1e52023-04-27 11:35:04 -0700826
827 See: uprev_versioned_package.
828 """
829 package_path = os.path.join(
830 "src",
831 "private-overlays",
832 "chromeos-partner-overlay",
833 "chromeos-base",
834 "borealis-dlc-nvidia",
835 )
836
837 version_pin_src_path = _get_version_pin_src_path(package_path)
838 version_no_rev = osutils.ReadFile(version_pin_src_path).strip()
839
840 return uprev_lib.uprev_ebuild_from_pin(package_path, version_no_rev, chroot)
841
842
Syed Faaiz Hussainef29cf82023-06-06 22:14:07 -0700843@uprevs_versioned_package("chromeos-base/borealis-dlc-chroot")
844def uprev_borealis_dlc_chroot(_build_targets, _refs, chroot):
Sergey Frolov8eb38c12023-06-21 19:17:17 -0600845 """Updates shared chromeos-base/borealis-dlc-chroot ebuild.
Syed Faaiz Hussainef29cf82023-06-06 22:14:07 -0700846
847 See: uprev_versioned_package.
848 """
849 package_path = os.path.join(
850 "src",
851 "private-overlays",
852 "chromeos-partner-overlay",
853 "chromeos-base",
854 "borealis-dlc-chroot",
855 )
856
857 version_pin_src_path = _get_version_pin_src_path(package_path)
858 version_no_rev = osutils.ReadFile(version_pin_src_path).strip()
859
860 return uprev_lib.uprev_ebuild_from_pin(package_path, version_no_rev, chroot)
861
862
Patrick Meiring5897add2020-09-16 16:30:17 +1000863def _get_version_pin_src_path(package_path):
Alex Klein1699fab2022-09-08 08:46:06 -0600864 """Returns the path to the VERSION-PIN file for the given package."""
865 return os.path.join(constants.SOURCE_ROOT, package_path, "VERSION-PIN")
Patrick Meiring5897add2020-09-16 16:30:17 +1000866
867
Alex Klein87531182019-08-12 15:23:37 -0600868@uprevs_versioned_package(constants.CHROME_CP)
Alex Klein4e839252022-01-06 13:29:18 -0700869def uprev_chrome_from_ref(build_targets, refs, _chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600870 """Uprev chrome and its related packages.
Alex Klein87531182019-08-12 15:23:37 -0600871
Alex Klein1699fab2022-09-08 08:46:06 -0600872 See: uprev_versioned_package.
873 """
Alex Kleinfee86da2023-01-20 18:40:06 -0700874 # Determine the version from the refs (tags), i.e. the chrome versions are
875 # the tag names.
Alex Klein1699fab2022-09-08 08:46:06 -0600876 chrome_version = uprev_lib.get_version_from_refs(refs)
877 logging.debug("Chrome version determined from refs: %s", chrome_version)
Alex Klein87531182019-08-12 15:23:37 -0600878
Alex Klein1699fab2022-09-08 08:46:06 -0600879 return uprev_chrome(chrome_version, build_targets, None)
Alex Kleinf69bd802021-06-22 15:43:49 -0600880
881
Alex Klein9ce3f682021-06-23 15:06:44 -0600882def revbump_chrome(
Alex Klein1699fab2022-09-08 08:46:06 -0600883 build_targets: List["build_target_lib.BuildTarget"] = None,
884 chroot: Optional["chroot_lib.Chroot"] = None,
Alex Klein9ce3f682021-06-23 15:06:44 -0600885) -> uprev_lib.UprevVersionedPackageResult:
Alex Klein1699fab2022-09-08 08:46:06 -0600886 """Attempt to revbump chrome.
Alex Kleinf69bd802021-06-22 15:43:49 -0600887
Alex Klein1699fab2022-09-08 08:46:06 -0600888 Revbumps are done by executing an uprev using the current stable version.
889 E.g. if chrome is on 1.2.3.4 and has a 1.2.3.4_rc-r2.ebuild, performing an
890 uprev on version 1.2.3.4 when there are applicable changes (e.g. to the 9999
891 ebuild) will result in a revbump to 1.2.3.4_rc-r3.ebuild.
892 """
893 chrome_version = uprev_lib.get_stable_chrome_version()
894 return uprev_chrome(chrome_version, build_targets, chroot)
Alex Kleinf69bd802021-06-22 15:43:49 -0600895
896
Alex Klein9ce3f682021-06-23 15:06:44 -0600897def uprev_chrome(
Alex Klein16ea1b32021-10-01 15:48:50 -0600898 chrome_version: str,
Alex Klein1699fab2022-09-08 08:46:06 -0600899 build_targets: Optional[List["build_target_lib.BuildTarget"]],
900 chroot: Optional["chroot_lib.Chroot"],
Alex Klein9ce3f682021-06-23 15:06:44 -0600901) -> uprev_lib.UprevVersionedPackageResult:
Alex Klein1699fab2022-09-08 08:46:06 -0600902 """Attempt to uprev chrome and its related packages to the given version."""
903 uprev_manager = uprev_lib.UprevChromeManager(
904 chrome_version, build_targets=build_targets, chroot=chroot
905 )
906 result = uprev_lib.UprevVersionedPackageResult()
907 # TODO(crbug.com/1080429): Handle all possible outcomes of a Chrome uprev
908 # attempt. The expected behavior is documented in the following table:
909 #
910 # Outcome of Chrome uprev attempt:
911 # NEWER_VERSION_EXISTS:
912 # Do nothing.
913 # SAME_VERSION_EXISTS or REVISION_BUMP:
914 # Uprev followers
915 # Assert not VERSION_BUMP (any other outcome is fine)
916 # VERSION_BUMP or NEW_EBUILD_CREATED:
917 # Uprev followers
918 # Assert that Chrome & followers are at same package version
Alex Klein0b2ec2d2021-06-23 15:56:45 -0600919
Alex Klein1699fab2022-09-08 08:46:06 -0600920 # Start with chrome itself so we can proceed accordingly.
921 chrome_result = uprev_manager.uprev(constants.CHROME_CP)
922 if chrome_result.newer_version_exists:
923 # Cannot use the given version (newer version already exists).
924 return result
925
926 # Also uprev related packages.
927 for package in constants.OTHER_CHROME_PACKAGES:
928 follower_result = uprev_manager.uprev(package)
929 if chrome_result.stable_version and follower_result.version_bump:
930 logging.warning(
931 "%s had a version bump, but no more than a revision bump "
932 "should have been possible.",
933 package,
934 )
935
936 if uprev_manager.modified_ebuilds:
937 # Record changes when we have them.
938 return result.add_result(chrome_version, uprev_manager.modified_ebuilds)
939
David Burger37f48672019-09-18 17:07:56 -0600940 return result
Alex Klein87531182019-08-12 15:23:37 -0600941
Alex Klein87531182019-08-12 15:23:37 -0600942
Alex Klein1699fab2022-09-08 08:46:06 -0600943def _get_latest_version_from_refs(
944 refs_prefix: str, refs: List[uprev_lib.GitRef]
945) -> str:
946 """Get the latest version from refs
Alex Klein0b2ec2d2021-06-23 15:56:45 -0600947
Alex Klein1699fab2022-09-08 08:46:06 -0600948 Versions are compared using |distutils.version.LooseVersion| and
949 the latest version is returned.
Alex Klein87531182019-08-12 15:23:37 -0600950
Alex Klein1699fab2022-09-08 08:46:06 -0600951 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600952 refs_prefix: The refs prefix of the tag format.
953 refs: The tags to parse for the latest version.
Alex Klein87531182019-08-12 15:23:37 -0600954
Alex Klein1699fab2022-09-08 08:46:06 -0600955 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600956 The latest version to use as string.
Alex Klein1699fab2022-09-08 08:46:06 -0600957 """
958 valid_refs = []
959 for gitiles in refs:
960 if gitiles.ref.startswith(refs_prefix):
961 valid_refs.append(gitiles.ref)
Ben Reiche779cf42020-12-15 03:21:31 +0000962
Alex Klein1699fab2022-09-08 08:46:06 -0600963 if not valid_refs:
964 return None
Ben Reiche779cf42020-12-15 03:21:31 +0000965
Alex Klein1699fab2022-09-08 08:46:06 -0600966 # Sort by version and take the latest version.
967 target_version_ref = sorted(valid_refs, key=LooseVersion, reverse=True)[0]
968 return target_version_ref.replace(refs_prefix, "")
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800969
970
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700971def _generate_platform_c_files(
972 replication_config: replication_config_pb2.ReplicationConfig,
Alex Klein1699fab2022-09-08 08:46:06 -0600973 chroot: "chroot_lib.Chroot",
974) -> List[str]:
975 """Generates platform C files from a platform JSON payload.
Andrew Lamb9563a152019-12-04 11:42:18 -0700976
Alex Klein1699fab2022-09-08 08:46:06 -0600977 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600978 replication_config: A ReplicationConfig that has already been run. If it
979 produced a build_config.json file, that file will be used to
980 generate platform C files. Otherwise, nothing will be generated.
981 chroot: The chroot to use to generate.
Andrew Lamb9563a152019-12-04 11:42:18 -0700982
Alex Klein1699fab2022-09-08 08:46:06 -0600983 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600984 A list of generated files.
Alex Klein1699fab2022-09-08 08:46:06 -0600985 """
986 # Generate the platform C files from the build config. Note that it would be
987 # more intuitive to generate the platform C files from the platform config;
Alex Kleinfee86da2023-01-20 18:40:06 -0700988 # however, cros_config_schema does not allow this, because the platform
989 # config payload is not always valid input. For example, if a property is
990 # both 'required' and 'build-only', it will fail schema validation. Thus,
991 # use the build config, and use '-f' to filter.
Alex Klein1699fab2022-09-08 08:46:06 -0600992 build_config_path = [
993 rule.destination_path
994 for rule in replication_config.file_replication_rules
995 if rule.destination_path.endswith("build_config.json")
996 ]
Andrew Lamb9563a152019-12-04 11:42:18 -0700997
Alex Klein1699fab2022-09-08 08:46:06 -0600998 if not build_config_path:
999 logging.info(
1000 "No build_config.json found, will not generate platform C files. "
1001 "Replication config: %s",
1002 replication_config,
1003 )
1004 return []
Andrew Lamb9563a152019-12-04 11:42:18 -07001005
Alex Klein1699fab2022-09-08 08:46:06 -06001006 if len(build_config_path) > 1:
1007 raise ValueError(
1008 "Expected at most one build_config.json destination path. "
1009 "Replication config: %s" % replication_config
1010 )
Andrew Lamb9563a152019-12-04 11:42:18 -07001011
Alex Klein1699fab2022-09-08 08:46:06 -06001012 build_config_path = build_config_path[0]
Andrew Lamb9563a152019-12-04 11:42:18 -07001013
Alex Klein1699fab2022-09-08 08:46:06 -06001014 # Paths to the build_config.json and dir to output C files to, in the
1015 # chroot.
1016 build_config_chroot_path = os.path.join(
1017 constants.CHROOT_SOURCE_ROOT, build_config_path
1018 )
1019 generated_output_chroot_dir = os.path.join(
1020 constants.CHROOT_SOURCE_ROOT, os.path.dirname(build_config_path)
1021 )
Andrew Lamb9563a152019-12-04 11:42:18 -07001022
Alex Klein1699fab2022-09-08 08:46:06 -06001023 command = [
1024 "cros_config_schema",
1025 "-m",
1026 build_config_chroot_path,
1027 "-g",
1028 generated_output_chroot_dir,
1029 "-f",
1030 '"TRUE"',
1031 ]
Andrew Lamb9563a152019-12-04 11:42:18 -07001032
Brian Norris3a585d22023-07-19 16:26:11 -07001033 chroot.run(command)
Andrew Lamb9563a152019-12-04 11:42:18 -07001034
Alex Klein1699fab2022-09-08 08:46:06 -06001035 # A relative (to the source root) path to the generated C files.
1036 generated_output_dir = os.path.dirname(build_config_path)
1037 generated_files = []
1038 expected_c_files = ["config.c", "ec_config.c", "ec_config.h"]
1039 for f in expected_c_files:
1040 if os.path.exists(
1041 os.path.join(constants.SOURCE_ROOT, generated_output_dir, f)
1042 ):
1043 generated_files.append(os.path.join(generated_output_dir, f))
Andrew Lamb9563a152019-12-04 11:42:18 -07001044
Alex Klein1699fab2022-09-08 08:46:06 -06001045 if len(expected_c_files) != len(generated_files):
1046 raise GeneratedCrosConfigFilesError(expected_c_files, generated_files)
Andrew Lamb9563a152019-12-04 11:42:18 -07001047
Alex Klein1699fab2022-09-08 08:46:06 -06001048 return generated_files
Andrew Lamb9563a152019-12-04 11:42:18 -07001049
1050
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001051def _get_private_overlay_package_root(ref: uprev_lib.GitRef, package: str):
Alex Klein1699fab2022-09-08 08:46:06 -06001052 """Returns the absolute path to the root of a given private overlay.
Andrew Lambe836f222019-12-09 12:27:38 -07001053
Alex Klein1699fab2022-09-08 08:46:06 -06001054 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001055 ref: GitRef for the private overlay.
1056 package: Path to the package in the overlay.
Alex Klein1699fab2022-09-08 08:46:06 -06001057 """
1058 # There might be a cleaner way to map from package -> path within the source
1059 # tree. For now, just use string patterns.
1060 private_overlay_ref_pattern = (
1061 r"/chromeos\/overlays\/overlay-([\w-]+)-private"
1062 )
1063 match = re.match(private_overlay_ref_pattern, ref.path)
1064 if not match:
1065 raise ValueError(
1066 "ref.path must match the pattern: %s. Actual ref: %s"
1067 % (private_overlay_ref_pattern, ref)
1068 )
Andrew Lambe836f222019-12-09 12:27:38 -07001069
Alex Klein1699fab2022-09-08 08:46:06 -06001070 overlay = match.group(1)
Andrew Lambe836f222019-12-09 12:27:38 -07001071
Alex Klein1699fab2022-09-08 08:46:06 -06001072 return os.path.join(
1073 constants.SOURCE_ROOT,
1074 "src/private-overlays/overlay-%s-private" % overlay,
1075 package,
1076 )
Andrew Lambe836f222019-12-09 12:27:38 -07001077
1078
Alex Klein1699fab2022-09-08 08:46:06 -06001079@uprevs_versioned_package("chromeos-base/chromeos-config-bsp")
Andrew Lambea9a8a22019-12-12 14:03:43 -07001080def replicate_private_config(_build_targets, refs, chroot):
Alex Kleinfee86da2023-01-20 18:40:06 -07001081 """Replicate private cros_config change to the corresponding public config.
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001082
Alex Klein1699fab2022-09-08 08:46:06 -06001083 See uprev_versioned_package for args
1084 """
1085 package = "chromeos-base/chromeos-config-bsp"
Andrew Lambea9a8a22019-12-12 14:03:43 -07001086
Alex Klein1699fab2022-09-08 08:46:06 -06001087 if len(refs) != 1:
1088 raise ValueError("Expected exactly one ref, actual %s" % refs)
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001089
Alex Klein1699fab2022-09-08 08:46:06 -06001090 # Expect a replication_config.jsonpb in the package root.
1091 package_root = _get_private_overlay_package_root(refs[0], package)
1092 replication_config_path = os.path.join(
1093 package_root, "replication_config.jsonpb"
1094 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001095
Alex Klein1699fab2022-09-08 08:46:06 -06001096 try:
1097 replication_config = json_format.Parse(
1098 osutils.ReadFile(replication_config_path),
1099 replication_config_pb2.ReplicationConfig(),
1100 )
1101 except IOError:
1102 raise ValueError(
1103 "Expected ReplicationConfig missing at %s" % replication_config_path
1104 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001105
Alex Klein1699fab2022-09-08 08:46:06 -06001106 replication_lib.Replicate(replication_config)
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001107
Alex Klein1699fab2022-09-08 08:46:06 -06001108 modified_files = [
1109 rule.destination_path
1110 for rule in replication_config.file_replication_rules
1111 ]
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001112
Alex Kleinfee86da2023-01-20 18:40:06 -07001113 # The generated platform C files are not easily filtered by replication
1114 # rules, i.e. JSON / proto filtering can be described by a FieldMask,
1115 # arbitrary C files cannot. Therefore, replicate and filter the JSON
1116 # payloads, and then generate filtered C files from the JSON payload.
Alex Klein1699fab2022-09-08 08:46:06 -06001117 modified_files.extend(
1118 _generate_platform_c_files(replication_config, chroot)
1119 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001120
Alex Klein1699fab2022-09-08 08:46:06 -06001121 # Use the private repo's commit hash as the new version.
1122 new_private_version = refs[0].revision
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001123
Alex Klein1699fab2022-09-08 08:46:06 -06001124 # modified_files should contain only relative paths at this point, but the
1125 # returned UprevVersionedPackageResult must contain only absolute paths.
1126 for i, modified_file in enumerate(modified_files):
1127 assert not os.path.isabs(modified_file)
1128 modified_files[i] = os.path.join(constants.SOURCE_ROOT, modified_file)
Andrew Lamb988f4da2019-12-10 10:16:43 -07001129
Alex Klein1699fab2022-09-08 08:46:06 -06001130 return uprev_lib.UprevVersionedPackageResult().add_result(
1131 new_private_version, modified_files
1132 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001133
1134
Alex Klein1699fab2022-09-08 08:46:06 -06001135@uprevs_versioned_package("chromeos-base/crosvm")
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001136def uprev_crosvm(_build_targets, refs, _chroot):
Alex Klein1699fab2022-09-08 08:46:06 -06001137 """Updates crosvm ebuilds to latest revision
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001138
Alex Klein1699fab2022-09-08 08:46:06 -06001139 crosvm is not versioned. We are updating to the latest commit on the main
1140 branch.
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001141
Alex Klein1699fab2022-09-08 08:46:06 -06001142 See: uprev_versioned_package.
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001143
Alex Klein1699fab2022-09-08 08:46:06 -06001144 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001145 UprevVersionedPackageResult: The result of updating crosvm ebuilds.
Alex Klein1699fab2022-09-08 08:46:06 -06001146 """
1147 overlay = os.path.join(
1148 constants.SOURCE_ROOT, constants.CHROMIUMOS_OVERLAY_DIR
1149 )
1150 repo_path = os.path.join(constants.SOURCE_ROOT, "src", "crosvm")
1151 manifest = git.ManifestCheckout.Cached(repo_path)
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001152
Alex Klein1699fab2022-09-08 08:46:06 -06001153 uprev_manager = uprev_lib.UprevOverlayManager([overlay], manifest)
1154 uprev_manager.uprev(
1155 package_list=[
1156 "chromeos-base/crosvm",
1157 "dev-rust/assertions",
1158 "dev-rust/cros_async",
1159 "dev-rust/cros_fuzz",
1160 "dev-rust/data_model",
1161 "dev-rust/enumn",
1162 "dev-rust/io_uring",
1163 "dev-rust/p9",
1164 "dev-rust/sync",
1165 "dev-rust/sys_util",
1166 "dev-rust/tempfile",
1167 "media-sound/audio_streams",
1168 ],
1169 force=True,
1170 )
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001171
Alex Klein1699fab2022-09-08 08:46:06 -06001172 updated_files = uprev_manager.modified_ebuilds
1173 result = uprev_lib.UprevVersionedPackageResult()
1174 result.add_result(refs[0].revision, updated_files)
1175 return result
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001176
1177
Yi Choua4854ac2022-11-14 10:54:24 +08001178@uprevs_versioned_package("chromeos-base/ti50-emulator")
1179def uprev_ti50_emulator(_build_targets, refs, _chroot):
1180 """Updates ti50-emulator ebuilds to latest revision
1181
1182 ti50-emulator is not versioned. We are updating to the latest commit on the
1183 main branch.
1184
1185 See: uprev_versioned_package.
1186
1187 Returns:
1188 UprevVersionedPackageResult: The result of updating ti50-emulator
Trent Aptedb18e36f2023-05-10 15:06:38 +10001189 ebuild.
Yi Choua4854ac2022-11-14 10:54:24 +08001190 """
1191 overlay = os.path.join(
1192 constants.SOURCE_ROOT, constants.CHROMEOS_OVERLAY_DIR
1193 )
1194
1195 # The ti50-emulator will touch multiple repos.
1196 manifest = git.ManifestCheckout.Cached(constants.SOURCE_ROOT)
1197
1198 uprev_manager = uprev_lib.UprevOverlayManager([overlay], manifest)
1199 uprev_manager.uprev(
1200 package_list=["chromeos-base/ti50-emulator"],
1201 force=True,
1202 )
1203
1204 updated_files = uprev_manager.modified_ebuilds
1205 result = uprev_lib.UprevVersionedPackageResult()
1206 result.add_result(refs[-1].revision, updated_files)
1207 return result
1208
1209
Jeremy Bettisaf96afb2023-01-11 16:09:58 -07001210@uprevs_versioned_package("chromeos-base/ec-devutils")
Jeremy Bettis0186d252023-01-19 14:47:46 -07001211def uprev_ecdevutils(_build_targets, refs, _chroot):
1212 """Updates ec-devutils ebuilds to latest revision
1213
Alex Kleinfee86da2023-01-20 18:40:06 -07001214 ec-devutils is not versioned. We are updating to the latest commit on the
1215 main branch.
Jeremy Bettis0186d252023-01-19 14:47:46 -07001216
1217 See: uprev_versioned_package.
1218
1219 Returns:
1220 UprevVersionedPackageResult: The result of updating ec-devutils ebuilds.
1221 """
1222 overlay = os.path.join(
1223 constants.SOURCE_ROOT, constants.CHROMIUMOS_OVERLAY_DIR
1224 )
1225 repo_path = os.path.join(constants.SOURCE_ROOT, "src", "platform", "ec")
1226 manifest = git.ManifestCheckout.Cached(repo_path)
1227
1228 uprev_manager = uprev_lib.UprevOverlayManager([overlay], manifest)
1229 uprev_manager.uprev(
1230 package_list=[
1231 "chromeos-base/ec-devutils",
1232 ],
1233 force=True,
1234 )
1235
1236 updated_files = uprev_manager.modified_ebuilds
1237 result = uprev_lib.UprevVersionedPackageResult()
1238 result.add_result(refs[0].revision, updated_files)
1239 return result
1240
1241
Jeremy Bettisaf96afb2023-01-11 16:09:58 -07001242@uprevs_versioned_package("chromeos-base/ec-utils")
Jeremy Bettisaf96afb2023-01-11 16:09:58 -07001243def uprev_ecutils(_build_targets, refs, _chroot):
1244 """Updates ec-utils ebuilds to latest revision
1245
1246 ec-utils is not versioned. We are updating to the latest commit on the main
1247 branch.
1248
1249 See: uprev_versioned_package.
1250
1251 Returns:
1252 UprevVersionedPackageResult: The result of updating ec-utils ebuilds.
1253 """
1254 overlay = os.path.join(
1255 constants.SOURCE_ROOT, constants.CHROMIUMOS_OVERLAY_DIR
1256 )
1257 repo_path = os.path.join(constants.SOURCE_ROOT, "src", "platform", "ec")
1258 manifest = git.ManifestCheckout.Cached(repo_path)
1259
1260 uprev_manager = uprev_lib.UprevOverlayManager([overlay], manifest)
1261 uprev_manager.uprev(
1262 package_list=[
Jeremy Bettisaf96afb2023-01-11 16:09:58 -07001263 "chromeos-base/ec-utils",
Jeremy Bettis0186d252023-01-19 14:47:46 -07001264 ],
1265 force=True,
1266 )
1267
1268 updated_files = uprev_manager.modified_ebuilds
1269 result = uprev_lib.UprevVersionedPackageResult()
1270 result.add_result(refs[0].revision, updated_files)
1271 return result
1272
1273
1274@uprevs_versioned_package("chromeos-base/ec-utils-test")
1275def uprev_ecutilstest(_build_targets, refs, _chroot):
1276 """Updates ec-utils-test ebuilds to latest revision
1277
Alex Kleinfee86da2023-01-20 18:40:06 -07001278 ec-utils-test is not versioned. We are updating to the latest commit on the
1279 main branch.
Jeremy Bettis0186d252023-01-19 14:47:46 -07001280
1281 See: uprev_versioned_package.
1282
1283 Returns:
Alex Kleinfee86da2023-01-20 18:40:06 -07001284 UprevVersionedPackageResult: The result of updating ec-utils-test
Trent Aptedb18e36f2023-05-10 15:06:38 +10001285 ebuilds.
Jeremy Bettis0186d252023-01-19 14:47:46 -07001286 """
1287 overlay = os.path.join(
1288 constants.SOURCE_ROOT, constants.CHROMIUMOS_OVERLAY_DIR
1289 )
1290 repo_path = os.path.join(constants.SOURCE_ROOT, "src", "platform", "ec")
1291 manifest = git.ManifestCheckout.Cached(repo_path)
1292
1293 uprev_manager = uprev_lib.UprevOverlayManager([overlay], manifest)
1294 uprev_manager.uprev(
1295 package_list=[
Jeremy Bettisaf96afb2023-01-11 16:09:58 -07001296 "chromeos-base/ec-utils-test",
1297 ],
1298 force=True,
1299 )
1300
1301 updated_files = uprev_manager.modified_ebuilds
1302 result = uprev_lib.UprevVersionedPackageResult()
1303 result.add_result(refs[0].revision, updated_files)
1304 return result
1305
1306
Luigi Semenzatoea25fe82023-08-03 17:11:25 -07001307def starbase_find_and_uprev(package_path: str, tarfile_name: str) -> List[str]:
1308 """Updates and uprevs the starbase artifacts ebuild.
1309
1310 This is factored out of uprev_starbase_artifacts for unit testing.
1311 """
1312
1313 # Find ebuild.
1314 ebuild_pattern = r"starbase-artifacts-(\d+\.\d+\.\d+)-r(\d+).ebuild$"
1315 ebuild_name: str
1316 ebuild_version: str
1317 ebuild_revision: int
1318 for file_or_dir_path in osutils.DirectoryIterator(package_path):
1319 file_or_dir = str(file_or_dir_path)
1320 m = re.search(ebuild_pattern, file_or_dir)
1321 if m:
1322 ebuild_name = file_or_dir
1323 ebuild_version = m.group(1)
1324 ebuild_revision = int(m.group(2))
1325 break
1326 else:
1327 raise Error("cannot find ebuild in %s" % package_path)
1328
1329 # Check that the fake git refs is as expected.
1330 tarfile_pattern = r"^starbase-artifacts-\d{8}-rc\d{3}.tar.zst$"
1331 if not re.match(tarfile_pattern, tarfile_name):
1332 raise ValueError(
1333 "Pattern %s doesn't match fake git ref %s" % tarfile_pattern,
1334 tarfile_name,
1335 )
1336
1337 # Change SRC_URI in ebuild.
1338 lines = []
1339 found = False
1340 old_ebuild_path = os.path.join(package_path, ebuild_name)
1341 for line in osutils.ReadText(old_ebuild_path).splitlines():
1342 if line.startswith("SRC_URI="):
1343 line = 'SRC_URI="${DISTFILES}/%s"' % tarfile_name
1344 found = True
1345 lines.append(line)
1346 if not found:
1347 raise Error("SRC_URI not found in ebuild %s" % ebuild_name)
1348
1349 new_revision = ebuild_revision + 1
1350 new_ebuild_name = "starbase-artifacts-%s-r%s.ebuild" % (
1351 ebuild_version,
1352 new_revision,
1353 )
1354 new_ebuild_path = os.path.join(package_path, new_ebuild_name)
1355 osutils.WriteFile(new_ebuild_path, "\n".join(lines) + "\n")
1356 osutils.SafeUnlink(old_ebuild_path)
1357
1358 # Update Manifest
1359 portage_util.UpdateEbuildManifest(package_path)
1360
1361 manifest_path = os.path.join(package_path, "Manifest")
1362 modified_files = [manifest_path, old_ebuild_path, new_ebuild_path]
1363
1364 return modified_files
1365
1366
1367@uprevs_versioned_package("chromeos-base/starbase-artifacts")
1368def uprev_starbase_artifacts(
1369 _build_targets: List["build_target_lib.BuildTarget"],
1370 refs: List[uprev_lib.GitRef],
1371 _chroot: "chroot_lib.Chroot",
1372) -> uprev_lib.UprevVersionedPackageResult:
1373 """Updates the starbase-artifacts ebuild to fetch latest tar file.
1374
1375 The Rapid workflow that builds a new version of the starbase artifacts tar
1376 file and uploads it to chromeos-localmirror-private also triggers this
1377 uprev, so that the next CrOS build can pick up the new artifacts.
1378
1379 See: uprev_versioned_package.
1380
1381 Returns:
1382 UprevVersionedPackageResult: The result of updating this ebuild.
1383 """
1384 package_path = str(
1385 constants.SOURCE_ROOT.joinpath(
1386 "private-overlays",
1387 "overlay-midna-private",
1388 "chromeos-base",
1389 "starbase-artifacts",
1390 )
1391 )
1392
1393 modified_files = starbase_find_and_uprev(package_path, refs[0].ref)
1394 result = uprev_lib.UprevVersionedPackageResult()
1395 result.add_result(refs[0].revision, modified_files)
1396 return result
1397
1398
Alex Klein5caab872021-09-10 11:44:37 -06001399def get_best_visible(
Alex Klein1699fab2022-09-08 08:46:06 -06001400 atom: str, build_target: Optional["build_target_lib.BuildTarget"] = None
Alex Klein5caab872021-09-10 11:44:37 -06001401) -> package_info.PackageInfo:
Alex Klein1699fab2022-09-08 08:46:06 -06001402 """Returns the best visible CPV for the given atom.
Alex Kleinbbef2b32019-08-27 10:38:50 -06001403
Alex Klein1699fab2022-09-08 08:46:06 -06001404 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001405 atom: The atom to look up.
1406 build_target: The build target whose sysroot should be searched, or the
1407 SDK if not provided.
Alex Kleinad6b48a2020-01-08 16:57:41 -07001408
Alex Klein1699fab2022-09-08 08:46:06 -06001409 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001410 The best visible package, or None if none are visible.
Alex Klein1699fab2022-09-08 08:46:06 -06001411 """
1412 assert atom
Alex Kleinbbef2b32019-08-27 10:38:50 -06001413
Alex Klein1699fab2022-09-08 08:46:06 -06001414 return portage_util.PortageqBestVisible(
1415 atom,
1416 board=build_target.name if build_target else None,
1417 sysroot=build_target.root if build_target else None,
1418 )
Alex Kleinda39c6d2019-09-16 14:36:36 -06001419
1420
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001421def has_prebuilt(
1422 atom: str,
Alex Klein1699fab2022-09-08 08:46:06 -06001423 build_target: "build_target_lib.BuildTarget" = None,
1424 useflags: Union[Iterable[str], str] = None,
1425) -> bool:
1426 """Check if a prebuilt exists.
Alex Kleinda39c6d2019-09-16 14:36:36 -06001427
Alex Klein1699fab2022-09-08 08:46:06 -06001428 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001429 atom: The package whose prebuilt is being queried.
1430 build_target: The build target whose sysroot should be searched, or the
1431 SDK if not provided.
1432 useflags: Any additional USE flags that should be set. May be a string
1433 of properly formatted USE flags, or an iterable of individual flags.
Alex Kleinad6b48a2020-01-08 16:57:41 -07001434
Alex Klein1699fab2022-09-08 08:46:06 -06001435 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001436 True if there is an available prebuilt, False otherwise.
Alex Klein1699fab2022-09-08 08:46:06 -06001437 """
1438 assert atom
Alex Kleinda39c6d2019-09-16 14:36:36 -06001439
Alex Klein1699fab2022-09-08 08:46:06 -06001440 board = build_target.name if build_target else None
1441 extra_env = None
1442 if useflags:
1443 new_flags = useflags
1444 if not isinstance(useflags, str):
1445 new_flags = " ".join(useflags)
Alex Klein149fd3b2019-12-16 16:01:05 -07001446
Alex Klein1699fab2022-09-08 08:46:06 -06001447 existing = os.environ.get("USE", "")
1448 final_flags = "%s %s" % (existing, new_flags)
1449 extra_env = {"USE": final_flags.strip()}
1450 return portage_util.HasPrebuilt(atom, board=board, extra_env=extra_env)
Alex Klein36b117f2019-09-30 15:13:46 -06001451
1452
David Burger0f9dd4e2019-10-08 12:33:42 -06001453def builds(atom, build_target, packages=None):
Alex Klein1699fab2022-09-08 08:46:06 -06001454 """Check if |build_target| builds |atom| (has it in its depgraph)."""
1455 cros_build_lib.AssertInsideChroot()
Alex Klein36b117f2019-09-30 15:13:46 -06001456
Mike Frysinger0e484972023-08-24 11:40:19 -04001457 logging.debug(
1458 "Checking if %s builds %s (packages=%s)", build_target, atom, packages
1459 )
Alex Klein1699fab2022-09-08 08:46:06 -06001460 pkgs = tuple(packages) if packages else None
1461 # TODO(crbug/1081828): Receive and use sysroot.
1462 graph, _sdk_graph = dependency.GetBuildDependency(
1463 build_target.root, build_target.name, pkgs
1464 )
1465 return any(atom in package for package in graph["package_deps"])
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001466
1467
Alex Klein6becabc2020-09-11 14:03:05 -06001468def needs_chrome_source(
Alex Klein1699fab2022-09-08 08:46:06 -06001469 build_target: "build_target_lib.BuildTarget",
Alex Klein6becabc2020-09-11 14:03:05 -06001470 compile_source=False,
1471 packages: Optional[List[package_info.PackageInfo]] = None,
Alex Klein1699fab2022-09-08 08:46:06 -06001472 useflags=None,
1473):
1474 """Check if the chrome source is needed.
Alex Klein6becabc2020-09-11 14:03:05 -06001475
Alex Klein1699fab2022-09-08 08:46:06 -06001476 The chrome source is needed if the build target builds chrome or any of its
1477 follower packages, and can't use a prebuilt for them either because it's not
1478 available, or because we can't use prebuilts because it must build from
1479 source.
1480 """
1481 cros_build_lib.AssertInsideChroot()
Alex Klein6becabc2020-09-11 14:03:05 -06001482
Sergey Frolov8eb38c12023-06-21 19:17:17 -06001483 # Find latest chrome PackageInfo.
1484 try:
1485 chrome_pi = portage_util.PortageqBestVisible(
1486 constants.CHROME_CP, board=build_target.name
1487 )
1488 chrome_cpvr = chrome_pi.cpvr
1489 except (portage_util.NoVisiblePackageError, package_info.ParseTypeError):
1490 chrome_cpvr = constants.CHROME_CP
1491
Alex Klein1699fab2022-09-08 08:46:06 -06001492 # Check if it builds chrome and/or a follower package.
1493 graph = depgraph.get_sysroot_dependency_graph(build_target.root, packages)
1494 builds_chrome = constants.CHROME_CP in graph
1495 builds_follower = {
1496 pkg: pkg in graph for pkg in constants.OTHER_CHROME_PACKAGES
1497 }
Alex Klein6becabc2020-09-11 14:03:05 -06001498
Alex Klein1699fab2022-09-08 08:46:06 -06001499 local_uprev = builds_chrome and revbump_chrome([build_target])
Alex Klein9ce3f682021-06-23 15:06:44 -06001500
Alex Kleinfee86da2023-01-20 18:40:06 -07001501 # When we are compiling source set False since we do not use prebuilts. When
1502 # not compiling from source, start with True, i.e. we have every prebuilt
Alex Klein1699fab2022-09-08 08:46:06 -06001503 # we've checked for up to this point.
1504 has_chrome_prebuilt = not compile_source
1505 has_follower_prebuilts = not compile_source
1506 # Save packages that need prebuilts for reporting.
1507 pkgs_needing_prebuilts = []
1508 if compile_source:
1509 # Need everything.
Sergey Frolov8eb38c12023-06-21 19:17:17 -06001510 pkgs_needing_prebuilts.append(chrome_cpvr)
Alex Klein1699fab2022-09-08 08:46:06 -06001511 pkgs_needing_prebuilts.extend(
1512 [pkg for pkg, builds_pkg in builds_follower.items() if builds_pkg]
1513 )
1514 else:
1515 # Check chrome itself.
1516 if builds_chrome:
1517 has_chrome_prebuilt = has_prebuilt(
Sergey Frolov8eb38c12023-06-21 19:17:17 -06001518 chrome_cpvr,
Alex Klein1699fab2022-09-08 08:46:06 -06001519 build_target=build_target,
1520 useflags=useflags,
1521 )
1522 if not has_chrome_prebuilt:
Sergey Frolov8eb38c12023-06-21 19:17:17 -06001523 pkgs_needing_prebuilts.append(chrome_cpvr)
Alex Klein1699fab2022-09-08 08:46:06 -06001524 # Check follower packages.
1525 for pkg, builds_pkg in builds_follower.items():
1526 if not builds_pkg:
1527 continue
1528 prebuilt = has_prebuilt(
1529 pkg, build_target=build_target, useflags=useflags
1530 )
1531 has_follower_prebuilts &= prebuilt
1532 if not prebuilt:
1533 pkgs_needing_prebuilts.append(pkg)
Alex Kleinfee86da2023-01-20 18:40:06 -07001534 # Postcondition: has_chrome_prebuilt and has_follower_prebuilts now
1535 # correctly reflect whether we actually have the corresponding prebuilts for
1536 # the build.
Alex Klein6becabc2020-09-11 14:03:05 -06001537
Alex Klein1699fab2022-09-08 08:46:06 -06001538 needs_chrome = builds_chrome and not has_chrome_prebuilt
1539 needs_follower = (
1540 any(builds_follower.values()) and not has_follower_prebuilts
1541 )
Alex Klein6becabc2020-09-11 14:03:05 -06001542
Alex Klein1699fab2022-09-08 08:46:06 -06001543 return NeedsChromeSourceResult(
1544 needs_chrome_source=needs_chrome or needs_follower,
1545 builds_chrome=builds_chrome,
1546 packages=[package_info.parse(p) for p in pkgs_needing_prebuilts],
1547 missing_chrome_prebuilt=not has_chrome_prebuilt,
1548 missing_follower_prebuilt=not has_follower_prebuilts,
1549 local_uprev=local_uprev,
1550 )
Alex Klein6becabc2020-09-11 14:03:05 -06001551
1552
Alex Klein68a28712021-11-08 11:08:30 -07001553class TargetVersions(NamedTuple):
Alex Klein1699fab2022-09-08 08:46:06 -06001554 """Data class for the info that makes up the "target versions"."""
1555
1556 android_version: str
1557 android_branch: str
1558 android_target: str
1559 chrome_version: str
1560 platform_version: str
1561 milestone_version: str
1562 full_version: str
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001563 lacros_version: str
Alex Klein68a28712021-11-08 11:08:30 -07001564
1565
1566def get_target_versions(
Alex Klein1699fab2022-09-08 08:46:06 -06001567 build_target: "build_target_lib.BuildTarget",
1568 packages: List[package_info.PackageInfo] = None,
Alex Klein68a28712021-11-08 11:08:30 -07001569) -> TargetVersions:
Alex Klein1699fab2022-09-08 08:46:06 -06001570 """Aggregate version info for a few key packages and the OS as a whole."""
1571 # Android version.
1572 android_version = determine_android_version(build_target.name)
1573 logging.info("Found android version: %s", android_version)
1574 # Android branch version.
1575 android_branch = determine_android_branch(build_target.name)
1576 logging.info("Found android branch version: %s", android_branch)
1577 # Android target version.
1578 android_target = determine_android_target(build_target.name)
1579 logging.info("Found android target version: %s", android_target)
Alex Klein68a28712021-11-08 11:08:30 -07001580
Alex Klein1699fab2022-09-08 08:46:06 -06001581 # TODO(crbug/1019770): Investigate cases where builds_chrome is true but
1582 # chrome_version is None.
Alex Klein68a28712021-11-08 11:08:30 -07001583
Mike Frysinger8c9d7582023-08-21 19:28:21 -04001584 # If no packages are set, assume virtual/target-os. Chrome & LaCrOS aren't
1585 # pulled in via any other target, and certainly wouldn't be enabled in those
1586 # but not in the main OS target.
1587 if not packages:
1588 packages = [package_info.parse(constants.TARGET_OS_PKG)]
1589
Alex Klein1699fab2022-09-08 08:46:06 -06001590 builds_chrome = builds(constants.CHROME_CP, build_target, packages=packages)
1591 chrome_version = None
1592 if builds_chrome:
1593 # Chrome version fetch.
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001594 chrome_version = determine_package_version(
1595 constants.CHROME_CP, build_target
1596 )
Alex Klein1699fab2022-09-08 08:46:06 -06001597 logging.info("Found chrome version: %s", chrome_version)
Alex Klein68a28712021-11-08 11:08:30 -07001598
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001599 builds_lacros = builds(constants.LACROS_CP, build_target, packages=packages)
1600 lacros_version = None
1601 if builds_lacros:
1602 # LaCrOS version fetch.
1603 lacros_version = determine_package_version(
1604 constants.LACROS_CP, build_target
1605 )
1606 logging.info("Found LaCrOS version: %s", lacros_version)
1607
Alex Klein1699fab2022-09-08 08:46:06 -06001608 # The ChromeOS version info.
1609 platform_version = determine_platform_version()
1610 milestone_version = determine_milestone_version()
1611 full_version = determine_full_version()
Alex Klein68a28712021-11-08 11:08:30 -07001612
Alex Klein1699fab2022-09-08 08:46:06 -06001613 return TargetVersions(
1614 android_version,
1615 android_branch,
1616 android_target,
1617 chrome_version,
1618 platform_version,
1619 milestone_version,
1620 full_version,
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001621 lacros_version,
Alex Klein1699fab2022-09-08 08:46:06 -06001622 )
Alex Klein68a28712021-11-08 11:08:30 -07001623
1624
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001625def determine_package_version(
1626 cpv_name: str,
Alex Klein1699fab2022-09-08 08:46:06 -06001627 build_target: "build_target_lib.BuildTarget",
1628) -> Optional[str]:
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001629 """Returns the current package version for the board (or in buildroot).
Michael Mortensenc2615b72019-10-15 08:12:24 -06001630
Alex Klein1699fab2022-09-08 08:46:06 -06001631 Args:
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001632 cpv_name: the name of the ebuild CPV
Alex Klein348e7692022-10-13 17:03:37 -06001633 build_target: The board build target.
Alex Kleinad6b48a2020-01-08 16:57:41 -07001634
Alex Klein1699fab2022-09-08 08:46:06 -06001635 Returns:
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001636 The version of the package, if available.
Alex Klein1699fab2022-09-08 08:46:06 -06001637 """
1638 # TODO(crbug/1019770): Long term we should not need the try/catch here once
1639 # the builds function above only returns True for chrome when
1640 # determine_chrome_version will succeed.
1641 try:
1642 pkg_info = portage_util.PortageqBestVisible(
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001643 cpv_name, build_target.name, cwd=constants.SOURCE_ROOT
Alex Klein1699fab2022-09-08 08:46:06 -06001644 )
1645 except cros_build_lib.RunCommandError as e:
1646 # Return None because portage failed when trying to determine the chrome
1647 # version.
1648 logging.warning("Caught exception in determine_chrome_package: %s", e)
1649 return None
1650 # Something like 78.0.3877.4_rc -> 78.0.3877.4
1651 return pkg_info.version.partition("_")[0]
Michael Mortensenc2615b72019-10-15 08:12:24 -06001652
1653
Alex Klein68a28712021-11-08 11:08:30 -07001654@functools.lru_cache()
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001655def determine_android_package(board: str) -> Optional[str]:
Alex Klein1699fab2022-09-08 08:46:06 -06001656 """Returns the active Android container package in use by the board.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001657
Alex Klein1699fab2022-09-08 08:46:06 -06001658 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001659 board: The board name this is specific to.
Alex Kleinad6b48a2020-01-08 16:57:41 -07001660
Alex Klein1699fab2022-09-08 08:46:06 -06001661 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001662 The android package string if there is one.
Alex Klein1699fab2022-09-08 08:46:06 -06001663 """
1664 try:
1665 packages = portage_util.GetPackageDependencies(
1666 "virtual/target-os", board=board
1667 )
1668 except cros_build_lib.RunCommandError as e:
1669 # Return None because a command (likely portage) failed when trying to
1670 # determine the package.
1671 logging.warning("Caught exception in determine_android_package: %s", e)
1672 return None
1673
1674 # We assume there is only one Android package in the depgraph.
1675 for package in packages:
1676 if package.startswith(
1677 "chromeos-base/android-container-"
1678 ) or package.startswith("chromeos-base/android-vm-"):
1679 return package
Michael Mortensene0f4b542019-10-24 15:30:23 -06001680 return None
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001681
1682
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001683def determine_android_version(board: str, package: str = None):
Alex Klein1699fab2022-09-08 08:46:06 -06001684 """Determine the current Android version in buildroot now and return it.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001685
Alex Klein1699fab2022-09-08 08:46:06 -06001686 This uses the typical portage logic to determine which version of Android
1687 is active right now in the buildroot.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001688
Alex Klein1699fab2022-09-08 08:46:06 -06001689 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001690 board: The board name this is specific to.
1691 package: The Android package, if already computed.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001692
Alex Klein1699fab2022-09-08 08:46:06 -06001693 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001694 The Android build ID of the container for the board.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001695
Alex Klein1699fab2022-09-08 08:46:06 -06001696 Raises:
Alex Klein348e7692022-10-13 17:03:37 -06001697 NoAndroidVersionError: if no unique Android version can be determined.
Alex Klein1699fab2022-09-08 08:46:06 -06001698 """
1699 if not package:
1700 package = determine_android_package(board)
1701 if not package:
1702 return None
Alex Klein7bd88b12023-05-19 15:39:55 -06001703 cpv = package_info.parse(package)
Alex Klein1699fab2022-09-08 08:46:06 -06001704 if not cpv:
1705 raise NoAndroidVersionError(
1706 "Android version could not be determined for %s" % board
1707 )
Alex Klein7bd88b12023-05-19 15:39:55 -06001708 return cpv.version
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001709
Alex Klein7a3a7dd2020-01-08 16:44:38 -07001710
Mike Frysinger8e1c99a2021-03-05 00:58:11 -05001711def determine_android_branch(board, package=None):
Alex Klein1699fab2022-09-08 08:46:06 -06001712 """Returns the Android branch in use by the active container ebuild."""
1713 if not package:
1714 package = determine_android_package(board)
1715 if not package:
1716 return None
1717 ebuild_path = portage_util.FindEbuildForBoardPackage(package, board)
1718 # We assume all targets pull from the same branch and that we always
1719 # have at least one of the following targets.
Shao-Chuan Leeca2cbcc2022-11-02 08:28:31 +09001720 # TODO(b/187795671): Do this in a less hacky way.
1721 targets = android.GetAllAndroidEbuildTargets()
Alex Klein1699fab2022-09-08 08:46:06 -06001722 ebuild_content = osutils.SourceEnvironment(ebuild_path, targets)
1723 for target in targets:
1724 if target in ebuild_content:
1725 branch = re.search(r"(.*?)-linux-", ebuild_content[target])
1726 if branch is not None:
1727 return branch.group(1)
1728 raise NoAndroidBranchError(
1729 "Android branch could not be determined for %s (ebuild empty?)" % board
1730 )
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001731
1732
Mike Frysinger8e1c99a2021-03-05 00:58:11 -05001733def determine_android_target(board, package=None):
Alex Klein1699fab2022-09-08 08:46:06 -06001734 """Returns the Android target in use by the active container ebuild."""
1735 if not package:
1736 package = determine_android_package(board)
1737 if not package:
1738 return None
1739 if package.startswith("chromeos-base/android-vm-"):
1740 return "bertha"
1741 elif package.startswith("chromeos-base/android-container-"):
1742 return "cheets"
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001743
Alex Klein1699fab2022-09-08 08:46:06 -06001744 raise NoAndroidTargetError(
1745 "Android Target cannot be determined for the package: %s" % package
1746 )
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001747
1748
1749def determine_platform_version():
Alex Klein1699fab2022-09-08 08:46:06 -06001750 """Returns the platform version from the source root."""
1751 # Platform version is something like '12575.0.0'.
1752 version = chromeos_version.VersionInfo.from_repo(constants.SOURCE_ROOT)
1753 return version.VersionString()
Michael Mortensen009cb662019-10-21 11:38:43 -06001754
1755
1756def determine_milestone_version():
Alex Klein1699fab2022-09-08 08:46:06 -06001757 """Returns the platform version from the source root."""
1758 # Milestone version is something like '79'.
1759 version = chromeos_version.VersionInfo.from_repo(constants.SOURCE_ROOT)
1760 return version.chrome_branch
Michael Mortensen009cb662019-10-21 11:38:43 -06001761
Alex Klein7a3a7dd2020-01-08 16:44:38 -07001762
Michael Mortensen009cb662019-10-21 11:38:43 -06001763def determine_full_version():
Alex Klein1699fab2022-09-08 08:46:06 -06001764 """Returns the full version from the source root."""
1765 # Full version is something like 'R79-12575.0.0'.
1766 milestone_version = determine_milestone_version()
1767 platform_version = determine_platform_version()
1768 full_version = "R%s-%s" % (milestone_version, platform_version)
1769 return full_version
Michael Mortensen71ef5682020-05-07 14:29:24 -06001770
1771
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001772def find_fingerprints(
Alex Klein1699fab2022-09-08 08:46:06 -06001773 build_target: "build_target_lib.BuildTarget",
1774) -> List[str]:
1775 """Returns a list of fingerprints for this build.
Michael Mortensende716a12020-05-15 11:27:00 -06001776
Alex Klein1699fab2022-09-08 08:46:06 -06001777 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001778 build_target: The build target.
Michael Mortensende716a12020-05-15 11:27:00 -06001779
Alex Klein1699fab2022-09-08 08:46:06 -06001780 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001781 List of fingerprint strings.
Alex Klein1699fab2022-09-08 08:46:06 -06001782 """
1783 cros_build_lib.AssertInsideChroot()
1784 fp_file = "cheets-fingerprint.txt"
1785 fp_path = os.path.join(
1786 image_lib.GetLatestImageLink(build_target.name), fp_file
1787 )
1788 if not os.path.isfile(fp_path):
1789 logging.info("Fingerprint file not found: %s", fp_path)
1790 return []
1791 logging.info("Reading fingerprint file: %s", fp_path)
1792 fingerprints = osutils.ReadFile(fp_path).splitlines()
1793 return fingerprints
Michael Mortensende716a12020-05-15 11:27:00 -06001794
1795
Alex Klein1699fab2022-09-08 08:46:06 -06001796def get_all_firmware_versions(build_target: "build_target_lib.BuildTarget"):
1797 """Extract firmware version for all models present.
Michael Mortensen59e30872020-05-18 14:12:49 -06001798
Alex Klein1699fab2022-09-08 08:46:06 -06001799 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001800 build_target: The build target.
Michael Mortensen59e30872020-05-18 14:12:49 -06001801
Alex Klein1699fab2022-09-08 08:46:06 -06001802 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001803 A dict of FirmwareVersions namedtuple instances by model.
1804 Each element will be populated based on whether it was present in the
1805 command output.
Alex Klein1699fab2022-09-08 08:46:06 -06001806 """
1807 cros_build_lib.AssertInsideChroot()
1808 result = {}
1809 # Note that example output for _get_firmware_version_cmd_result is available
1810 # in the packages_unittest.py for testing get_all_firmware_versions.
1811 cmd_result = _get_firmware_version_cmd_result(build_target)
Michael Mortensen59e30872020-05-18 14:12:49 -06001812
Alex Klein1699fab2022-09-08 08:46:06 -06001813 if cmd_result:
1814 # There is a blank line between the version info for each model.
1815 firmware_version_payloads = cmd_result.split("\n\n")
1816 for firmware_version_payload in firmware_version_payloads:
1817 if "BIOS" in firmware_version_payload:
1818 firmware_version = _find_firmware_versions(
1819 firmware_version_payload
1820 )
1821 result[firmware_version.model] = firmware_version
1822 return result
Michael Mortensen59e30872020-05-18 14:12:49 -06001823
1824
Benjamin Shai0858cd32022-01-10 20:23:49 +00001825class FirmwareVersions(NamedTuple):
Alex Klein1699fab2022-09-08 08:46:06 -06001826 """Tuple to hold firmware versions, with truthiness."""
Benjamin Shai0858cd32022-01-10 20:23:49 +00001827
Alex Klein1699fab2022-09-08 08:46:06 -06001828 model: Optional[str]
1829 main: Optional[str]
1830 main_rw: Optional[str]
1831 ec: Optional[str]
1832 ec_rw: Optional[str]
1833
1834 def __bool__(self):
1835 return bool(
1836 self.model or self.main or self.main_rw or self.ec or self.ec_rw
1837 )
Michael Mortensen71ef5682020-05-07 14:29:24 -06001838
1839
Alex Klein1699fab2022-09-08 08:46:06 -06001840def get_firmware_versions(build_target: "build_target_lib.BuildTarget"):
1841 """Extract version information from the firmware updater, if one exists.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001842
Alex Klein1699fab2022-09-08 08:46:06 -06001843 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001844 build_target: The build target.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001845
Alex Klein1699fab2022-09-08 08:46:06 -06001846 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001847 A FirmwareVersions namedtuple instance.
1848 Each element will either be set to the string output by the firmware
1849 updater shellball, or None if there is no firmware updater.
Alex Klein1699fab2022-09-08 08:46:06 -06001850 """
1851 cros_build_lib.AssertInsideChroot()
1852 cmd_result = _get_firmware_version_cmd_result(build_target)
1853 if cmd_result:
1854 return _find_firmware_versions(cmd_result)
1855 else:
1856 return FirmwareVersions(None, None, None, None, None)
Michael Mortensen71ef5682020-05-07 14:29:24 -06001857
1858
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001859def _get_firmware_version_cmd_result(
Alex Klein1699fab2022-09-08 08:46:06 -06001860 build_target: "build_target_lib.BuildTarget",
1861) -> Optional[str]:
1862 """Gets the raw result output of the firmware updater version command.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001863
Alex Klein1699fab2022-09-08 08:46:06 -06001864 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001865 build_target: The build target.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001866
Alex Klein1699fab2022-09-08 08:46:06 -06001867 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001868 Command execution result.
Alex Klein1699fab2022-09-08 08:46:06 -06001869 """
1870 updater = os.path.join(
1871 build_target.root, "usr/sbin/chromeos-firmwareupdate"
1872 )
1873 logging.info("Calling updater %s", updater)
1874 # Call the updater using the chroot-based path.
1875 try:
1876 return cros_build_lib.run(
1877 [updater, "-V"],
1878 capture_output=True,
1879 log_output=True,
1880 encoding="utf-8",
1881 ).stdout
1882 except cros_build_lib.RunCommandError:
1883 # Updater probably doesn't exist (e.g. betty).
1884 return None
Michael Mortensen71ef5682020-05-07 14:29:24 -06001885
1886
1887def _find_firmware_versions(cmd_output):
Alex Klein1699fab2022-09-08 08:46:06 -06001888 """Finds firmware version output via regex matches against the cmd_output.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001889
Alex Klein1699fab2022-09-08 08:46:06 -06001890 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001891 cmd_output: The raw output to search against.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001892
Alex Klein1699fab2022-09-08 08:46:06 -06001893 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001894 FirmwareVersions namedtuple with results.
1895 Each element will either be set to the string output by the firmware
1896 updater shellball, or None if there is no match.
Alex Klein1699fab2022-09-08 08:46:06 -06001897 """
Michael Mortensen71ef5682020-05-07 14:29:24 -06001898
Alex Klein1699fab2022-09-08 08:46:06 -06001899 # Sometimes a firmware bundle includes a special combination of RO+RW
1900 # firmware. In this case, the RW firmware version is indicated with a "(RW)
1901 # version" field. In other cases, the "(RW) version" field is not present.
1902 # Therefore, search for the "(RW)" fields first and if they aren't present,
1903 # fallback to the other format. e.g. just "BIOS version:".
1904 # TODO(mmortensen): Use JSON once the firmware updater supports it.
1905 main = None
1906 main_rw = None
1907 ec = None
1908 ec_rw = None
1909 model = None
Michael Mortensen71ef5682020-05-07 14:29:24 -06001910
Alex Klein1699fab2022-09-08 08:46:06 -06001911 match = re.search(r"BIOS version:\s*(?P<version>.*)", cmd_output)
1912 if match:
1913 main = match.group("version")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001914
Alex Klein1699fab2022-09-08 08:46:06 -06001915 match = re.search(r"BIOS \(RW\) version:\s*(?P<version>.*)", cmd_output)
1916 if match:
1917 main_rw = match.group("version")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001918
Alex Klein1699fab2022-09-08 08:46:06 -06001919 match = re.search(r"EC version:\s*(?P<version>.*)", cmd_output)
1920 if match:
1921 ec = match.group("version")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001922
Alex Klein1699fab2022-09-08 08:46:06 -06001923 match = re.search(r"EC \(RW\) version:\s*(?P<version>.*)", cmd_output)
1924 if match:
1925 ec_rw = match.group("version")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001926
Alex Klein1699fab2022-09-08 08:46:06 -06001927 match = re.search(r"Model:\s*(?P<model>.*)", cmd_output)
1928 if match:
1929 model = match.group("model")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001930
Alex Klein1699fab2022-09-08 08:46:06 -06001931 return FirmwareVersions(model, main, main_rw, ec, ec_rw)
Michael Mortensena4af79e2020-05-06 16:18:48 -06001932
1933
Benjamin Shai0858cd32022-01-10 20:23:49 +00001934class MainEcFirmwareVersions(NamedTuple):
Alex Klein1699fab2022-09-08 08:46:06 -06001935 """Tuple to hold main and ec firmware versions, with truthiness."""
Benjamin Shai0858cd32022-01-10 20:23:49 +00001936
Alex Klein1699fab2022-09-08 08:46:06 -06001937 main_fw_version: Optional[str]
1938 ec_fw_version: Optional[str]
1939
1940 def __bool__(self):
1941 return bool(self.main_fw_version or self.ec_fw_version)
Benjamin Shai0858cd32022-01-10 20:23:49 +00001942
Michael Mortensena4af79e2020-05-06 16:18:48 -06001943
Alex Klein1699fab2022-09-08 08:46:06 -06001944def determine_firmware_versions(build_target: "build_target_lib.BuildTarget"):
1945 """Returns a namedtuple with main and ec firmware versions.
Michael Mortensena4af79e2020-05-06 16:18:48 -06001946
Alex Klein1699fab2022-09-08 08:46:06 -06001947 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001948 build_target: The build target.
Michael Mortensena4af79e2020-05-06 16:18:48 -06001949
Alex Klein1699fab2022-09-08 08:46:06 -06001950 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001951 MainEcFirmwareVersions namedtuple with results.
Alex Klein1699fab2022-09-08 08:46:06 -06001952 """
1953 fw_versions = get_firmware_versions(build_target)
1954 main_fw_version = fw_versions.main_rw or fw_versions.main
1955 ec_fw_version = fw_versions.ec_rw or fw_versions.ec
Michael Mortensena4af79e2020-05-06 16:18:48 -06001956
Alex Klein1699fab2022-09-08 08:46:06 -06001957 return MainEcFirmwareVersions(main_fw_version, ec_fw_version)
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001958
Benjamin Shai0858cd32022-01-10 20:23:49 +00001959
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001960def determine_kernel_version(
Alex Klein1699fab2022-09-08 08:46:06 -06001961 build_target: "build_target_lib.BuildTarget",
Lizzy Presland0b978e62022-09-09 16:55:29 +00001962) -> str:
Alex Klein1699fab2022-09-08 08:46:06 -06001963 """Returns a string containing the kernel version for this build target.
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001964
Alex Klein1699fab2022-09-08 08:46:06 -06001965 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001966 build_target: The build target.
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001967
Alex Klein1699fab2022-09-08 08:46:06 -06001968 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001969 The kernel versions, or empty string.
Alex Klein1699fab2022-09-08 08:46:06 -06001970 """
Lizzy Presland0b978e62022-09-09 16:55:29 +00001971 target_virtual_pkg = "virtual/linux-sources"
Alex Klein1699fab2022-09-08 08:46:06 -06001972 try:
Lizzy Presland0b978e62022-09-09 16:55:29 +00001973 candidate_packages = portage_util.GetFlattenedDepsForPackage(
1974 target_virtual_pkg,
1975 sysroot=build_target.root,
1976 board=build_target.name,
1977 depth=1,
1978 )
1979 installed_packages = portage_util.GetPackageDependencies(
1980 target_virtual_pkg, board=build_target.name
Alex Klein1699fab2022-09-08 08:46:06 -06001981 )
1982 except cros_build_lib.RunCommandError as e:
1983 logging.warning("Unable to get package list for metadata: %s", e)
Lizzy Presland0b978e62022-09-09 16:55:29 +00001984 return ""
1985 if not candidate_packages:
1986 raise KernelVersionError("No package found in FlattenedDepsForPackage")
1987 if not installed_packages:
1988 raise KernelVersionError("No package found in GetPackageDependencies")
1989 packages = [
1990 p
1991 for p in installed_packages
1992 if p in candidate_packages and target_virtual_pkg not in p
1993 ]
1994 if len(packages) == 0:
1995 raise KernelVersionError(
1996 "No matches for installed packages were found in candidate "
1997 "packages. Did GetFlattenedDepsForPackage search all possible "
1998 "package versions?\tInstalled: %s\tCandidates: %s"
1999 % (" ".join(installed_packages), " ".join(candidate_packages))
2000 )
2001 if len(packages) > 1:
2002 raise KernelVersionError(
2003 "Too many packages found in intersection of installed packages and "
2004 "possible kernel versions (%s)" % "".join(packages)
2005 )
2006 kernel_version = package_info.SplitCPV(packages[0]).version
2007 logging.info("Found active kernel version: %s", kernel_version)
2008 return kernel_version
Michael Mortensen125bb012020-05-21 14:02:10 -06002009
2010
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07002011def get_models(
Alex Klein1699fab2022-09-08 08:46:06 -06002012 build_target: "build_target_lib.BuildTarget", log_output: bool = True
2013) -> Optional[List[str]]:
2014 """Obtain a list of models supported by a unified board.
Michael Mortensen125bb012020-05-21 14:02:10 -06002015
Alex Klein1699fab2022-09-08 08:46:06 -06002016 This ignored whitelabel models since GoldenEye has no specific support for
2017 these at present.
Michael Mortensen125bb012020-05-21 14:02:10 -06002018
Alex Klein1699fab2022-09-08 08:46:06 -06002019 Args:
Alex Klein348e7692022-10-13 17:03:37 -06002020 build_target: The build target.
2021 log_output: Whether to log the output of the cros_config_host
2022 invocation.
Michael Mortensen125bb012020-05-21 14:02:10 -06002023
Alex Klein1699fab2022-09-08 08:46:06 -06002024 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06002025 A list of models supported by this board, if it is a unified build;
2026 None, if it is not a unified build.
Alex Klein1699fab2022-09-08 08:46:06 -06002027 """
2028 return _run_cros_config_host(
2029 build_target, ["list-models"], log_output=log_output
2030 )
Michael Mortensen125bb012020-05-21 14:02:10 -06002031
2032
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07002033def get_key_id(
Alex Klein1699fab2022-09-08 08:46:06 -06002034 build_target: "build_target_lib.BuildTarget", model: str
2035) -> Optional[str]:
2036 """Obtain the key_id for a model within the build_target.
Michael Mortensen359c1f32020-05-28 19:35:42 -06002037
Alex Klein1699fab2022-09-08 08:46:06 -06002038 Args:
Alex Klein348e7692022-10-13 17:03:37 -06002039 build_target: The build target.
2040 model: The model name
Michael Mortensen359c1f32020-05-28 19:35:42 -06002041
Alex Klein1699fab2022-09-08 08:46:06 -06002042 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06002043 A key_id or None.
Alex Klein1699fab2022-09-08 08:46:06 -06002044 """
2045 model_arg = "--model=" + model
2046 key_id_list = _run_cros_config_host(
2047 build_target, [model_arg, "get", "/firmware-signing", "key-id"]
2048 )
2049 key_id = None
2050 if len(key_id_list) == 1:
2051 key_id = key_id_list[0]
2052 return key_id
Michael Mortensen359c1f32020-05-28 19:35:42 -06002053
2054
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07002055def _run_cros_config_host(
Alex Klein1699fab2022-09-08 08:46:06 -06002056 build_target: "build_target_lib.BuildTarget",
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07002057 args: List[str],
Alex Klein1699fab2022-09-08 08:46:06 -06002058 log_output: bool = True,
2059) -> Optional[List[str]]:
2060 """Run the cros_config_host tool.
Michael Mortensen125bb012020-05-21 14:02:10 -06002061
Alex Klein1699fab2022-09-08 08:46:06 -06002062 Args:
Alex Klein348e7692022-10-13 17:03:37 -06002063 build_target: The build target.
2064 args: List of arguments to pass.
2065 log_output: Whether to log the output of the cros_config_host.
Michael Mortensen125bb012020-05-21 14:02:10 -06002066
Alex Klein1699fab2022-09-08 08:46:06 -06002067 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06002068 Output of the tool
Alex Klein1699fab2022-09-08 08:46:06 -06002069 """
2070 cros_build_lib.AssertInsideChroot()
2071 tool = "/usr/bin/cros_config_host"
2072 if not os.path.isfile(tool):
2073 return None
Michael Mortensen125bb012020-05-21 14:02:10 -06002074
Alex Klein1699fab2022-09-08 08:46:06 -06002075 config_fname = build_target.full_path(
2076 "usr/share/chromeos-config/yaml/config.yaml"
2077 )
Michael Mortensen125bb012020-05-21 14:02:10 -06002078
Alex Klein1699fab2022-09-08 08:46:06 -06002079 result = cros_build_lib.run(
2080 [tool, "-c", config_fname] + args,
2081 capture_output=True,
2082 encoding="utf-8",
2083 log_output=log_output,
2084 check=False,
2085 )
2086 if result.returncode:
2087 # Show the output for debugging purposes.
2088 if "No such file or directory" not in result.stderr:
2089 logging.error("cros_config_host failed: %s\n", result.stderr)
2090 return None
2091 return result.stdout.strip().splitlines()