blob: 7bbd3228f105b07a0c1c43562019349de9461640 [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
Yury Khmelc0a18442022-11-01 16:56:22 -070036from chromite.service import packages
David Rileyc0da9d92016-02-01 12:11:01 -080037
38
39# Dir where all the action happens.
Alex Klein1699fab2022-09-08 08:46:06 -060040_OVERLAY_DIR = "%(srcroot)s/private-overlays/project-cheets-private/"
David Rileyc0da9d92016-02-01 12:11:01 -080041
Junichi Uekawa6d61ab02020-04-15 14:52:28 +090042_GIT_COMMIT_MESSAGE = """Marking latest for %(android_package)s ebuild with \
43version %(android_version)s as stable.
44
45BUG=None
46TEST=CQ
47"""
David Rileyc0da9d92016-02-01 12:11:01 -080048
Shao-Chuan Lee3aa76522021-12-03 17:18:58 +090049_GIT_COMMIT_MESSAGE_LKGB = """%(android_package)s: Mark version \
50%(android_version)s as LKGB.
51
52Generated by Android PFQ. This change will trigger PUpr to actually submit an
53Android uprev.
54
55For details, see "Migration plan" section of go/android-uprev-recipes.
56
57BUG=None
58TEST=CQ
59"""
60
Alex Klein1699fab2022-09-08 08:46:06 -060061_RUNTIME_ARTIFACTS_BUCKET_URL = "gs://chromeos-arc-images/runtime_artifacts"
David Rileyc0da9d92016-02-01 12:11:01 -080062
David Rileyc0da9d92016-02-01 12:11:01 -080063
64def FindAndroidCandidates(package_dir):
Alex Klein1699fab2022-09-08 08:46:06 -060065 """Return a tuple of Android's unstable ebuild and stable ebuilds.
David Rileyc0da9d92016-02-01 12:11:01 -080066
Alex Klein1699fab2022-09-08 08:46:06 -060067 Args:
68 package_dir: The path to where the package ebuild is stored.
David Rileyc0da9d92016-02-01 12:11:01 -080069
Alex Klein1699fab2022-09-08 08:46:06 -060070 Returns:
71 Tuple [unstable_ebuild, stable_ebuilds].
David Rileyc0da9d92016-02-01 12:11:01 -080072
Alex Klein1699fab2022-09-08 08:46:06 -060073 Raises:
74 Exception: if no unstable ebuild exists for Android.
75 """
76 stable_ebuilds = []
77 unstable_ebuilds = []
78 for path in glob.glob(os.path.join(package_dir, "*.ebuild")):
79 ebuild = portage_util.EBuild(path)
80 if ebuild.version == "9999":
81 unstable_ebuilds.append(ebuild)
82 else:
83 stable_ebuilds.append(ebuild)
David Rileyc0da9d92016-02-01 12:11:01 -080084
Alex Klein1699fab2022-09-08 08:46:06 -060085 # Apply some confidence checks.
86 if not unstable_ebuilds:
87 raise Exception("Missing 9999 ebuild for %s" % package_dir)
88 if not stable_ebuilds:
89 logging.warning("Missing stable ebuild for %s", package_dir)
David Rileyc0da9d92016-02-01 12:11:01 -080090
Alex Klein1699fab2022-09-08 08:46:06 -060091 return portage_util.BestEBuild(unstable_ebuilds), stable_ebuilds
David Rileyc0da9d92016-02-01 12:11:01 -080092
93
Shao-Chuan Lee085e3d72020-05-11 16:00:42 +090094def PrintUprevMetadata(build_branch, stable_candidate, new_ebuild):
Alex Klein1699fab2022-09-08 08:46:06 -060095 """Shows metadata on buildbot page at UprevAndroid step.
David Rileyc0da9d92016-02-01 12:11:01 -080096
Alex Klein1699fab2022-09-08 08:46:06 -060097 Args:
98 build_branch: The branch of Android builds.
99 stable_candidate: The existing stable ebuild.
100 new_ebuild: The newly written ebuild.
David Rileyc0da9d92016-02-01 12:11:01 -0800101 """
Alex Klein1699fab2022-09-08 08:46:06 -0600102 # Examples:
103 # "android-container-pi revved 6461825-r1 -> 6468247-r1"
104 # "android-container-pi revved 6461825-r1 -> 6461825-r2 (ebuild update only)"
105 msg = "%s revved %s -> %s" % (
106 stable_candidate.pkgname,
107 stable_candidate.version,
108 new_ebuild.version,
109 )
David Rileyc0da9d92016-02-01 12:11:01 -0800110
Alex Klein1699fab2022-09-08 08:46:06 -0600111 old_android = stable_candidate.version_no_rev
112 new_android = new_ebuild.version_no_rev
David Rileyc0da9d92016-02-01 12:11:01 -0800113
Alex Klein1699fab2022-09-08 08:46:06 -0600114 if old_android == new_android:
115 msg += " (ebuild update only)"
116 else:
117 ab_link = (
118 "https://android-build.googleplex.com"
119 "/builds/%s/branches/%s/cls?end=%s"
120 % (new_android, build_branch, old_android)
121 )
122 cbuildbot_alerts.PrintBuildbotLink("Android changelog", ab_link)
David Rileyc0da9d92016-02-01 12:11:01 -0800123
Alex Klein1699fab2022-09-08 08:46:06 -0600124 cbuildbot_alerts.PrintBuildbotStepText(msg)
125 cbuildbot_alerts.PrintKitchenSetBuildProperty(
126 "android_uprev",
127 json.dumps(
128 {
129 "branch": build_branch,
130 "new": new_ebuild.version,
131 "old": stable_candidate.version,
132 "pkgname": stable_candidate.pkgname,
133 }
134 ),
135 )
David Rileyc0da9d92016-02-01 12:11:01 -0800136
khmel@chromium.orgd3ec3d72020-04-29 15:57:35 -0700137
Alex Klein1699fab2022-09-08 08:46:06 -0600138def FindDataCollectorArtifacts(
Yury Khmelc0a18442022-11-01 16:56:22 -0700139 gs_context,
140 android_version,
141 package_name,
142 runtime_artifacts_bucket_url,
143 version_reference,
Alex Klein1699fab2022-09-08 08:46:06 -0600144):
145 r"""Finds and includes into variables artifacts from arc.DataCollector.
David Rileyc0da9d92016-02-01 12:11:01 -0800146
Alex Klein1699fab2022-09-08 08:46:06 -0600147 This is used from UpdateDataCollectorArtifacts in order to check the
148 particular version.
David Rileyc0da9d92016-02-01 12:11:01 -0800149
Alex Klein1699fab2022-09-08 08:46:06 -0600150 Args:
151 gs_context: context to execute gsutil
152 android_version: The \d+ build id of Android.
Yury Khmelc0a18442022-11-01 16:56:22 -0700153 package_name: android package name. Used as folder to locate the cache.
Alex Klein1699fab2022-09-08 08:46:06 -0600154 runtime_artifacts_bucket_url: root of runtime artifacts
Alex Klein1699fab2022-09-08 08:46:06 -0600155 version_reference: which version to use as a reference. Could be '${PV}' in
156 case version of data collector artifacts matches the
157 Android version or direct version in case of override.
David Rileyc0da9d92016-02-01 12:11:01 -0800158
Alex Klein1699fab2022-09-08 08:46:06 -0600159 Returns:
160 dictionary with filled ebuild variables. This dictionary is empty in case
161 no artificats are found.
162 """
163 variables = {}
David Rileyc0da9d92016-02-01 12:11:01 -0800164
Yury Khmel697863b2022-10-25 09:02:57 -0700165 buckets = ["ureadahead_pack_host", "gms_core_cache", "tts_cache"]
Alex Klein1699fab2022-09-08 08:46:06 -0600166 archs = ["arm", "arm64", "x86", "x86_64"]
167 build_types = ["user", "userdebug"]
David Rileyc0da9d92016-02-01 12:11:01 -0800168
Alex Klein1699fab2022-09-08 08:46:06 -0600169 for bucket in buckets:
170 for arch in archs:
171 for build_type in build_types:
Yury Khmelc0a18442022-11-01 16:56:22 -0700172 # TODO(b/255854925): remove path without |package_name|.
173 # |package_name| is required to separate artefacts for bertha
174 # and cheets.
175 root_paths = [
176 f"{runtime_artifacts_bucket_url}/{package_name}/{bucket}_{arch}_{build_type}",
177 f"{runtime_artifacts_bucket_url}/{bucket}_{arch}_{build_type}",
178 ]
179
180 for _, root_path in enumerate(root_paths):
181 path = f"{root_path}_{android_version}.tar"
182 if gs_context.Exists(path):
183 variables[
184 (f"{arch}_{build_type}_{bucket}").upper()
185 ] = f"{root_path}_{version_reference}.tar"
186 break
Alex Klein1699fab2022-09-08 08:46:06 -0600187
188 return variables
189
190
191def UpdateDataCollectorArtifacts(
Yury Khmelc0a18442022-11-01 16:56:22 -0700192 android_version, runtime_artifacts_bucket_url, package_name
Alex Klein1699fab2022-09-08 08:46:06 -0600193):
194 r"""Finds and includes into variables artifacts from arc.DataCollector.
195
196 This verifies default android version. In case artificts are not found for
197 default Android version it tries to find artifacts for pinned version. If
198 pinned version is provided, it is required artifacts exist for the pinned
199 version.
200
201 Args:
202 android_version: The \d+ build id of Android.
203 runtime_artifacts_bucket_url: root of runtime artifacts
Yury Khmelc0a18442022-11-01 16:56:22 -0700204 package_name: android package name. Used to determine the pinned version if exists.
Alex Klein1699fab2022-09-08 08:46:06 -0600205
206 Returns:
207 dictionary with filled ebuild variables.
208 """
209
210 gs_context = gs.GSContext()
211 # Check the existing version. If we find any artifacts, use them.
212 variables = FindDataCollectorArtifacts(
Yury Khmelc0a18442022-11-01 16:56:22 -0700213 gs_context,
214 android_version,
215 package_name,
216 runtime_artifacts_bucket_url,
217 "${PV}",
Alex Klein1699fab2022-09-08 08:46:06 -0600218 )
219 if variables:
220 # Data artificts were found.
221 return variables
222
223 # Check pinned version for the current branch.
Yury Khmelc0a18442022-11-01 16:56:22 -0700224 milestone = packages.determine_milestone_version()
225 pin_path = f"{runtime_artifacts_bucket_url}/{package_name}/M{milestone}_pin_version"
Alex Klein1699fab2022-09-08 08:46:06 -0600226 if not gs_context.Exists(pin_path):
227 # No pinned version.
228 logging.warning(
229 "No data collector artifacts were found for %s", android_version
230 )
231 return variables
232
233 pin_version = gs_context.Cat(pin_path, encoding="utf-8").rstrip()
234 logging.info("Pinned version %s overrides %s", pin_version, android_version)
235 variables = FindDataCollectorArtifacts(
Yury Khmelc0a18442022-11-01 16:56:22 -0700236 gs_context,
237 pin_version,
238 package_name,
239 runtime_artifacts_bucket_url,
240 pin_version,
Alex Klein1699fab2022-09-08 08:46:06 -0600241 )
242 if not variables:
243 # If pin version set it must contain data.
244 raise Exception(
Yury Khmelc0a18442022-11-01 16:56:22 -0700245 "Pinned version %s does not contain artificats" % pin_path
Alex Klein1699fab2022-09-08 08:46:06 -0600246 )
Alex Klein1699fab2022-09-08 08:46:06 -0600247 return variables
248
249
250def MarkAndroidEBuildAsStable(
251 stable_candidate,
252 unstable_ebuild,
253 android_package,
254 android_version,
255 package_dir,
Alex Klein1699fab2022-09-08 08:46:06 -0600256 arc_bucket_url,
257 runtime_artifacts_bucket_url,
258):
259 r"""Uprevs the Android ebuild.
260
261 This is the main function that uprevs from a stable candidate
262 to its new version.
263
264 Args:
265 stable_candidate: ebuild that corresponds to the stable ebuild we are
266 revving from. If None, builds the a new ebuild given the version
267 with revision set to 1.
268 unstable_ebuild: ebuild corresponding to the unstable ebuild for Android.
269 android_package: android package name.
270 android_version: The \d+ build id of Android.
271 package_dir: Path to the android-container package dir.
Alex Klein1699fab2022-09-08 08:46:06 -0600272 arc_bucket_url: URL of the target ARC build gs bucket.
273 runtime_artifacts_bucket_url: root of runtime artifacts
274
275 Returns:
276 Tuple[str, List[str], List[str]] if revved, or None
277 1. Full portage version atom (including rc's, etc) that was revved.
278 2. List of files to be `git add`ed.
279 3. List of files to be `git rm`ed.
280 """
281
282 def IsTheNewEBuildRedundant(new_ebuild, stable_ebuild):
283 """Returns True if the new ebuild is redundant.
284
285 This is True if there if the current stable ebuild is the exact same copy
286 of the new one.
287 """
288 if not stable_ebuild:
289 return False
290
291 if stable_candidate.version_no_rev == new_ebuild.version_no_rev:
292 return filecmp.cmp(
293 new_ebuild.ebuild_path, stable_ebuild.ebuild_path, shallow=False
294 )
295 return False
296
297 # Case where we have the last stable candidate with same version just rev.
298 if stable_candidate and stable_candidate.version_no_rev == android_version:
299 new_ebuild_path = "%s-r%d.ebuild" % (
300 stable_candidate.ebuild_path_no_revision,
301 stable_candidate.current_revision + 1,
302 )
303 else:
304 pf = "%s-%s-r1" % (android_package, android_version)
305 new_ebuild_path = os.path.join(package_dir, "%s.ebuild" % pf)
306
Shao-Chuan Lee20c51d82022-10-26 21:56:56 +0900307 # TODO(b/255705023): Remove build_branch once the following no longer relies
308 # on it.
309 build_branch = android.GetAndroidBranchForPackage(android_package)
Shao-Chuan Leeca2cbcc2022-11-02 08:28:31 +0900310 build_targets = android.GetAndroidEbuildTargetsForPackage(android_package)
Alex Klein1699fab2022-09-08 08:46:06 -0600311 variables = {"BASE_URL": arc_bucket_url}
312 for var, target in build_targets.items():
Shao-Chuan Lee20c51d82022-10-26 21:56:56 +0900313 # TODO(b/255705023): Have MirrorArtifacts generate the mapping for us.
Alex Klein1699fab2022-09-08 08:46:06 -0600314 variables[var] = f"{build_branch}-linux-{target}"
315
316 variables.update(
317 UpdateDataCollectorArtifacts(
Yury Khmelc0a18442022-11-01 16:56:22 -0700318 android_version,
319 runtime_artifacts_bucket_url,
320 android_package,
Alex Klein1699fab2022-09-08 08:46:06 -0600321 )
322 )
323
324 portage_util.EBuild.MarkAsStable(
325 unstable_ebuild.ebuild_path,
326 new_ebuild_path,
327 variables,
328 make_stable=True,
329 )
330 new_ebuild = portage_util.EBuild(new_ebuild_path)
331
332 # Determine whether this is ebuild is redundant.
333 if IsTheNewEBuildRedundant(new_ebuild, stable_candidate):
334 msg = "Previous ebuild with same version found and ebuild is redundant."
335 logging.info(msg)
336 cbuildbot_alerts.PrintBuildbotStepText(
337 "%s %s not revved"
338 % (stable_candidate.pkgname, stable_candidate.version)
339 )
340 osutils.SafeUnlink(new_ebuild_path)
341 return None
342
343 # PFQ runs should always be able to find a stable candidate.
344 if stable_candidate:
345 PrintUprevMetadata(build_branch, stable_candidate, new_ebuild)
346
347 files_to_add = [new_ebuild_path]
348 files_to_remove = []
349 if stable_candidate and not stable_candidate.IsSticky():
350 osutils.SafeUnlink(stable_candidate.ebuild_path)
351 files_to_remove.append(stable_candidate.ebuild_path)
352
353 # Update ebuild manifest and git add it.
354 gen_manifest_cmd = ["ebuild", new_ebuild_path, "manifest", "--force"]
355 cros_build_lib.run(gen_manifest_cmd, extra_env=None, print_cmd=True)
356 files_to_add.append(os.path.join(package_dir, "Manifest"))
357
358 return (
359 f"{new_ebuild.package}-{new_ebuild.version}",
360 files_to_add,
361 files_to_remove,
362 )
David Rileyc0da9d92016-02-01 12:11:01 -0800363
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900364
Shao-Chuan Lee007dbe82021-02-09 14:05:39 +0900365def _PrepareGitBranch(overlay_dir):
Alex Klein1699fab2022-09-08 08:46:06 -0600366 """Prepares a git branch for the uprev commit.
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900367
Alex Klein1699fab2022-09-08 08:46:06 -0600368 If the overlay project is currently on a branch (e.g. patches are being
369 applied), rebase the new branch on top of it.
Shao-Chuan Lee007dbe82021-02-09 14:05:39 +0900370
Alex Klein1699fab2022-09-08 08:46:06 -0600371 Args:
372 overlay_dir: The overlay directory.
373 """
374 existing_branch = git.GetCurrentBranch(overlay_dir)
375 repo_util.Repository.MustFind(overlay_dir).StartBranch(
376 constants.STABLE_EBUILD_BRANCH, projects=["."], cwd=overlay_dir
377 )
378 if existing_branch:
379 git.RunGit(overlay_dir, ["rebase", existing_branch])
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900380
381
382def _CommitChange(message, android_package_dir, files_to_add, files_to_remove):
Alex Klein1699fab2022-09-08 08:46:06 -0600383 """Commit changes to git with list of files to add/remove."""
384 git.RunGit(android_package_dir, ["add", "--"] + files_to_add)
385 if files_to_remove:
386 git.RunGit(android_package_dir, ["rm", "--"] + files_to_remove)
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900387
Alex Klein1699fab2022-09-08 08:46:06 -0600388 portage_util.EBuild.CommitChange(message, android_package_dir)
David Rileyc0da9d92016-02-01 12:11:01 -0800389
390
391def GetParser():
Alex Klein1699fab2022-09-08 08:46:06 -0600392 """Creates the argument parser."""
393 parser = commandline.ArgumentParser()
394 parser.add_argument("-b", "--boards")
395 parser.add_argument(
396 "--android_bucket_url",
397 default=android.ANDROID_BUCKET_URL,
398 type="gs_path",
399 )
Shao-Chuan Lee20c51d82022-10-26 21:56:56 +0900400 # TODO(b/255705023): Clean this up.
Alex Klein1699fab2022-09-08 08:46:06 -0600401 parser.add_argument(
402 "--android_build_branch",
Shao-Chuan Lee20c51d82022-10-26 21:56:56 +0900403 help="DEPRECATED",
Alex Klein1699fab2022-09-08 08:46:06 -0600404 )
405 parser.add_argument(
406 "--android_package",
407 required=True,
Shao-Chuan Leeca2cbcc2022-11-02 08:28:31 +0900408 choices=android.GetAllAndroidPackages(),
Alex Klein1699fab2022-09-08 08:46:06 -0600409 help="Android package to uprev",
410 )
411 parser.add_argument(
412 "--arc_bucket_url", default=constants.ARC_BUCKET_URL, type="gs_path"
413 )
414 parser.add_argument("-f", "--force_version", help="Android build id to use")
415 parser.add_argument(
416 "-s",
417 "--srcroot",
418 default=os.path.join(constants.SOURCE_ROOT, "src"),
419 help="Path to the src directory",
420 )
421 parser.add_argument(
422 "--runtime_artifacts_bucket_url",
423 default=_RUNTIME_ARTIFACTS_BUCKET_URL,
424 type="gs_path",
425 )
426 parser.add_argument(
427 "--skip_commit",
428 action="store_true",
429 help="Skip commiting uprev changes to git",
430 )
431 parser.add_argument(
432 "--update_lkgb",
433 action="store_true",
434 help=(
435 "Update the LKGB file instead of uprevving ebuilds "
436 "and populating artifacts. "
437 "Requires --force_version be set."
438 ),
439 )
440 return parser
David Rileyc0da9d92016-02-01 12:11:01 -0800441
442
443def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -0600444 cbuildbot_alerts.EnableBuildbotMarkers()
445 parser = GetParser()
446 options = parser.parse_args(argv)
447 options.Freeze()
David Rileyc0da9d92016-02-01 12:11:01 -0800448
Alex Klein1699fab2022-09-08 08:46:06 -0600449 overlay_dir = os.path.abspath(_OVERLAY_DIR % {"srcroot": options.srcroot})
450 android_package_dir = android.GetAndroidPackageDir(
451 options.android_package, overlay_dir=overlay_dir
452 )
Shao-Chuan Lee3aa76522021-12-03 17:18:58 +0900453
454 if not options.skip_commit:
Alex Klein1699fab2022-09-08 08:46:06 -0600455 _PrepareGitBranch(overlay_dir)
Shao-Chuan Lee3aa76522021-12-03 17:18:58 +0900456
Alex Klein1699fab2022-09-08 08:46:06 -0600457 if options.update_lkgb:
458 if not options.force_version:
459 raise Exception("--force_version is required with --update_lkgb")
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900460
Alex Klein1699fab2022-09-08 08:46:06 -0600461 # Attempt to read current LKGB, if available.
462 current_lkgb = None
463 try:
464 current_lkgb = android.ReadLKGB(android_package_dir)
465 except android.MissingLKGBError:
466 logging.info("LKGB file is missing, creating a new one.")
467 except android.InvalidLKGBError:
468 logging.warning("Current LKGB file is invalid, overwriting.")
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900469
Alex Klein1699fab2022-09-08 08:46:06 -0600470 # Do nothing if LKGB is already set to the requested version.
471 if current_lkgb == options.force_version:
472 logging.warning(
473 "LKGB of %s is already %s, doing nothing.",
474 options.android_package,
475 options.force_version,
476 )
477 cbuildbot_alerts.PrintBuildbotStepText(
478 "LKGB of %s unchanged @ %s"
479 % (options.android_package, options.force_version)
480 )
481 return
David Rileyc0da9d92016-02-01 12:11:01 -0800482
Alex Klein1699fab2022-09-08 08:46:06 -0600483 # Actually update the LKGB.
484 path = android.WriteLKGB(android_package_dir, options.force_version)
485 cbuildbot_alerts.PrintBuildbotStepText(
486 "LKGB of %s updated %s -> %s"
487 % (options.android_package, current_lkgb, options.force_version)
488 )
David Rileyc0da9d92016-02-01 12:11:01 -0800489
Alex Klein1699fab2022-09-08 08:46:06 -0600490 if not options.skip_commit:
491 _CommitChange(
492 _GIT_COMMIT_MESSAGE_LKGB
493 % {
494 "android_package": options.android_package,
495 "android_version": options.force_version,
496 },
497 android_package_dir,
498 [path],
499 [],
500 )
501 return
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900502
Shao-Chuan Lee20c51d82022-10-26 21:56:56 +0900503 # TODO(b/255705023): Clean this up.
504 android_build_branch = android.GetAndroidBranchForPackage(
505 options.android_package
Alex Klein1699fab2022-09-08 08:46:06 -0600506 )
Shao-Chuan Lee20c51d82022-10-26 21:56:56 +0900507 if (
508 options.android_build_branch
509 and options.android_build_branch != android_build_branch
510 ):
511 logging.warning(
512 "--android_build_branch is deprecated; using default branch %s",
513 android_build_branch,
514 )
Shao-Chuan Leedea458f2021-11-25 23:46:53 +0900515
Alex Klein1699fab2022-09-08 08:46:06 -0600516 (unstable_ebuild, stable_ebuilds) = FindAndroidCandidates(
517 android_package_dir
518 )
519 # Mirror artifacts, i.e., images and some sdk tools (e.g., adb, aapt).
520 version_to_uprev = android.MirrorArtifacts(
Shao-Chuan Lee20c51d82022-10-26 21:56:56 +0900521 options.android_package,
Alex Klein1699fab2022-09-08 08:46:06 -0600522 options.android_bucket_url,
Alex Klein1699fab2022-09-08 08:46:06 -0600523 options.arc_bucket_url,
524 android_package_dir,
525 options.force_version,
526 )
David Rileyc0da9d92016-02-01 12:11:01 -0800527
Alex Klein1699fab2022-09-08 08:46:06 -0600528 stable_candidate = portage_util.BestEBuild(stable_ebuilds)
Shao-Chuan Leedea458f2021-11-25 23:46:53 +0900529
Alex Klein1699fab2022-09-08 08:46:06 -0600530 if stable_candidate:
531 logging.info("Stable candidate found %s", stable_candidate.version)
532 else:
533 logging.info("No stable candidate found.")
534
535 revved = MarkAndroidEBuildAsStable(
536 stable_candidate,
537 unstable_ebuild,
538 options.android_package,
539 version_to_uprev,
540 android_package_dir,
Alex Klein1699fab2022-09-08 08:46:06 -0600541 options.arc_bucket_url,
542 options.runtime_artifacts_bucket_url,
543 )
544
545 output = dict(revved=bool(revved))
546
547 if revved:
548 android_atom, files_to_add, files_to_remove = revved
549 if not options.skip_commit:
550 _CommitChange(
551 _GIT_COMMIT_MESSAGE
552 % {
553 "android_package": options.android_package,
554 "android_version": version_to_uprev,
555 },
556 android_package_dir,
557 files_to_add,
558 files_to_remove,
559 )
560 if options.boards:
561 cros_mark_as_stable.CleanStalePackages(
562 options.srcroot, options.boards.split(":"), [android_atom]
563 )
564
565 output["android_atom"] = android_atom
566 # This field is read by the PUpr uprev handler for creating CLs. We cannot
567 # return absolute paths because this script runs inside chroot but the uprev
568 # handler runs outside.
569 # Here we return paths relative to |overlay_dir|.
570 output["modified_files"] = [
571 os.path.relpath(f, overlay_dir)
572 for f in files_to_add + files_to_remove
573 ]
574
575 # The output is being parsed by service.packages.uprev_android and has to be
576 # in its own single line. When invoked from chromite API endpoints, entering
577 # chroot can generate junk messages on stdout, so we prefix our output with a
578 # line break to further ensure that.
579 print("\n" + json.dumps(output, sort_keys=True))