blob: 757cae718400830d64d8f272f132685e44a1569f [file] [log] [blame]
David Rileyc0da9d92016-02-01 12:11:01 -08001# Copyright 2016 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""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_build_branch=git_pi-arc \
13 --android_package=android-container-pi
Shuhei Takahashi6d02c192017-04-05 14:01:24 +090014
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +090015Returns {"android_atom": "chromeos-base/android-container-pi-6417892-r1"}
David Rileyc0da9d92016-02-01 12:11:01 -080016
Shao-Chuan Lee9c39e0c2020-04-24 11:40:34 +090017emerge-eve =chromeos-base/android-container-pi-6417892-r1
David Rileyc0da9d92016-02-01 12:11:01 -080018"""
19
David Rileyc0da9d92016-02-01 12:11:01 -080020import filecmp
21import glob
Junichi Uekawad21f94d2020-07-27 15:50:05 +090022import json
Chris McDonaldb55b7032021-06-17 16:41:32 -060023import logging
David Rileyc0da9d92016-02-01 12:11:01 -080024import os
25
Chris McDonaldb55b7032021-06-17 16:41:32 -060026from chromite.cbuildbot import cbuildbot_alerts
David Rileyc0da9d92016-02-01 12:11:01 -080027from chromite.lib import commandline
Chris McDonaldb55b7032021-06-17 16:41:32 -060028from chromite.lib import constants
David Rileyc0da9d92016-02-01 12:11:01 -080029from chromite.lib import cros_build_lib
David Rileyc0da9d92016-02-01 12:11:01 -080030from chromite.lib import git
31from chromite.lib import gs
Shao-Chuan Lee301a4192021-02-08 11:53:49 +090032from chromite.lib import osutils
David Rileyc0da9d92016-02-01 12:11:01 -080033from chromite.lib import portage_util
Shao-Chuan Lee007dbe82021-02-09 14:05:39 +090034from chromite.lib import repo_util
David Rileyc0da9d92016-02-01 12:11:01 -080035from chromite.scripts import cros_mark_as_stable
Shao-Chuan Lee7fd0ca12021-03-19 15:57:40 +090036from chromite.service import android
David Rileyc0da9d92016-02-01 12:11:01 -080037
38
39# Dir where all the action happens.
40_OVERLAY_DIR = '%(srcroot)s/private-overlays/project-cheets-private/'
41
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
khmel@chromium.orgd3ec3d72020-04-29 15:57:35 -070061_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):
65 """Return a tuple of Android's unstable ebuild and stable ebuilds.
66
67 Args:
68 package_dir: The path to where the package ebuild is stored.
69
70 Returns:
71 Tuple [unstable_ebuild, stable_ebuilds].
72
73 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)
84
85 # Apply some sanity checks.
86 if not unstable_ebuilds:
87 raise Exception('Missing 9999 ebuild for %s' % package_dir)
88 if not stable_ebuilds:
Lann Martinffb95162018-08-28 12:02:54 -060089 logging.warning('Missing stable ebuild for %s', package_dir)
David Rileyc0da9d92016-02-01 12:11:01 -080090
91 return portage_util.BestEBuild(unstable_ebuilds), stable_ebuilds
92
93
Shao-Chuan Lee085e3d72020-05-11 16:00:42 +090094def PrintUprevMetadata(build_branch, stable_candidate, new_ebuild):
95 """Shows metadata on buildbot page at UprevAndroid step.
David Rileyc0da9d92016-02-01 12:11:01 -080096
97 Args:
Shao-Chuan Lee085e3d72020-05-11 16:00:42 +090098 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 """
Shao-Chuan Lee085e3d72020-05-11 16:00:42 +0900102 # 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' % (stable_candidate.pkgname,
106 stable_candidate.version,
107 new_ebuild.version)
108
109 old_android = stable_candidate.version_no_rev
110 new_android = new_ebuild.version_no_rev
111
112 if old_android == new_android:
113 msg += ' (ebuild update only)'
114 else:
115 ab_link = ('https://android-build.googleplex.com'
116 '/builds/%s/branches/%s/cls?end=%s'
117 % (new_android, build_branch, old_android))
Chris McDonaldb55b7032021-06-17 16:41:32 -0600118 cbuildbot_alerts.PrintBuildbotLink('Android changelog', ab_link)
Shao-Chuan Lee085e3d72020-05-11 16:00:42 +0900119
Chris McDonaldb55b7032021-06-17 16:41:32 -0600120 cbuildbot_alerts.PrintBuildbotStepText(msg)
121 cbuildbot_alerts.PrintKitchenSetBuildProperty('android_uprev', json.dumps({
Junichi Uekawad21f94d2020-07-27 15:50:05 +0900122 'branch': build_branch,
123 'new': new_ebuild.version,
124 'old': stable_candidate.version,
125 'pkgname': stable_candidate.pkgname,
126 }))
David Rileyc0da9d92016-02-01 12:11:01 -0800127
128
Yury Khmelb009aeb2020-08-19 19:40:00 -0700129def FindDataCollectorArtifacts(gs_context,
130 android_version,
131 runtime_artifacts_bucket_url,
132 version_reference):
khmel@chromium.orgd3ec3d72020-04-29 15:57:35 -0700133 r"""Finds and includes into variables artifacts from arc.DataCollector.
134
Yury Khmelb009aeb2020-08-19 19:40:00 -0700135 This is used from UpdateDataCollectorArtifacts in order to check the
136 particular version.
137
khmel@chromium.orgd3ec3d72020-04-29 15:57:35 -0700138 Args:
Yury Khmelb009aeb2020-08-19 19:40:00 -0700139 gs_context: context to execute gsutil
khmel@chromium.orgd3ec3d72020-04-29 15:57:35 -0700140 android_version: The \d+ build id of Android.
141 runtime_artifacts_bucket_url: root of runtime artifacts
Yury Khmelb009aeb2020-08-19 19:40:00 -0700142 build_branch: build branch. Used to determine the pinned version if exists.
143 version_reference: which version to use as a reference. Could be '${PV}' in
144 case version of data collector artifacts matches the
145 Android version or direct version in case of override.
khmel@chromium.orgd3ec3d72020-04-29 15:57:35 -0700146
147 Returns:
Yury Khmelb009aeb2020-08-19 19:40:00 -0700148 dictionary with filled ebuild variables. This dictionary is empty in case
149 no artificats are found.
khmel@chromium.orgd3ec3d72020-04-29 15:57:35 -0700150 """
khmel@chromium.orgd3ec3d72020-04-29 15:57:35 -0700151 variables = {}
Yury Khmelb009aeb2020-08-19 19:40:00 -0700152
khmel@chromium.orgd3ec3d72020-04-29 15:57:35 -0700153 buckets = ['ureadahead_pack', 'gms_core_cache']
154 archs = ['arm', 'arm64', 'x86', 'x86_64']
155 build_types = ['user', 'userdebug']
156
khmel@chromium.orgd3ec3d72020-04-29 15:57:35 -0700157 for bucket in buckets:
158 for arch in archs:
159 for build_type in build_types:
160 path = (f'{runtime_artifacts_bucket_url}/{bucket}_{arch}_{build_type}_'
Yury Khmele1b74402020-05-18 08:41:35 -0700161 f'{android_version}.tar')
khmel@chromium.orgd3ec3d72020-04-29 15:57:35 -0700162 if gs_context.Exists(path):
163 variables[(f'{arch}_{build_type}_{bucket}').upper()] = (
164 f'{runtime_artifacts_bucket_url}/{bucket}_{arch}_{build_type}_'
165 f'{version_reference}.tar')
166
167 return variables
168
169
Yury Khmelb009aeb2020-08-19 19:40:00 -0700170def UpdateDataCollectorArtifacts(android_version,
171 runtime_artifacts_bucket_url,
172 build_branch):
173 r"""Finds and includes into variables artifacts from arc.DataCollector.
174
175 This verifies default android version. In case artificts are not found for
176 default Android version it tries to find artifacts for pinned version. If
177 pinned version is provided, it is required artifacts exist for the pinned
178 version.
179
180 Args:
181 android_version: The \d+ build id of Android.
182 runtime_artifacts_bucket_url: root of runtime artifacts
183 build_branch: build branch. Used to determine the pinned version if exists.
184
185 Returns:
186 dictionary with filled ebuild variables.
187 """
188
189 gs_context = gs.GSContext()
190 # Check the existing version. If we find any artifacts, use them.
191 variables = FindDataCollectorArtifacts(gs_context,
192 android_version,
193 runtime_artifacts_bucket_url,
194 '${PV}')
195 if variables:
196 # Data artificts were found.
197 return variables
198
199 # Check pinned version for the current branch.
200 pin_path = (f'{runtime_artifacts_bucket_url}/{build_branch}_pin_version')
201 if not gs_context.Exists(pin_path):
202 # No pinned version.
203 logging.warning(
204 'No data collector artifacts were found for %s',
205 android_version)
206 return variables
207
208 pin_version = gs_context.Cat(pin_path, encoding='utf-8').rstrip()
209 logging.info('Pinned version %s overrides %s',
210 pin_version, android_version)
211 variables = FindDataCollectorArtifacts(gs_context,
212 pin_version,
213 runtime_artifacts_bucket_url,
214 pin_version)
215 if not variables:
216 # If pin version set it must contain data.
217 raise Exception('Pinned version %s:%s does not contain artificats' % (
218 build_branch, pin_version))
219
220 return variables
221
222
Hidehiko Abe4fd94ae2017-01-24 18:59:55 +0900223def MarkAndroidEBuildAsStable(stable_candidate, unstable_ebuild,
224 android_package, android_version, package_dir,
khmel@chromium.orgd3ec3d72020-04-29 15:57:35 -0700225 build_branch, arc_bucket_url,
Shao-Chuan Lee7e9eacb2021-03-19 16:45:21 +0900226 runtime_artifacts_bucket_url):
David Rileyc0da9d92016-02-01 12:11:01 -0800227 r"""Uprevs the Android ebuild.
228
229 This is the main function that uprevs from a stable candidate
230 to its new version.
231
232 Args:
233 stable_candidate: ebuild that corresponds to the stable ebuild we are
234 revving from. If None, builds the a new ebuild given the version
235 with revision set to 1.
236 unstable_ebuild: ebuild corresponding to the unstable ebuild for Android.
Hidehiko Abe4fd94ae2017-01-24 18:59:55 +0900237 android_package: android package name.
David Rileyc0da9d92016-02-01 12:11:01 -0800238 android_version: The \d+ build id of Android.
David Rileyc0da9d92016-02-01 12:11:01 -0800239 package_dir: Path to the android-container package dir.
David Riley73f00d92016-02-16 18:54:20 -0800240 build_branch: branch of Android builds.
241 arc_bucket_url: URL of the target ARC build gs bucket.
khmel@chromium.orgd3ec3d72020-04-29 15:57:35 -0700242 runtime_artifacts_bucket_url: root of runtime artifacts
David Rileyc0da9d92016-02-01 12:11:01 -0800243
244 Returns:
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900245 Tuple[str, List[str], List[str]] if revved, or None
246 1. Full portage version atom (including rc's, etc) that was revved.
247 2. List of files to be `git add`ed.
248 3. List of files to be `git rm`ed.
David Rileyc0da9d92016-02-01 12:11:01 -0800249 """
250 def IsTheNewEBuildRedundant(new_ebuild, stable_ebuild):
251 """Returns True if the new ebuild is redundant.
252
253 This is True if there if the current stable ebuild is the exact same copy
254 of the new one.
255 """
256 if not stable_ebuild:
257 return False
258
David Riley676f5402016-02-12 17:24:23 -0800259 if stable_candidate.version_no_rev == new_ebuild.version_no_rev:
David Rileyc0da9d92016-02-01 12:11:01 -0800260 return filecmp.cmp(
261 new_ebuild.ebuild_path, stable_ebuild.ebuild_path, shallow=False)
khmel@chromium.orgd3ec3d72020-04-29 15:57:35 -0700262 return False
David Rileyc0da9d92016-02-01 12:11:01 -0800263
264 # Case where we have the last stable candidate with same version just rev.
David Riley676f5402016-02-12 17:24:23 -0800265 if stable_candidate and stable_candidate.version_no_rev == android_version:
David Rileyc0da9d92016-02-01 12:11:01 -0800266 new_ebuild_path = '%s-r%d.ebuild' % (
267 stable_candidate.ebuild_path_no_revision,
268 stable_candidate.current_revision + 1)
269 else:
Hidehiko Abe4fd94ae2017-01-24 18:59:55 +0900270 pf = '%s-%s-r1' % (android_package, android_version)
David Rileyc0da9d92016-02-01 12:11:01 -0800271 new_ebuild_path = os.path.join(package_dir, '%s.ebuild' % pf)
272
Shao-Chuan Lee7e9eacb2021-03-19 16:45:21 +0900273 build_targets = constants.ANDROID_BRANCH_TO_BUILD_TARGETS[build_branch]
David Riley73f00d92016-02-16 18:54:20 -0800274 variables = {'BASE_URL': arc_bucket_url}
Shao-Chuan Lee41fe3522021-03-22 15:50:31 +0900275 for var, target in build_targets.items():
276 variables[var] = f'{build_branch}-linux-{target}'
David Rileyc0da9d92016-02-01 12:11:01 -0800277
khmel@chromium.orgd3ec3d72020-04-29 15:57:35 -0700278 variables.update(UpdateDataCollectorArtifacts(
Yury Khmelb009aeb2020-08-19 19:40:00 -0700279 android_version, runtime_artifacts_bucket_url, build_branch))
khmel@chromium.orgd3ec3d72020-04-29 15:57:35 -0700280
David Rileyc0da9d92016-02-01 12:11:01 -0800281 portage_util.EBuild.MarkAsStable(
282 unstable_ebuild.ebuild_path, new_ebuild_path,
283 variables, make_stable=True)
284 new_ebuild = portage_util.EBuild(new_ebuild_path)
285
286 # Determine whether this is ebuild is redundant.
287 if IsTheNewEBuildRedundant(new_ebuild, stable_candidate):
288 msg = 'Previous ebuild with same version found and ebuild is redundant.'
289 logging.info(msg)
Chris McDonaldb55b7032021-06-17 16:41:32 -0600290 cbuildbot_alerts.PrintBuildbotStepText('%s %s not revved'
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900291 % (stable_candidate.pkgname,
292 stable_candidate.version))
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900293 osutils.SafeUnlink(new_ebuild_path)
David Rileyc0da9d92016-02-01 12:11:01 -0800294 return None
295
Shao-Chuan Lee085e3d72020-05-11 16:00:42 +0900296 # PFQ runs should always be able to find a stable candidate.
David Rileyc0da9d92016-02-01 12:11:01 -0800297 if stable_candidate:
Shao-Chuan Lee085e3d72020-05-11 16:00:42 +0900298 PrintUprevMetadata(build_branch, stable_candidate, new_ebuild)
David Rileyc0da9d92016-02-01 12:11:01 -0800299
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900300 files_to_add = [new_ebuild_path]
301 files_to_remove = []
David Rileyc0da9d92016-02-01 12:11:01 -0800302 if stable_candidate and not stable_candidate.IsSticky():
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900303 osutils.SafeUnlink(stable_candidate.ebuild_path)
304 files_to_remove.append(stable_candidate.ebuild_path)
David Rileyc0da9d92016-02-01 12:11:01 -0800305
306 # Update ebuild manifest and git add it.
307 gen_manifest_cmd = ['ebuild', new_ebuild_path, 'manifest', '--force']
Mike Frysinger45602c72019-09-22 02:15:11 -0400308 cros_build_lib.run(gen_manifest_cmd, extra_env=None, print_cmd=True)
Shao-Chuan Lee556015c2021-11-26 00:03:28 +0900309 files_to_add.append(os.path.join(package_dir, 'Manifest'))
David Rileyc0da9d92016-02-01 12:11:01 -0800310
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900311 return (
312 f'{new_ebuild.package}-{new_ebuild.version}',
313 files_to_add,
314 files_to_remove,
315 )
David Rileyc0da9d92016-02-01 12:11:01 -0800316
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900317
Shao-Chuan Lee007dbe82021-02-09 14:05:39 +0900318def _PrepareGitBranch(overlay_dir):
319 """Prepares a git branch for the uprev commit.
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900320
Shao-Chuan Lee007dbe82021-02-09 14:05:39 +0900321 If the overlay project is currently on a branch (e.g. patches are being
322 applied), rebase the new branch on top of it.
323
324 Args:
325 overlay_dir: The overlay directory.
326 """
327 existing_branch = git.GetCurrentBranch(overlay_dir)
328 repo_util.Repository.MustFind(overlay_dir).StartBranch(
329 constants.STABLE_EBUILD_BRANCH, projects=['.'], cwd=overlay_dir)
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900330 if existing_branch:
331 git.RunGit(overlay_dir, ['rebase', existing_branch])
332
333
334def _CommitChange(message, android_package_dir, files_to_add, files_to_remove):
335 """Commit changes to git with list of files to add/remove."""
336 git.RunGit(android_package_dir, ['add', '--'] + files_to_add)
Shao-Chuan Lee3aa76522021-12-03 17:18:58 +0900337 if files_to_remove:
338 git.RunGit(android_package_dir, ['rm', '--'] + files_to_remove)
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900339
340 portage_util.EBuild.CommitChange(message, android_package_dir)
David Rileyc0da9d92016-02-01 12:11:01 -0800341
342
343def GetParser():
344 """Creates the argument parser."""
345 parser = commandline.ArgumentParser()
346 parser.add_argument('-b', '--boards')
347 parser.add_argument('--android_bucket_url',
Shao-Chuan Leee3940152021-03-25 14:44:36 +0900348 default=android.ANDROID_BUCKET_URL,
David Riley73f00d92016-02-16 18:54:20 -0800349 type='gs_path')
David Rileyc0da9d92016-02-01 12:11:01 -0800350 parser.add_argument('--android_build_branch',
Shao-Chuan Lee7e9eacb2021-03-19 16:45:21 +0900351 choices=constants.ANDROID_BRANCH_TO_BUILD_TARGETS,
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900352 help='Android branch to import from, overriding default')
Hidehiko Abe4fd94ae2017-01-24 18:59:55 +0900353 parser.add_argument('--android_package',
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900354 required=True,
355 choices=constants.ANDROID_ALL_PACKAGES,
356 help='Android package to uprev')
David Riley73f00d92016-02-16 18:54:20 -0800357 parser.add_argument('--arc_bucket_url',
358 default=constants.ARC_BUCKET_URL,
359 type='gs_path')
David Rileyc0da9d92016-02-01 12:11:01 -0800360 parser.add_argument('-f', '--force_version',
361 help='Android build id to use')
362 parser.add_argument('-s', '--srcroot',
Shao-Chuan Lee3aa76522021-12-03 17:18:58 +0900363 default=os.path.join(constants.SOURCE_ROOT, 'src'),
David Rileyc0da9d92016-02-01 12:11:01 -0800364 help='Path to the src directory')
khmel@chromium.orgd3ec3d72020-04-29 15:57:35 -0700365 parser.add_argument('--runtime_artifacts_bucket_url',
366 default=_RUNTIME_ARTIFACTS_BUCKET_URL,
367 type='gs_path')
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900368 parser.add_argument('--skip_commit',
369 action='store_true',
370 help='Skip commiting uprev changes to git')
Shao-Chuan Lee3aa76522021-12-03 17:18:58 +0900371 parser.add_argument('--update_lkgb',
372 action='store_true',
373 help=('Update the LKGB file instead of uprevving ebuilds '
374 'and populating artifacts. '
375 'Requires --force_version be set.'))
David Rileyc0da9d92016-02-01 12:11:01 -0800376 return parser
377
378
379def main(argv):
Chris McDonaldb55b7032021-06-17 16:41:32 -0600380 cbuildbot_alerts.EnableBuildbotMarkers()
David Rileyc0da9d92016-02-01 12:11:01 -0800381 parser = GetParser()
382 options = parser.parse_args(argv)
383 options.Freeze()
384
385 overlay_dir = os.path.abspath(_OVERLAY_DIR % {'srcroot': options.srcroot})
Shao-Chuan Lee0e787d22021-12-13 21:33:35 +0900386 android_package_dir = android.GetAndroidPackageDir(options.android_package,
387 overlay_dir=overlay_dir)
David Rileyc0da9d92016-02-01 12:11:01 -0800388
Shao-Chuan Lee3aa76522021-12-03 17:18:58 +0900389 if not options.skip_commit:
390 _PrepareGitBranch(overlay_dir)
391
392 if options.update_lkgb:
393 if not options.force_version:
394 raise Exception('--force_version is required with --update_lkgb')
395
Shao-Chuan Lee6cd897a2021-12-10 14:04:32 +0900396 # Attempt to read current LKGB, if available.
397 current_lkgb = None
398 try:
399 current_lkgb = android.ReadLKGB(android_package_dir)
400 except android.MissingLKGBError:
401 logging.info('LKGB file is missing, creating a new one.')
402 except android.InvalidLKGBError:
403 logging.warning('Current LKGB file is invalid, overwriting.')
404
Shao-Chuan Lee3aa76522021-12-03 17:18:58 +0900405 # Do nothing if LKGB is already set to the requested version.
Shao-Chuan Lee3aa76522021-12-03 17:18:58 +0900406 if current_lkgb == options.force_version:
407 logging.warning('LKGB of %s is already %s, doing nothing.',
408 options.android_package, options.force_version)
409 cbuildbot_alerts.PrintBuildbotStepText(
410 'LKGB of %s unchanged @ %s' % (options.android_package,
411 options.force_version))
412 return
413
414 # Actually update the LKGB.
415 path = android.WriteLKGB(android_package_dir, options.force_version)
416 cbuildbot_alerts.PrintBuildbotStepText(
417 'LKGB of %s updated %s -> %s' % (options.android_package,
418 current_lkgb, options.force_version))
419
420 if not options.skip_commit:
421 _CommitChange(
422 _GIT_COMMIT_MESSAGE_LKGB % {
423 'android_package': options.android_package,
424 'android_version': options.force_version},
425 android_package_dir,
426 [path],
427 [],
428 )
429 return
430
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900431 # Use default Android branch if not overridden.
432 android_build_branch = (
433 options.android_build_branch or
434 android.GetAndroidBranchForPackage(options.android_package))
435
David Rileyc0da9d92016-02-01 12:11:01 -0800436 (unstable_ebuild, stable_ebuilds) = FindAndroidCandidates(android_package_dir)
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900437 # Mirror artifacts, i.e., images and some sdk tools (e.g., adb, aapt).
Shao-Chuan Lee7fd0ca12021-03-19 15:57:40 +0900438 version_to_uprev = android.MirrorArtifacts(options.android_bucket_url,
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900439 android_build_branch,
Shao-Chuan Lee6865ff52021-03-19 18:29:18 +0900440 options.arc_bucket_url,
441 android_package_dir,
Shao-Chuan Lee7fd0ca12021-03-19 15:57:40 +0900442 options.force_version)
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900443
David Rileyc0da9d92016-02-01 12:11:01 -0800444 stable_candidate = portage_util.BestEBuild(stable_ebuilds)
445
446 if stable_candidate:
Lann Martinffb95162018-08-28 12:02:54 -0600447 logging.info('Stable candidate found %s', stable_candidate.version)
David Rileyc0da9d92016-02-01 12:11:01 -0800448 else:
449 logging.info('No stable candidate found.')
450
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900451 revved = MarkAndroidEBuildAsStable(
Hidehiko Abe4fd94ae2017-01-24 18:59:55 +0900452 stable_candidate, unstable_ebuild, options.android_package,
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900453 version_to_uprev, android_package_dir, android_build_branch,
454 options.arc_bucket_url, options.runtime_artifacts_bucket_url)
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900455
Shao-Chuan Lee6badd122021-11-30 14:27:51 +0900456 output = dict(revved=bool(revved))
Shao-Chuan Leedea458f2021-11-25 23:46:53 +0900457
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900458 if revved:
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900459 android_atom, files_to_add, files_to_remove = revved
Shao-Chuan Lee301a4192021-02-08 11:53:49 +0900460 if not options.skip_commit:
461 _CommitChange(
462 _GIT_COMMIT_MESSAGE % {'android_package': options.android_package,
463 'android_version': version_to_uprev},
464 android_package_dir,
465 files_to_add,
466 files_to_remove,
467 )
David Rileyc0da9d92016-02-01 12:11:01 -0800468 if options.boards:
469 cros_mark_as_stable.CleanStalePackages(options.srcroot,
470 options.boards.split(':'),
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900471 [android_atom])
David Rileyc0da9d92016-02-01 12:11:01 -0800472
Shao-Chuan Leedea458f2021-11-25 23:46:53 +0900473 output['android_atom'] = android_atom
Shao-Chuan Lee62cfbc72021-11-30 14:15:07 +0900474 # This field is read by the PUpr uprev handler for creating CLs. We cannot
475 # return absolute paths because this script runs inside chroot but the uprev
476 # handler runs outside.
477 # Here we return paths relative to |overlay_dir|.
478 output['modified_files'] = [os.path.relpath(f, overlay_dir)
479 for f in files_to_add + files_to_remove]
Shao-Chuan Leedea458f2021-11-25 23:46:53 +0900480
481 # The output is being parsed by service.packages.uprev_android and has to be
482 # in its own single line. When invoked from chromite API endpoints, entering
483 # chroot can generate junk messages on stdout, so we prefix our output with a
484 # line break to further ensure that.
Shao-Chuan Lee6badd122021-11-30 14:27:51 +0900485 print('\n' + json.dumps(output, sort_keys=True))