blob: c14818b185de216160d33551a1f9cd9ba2d5ff0a [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2016 The ChromiumOS Authors
David Rileyc0da9d92016-02-01 12:11:01 -08002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""This module uprevs Android for cbuildbot.
6
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +09007After calling, it prints out a JSON representing the result, with the new
8Android version atom string included. A caller could then use this atom with
9emerge to build the newly uprevved version of Android e.g.
David Rileyc0da9d92016-02-01 12:11:01 -080010
Shuhei Takahashi6d02c192017-04-05 14:01:24 +090011./cros_mark_android_as_stable \
Shao-Chuan Lee9c39e0c2020-04-24 11:40:34 +090012 --android_package=android-container-pi
Shuhei Takahashi6d02c192017-04-05 14:01:24 +090013
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +090014Returns {"android_atom": "chromeos-base/android-container-pi-6417892-r1"}
David Rileyc0da9d92016-02-01 12:11:01 -080015
Shao-Chuan Lee9c39e0c2020-04-24 11:40:34 +090016emerge-eve =chromeos-base/android-container-pi-6417892-r1
David Rileyc0da9d92016-02-01 12:11:01 -080017"""
18
David Rileyc0da9d92016-02-01 12:11:01 -080019import filecmp
20import glob
Junichi Uekawad21f94d2020-07-27 15:50:05 +090021import json
Chris McDonaldb55b7032021-06-17 16:41:32 -060022import logging
David Rileyc0da9d92016-02-01 12:11:01 -080023import os
24
Chris McDonaldb55b7032021-06-17 16:41:32 -060025from chromite.cbuildbot import cbuildbot_alerts
David Rileyc0da9d92016-02-01 12:11:01 -080026from chromite.lib import commandline
Chris McDonaldb55b7032021-06-17 16:41:32 -060027from chromite.lib import constants
David Rileyc0da9d92016-02-01 12:11:01 -080028from chromite.lib import cros_build_lib
David Rileyc0da9d92016-02-01 12:11:01 -080029from chromite.lib import git
30from chromite.lib import gs
Shao-Chuan Lee301a4192021-02-08 11:53:49 +090031from chromite.lib import osutils
David Rileyc0da9d92016-02-01 12:11:01 -080032from chromite.lib import portage_util
Shao-Chuan Lee007dbe82021-02-09 14:05:39 +090033from chromite.lib import repo_util
David Rileyc0da9d92016-02-01 12:11:01 -080034from chromite.scripts import cros_mark_as_stable
Shao-Chuan Lee7fd0ca12021-03-19 15:57:40 +090035from chromite.service import android
David Rileyc0da9d92016-02-01 12:11:01 -080036
37
38# Dir where all the action happens.
Alex Klein1699fab2022-09-08 08:46:06 -060039_OVERLAY_DIR = "%(srcroot)s/private-overlays/project-cheets-private/"
David Rileyc0da9d92016-02-01 12:11:01 -080040
Junichi Uekawa6d61ab02020-04-15 14:52:28 +090041_GIT_COMMIT_MESSAGE = """Marking latest for %(android_package)s ebuild with \
42version %(android_version)s as stable.
43
44BUG=None
45TEST=CQ
46"""
David Rileyc0da9d92016-02-01 12:11:01 -080047
Shao-Chuan Lee3aa76522021-12-03 17:18:58 +090048_GIT_COMMIT_MESSAGE_LKGB = """%(android_package)s: Mark version \
49%(android_version)s as LKGB.
50
51Generated by Android PFQ. This change will trigger PUpr to actually submit an
52Android uprev.
53
54For details, see "Migration plan" section of go/android-uprev-recipes.
55
56BUG=None
57TEST=CQ
58"""
59
Alex Klein1699fab2022-09-08 08:46:06 -060060_RUNTIME_ARTIFACTS_BUCKET_URL = "gs://chromeos-arc-images/runtime_artifacts"
David Rileyc0da9d92016-02-01 12:11:01 -080061
David Rileyc0da9d92016-02-01 12:11:01 -080062
63def FindAndroidCandidates(package_dir):
Alex Klein1699fab2022-09-08 08:46:06 -060064 """Return a tuple of Android's unstable ebuild and stable ebuilds.
David Rileyc0da9d92016-02-01 12:11:01 -080065
Alex Klein1699fab2022-09-08 08:46:06 -060066 Args:
67 package_dir: The path to where the package ebuild is stored.
David Rileyc0da9d92016-02-01 12:11:01 -080068
Alex Klein1699fab2022-09-08 08:46:06 -060069 Returns:
70 Tuple [unstable_ebuild, stable_ebuilds].
David Rileyc0da9d92016-02-01 12:11:01 -080071
Alex Klein1699fab2022-09-08 08:46:06 -060072 Raises:
73 Exception: if no unstable ebuild exists for Android.
74 """
75 stable_ebuilds = []
76 unstable_ebuilds = []
77 for path in glob.glob(os.path.join(package_dir, "*.ebuild")):
78 ebuild = portage_util.EBuild(path)
79 if ebuild.version == "9999":
80 unstable_ebuilds.append(ebuild)
81 else:
82 stable_ebuilds.append(ebuild)
David Rileyc0da9d92016-02-01 12:11:01 -080083
Alex Klein1699fab2022-09-08 08:46:06 -060084 # Apply some confidence checks.
85 if not unstable_ebuilds:
86 raise Exception("Missing 9999 ebuild for %s" % package_dir)
87 if not stable_ebuilds:
88 logging.warning("Missing stable ebuild for %s", package_dir)
David Rileyc0da9d92016-02-01 12:11:01 -080089
Alex Klein1699fab2022-09-08 08:46:06 -060090 return portage_util.BestEBuild(unstable_ebuilds), stable_ebuilds
David Rileyc0da9d92016-02-01 12:11:01 -080091
92
Shao-Chuan Lee085e3d72020-05-11 16:00:42 +090093def PrintUprevMetadata(build_branch, stable_candidate, new_ebuild):
Alex Klein1699fab2022-09-08 08:46:06 -060094 """Shows metadata on buildbot page at UprevAndroid step.
David Rileyc0da9d92016-02-01 12:11:01 -080095
Alex Klein1699fab2022-09-08 08:46:06 -060096 Args:
97 build_branch: The branch of Android builds.
98 stable_candidate: The existing stable ebuild.
99 new_ebuild: The newly written ebuild.
David Rileyc0da9d92016-02-01 12:11:01 -0800100 """
Alex Klein1699fab2022-09-08 08:46:06 -0600101 # Examples:
102 # "android-container-pi revved 6461825-r1 -> 6468247-r1"
103 # "android-container-pi revved 6461825-r1 -> 6461825-r2 (ebuild update only)"
104 msg = "%s revved %s -> %s" % (
105 stable_candidate.pkgname,
106 stable_candidate.version,
107 new_ebuild.version,
108 )
David Rileyc0da9d92016-02-01 12:11:01 -0800109
Alex Klein1699fab2022-09-08 08:46:06 -0600110 old_android = stable_candidate.version_no_rev
111 new_android = new_ebuild.version_no_rev
David Rileyc0da9d92016-02-01 12:11:01 -0800112
Alex Klein1699fab2022-09-08 08:46:06 -0600113 if old_android == new_android:
114 msg += " (ebuild update only)"
115 else:
116 ab_link = (
117 "https://android-build.googleplex.com"
118 "/builds/%s/branches/%s/cls?end=%s"
119 % (new_android, build_branch, old_android)
120 )
121 cbuildbot_alerts.PrintBuildbotLink("Android changelog", ab_link)
David Rileyc0da9d92016-02-01 12:11:01 -0800122
Alex Klein1699fab2022-09-08 08:46:06 -0600123 cbuildbot_alerts.PrintBuildbotStepText(msg)
124 cbuildbot_alerts.PrintKitchenSetBuildProperty(
125 "android_uprev",
126 json.dumps(
127 {
128 "branch": build_branch,
129 "new": new_ebuild.version,
130 "old": stable_candidate.version,
131 "pkgname": stable_candidate.pkgname,
132 }
133 ),
134 )
David Rileyc0da9d92016-02-01 12:11:01 -0800135
khmel@chromium.orgd3ec3d72020-04-29 15:57:35 -0700136
Alex Klein1699fab2022-09-08 08:46:06 -0600137def FindDataCollectorArtifacts(
138 gs_context, android_version, runtime_artifacts_bucket_url, version_reference
139):
140 r"""Finds and includes into variables artifacts from arc.DataCollector.
David Rileyc0da9d92016-02-01 12:11:01 -0800141
Alex Klein1699fab2022-09-08 08:46:06 -0600142 This is used from UpdateDataCollectorArtifacts in order to check the
143 particular version.
David Rileyc0da9d92016-02-01 12:11:01 -0800144
Alex Klein1699fab2022-09-08 08:46:06 -0600145 Args:
146 gs_context: context to execute gsutil
147 android_version: The \d+ build id of Android.
148 runtime_artifacts_bucket_url: root of runtime artifacts
149 build_branch: build branch. Used to determine the pinned version if exists.
150 version_reference: which version to use as a reference. Could be '${PV}' in
151 case version of data collector artifacts matches the
152 Android version or direct version in case of override.
David Rileyc0da9d92016-02-01 12:11:01 -0800153
Alex Klein1699fab2022-09-08 08:46:06 -0600154 Returns:
155 dictionary with filled ebuild variables. This dictionary is empty in case
156 no artificats are found.
157 """
158 variables = {}
David Rileyc0da9d92016-02-01 12:11:01 -0800159
Yury Khmel697863b2022-10-25 09:02:57 -0700160 buckets = ["ureadahead_pack_host", "gms_core_cache", "tts_cache"]
Alex Klein1699fab2022-09-08 08:46:06 -0600161 archs = ["arm", "arm64", "x86", "x86_64"]
162 build_types = ["user", "userdebug"]
David Rileyc0da9d92016-02-01 12:11:01 -0800163
Alex Klein1699fab2022-09-08 08:46:06 -0600164 for bucket in buckets:
165 for arch in archs:
166 for build_type in build_types:
167 path = (
168 f"{runtime_artifacts_bucket_url}/{bucket}_{arch}_{build_type}_"
169 f"{android_version}.tar"
170 )
171 if gs_context.Exists(path):
172 variables[(f"{arch}_{build_type}_{bucket}").upper()] = (
173 f"{runtime_artifacts_bucket_url}/{bucket}_{arch}_{build_type}_"
174 f"{version_reference}.tar"
175 )
176
177 return variables
178
179
180def UpdateDataCollectorArtifacts(
181 android_version, runtime_artifacts_bucket_url, build_branch
182):
183 r"""Finds and includes into variables artifacts from arc.DataCollector.
184
185 This verifies default android version. In case artificts are not found for
186 default Android version it tries to find artifacts for pinned version. If
187 pinned version is provided, it is required artifacts exist for the pinned
188 version.
189
190 Args:
191 android_version: The \d+ build id of Android.
192 runtime_artifacts_bucket_url: root of runtime artifacts
193 build_branch: build branch. Used to determine the pinned version if exists.
194
195 Returns:
196 dictionary with filled ebuild variables.
197 """
198
199 gs_context = gs.GSContext()
200 # Check the existing version. If we find any artifacts, use them.
201 variables = FindDataCollectorArtifacts(
202 gs_context, android_version, runtime_artifacts_bucket_url, "${PV}"
203 )
204 if variables:
205 # Data artificts were found.
206 return variables
207
208 # Check pinned version for the current branch.
209 pin_path = f"{runtime_artifacts_bucket_url}/{build_branch}_pin_version"
210 if not gs_context.Exists(pin_path):
211 # No pinned version.
212 logging.warning(
213 "No data collector artifacts were found for %s", android_version
214 )
215 return variables
216
217 pin_version = gs_context.Cat(pin_path, encoding="utf-8").rstrip()
218 logging.info("Pinned version %s overrides %s", pin_version, android_version)
219 variables = FindDataCollectorArtifacts(
220 gs_context, pin_version, runtime_artifacts_bucket_url, pin_version
221 )
222 if not variables:
223 # If pin version set it must contain data.
224 raise Exception(
225 "Pinned version %s:%s does not contain artificats"
226 % (build_branch, pin_version)
227 )
228
229 return variables
230
231
232def MarkAndroidEBuildAsStable(
233 stable_candidate,
234 unstable_ebuild,
235 android_package,
236 android_version,
237 package_dir,
Alex Klein1699fab2022-09-08 08:46:06 -0600238 arc_bucket_url,
239 runtime_artifacts_bucket_url,
240):
241 r"""Uprevs the Android ebuild.
242
243 This is the main function that uprevs from a stable candidate
244 to its new version.
245
246 Args:
247 stable_candidate: ebuild that corresponds to the stable ebuild we are
248 revving from. If None, builds the a new ebuild given the version
249 with revision set to 1.
250 unstable_ebuild: ebuild corresponding to the unstable ebuild for Android.
251 android_package: android package name.
252 android_version: The \d+ build id of Android.
253 package_dir: Path to the android-container package dir.
Alex Klein1699fab2022-09-08 08:46:06 -0600254 arc_bucket_url: URL of the target ARC build gs bucket.
255 runtime_artifacts_bucket_url: root of runtime artifacts
256
257 Returns:
258 Tuple[str, List[str], List[str]] if revved, or None
259 1. Full portage version atom (including rc's, etc) that was revved.
260 2. List of files to be `git add`ed.
261 3. List of files to be `git rm`ed.
262 """
263
264 def IsTheNewEBuildRedundant(new_ebuild, stable_ebuild):
265 """Returns True if the new ebuild is redundant.
266
267 This is True if there if the current stable ebuild is the exact same copy
268 of the new one.
269 """
270 if not stable_ebuild:
271 return False
272
273 if stable_candidate.version_no_rev == new_ebuild.version_no_rev:
274 return filecmp.cmp(
275 new_ebuild.ebuild_path, stable_ebuild.ebuild_path, shallow=False
276 )
277 return False
278
279 # Case where we have the last stable candidate with same version just rev.
280 if stable_candidate and stable_candidate.version_no_rev == android_version:
281 new_ebuild_path = "%s-r%d.ebuild" % (
282 stable_candidate.ebuild_path_no_revision,
283 stable_candidate.current_revision + 1,
284 )
285 else:
286 pf = "%s-%s-r1" % (android_package, android_version)
287 new_ebuild_path = os.path.join(package_dir, "%s.ebuild" % pf)
288
Shao-Chuan Lee20c51d82022-10-26 21:56:56 +0900289 # TODO(b/255705023): Remove build_branch once the following no longer relies
290 # on it.
291 build_branch = android.GetAndroidBranchForPackage(android_package)
Shao-Chuan Leeca2cbcc2022-11-02 08:28:31 +0900292 build_targets = android.GetAndroidEbuildTargetsForPackage(android_package)
Alex Klein1699fab2022-09-08 08:46:06 -0600293 variables = {"BASE_URL": arc_bucket_url}
294 for var, target in build_targets.items():
Shao-Chuan Lee20c51d82022-10-26 21:56:56 +0900295 # TODO(b/255705023): Have MirrorArtifacts generate the mapping for us.
Alex Klein1699fab2022-09-08 08:46:06 -0600296 variables[var] = f"{build_branch}-linux-{target}"
297
Shao-Chuan Lee20c51d82022-10-26 21:56:56 +0900298 # TODO(b/255854925): Pin DataCollector artifacts by android_package instead
299 # of build_branch.
Alex Klein1699fab2022-09-08 08:46:06 -0600300 variables.update(
301 UpdateDataCollectorArtifacts(
302 android_version, runtime_artifacts_bucket_url, build_branch
303 )
304 )
305
306 portage_util.EBuild.MarkAsStable(
307 unstable_ebuild.ebuild_path,
308 new_ebuild_path,
309 variables,
310 make_stable=True,
311 )
312 new_ebuild = portage_util.EBuild(new_ebuild_path)
313
314 # Determine whether this is ebuild is redundant.
315 if IsTheNewEBuildRedundant(new_ebuild, stable_candidate):
316 msg = "Previous ebuild with same version found and ebuild is redundant."
317 logging.info(msg)
318 cbuildbot_alerts.PrintBuildbotStepText(
319 "%s %s not revved"
320 % (stable_candidate.pkgname, stable_candidate.version)
321 )
322 osutils.SafeUnlink(new_ebuild_path)
323 return None
324
325 # PFQ runs should always be able to find a stable candidate.
326 if stable_candidate:
327 PrintUprevMetadata(build_branch, stable_candidate, new_ebuild)
328
329 files_to_add = [new_ebuild_path]
330 files_to_remove = []
331 if stable_candidate and not stable_candidate.IsSticky():
332 osutils.SafeUnlink(stable_candidate.ebuild_path)
333 files_to_remove.append(stable_candidate.ebuild_path)
334
335 # Update ebuild manifest and git add it.
336 gen_manifest_cmd = ["ebuild", new_ebuild_path, "manifest", "--force"]
337 cros_build_lib.run(gen_manifest_cmd, extra_env=None, print_cmd=True)
338 files_to_add.append(os.path.join(package_dir, "Manifest"))
339
340 return (
341 f"{new_ebuild.package}-{new_ebuild.version}",
342 files_to_add,
343 files_to_remove,
344 )
David Rileyc0da9d92016-02-01 12:11:01 -0800345
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900346
Shao-Chuan Lee007dbe82021-02-09 14:05:39 +0900347def _PrepareGitBranch(overlay_dir):
Alex Klein1699fab2022-09-08 08:46:06 -0600348 """Prepares a git branch for the uprev commit.
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900349
Alex Klein1699fab2022-09-08 08:46:06 -0600350 If the overlay project is currently on a branch (e.g. patches are being
351 applied), rebase the new branch on top of it.
Shao-Chuan Lee007dbe82021-02-09 14:05:39 +0900352
Alex Klein1699fab2022-09-08 08:46:06 -0600353 Args:
354 overlay_dir: The overlay directory.
355 """
356 existing_branch = git.GetCurrentBranch(overlay_dir)
357 repo_util.Repository.MustFind(overlay_dir).StartBranch(
358 constants.STABLE_EBUILD_BRANCH, projects=["."], cwd=overlay_dir
359 )
360 if existing_branch:
361 git.RunGit(overlay_dir, ["rebase", existing_branch])
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900362
363
364def _CommitChange(message, android_package_dir, files_to_add, files_to_remove):
Alex Klein1699fab2022-09-08 08:46:06 -0600365 """Commit changes to git with list of files to add/remove."""
366 git.RunGit(android_package_dir, ["add", "--"] + files_to_add)
367 if files_to_remove:
368 git.RunGit(android_package_dir, ["rm", "--"] + files_to_remove)
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900369
Alex Klein1699fab2022-09-08 08:46:06 -0600370 portage_util.EBuild.CommitChange(message, android_package_dir)
David Rileyc0da9d92016-02-01 12:11:01 -0800371
372
373def GetParser():
Alex Klein1699fab2022-09-08 08:46:06 -0600374 """Creates the argument parser."""
375 parser = commandline.ArgumentParser()
376 parser.add_argument("-b", "--boards")
377 parser.add_argument(
378 "--android_bucket_url",
379 default=android.ANDROID_BUCKET_URL,
380 type="gs_path",
381 )
Shao-Chuan Lee20c51d82022-10-26 21:56:56 +0900382 # TODO(b/255705023): Clean this up.
Alex Klein1699fab2022-09-08 08:46:06 -0600383 parser.add_argument(
384 "--android_build_branch",
Shao-Chuan Lee20c51d82022-10-26 21:56:56 +0900385 help="DEPRECATED",
Alex Klein1699fab2022-09-08 08:46:06 -0600386 )
387 parser.add_argument(
388 "--android_package",
389 required=True,
Shao-Chuan Leeca2cbcc2022-11-02 08:28:31 +0900390 choices=android.GetAllAndroidPackages(),
Alex Klein1699fab2022-09-08 08:46:06 -0600391 help="Android package to uprev",
392 )
393 parser.add_argument(
394 "--arc_bucket_url", default=constants.ARC_BUCKET_URL, type="gs_path"
395 )
396 parser.add_argument("-f", "--force_version", help="Android build id to use")
397 parser.add_argument(
398 "-s",
399 "--srcroot",
400 default=os.path.join(constants.SOURCE_ROOT, "src"),
401 help="Path to the src directory",
402 )
403 parser.add_argument(
404 "--runtime_artifacts_bucket_url",
405 default=_RUNTIME_ARTIFACTS_BUCKET_URL,
406 type="gs_path",
407 )
408 parser.add_argument(
409 "--skip_commit",
410 action="store_true",
411 help="Skip commiting uprev changes to git",
412 )
413 parser.add_argument(
414 "--update_lkgb",
415 action="store_true",
416 help=(
417 "Update the LKGB file instead of uprevving ebuilds "
418 "and populating artifacts. "
419 "Requires --force_version be set."
420 ),
421 )
422 return parser
David Rileyc0da9d92016-02-01 12:11:01 -0800423
424
425def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -0600426 cbuildbot_alerts.EnableBuildbotMarkers()
427 parser = GetParser()
428 options = parser.parse_args(argv)
429 options.Freeze()
David Rileyc0da9d92016-02-01 12:11:01 -0800430
Alex Klein1699fab2022-09-08 08:46:06 -0600431 overlay_dir = os.path.abspath(_OVERLAY_DIR % {"srcroot": options.srcroot})
432 android_package_dir = android.GetAndroidPackageDir(
433 options.android_package, overlay_dir=overlay_dir
434 )
Shao-Chuan Lee3aa76522021-12-03 17:18:58 +0900435
436 if not options.skip_commit:
Alex Klein1699fab2022-09-08 08:46:06 -0600437 _PrepareGitBranch(overlay_dir)
Shao-Chuan Lee3aa76522021-12-03 17:18:58 +0900438
Alex Klein1699fab2022-09-08 08:46:06 -0600439 if options.update_lkgb:
440 if not options.force_version:
441 raise Exception("--force_version is required with --update_lkgb")
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900442
Alex Klein1699fab2022-09-08 08:46:06 -0600443 # Attempt to read current LKGB, if available.
444 current_lkgb = None
445 try:
446 current_lkgb = android.ReadLKGB(android_package_dir)
447 except android.MissingLKGBError:
448 logging.info("LKGB file is missing, creating a new one.")
449 except android.InvalidLKGBError:
450 logging.warning("Current LKGB file is invalid, overwriting.")
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900451
Alex Klein1699fab2022-09-08 08:46:06 -0600452 # Do nothing if LKGB is already set to the requested version.
453 if current_lkgb == options.force_version:
454 logging.warning(
455 "LKGB of %s is already %s, doing nothing.",
456 options.android_package,
457 options.force_version,
458 )
459 cbuildbot_alerts.PrintBuildbotStepText(
460 "LKGB of %s unchanged @ %s"
461 % (options.android_package, options.force_version)
462 )
463 return
David Rileyc0da9d92016-02-01 12:11:01 -0800464
Alex Klein1699fab2022-09-08 08:46:06 -0600465 # Actually update the LKGB.
466 path = android.WriteLKGB(android_package_dir, options.force_version)
467 cbuildbot_alerts.PrintBuildbotStepText(
468 "LKGB of %s updated %s -> %s"
469 % (options.android_package, current_lkgb, options.force_version)
470 )
David Rileyc0da9d92016-02-01 12:11:01 -0800471
Alex Klein1699fab2022-09-08 08:46:06 -0600472 if not options.skip_commit:
473 _CommitChange(
474 _GIT_COMMIT_MESSAGE_LKGB
475 % {
476 "android_package": options.android_package,
477 "android_version": options.force_version,
478 },
479 android_package_dir,
480 [path],
481 [],
482 )
483 return
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900484
Shao-Chuan Lee20c51d82022-10-26 21:56:56 +0900485 # TODO(b/255705023): Clean this up.
486 android_build_branch = android.GetAndroidBranchForPackage(
487 options.android_package
Alex Klein1699fab2022-09-08 08:46:06 -0600488 )
Shao-Chuan Lee20c51d82022-10-26 21:56:56 +0900489 if (
490 options.android_build_branch
491 and options.android_build_branch != android_build_branch
492 ):
493 logging.warning(
494 "--android_build_branch is deprecated; using default branch %s",
495 android_build_branch,
496 )
Shao-Chuan Leedea458f2021-11-25 23:46:53 +0900497
Alex Klein1699fab2022-09-08 08:46:06 -0600498 (unstable_ebuild, stable_ebuilds) = FindAndroidCandidates(
499 android_package_dir
500 )
501 # Mirror artifacts, i.e., images and some sdk tools (e.g., adb, aapt).
502 version_to_uprev = android.MirrorArtifacts(
Shao-Chuan Lee20c51d82022-10-26 21:56:56 +0900503 options.android_package,
Alex Klein1699fab2022-09-08 08:46:06 -0600504 options.android_bucket_url,
Alex Klein1699fab2022-09-08 08:46:06 -0600505 options.arc_bucket_url,
506 android_package_dir,
507 options.force_version,
508 )
David Rileyc0da9d92016-02-01 12:11:01 -0800509
Alex Klein1699fab2022-09-08 08:46:06 -0600510 stable_candidate = portage_util.BestEBuild(stable_ebuilds)
Shao-Chuan Leedea458f2021-11-25 23:46:53 +0900511
Alex Klein1699fab2022-09-08 08:46:06 -0600512 if stable_candidate:
513 logging.info("Stable candidate found %s", stable_candidate.version)
514 else:
515 logging.info("No stable candidate found.")
516
517 revved = MarkAndroidEBuildAsStable(
518 stable_candidate,
519 unstable_ebuild,
520 options.android_package,
521 version_to_uprev,
522 android_package_dir,
Alex Klein1699fab2022-09-08 08:46:06 -0600523 options.arc_bucket_url,
524 options.runtime_artifacts_bucket_url,
525 )
526
527 output = dict(revved=bool(revved))
528
529 if revved:
530 android_atom, files_to_add, files_to_remove = revved
531 if not options.skip_commit:
532 _CommitChange(
533 _GIT_COMMIT_MESSAGE
534 % {
535 "android_package": options.android_package,
536 "android_version": version_to_uprev,
537 },
538 android_package_dir,
539 files_to_add,
540 files_to_remove,
541 )
542 if options.boards:
543 cros_mark_as_stable.CleanStalePackages(
544 options.srcroot, options.boards.split(":"), [android_atom]
545 )
546
547 output["android_atom"] = android_atom
548 # This field is read by the PUpr uprev handler for creating CLs. We cannot
549 # return absolute paths because this script runs inside chroot but the uprev
550 # handler runs outside.
551 # Here we return paths relative to |overlay_dir|.
552 output["modified_files"] = [
553 os.path.relpath(f, overlay_dir)
554 for f in files_to_add + files_to_remove
555 ]
556
557 # The output is being parsed by service.packages.uprev_android and has to be
558 # in its own single line. When invoked from chromite API endpoints, entering
559 # chroot can generate junk messages on stdout, so we prefix our output with a
560 # line break to further ensure that.
561 print("\n" + json.dumps(output, sort_keys=True))