blob: 1a63e62fe9cddd09988390344f59db43abb314a6 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
David Rileyc0da9d92016-02-01 12:11:01 -08002# Copyright 2016 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""This module uprevs Android for cbuildbot.
7
8After calling, it prints outs ANDROID_VERSION_ATOM=(version atom string). A
9caller could then use this atom with emerge to build the newly uprevved version
10of Android e.g.
11
Shuhei Takahashi6d02c192017-04-05 14:01:24 +090012./cros_mark_android_as_stable \
Shao-Chuan Lee9c39e0c2020-04-24 11:40:34 +090013 --android_build_branch=git_pi-arc \
14 --android_package=android-container-pi
Shuhei Takahashi6d02c192017-04-05 14:01:24 +090015
Shao-Chuan Lee9c39e0c2020-04-24 11:40:34 +090016Returns chromeos-base/android-container-pi-6417892-r1
David Rileyc0da9d92016-02-01 12:11:01 -080017
Shao-Chuan Lee9c39e0c2020-04-24 11:40:34 +090018emerge-eve =chromeos-base/android-container-pi-6417892-r1
David Rileyc0da9d92016-02-01 12:11:01 -080019"""
20
21from __future__ import print_function
22
23import filecmp
24import glob
25import os
Hidehiko Abe12727dd2016-05-27 23:23:45 +090026import re
khmel@google.com96c193e2018-05-10 14:00:38 -070027import time
Mike Frysinger00a02292020-04-19 06:28:03 -040028import sys
David Rileyc0da9d92016-02-01 12:11:01 -080029
Aviv Keshetb7519e12016-10-04 00:50:00 -070030from chromite.lib import constants
David Rileyc0da9d92016-02-01 12:11:01 -080031from chromite.lib import commandline
32from chromite.lib import cros_build_lib
33from chromite.lib import cros_logging as logging
34from chromite.lib import git
35from chromite.lib import gs
36from chromite.lib import portage_util
37from chromite.scripts import cros_mark_as_stable
38
39
Mike Frysinger00a02292020-04-19 06:28:03 -040040assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
41
42
David Rileyc0da9d92016-02-01 12:11:01 -080043# Dir where all the action happens.
44_OVERLAY_DIR = '%(srcroot)s/private-overlays/project-cheets-private/'
45
Junichi Uekawa6d61ab02020-04-15 14:52:28 +090046_GIT_COMMIT_MESSAGE = """Marking latest for %(android_package)s ebuild with \
47version %(android_version)s as stable.
48
49BUG=None
50TEST=CQ
51"""
David Rileyc0da9d92016-02-01 12:11:01 -080052
53# URLs that print lists of Android revisions between two build ids.
Shao-Chuan Lee561b6062020-05-01 14:51:35 +090054_ANDROID_VERSION_URL = ('https://android-build.googleplex.com'
55 '/builds/%(new)s/branches/%(branch)s/cls?end=%(old)s')
David Rileyc0da9d92016-02-01 12:11:01 -080056
57
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +090058def IsBuildIdValid(bucket_url, build_branch, build_id, targets):
David Rileyc0da9d92016-02-01 12:11:01 -080059 """Checks that a specific build_id is valid.
60
61 Looks for that build_id for all builds. Confirms that the subpath can
62 be found and that the zip file is present in that subdirectory.
63
64 Args:
65 bucket_url: URL of Android build gs bucket
66 build_branch: branch of Android builds
67 build_id: A string. The Android build id number to check.
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +090068 targets: Dict from build key to (targe build suffix, artifact file pattern)
69 pair.
David Rileyc0da9d92016-02-01 12:11:01 -080070
71 Returns:
72 Returns subpaths dictionary if build_id is valid.
73 None if the build_id is not valid.
74 """
75 gs_context = gs.GSContext()
76 subpaths_dict = {}
Mike Frysinger0bdbc102019-06-13 15:27:29 -040077 for build, (target, _) in targets.items():
David Rileyc0da9d92016-02-01 12:11:01 -080078 build_dir = '%s-%s' % (build_branch, target)
79 build_id_path = os.path.join(bucket_url, build_dir, build_id)
80
81 # Find name of subpath.
82 try:
83 subpaths = gs_context.List(build_id_path)
84 except gs.GSNoSuchKey:
Mike Frysinger968c1142020-05-09 00:37:56 -040085 logging.warning(
David Rileyc0da9d92016-02-01 12:11:01 -080086 'Directory [%s] does not contain any subpath, ignoring it.',
87 build_id_path)
88 return None
89 if len(subpaths) > 1:
Mike Frysinger968c1142020-05-09 00:37:56 -040090 logging.warning(
David Rileyc0da9d92016-02-01 12:11:01 -080091 'Directory [%s] contains more than one subpath, ignoring it.',
92 build_id_path)
93 return None
94
95 subpath_dir = subpaths[0].url.rstrip('/')
96 subpath_name = os.path.basename(subpath_dir)
97
98 # Look for a zipfile ending in the build_id number.
99 try:
Hidehiko Abe12727dd2016-05-27 23:23:45 +0900100 gs_context.List(subpath_dir)
David Rileyc0da9d92016-02-01 12:11:01 -0800101 except gs.GSNoSuchKey:
Mike Frysinger968c1142020-05-09 00:37:56 -0400102 logging.warning(
Hidehiko Abe12727dd2016-05-27 23:23:45 +0900103 'Did not find a file for build id [%s] in directory [%s].',
David Rileyc0da9d92016-02-01 12:11:01 -0800104 build_id, subpath_dir)
105 return None
106
107 # Record subpath for the build.
108 subpaths_dict[build] = subpath_name
109
110 # If we got here, it means we found an appropriate build for all platforms.
111 return subpaths_dict
112
113
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900114def GetLatestBuild(bucket_url, build_branch, targets):
David Rileyc0da9d92016-02-01 12:11:01 -0800115 """Searches the gs bucket for the latest green build.
116
117 Args:
118 bucket_url: URL of Android build gs bucket
119 build_branch: branch of Android builds
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900120 targets: Dict from build key to (targe build suffix, artifact file pattern)
121 pair.
David Rileyc0da9d92016-02-01 12:11:01 -0800122
123 Returns:
124 Tuple of (latest version string, subpaths dictionary)
125 If no latest build can be found, returns None, None
126 """
127 gs_context = gs.GSContext()
128 common_build_ids = None
129 # Find builds for each target.
Mike Frysinger0bdbc102019-06-13 15:27:29 -0400130 for target, _ in targets.values():
David Rileyc0da9d92016-02-01 12:11:01 -0800131 build_dir = '-'.join((build_branch, target))
132 base_path = os.path.join(bucket_url, build_dir)
133 build_ids = []
134 for gs_result in gs_context.List(base_path):
135 # Remove trailing slashes and get the base name, which is the build_id.
136 build_id = os.path.basename(gs_result.url.rstrip('/'))
137 if not build_id.isdigit():
Mike Frysinger968c1142020-05-09 00:37:56 -0400138 logging.warning('Directory [%s] does not look like a valid build_id.',
139 gs_result.url)
David Rileyc0da9d92016-02-01 12:11:01 -0800140 continue
141 build_ids.append(build_id)
142
143 # Update current list of builds.
144 if common_build_ids is None:
145 # First run, populate it with the first platform.
146 common_build_ids = set(build_ids)
147 else:
148 # Already populated, find the ones that are common.
149 common_build_ids.intersection_update(build_ids)
150
151 if common_build_ids is None:
Mike Frysinger968c1142020-05-09 00:37:56 -0400152 logging.warning('Did not find a build_id common to all platforms.')
David Rileyc0da9d92016-02-01 12:11:01 -0800153 return None, None
154
155 # Otherwise, find the most recent one that is valid.
156 for build_id in sorted(common_build_ids, key=int, reverse=True):
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900157 subpaths = IsBuildIdValid(bucket_url, build_branch, build_id, targets)
David Rileyc0da9d92016-02-01 12:11:01 -0800158 if subpaths:
159 return build_id, subpaths
160
161 # If not found, no build_id is valid.
Mike Frysinger968c1142020-05-09 00:37:56 -0400162 logging.warning('Did not find a build_id valid on all platforms.')
David Rileyc0da9d92016-02-01 12:11:01 -0800163 return None, None
164
165
166def FindAndroidCandidates(package_dir):
167 """Return a tuple of Android's unstable ebuild and stable ebuilds.
168
169 Args:
170 package_dir: The path to where the package ebuild is stored.
171
172 Returns:
173 Tuple [unstable_ebuild, stable_ebuilds].
174
175 Raises:
176 Exception: if no unstable ebuild exists for Android.
177 """
178 stable_ebuilds = []
179 unstable_ebuilds = []
180 for path in glob.glob(os.path.join(package_dir, '*.ebuild')):
181 ebuild = portage_util.EBuild(path)
182 if ebuild.version == '9999':
183 unstable_ebuilds.append(ebuild)
184 else:
185 stable_ebuilds.append(ebuild)
186
187 # Apply some sanity checks.
188 if not unstable_ebuilds:
189 raise Exception('Missing 9999 ebuild for %s' % package_dir)
190 if not stable_ebuilds:
Lann Martinffb95162018-08-28 12:02:54 -0600191 logging.warning('Missing stable ebuild for %s', package_dir)
David Rileyc0da9d92016-02-01 12:11:01 -0800192
193 return portage_util.BestEBuild(unstable_ebuilds), stable_ebuilds
194
195
Nicolas Norvezb08f54d2016-12-05 17:58:54 -0800196def _GetArcBasename(build, basename):
197 """Tweaks filenames between Android bucket and ARC++ bucket.
198
199 Android builders create build artifacts with the same name for -user and
200 -userdebug builds, which breaks the android-container ebuild (b/33072485).
201 When copying the artifacts from the Android bucket to the ARC++ bucket some
202 artifacts will be renamed from the usual pattern
203 *cheets_${ARCH}-target_files-S{VERSION}.zip to
204 cheets_${BUILD_NAME}-target_files-S{VERSION}.zip which will typically look
205 like cheets_(${LABEL})*${ARCH}_userdebug-target_files-S{VERSION}.zip.
206
207 Args:
208 build: the build being mirrored, e.g. 'X86', 'ARM', 'X86_USERDEBUG'.
209 basename: the basename of the artifact to copy.
210
211 Returns:
212 The basename of the destination.
213 """
214 if build not in constants.ARC_BUILDS_NEED_ARTIFACTS_RENAMED:
215 return basename
Yūki Ishiief1ada92018-03-27 15:46:15 +0900216 if basename in constants.ARC_ARTIFACTS_RENAME_NOT_NEEDED:
217 return basename
Nicolas Norvezb08f54d2016-12-05 17:58:54 -0800218 to_discard, sep, to_keep = basename.partition('-')
219 if not sep:
220 logging.error(('Build %s: Could not find separator "-" in artifact'
221 ' basename %s'), build, basename)
222 return basename
Bernie Thompson63ed5612017-08-16 12:27:34 -0700223 if 'cheets_' in to_discard:
224 return 'cheets_%s-%s' % (build.lower(), to_keep)
225 elif 'bertha_' in to_discard:
226 return 'bertha_%s-%s' % (build.lower(), to_keep)
227 logging.error('Build %s: Unexpected artifact basename %s',
228 build, basename)
229 return basename
Nicolas Norvezb08f54d2016-12-05 17:58:54 -0800230
231
David Riley73f00d92016-02-16 18:54:20 -0800232def CopyToArcBucket(android_bucket_url, build_branch, build_id, subpaths,
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900233 targets, arc_bucket_url, acls):
David Riley73f00d92016-02-16 18:54:20 -0800234 """Copies from source Android bucket to ARC++ specific bucket.
235
236 Copies each build to the ARC bucket eliminating the subpath.
237 Applies build specific ACLs for each file.
238
239 Args:
240 android_bucket_url: URL of Android build gs bucket
241 build_branch: branch of Android builds
242 build_id: A string. The Android build id number to check.
243 subpaths: Subpath dictionary for each build to copy.
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900244 targets: Dict from build key to (targe build suffix, artifact file pattern)
245 pair.
David Riley73f00d92016-02-16 18:54:20 -0800246 arc_bucket_url: URL of the target ARC build gs bucket
247 acls: ACLs dictionary for each build to copy.
248 """
249 gs_context = gs.GSContext()
Mike Frysinger0bdbc102019-06-13 15:27:29 -0400250 for build, subpath in subpaths.items():
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900251 target, pattern = targets[build]
David Riley73f00d92016-02-16 18:54:20 -0800252 build_dir = '%s-%s' % (build_branch, target)
253 android_dir = os.path.join(android_bucket_url, build_dir, build_id, subpath)
254 arc_dir = os.path.join(arc_bucket_url, build_dir, build_id)
255
Hidehiko Abe12727dd2016-05-27 23:23:45 +0900256 # Copy all target files from android_dir to arc_dir, setting ACLs.
257 for targetfile in gs_context.List(android_dir):
258 if re.search(pattern, targetfile.url):
259 basename = os.path.basename(targetfile.url)
Nicolas Norvezb08f54d2016-12-05 17:58:54 -0800260 arc_path = os.path.join(arc_dir, _GetArcBasename(build, basename))
David Riley73f00d92016-02-16 18:54:20 -0800261 acl = acls[build]
262 needs_copy = True
khmel@google.com96c193e2018-05-10 14:00:38 -0700263 retry_count = 2
David Riley73f00d92016-02-16 18:54:20 -0800264
khmel@google.com96c193e2018-05-10 14:00:38 -0700265 # Retry in case race condition when several boards trying to copy the
266 # same resource
267 while True:
268 # Check a pre-existing file with the original source.
269 if gs_context.Exists(arc_path):
270 if (gs_context.Stat(targetfile.url).hash_crc32c !=
271 gs_context.Stat(arc_path).hash_crc32c):
Mike Frysinger968c1142020-05-09 00:37:56 -0400272 logging.warning('Removing incorrect file %s', arc_path)
khmel@google.com96c193e2018-05-10 14:00:38 -0700273 gs_context.Remove(arc_path)
274 else:
275 logging.info('Skipping already copied file %s', arc_path)
276 needs_copy = False
David Riley73f00d92016-02-16 18:54:20 -0800277
khmel@google.com96c193e2018-05-10 14:00:38 -0700278 # Copy if necessary, and set the ACL unconditionally.
279 # The Stat() call above doesn't verify the ACL is correct and
280 # the ChangeACL should be relatively cheap compared to the copy.
281 # This covers the following caes:
282 # - handling an interrupted copy from a previous run.
283 # - rerunning the copy in case one of the googlestorage_acl_X.txt
284 # files changes (e.g. we add a new variant which reuses a build).
285 if needs_copy:
286 logging.info('Copying %s -> %s (acl %s)',
287 targetfile.url, arc_path, acl)
288 try:
289 gs_context.Copy(targetfile.url, arc_path, version=0)
290 except gs.GSContextPreconditionFailed as error:
291 if not retry_count:
292 raise error
293 # Retry one more time after a short delay
294 logging.warning('Will retry copying %s -> %s',
295 targetfile.url, arc_path)
296 time.sleep(5)
297 retry_count = retry_count - 1
298 continue
299 gs_context.ChangeACL(arc_path, acl_args_file=acl)
300 break
David Riley73f00d92016-02-16 18:54:20 -0800301
302
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900303def MirrorArtifacts(android_bucket_url, android_build_branch, arc_bucket_url,
304 acls, targets, version=None):
305 """Mirrors artifacts from Android bucket to ARC bucket.
306
307 First, this function identifies which build version should be copied,
308 if not given. Please see GetLatestBuild() and IsBuildIdValid() for details.
309
310 On build version identified, then copies target artifacts to the ARC bucket,
311 with setting ACLs.
312
313 Args:
314 android_bucket_url: URL of Android build gs bucket
315 android_build_branch: branch of Android builds
316 arc_bucket_url: URL of the target ARC build gs bucket
317 acls: ACLs dictionary for each build to copy.
318 targets: Dict from build key to (targe build suffix, artifact file pattern)
319 pair.
320 version: (optional) A string. The Android build id number to check.
321 If not passed, detect latest good build version.
322
323 Returns:
324 Mirrored version.
325 """
326 if version:
327 subpaths = IsBuildIdValid(
328 android_bucket_url, android_build_branch, version, targets)
329 if not subpaths:
Lann Martinffb95162018-08-28 12:02:54 -0600330 logging.error('Requested build %s is not valid', version)
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900331 else:
332 version, subpaths = GetLatestBuild(
333 android_bucket_url, android_build_branch, targets)
334
335 CopyToArcBucket(android_bucket_url, android_build_branch, version, subpaths,
336 targets, arc_bucket_url, acls)
khmel@google.com778a1cd2018-04-13 11:11:58 -0700337
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900338 return version
339
340
David Riley73f00d92016-02-16 18:54:20 -0800341def MakeAclDict(package_dir):
342 """Creates a dictionary of acl files for each build type.
343
344 Args:
345 package_dir: The path to where the package acl files are stored.
346
347 Returns:
348 Returns acls dictionary.
349 """
350 return dict(
351 (k, os.path.join(package_dir, v))
352 for k, v in constants.ARC_BUCKET_ACLS.items()
353 )
354
355
Qijiang Fan6588cc92019-11-20 13:26:04 +0900356def MakeBuildTargetDict(package_name, build_branch):
Nicolas Norvez4bd854f2017-05-23 10:04:45 -0700357 """Creates a dictionary of build targets.
358
Bernie Thompson63ed5612017-08-16 12:27:34 -0700359 Not all targets are common between branches, for example
Nicolas Norvez4bd854f2017-05-23 10:04:45 -0700360 sdk_google_cheets_x86 only exists on N.
361 This generates a dictionary listing the available build targets for a
362 specific branch.
363
364 Args:
Qijiang Fan6588cc92019-11-20 13:26:04 +0900365 package_name: package name of chromeos arc package.
Nicolas Norvez4bd854f2017-05-23 10:04:45 -0700366 build_branch: branch of Android builds.
367
368 Returns:
369 Returns build target dictionary.
370
371 Raises:
372 ValueError: if the Android build branch is invalid.
373 """
Qijiang Fan6588cc92019-11-20 13:26:04 +0900374 if constants.ANDROID_CONTAINER_PACKAGE_KEYWORD in package_name:
Federico 'Morg' Pareschi041ee652020-03-10 15:09:42 +0900375 target_list = {
376 constants.ANDROID_MST_BUILD_BRANCH:
377 constants.ANDROID_MST_BUILD_TARGETS,
Federico 'Morg' Pareschi041ee652020-03-10 15:09:42 +0900378 constants.ANDROID_PI_BUILD_BRANCH:
379 constants.ANDROID_PI_BUILD_TARGETS,
380 constants.ANDROID_QT_BUILD_BRANCH:
381 constants.ANDROID_QT_BUILD_TARGETS,
382 constants.ANDROID_RVC_BUILD_BRANCH:
383 constants.ANDROID_RVC_BUILD_TARGETS,
384 }
Qijiang Fan6588cc92019-11-20 13:26:04 +0900385 elif constants.ANDROID_VM_PACKAGE_KEYWORD in package_name:
Federico 'Morg' Pareschi041ee652020-03-10 15:09:42 +0900386 target_list = {
387 constants.ANDROID_VMPI_BUILD_BRANCH:
388 constants.ANDROID_VMPI_BUILD_TARGETS,
389 constants.ANDROID_VMMST_BUILD_BRANCH:
390 constants.ANDROID_VMMST_BUILD_TARGETS,
391 constants.ANDROID_VMRVC_BUILD_BRANCH:
392 constants.ANDROID_VMRVC_BUILD_TARGETS,
393 }
394 else:
395 raise ValueError('Unknown package: %s' % package_name)
396 target = target_list.get(build_branch)
397 if not target:
Qijiang Fan6588cc92019-11-20 13:26:04 +0900398 raise ValueError('Unknown branch: %s' % build_branch)
Federico 'Morg' Pareschi041ee652020-03-10 15:09:42 +0900399 return target
Nicolas Norvez4bd854f2017-05-23 10:04:45 -0700400
401
David Rileyc0da9d92016-02-01 12:11:01 -0800402def GetAndroidRevisionListLink(build_branch, old_android, new_android):
403 """Returns a link to the list of revisions between two Android versions
404
405 Given two AndroidEBuilds, generate a link to a page that prints the
406 Android changes between those two revisions, inclusive.
407
408 Args:
409 build_branch: branch of Android builds
410 old_android: ebuild for the version to diff from
411 new_android: ebuild for the version to which to diff
412
413 Returns:
414 The desired URL.
415 """
416 return _ANDROID_VERSION_URL % {'branch': build_branch,
Hidehiko Abec9ecf262017-07-05 15:17:41 +0900417 'old': old_android.version_no_rev,
418 'new': new_android.version_no_rev}
David Rileyc0da9d92016-02-01 12:11:01 -0800419
420
Hidehiko Abe4fd94ae2017-01-24 18:59:55 +0900421def MarkAndroidEBuildAsStable(stable_candidate, unstable_ebuild,
422 android_package, android_version, package_dir,
Nicolas Norvez4bd854f2017-05-23 10:04:45 -0700423 build_branch, arc_bucket_url, build_targets):
David Rileyc0da9d92016-02-01 12:11:01 -0800424 r"""Uprevs the Android ebuild.
425
426 This is the main function that uprevs from a stable candidate
427 to its new version.
428
429 Args:
430 stable_candidate: ebuild that corresponds to the stable ebuild we are
431 revving from. If None, builds the a new ebuild given the version
432 with revision set to 1.
433 unstable_ebuild: ebuild corresponding to the unstable ebuild for Android.
Hidehiko Abe4fd94ae2017-01-24 18:59:55 +0900434 android_package: android package name.
David Rileyc0da9d92016-02-01 12:11:01 -0800435 android_version: The \d+ build id of Android.
David Rileyc0da9d92016-02-01 12:11:01 -0800436 package_dir: Path to the android-container package dir.
David Riley73f00d92016-02-16 18:54:20 -0800437 build_branch: branch of Android builds.
438 arc_bucket_url: URL of the target ARC build gs bucket.
Nicolas Norvez4bd854f2017-05-23 10:04:45 -0700439 build_targets: build targets for this particular Android branch.
David Rileyc0da9d92016-02-01 12:11:01 -0800440
441 Returns:
442 Full portage version atom (including rc's, etc) that was revved.
443 """
444 def IsTheNewEBuildRedundant(new_ebuild, stable_ebuild):
445 """Returns True if the new ebuild is redundant.
446
447 This is True if there if the current stable ebuild is the exact same copy
448 of the new one.
449 """
450 if not stable_ebuild:
451 return False
452
David Riley676f5402016-02-12 17:24:23 -0800453 if stable_candidate.version_no_rev == new_ebuild.version_no_rev:
David Rileyc0da9d92016-02-01 12:11:01 -0800454 return filecmp.cmp(
455 new_ebuild.ebuild_path, stable_ebuild.ebuild_path, shallow=False)
456
457 # Case where we have the last stable candidate with same version just rev.
David Riley676f5402016-02-12 17:24:23 -0800458 if stable_candidate and stable_candidate.version_no_rev == android_version:
David Rileyc0da9d92016-02-01 12:11:01 -0800459 new_ebuild_path = '%s-r%d.ebuild' % (
460 stable_candidate.ebuild_path_no_revision,
461 stable_candidate.current_revision + 1)
462 else:
Hidehiko Abe4fd94ae2017-01-24 18:59:55 +0900463 pf = '%s-%s-r1' % (android_package, android_version)
David Rileyc0da9d92016-02-01 12:11:01 -0800464 new_ebuild_path = os.path.join(package_dir, '%s.ebuild' % pf)
465
David Riley73f00d92016-02-16 18:54:20 -0800466 variables = {'BASE_URL': arc_bucket_url}
Mike Frysinger0bdbc102019-06-13 15:27:29 -0400467 for build, (target, _) in build_targets.items():
David Riley73f00d92016-02-16 18:54:20 -0800468 variables[build + '_TARGET'] = '%s-%s' % (build_branch, target)
David Rileyc0da9d92016-02-01 12:11:01 -0800469
470 portage_util.EBuild.MarkAsStable(
471 unstable_ebuild.ebuild_path, new_ebuild_path,
472 variables, make_stable=True)
473 new_ebuild = portage_util.EBuild(new_ebuild_path)
474
475 # Determine whether this is ebuild is redundant.
476 if IsTheNewEBuildRedundant(new_ebuild, stable_candidate):
477 msg = 'Previous ebuild with same version found and ebuild is redundant.'
478 logging.info(msg)
479 os.unlink(new_ebuild_path)
480 return None
481
482 if stable_candidate:
483 logging.PrintBuildbotLink('Android revisions',
484 GetAndroidRevisionListLink(build_branch,
485 stable_candidate,
486 new_ebuild))
487
488 git.RunGit(package_dir, ['add', new_ebuild_path])
489 if stable_candidate and not stable_candidate.IsSticky():
490 git.RunGit(package_dir, ['rm', stable_candidate.ebuild_path])
491
492 # Update ebuild manifest and git add it.
493 gen_manifest_cmd = ['ebuild', new_ebuild_path, 'manifest', '--force']
Mike Frysinger45602c72019-09-22 02:15:11 -0400494 cros_build_lib.run(gen_manifest_cmd, extra_env=None, print_cmd=True)
David Rileyc0da9d92016-02-01 12:11:01 -0800495 git.RunGit(package_dir, ['add', 'Manifest'])
496
497 portage_util.EBuild.CommitChange(
Hidehiko Abe4fd94ae2017-01-24 18:59:55 +0900498 _GIT_COMMIT_MESSAGE % {'android_package': android_package,
David Rileyc0da9d92016-02-01 12:11:01 -0800499 'android_version': android_version},
500 package_dir)
501
502 return '%s-%s' % (new_ebuild.package, new_ebuild.version)
503
504
505def GetParser():
506 """Creates the argument parser."""
507 parser = commandline.ArgumentParser()
508 parser.add_argument('-b', '--boards')
509 parser.add_argument('--android_bucket_url',
David Riley73f00d92016-02-16 18:54:20 -0800510 default=constants.ANDROID_BUCKET_URL,
511 type='gs_path')
David Rileyc0da9d92016-02-01 12:11:01 -0800512 parser.add_argument('--android_build_branch',
Shuhei Takahashi6d02c192017-04-05 14:01:24 +0900513 required=True,
514 help='Android branch to import from. '
515 'Ex: git_mnc-dr-arc-dev')
Hidehiko Abe4fd94ae2017-01-24 18:59:55 +0900516 parser.add_argument('--android_package',
517 default=constants.ANDROID_PACKAGE_NAME)
David Riley73f00d92016-02-16 18:54:20 -0800518 parser.add_argument('--arc_bucket_url',
519 default=constants.ARC_BUCKET_URL,
520 type='gs_path')
David Rileyc0da9d92016-02-01 12:11:01 -0800521 parser.add_argument('-f', '--force_version',
522 help='Android build id to use')
523 parser.add_argument('-s', '--srcroot',
524 default=os.path.join(os.environ['HOME'], 'trunk', 'src'),
525 help='Path to the src directory')
526 parser.add_argument('-t', '--tracking_branch', default='cros/master',
527 help='Branch we are tracking changes against')
528 return parser
529
530
531def main(argv):
Hidehiko Abec9ecf262017-07-05 15:17:41 +0900532 logging.EnableBuildbotMarkers()
David Rileyc0da9d92016-02-01 12:11:01 -0800533 parser = GetParser()
534 options = parser.parse_args(argv)
535 options.Freeze()
536
537 overlay_dir = os.path.abspath(_OVERLAY_DIR % {'srcroot': options.srcroot})
Hidehiko Abe4fd94ae2017-01-24 18:59:55 +0900538 android_package_dir = os.path.join(
539 overlay_dir,
540 portage_util.GetFullAndroidPortagePackageName(options.android_package))
David Rileyc0da9d92016-02-01 12:11:01 -0800541 version_to_uprev = None
David Rileyc0da9d92016-02-01 12:11:01 -0800542
543 (unstable_ebuild, stable_ebuilds) = FindAndroidCandidates(android_package_dir)
David Riley73f00d92016-02-16 18:54:20 -0800544 acls = MakeAclDict(android_package_dir)
Qijiang Fan6588cc92019-11-20 13:26:04 +0900545 build_targets = MakeBuildTargetDict(options.android_package,
546 options.android_build_branch)
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900547 # Mirror artifacts, i.e., images and some sdk tools (e.g., adb, aapt).
548 version_to_uprev = MirrorArtifacts(options.android_bucket_url,
549 options.android_build_branch,
550 options.arc_bucket_url, acls,
Nicolas Norvez4bd854f2017-05-23 10:04:45 -0700551 build_targets,
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900552 options.force_version)
553
David Rileyc0da9d92016-02-01 12:11:01 -0800554 stable_candidate = portage_util.BestEBuild(stable_ebuilds)
555
556 if stable_candidate:
Lann Martinffb95162018-08-28 12:02:54 -0600557 logging.info('Stable candidate found %s', stable_candidate.version)
David Rileyc0da9d92016-02-01 12:11:01 -0800558 else:
559 logging.info('No stable candidate found.')
560
561 tracking_branch = 'remotes/m/%s' % os.path.basename(options.tracking_branch)
562 existing_branch = git.GetCurrentBranch(android_package_dir)
563 work_branch = cros_mark_as_stable.GitBranch(constants.STABLE_EBUILD_BRANCH,
564 tracking_branch,
565 android_package_dir)
566 work_branch.CreateBranch()
567
568 # In the case of uprevving overlays that have patches applied to them,
569 # include the patched changes in the stabilizing branch.
570 if existing_branch:
571 git.RunGit(overlay_dir, ['rebase', existing_branch])
572
573 android_version_atom = MarkAndroidEBuildAsStable(
Hidehiko Abe4fd94ae2017-01-24 18:59:55 +0900574 stable_candidate, unstable_ebuild, options.android_package,
David Riley73f00d92016-02-16 18:54:20 -0800575 version_to_uprev, android_package_dir,
Nicolas Norvez4bd854f2017-05-23 10:04:45 -0700576 options.android_build_branch, options.arc_bucket_url, build_targets)
David Rileyc0da9d92016-02-01 12:11:01 -0800577 if android_version_atom:
578 if options.boards:
579 cros_mark_as_stable.CleanStalePackages(options.srcroot,
580 options.boards.split(':'),
581 [android_version_atom])
582
583 # Explicit print to communicate to caller.
584 print('ANDROID_VERSION_ATOM=%s' % android_version_atom)