blob: e13e37e67012fa7f08b2dc350da96ca7b77e84d5 [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
Mike Frysinger00a02292020-04-19 06:28:03 -040023import base64
David Rileyc0da9d92016-02-01 12:11:01 -080024import filecmp
khmel@google.com778a1cd2018-04-13 11:11:58 -070025import hashlib
David Rileyc0da9d92016-02-01 12:11:01 -080026import glob
27import os
Hidehiko Abe12727dd2016-05-27 23:23:45 +090028import re
khmel@google.com778a1cd2018-04-13 11:11:58 -070029import shutil
30import tempfile
khmel@google.com96c193e2018-05-10 14:00:38 -070031import time
khmel@google.com778a1cd2018-04-13 11:11:58 -070032import subprocess
Mike Frysinger00a02292020-04-19 06:28:03 -040033import sys
David Rileyc0da9d92016-02-01 12:11:01 -080034
Aviv Keshetb7519e12016-10-04 00:50:00 -070035from chromite.lib import constants
David Rileyc0da9d92016-02-01 12:11:01 -080036from chromite.lib import commandline
37from chromite.lib import cros_build_lib
38from chromite.lib import cros_logging as logging
39from chromite.lib import git
40from chromite.lib import gs
41from chromite.lib import portage_util
42from chromite.scripts import cros_mark_as_stable
43
44
Mike Frysinger00a02292020-04-19 06:28:03 -040045assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
46
47
David Rileyc0da9d92016-02-01 12:11:01 -080048# Dir where all the action happens.
49_OVERLAY_DIR = '%(srcroot)s/private-overlays/project-cheets-private/'
50
Junichi Uekawa6d61ab02020-04-15 14:52:28 +090051_GIT_COMMIT_MESSAGE = """Marking latest for %(android_package)s ebuild with \
52version %(android_version)s as stable.
53
54BUG=None
55TEST=CQ
56"""
David Rileyc0da9d92016-02-01 12:11:01 -080057
58# URLs that print lists of Android revisions between two build ids.
59_ANDROID_VERSION_URL = ('http://android-build-uber.corp.google.com/repo.html?'
60 'last_bid=%(old)s&bid=%(new)s&branch=%(branch)s')
61
62
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +090063def IsBuildIdValid(bucket_url, build_branch, build_id, targets):
David Rileyc0da9d92016-02-01 12:11:01 -080064 """Checks that a specific build_id is valid.
65
66 Looks for that build_id for all builds. Confirms that the subpath can
67 be found and that the zip file is present in that subdirectory.
68
69 Args:
70 bucket_url: URL of Android build gs bucket
71 build_branch: branch of Android builds
72 build_id: A string. The Android build id number to check.
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +090073 targets: Dict from build key to (targe build suffix, artifact file pattern)
74 pair.
David Rileyc0da9d92016-02-01 12:11:01 -080075
76 Returns:
77 Returns subpaths dictionary if build_id is valid.
78 None if the build_id is not valid.
79 """
80 gs_context = gs.GSContext()
81 subpaths_dict = {}
Mike Frysinger0bdbc102019-06-13 15:27:29 -040082 for build, (target, _) in targets.items():
David Rileyc0da9d92016-02-01 12:11:01 -080083 build_dir = '%s-%s' % (build_branch, target)
84 build_id_path = os.path.join(bucket_url, build_dir, build_id)
85
86 # Find name of subpath.
87 try:
88 subpaths = gs_context.List(build_id_path)
89 except gs.GSNoSuchKey:
90 logging.warn(
91 'Directory [%s] does not contain any subpath, ignoring it.',
92 build_id_path)
93 return None
94 if len(subpaths) > 1:
95 logging.warn(
96 'Directory [%s] contains more than one subpath, ignoring it.',
97 build_id_path)
98 return None
99
100 subpath_dir = subpaths[0].url.rstrip('/')
101 subpath_name = os.path.basename(subpath_dir)
102
103 # Look for a zipfile ending in the build_id number.
104 try:
Hidehiko Abe12727dd2016-05-27 23:23:45 +0900105 gs_context.List(subpath_dir)
David Rileyc0da9d92016-02-01 12:11:01 -0800106 except gs.GSNoSuchKey:
107 logging.warn(
Hidehiko Abe12727dd2016-05-27 23:23:45 +0900108 'Did not find a file for build id [%s] in directory [%s].',
David Rileyc0da9d92016-02-01 12:11:01 -0800109 build_id, subpath_dir)
110 return None
111
112 # Record subpath for the build.
113 subpaths_dict[build] = subpath_name
114
115 # If we got here, it means we found an appropriate build for all platforms.
116 return subpaths_dict
117
118
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900119def GetLatestBuild(bucket_url, build_branch, targets):
David Rileyc0da9d92016-02-01 12:11:01 -0800120 """Searches the gs bucket for the latest green build.
121
122 Args:
123 bucket_url: URL of Android build gs bucket
124 build_branch: branch of Android builds
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900125 targets: Dict from build key to (targe build suffix, artifact file pattern)
126 pair.
David Rileyc0da9d92016-02-01 12:11:01 -0800127
128 Returns:
129 Tuple of (latest version string, subpaths dictionary)
130 If no latest build can be found, returns None, None
131 """
132 gs_context = gs.GSContext()
133 common_build_ids = None
134 # Find builds for each target.
Mike Frysinger0bdbc102019-06-13 15:27:29 -0400135 for target, _ in targets.values():
David Rileyc0da9d92016-02-01 12:11:01 -0800136 build_dir = '-'.join((build_branch, target))
137 base_path = os.path.join(bucket_url, build_dir)
138 build_ids = []
139 for gs_result in gs_context.List(base_path):
140 # Remove trailing slashes and get the base name, which is the build_id.
141 build_id = os.path.basename(gs_result.url.rstrip('/'))
142 if not build_id.isdigit():
143 logging.warn('Directory [%s] does not look like a valid build_id.',
144 gs_result.url)
145 continue
146 build_ids.append(build_id)
147
148 # Update current list of builds.
149 if common_build_ids is None:
150 # First run, populate it with the first platform.
151 common_build_ids = set(build_ids)
152 else:
153 # Already populated, find the ones that are common.
154 common_build_ids.intersection_update(build_ids)
155
156 if common_build_ids is None:
157 logging.warn('Did not find a build_id common to all platforms.')
158 return None, None
159
160 # Otherwise, find the most recent one that is valid.
161 for build_id in sorted(common_build_ids, key=int, reverse=True):
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900162 subpaths = IsBuildIdValid(bucket_url, build_branch, build_id, targets)
David Rileyc0da9d92016-02-01 12:11:01 -0800163 if subpaths:
164 return build_id, subpaths
165
166 # If not found, no build_id is valid.
167 logging.warn('Did not find a build_id valid on all platforms.')
168 return None, None
169
170
171def FindAndroidCandidates(package_dir):
172 """Return a tuple of Android's unstable ebuild and stable ebuilds.
173
174 Args:
175 package_dir: The path to where the package ebuild is stored.
176
177 Returns:
178 Tuple [unstable_ebuild, stable_ebuilds].
179
180 Raises:
181 Exception: if no unstable ebuild exists for Android.
182 """
183 stable_ebuilds = []
184 unstable_ebuilds = []
185 for path in glob.glob(os.path.join(package_dir, '*.ebuild')):
186 ebuild = portage_util.EBuild(path)
187 if ebuild.version == '9999':
188 unstable_ebuilds.append(ebuild)
189 else:
190 stable_ebuilds.append(ebuild)
191
192 # Apply some sanity checks.
193 if not unstable_ebuilds:
194 raise Exception('Missing 9999 ebuild for %s' % package_dir)
195 if not stable_ebuilds:
Lann Martinffb95162018-08-28 12:02:54 -0600196 logging.warning('Missing stable ebuild for %s', package_dir)
David Rileyc0da9d92016-02-01 12:11:01 -0800197
198 return portage_util.BestEBuild(unstable_ebuilds), stable_ebuilds
199
200
Nicolas Norvezb08f54d2016-12-05 17:58:54 -0800201def _GetArcBasename(build, basename):
202 """Tweaks filenames between Android bucket and ARC++ bucket.
203
204 Android builders create build artifacts with the same name for -user and
205 -userdebug builds, which breaks the android-container ebuild (b/33072485).
206 When copying the artifacts from the Android bucket to the ARC++ bucket some
207 artifacts will be renamed from the usual pattern
208 *cheets_${ARCH}-target_files-S{VERSION}.zip to
209 cheets_${BUILD_NAME}-target_files-S{VERSION}.zip which will typically look
210 like cheets_(${LABEL})*${ARCH}_userdebug-target_files-S{VERSION}.zip.
211
212 Args:
213 build: the build being mirrored, e.g. 'X86', 'ARM', 'X86_USERDEBUG'.
214 basename: the basename of the artifact to copy.
215
216 Returns:
217 The basename of the destination.
218 """
219 if build not in constants.ARC_BUILDS_NEED_ARTIFACTS_RENAMED:
220 return basename
Yūki Ishiief1ada92018-03-27 15:46:15 +0900221 if basename in constants.ARC_ARTIFACTS_RENAME_NOT_NEEDED:
222 return basename
Nicolas Norvezb08f54d2016-12-05 17:58:54 -0800223 to_discard, sep, to_keep = basename.partition('-')
224 if not sep:
225 logging.error(('Build %s: Could not find separator "-" in artifact'
226 ' basename %s'), build, basename)
227 return basename
Bernie Thompson63ed5612017-08-16 12:27:34 -0700228 if 'cheets_' in to_discard:
229 return 'cheets_%s-%s' % (build.lower(), to_keep)
230 elif 'bertha_' in to_discard:
231 return 'bertha_%s-%s' % (build.lower(), to_keep)
232 logging.error('Build %s: Unexpected artifact basename %s',
233 build, basename)
234 return basename
Nicolas Norvezb08f54d2016-12-05 17:58:54 -0800235
236
David Riley73f00d92016-02-16 18:54:20 -0800237def CopyToArcBucket(android_bucket_url, build_branch, build_id, subpaths,
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900238 targets, arc_bucket_url, acls):
David Riley73f00d92016-02-16 18:54:20 -0800239 """Copies from source Android bucket to ARC++ specific bucket.
240
241 Copies each build to the ARC bucket eliminating the subpath.
242 Applies build specific ACLs for each file.
243
244 Args:
245 android_bucket_url: URL of Android build gs bucket
246 build_branch: branch of Android builds
247 build_id: A string. The Android build id number to check.
248 subpaths: Subpath dictionary for each build to copy.
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900249 targets: Dict from build key to (targe build suffix, artifact file pattern)
250 pair.
David Riley73f00d92016-02-16 18:54:20 -0800251 arc_bucket_url: URL of the target ARC build gs bucket
252 acls: ACLs dictionary for each build to copy.
253 """
254 gs_context = gs.GSContext()
Mike Frysinger0bdbc102019-06-13 15:27:29 -0400255 for build, subpath in subpaths.items():
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900256 target, pattern = targets[build]
David Riley73f00d92016-02-16 18:54:20 -0800257 build_dir = '%s-%s' % (build_branch, target)
258 android_dir = os.path.join(android_bucket_url, build_dir, build_id, subpath)
259 arc_dir = os.path.join(arc_bucket_url, build_dir, build_id)
260
Hidehiko Abe12727dd2016-05-27 23:23:45 +0900261 # Copy all target files from android_dir to arc_dir, setting ACLs.
262 for targetfile in gs_context.List(android_dir):
263 if re.search(pattern, targetfile.url):
264 basename = os.path.basename(targetfile.url)
Nicolas Norvezb08f54d2016-12-05 17:58:54 -0800265 arc_path = os.path.join(arc_dir, _GetArcBasename(build, basename))
David Riley73f00d92016-02-16 18:54:20 -0800266 acl = acls[build]
267 needs_copy = True
khmel@google.com96c193e2018-05-10 14:00:38 -0700268 retry_count = 2
David Riley73f00d92016-02-16 18:54:20 -0800269
khmel@google.com96c193e2018-05-10 14:00:38 -0700270 # Retry in case race condition when several boards trying to copy the
271 # same resource
272 while True:
273 # Check a pre-existing file with the original source.
274 if gs_context.Exists(arc_path):
275 if (gs_context.Stat(targetfile.url).hash_crc32c !=
276 gs_context.Stat(arc_path).hash_crc32c):
277 logging.warn('Removing incorrect file %s', arc_path)
278 gs_context.Remove(arc_path)
279 else:
280 logging.info('Skipping already copied file %s', arc_path)
281 needs_copy = False
David Riley73f00d92016-02-16 18:54:20 -0800282
khmel@google.com96c193e2018-05-10 14:00:38 -0700283 # Copy if necessary, and set the ACL unconditionally.
284 # The Stat() call above doesn't verify the ACL is correct and
285 # the ChangeACL should be relatively cheap compared to the copy.
286 # This covers the following caes:
287 # - handling an interrupted copy from a previous run.
288 # - rerunning the copy in case one of the googlestorage_acl_X.txt
289 # files changes (e.g. we add a new variant which reuses a build).
290 if needs_copy:
291 logging.info('Copying %s -> %s (acl %s)',
292 targetfile.url, arc_path, acl)
293 try:
294 gs_context.Copy(targetfile.url, arc_path, version=0)
295 except gs.GSContextPreconditionFailed as error:
296 if not retry_count:
297 raise error
298 # Retry one more time after a short delay
299 logging.warning('Will retry copying %s -> %s',
300 targetfile.url, arc_path)
301 time.sleep(5)
302 retry_count = retry_count - 1
303 continue
304 gs_context.ChangeACL(arc_path, acl_args_file=acl)
305 break
David Riley73f00d92016-02-16 18:54:20 -0800306
307
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900308def MirrorArtifacts(android_bucket_url, android_build_branch, arc_bucket_url,
309 acls, targets, version=None):
310 """Mirrors artifacts from Android bucket to ARC bucket.
311
312 First, this function identifies which build version should be copied,
313 if not given. Please see GetLatestBuild() and IsBuildIdValid() for details.
314
315 On build version identified, then copies target artifacts to the ARC bucket,
316 with setting ACLs.
317
318 Args:
319 android_bucket_url: URL of Android build gs bucket
320 android_build_branch: branch of Android builds
321 arc_bucket_url: URL of the target ARC build gs bucket
322 acls: ACLs dictionary for each build to copy.
323 targets: Dict from build key to (targe build suffix, artifact file pattern)
324 pair.
325 version: (optional) A string. The Android build id number to check.
326 If not passed, detect latest good build version.
327
328 Returns:
329 Mirrored version.
330 """
331 if version:
332 subpaths = IsBuildIdValid(
333 android_bucket_url, android_build_branch, version, targets)
334 if not subpaths:
Lann Martinffb95162018-08-28 12:02:54 -0600335 logging.error('Requested build %s is not valid', version)
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900336 else:
337 version, subpaths = GetLatestBuild(
338 android_bucket_url, android_build_branch, targets)
339
340 CopyToArcBucket(android_bucket_url, android_build_branch, version, subpaths,
341 targets, arc_bucket_url, acls)
khmel@google.com778a1cd2018-04-13 11:11:58 -0700342
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900343 return version
344
345
David Riley73f00d92016-02-16 18:54:20 -0800346def MakeAclDict(package_dir):
347 """Creates a dictionary of acl files for each build type.
348
349 Args:
350 package_dir: The path to where the package acl files are stored.
351
352 Returns:
353 Returns acls dictionary.
354 """
355 return dict(
356 (k, os.path.join(package_dir, v))
357 for k, v in constants.ARC_BUCKET_ACLS.items()
358 )
359
360
Qijiang Fan6588cc92019-11-20 13:26:04 +0900361def MakeBuildTargetDict(package_name, build_branch):
Nicolas Norvez4bd854f2017-05-23 10:04:45 -0700362 """Creates a dictionary of build targets.
363
Bernie Thompson63ed5612017-08-16 12:27:34 -0700364 Not all targets are common between branches, for example
Nicolas Norvez4bd854f2017-05-23 10:04:45 -0700365 sdk_google_cheets_x86 only exists on N.
366 This generates a dictionary listing the available build targets for a
367 specific branch.
368
369 Args:
Qijiang Fan6588cc92019-11-20 13:26:04 +0900370 package_name: package name of chromeos arc package.
Nicolas Norvez4bd854f2017-05-23 10:04:45 -0700371 build_branch: branch of Android builds.
372
373 Returns:
374 Returns build target dictionary.
375
376 Raises:
377 ValueError: if the Android build branch is invalid.
378 """
Qijiang Fan6588cc92019-11-20 13:26:04 +0900379 if constants.ANDROID_CONTAINER_PACKAGE_KEYWORD in package_name:
Federico 'Morg' Pareschi041ee652020-03-10 15:09:42 +0900380 target_list = {
381 constants.ANDROID_MST_BUILD_BRANCH:
382 constants.ANDROID_MST_BUILD_TARGETS,
Federico 'Morg' Pareschi041ee652020-03-10 15:09:42 +0900383 constants.ANDROID_PI_BUILD_BRANCH:
384 constants.ANDROID_PI_BUILD_TARGETS,
385 constants.ANDROID_QT_BUILD_BRANCH:
386 constants.ANDROID_QT_BUILD_TARGETS,
387 constants.ANDROID_RVC_BUILD_BRANCH:
388 constants.ANDROID_RVC_BUILD_TARGETS,
389 }
Qijiang Fan6588cc92019-11-20 13:26:04 +0900390 elif constants.ANDROID_VM_PACKAGE_KEYWORD in package_name:
Federico 'Morg' Pareschi041ee652020-03-10 15:09:42 +0900391 target_list = {
392 constants.ANDROID_VMPI_BUILD_BRANCH:
393 constants.ANDROID_VMPI_BUILD_TARGETS,
394 constants.ANDROID_VMMST_BUILD_BRANCH:
395 constants.ANDROID_VMMST_BUILD_TARGETS,
396 constants.ANDROID_VMRVC_BUILD_BRANCH:
397 constants.ANDROID_VMRVC_BUILD_TARGETS,
398 }
399 else:
400 raise ValueError('Unknown package: %s' % package_name)
401 target = target_list.get(build_branch)
402 if not target:
Qijiang Fan6588cc92019-11-20 13:26:04 +0900403 raise ValueError('Unknown branch: %s' % build_branch)
Federico 'Morg' Pareschi041ee652020-03-10 15:09:42 +0900404 return target
Nicolas Norvez4bd854f2017-05-23 10:04:45 -0700405
406
David Rileyc0da9d92016-02-01 12:11:01 -0800407def GetAndroidRevisionListLink(build_branch, old_android, new_android):
408 """Returns a link to the list of revisions between two Android versions
409
410 Given two AndroidEBuilds, generate a link to a page that prints the
411 Android changes between those two revisions, inclusive.
412
413 Args:
414 build_branch: branch of Android builds
415 old_android: ebuild for the version to diff from
416 new_android: ebuild for the version to which to diff
417
418 Returns:
419 The desired URL.
420 """
421 return _ANDROID_VERSION_URL % {'branch': build_branch,
Hidehiko Abec9ecf262017-07-05 15:17:41 +0900422 'old': old_android.version_no_rev,
423 'new': new_android.version_no_rev}
David Rileyc0da9d92016-02-01 12:11:01 -0800424
425
Hidehiko Abe4fd94ae2017-01-24 18:59:55 +0900426def MarkAndroidEBuildAsStable(stable_candidate, unstable_ebuild,
427 android_package, android_version, package_dir,
Nicolas Norvez4bd854f2017-05-23 10:04:45 -0700428 build_branch, arc_bucket_url, build_targets):
David Rileyc0da9d92016-02-01 12:11:01 -0800429 r"""Uprevs the Android ebuild.
430
431 This is the main function that uprevs from a stable candidate
432 to its new version.
433
434 Args:
435 stable_candidate: ebuild that corresponds to the stable ebuild we are
436 revving from. If None, builds the a new ebuild given the version
437 with revision set to 1.
438 unstable_ebuild: ebuild corresponding to the unstable ebuild for Android.
Hidehiko Abe4fd94ae2017-01-24 18:59:55 +0900439 android_package: android package name.
David Rileyc0da9d92016-02-01 12:11:01 -0800440 android_version: The \d+ build id of Android.
David Rileyc0da9d92016-02-01 12:11:01 -0800441 package_dir: Path to the android-container package dir.
David Riley73f00d92016-02-16 18:54:20 -0800442 build_branch: branch of Android builds.
443 arc_bucket_url: URL of the target ARC build gs bucket.
Nicolas Norvez4bd854f2017-05-23 10:04:45 -0700444 build_targets: build targets for this particular Android branch.
David Rileyc0da9d92016-02-01 12:11:01 -0800445
446 Returns:
447 Full portage version atom (including rc's, etc) that was revved.
448 """
449 def IsTheNewEBuildRedundant(new_ebuild, stable_ebuild):
450 """Returns True if the new ebuild is redundant.
451
452 This is True if there if the current stable ebuild is the exact same copy
453 of the new one.
454 """
455 if not stable_ebuild:
456 return False
457
David Riley676f5402016-02-12 17:24:23 -0800458 if stable_candidate.version_no_rev == new_ebuild.version_no_rev:
David Rileyc0da9d92016-02-01 12:11:01 -0800459 return filecmp.cmp(
460 new_ebuild.ebuild_path, stable_ebuild.ebuild_path, shallow=False)
461
462 # Case where we have the last stable candidate with same version just rev.
David Riley676f5402016-02-12 17:24:23 -0800463 if stable_candidate and stable_candidate.version_no_rev == android_version:
David Rileyc0da9d92016-02-01 12:11:01 -0800464 new_ebuild_path = '%s-r%d.ebuild' % (
465 stable_candidate.ebuild_path_no_revision,
466 stable_candidate.current_revision + 1)
467 else:
Hidehiko Abe4fd94ae2017-01-24 18:59:55 +0900468 pf = '%s-%s-r1' % (android_package, android_version)
David Rileyc0da9d92016-02-01 12:11:01 -0800469 new_ebuild_path = os.path.join(package_dir, '%s.ebuild' % pf)
470
David Riley73f00d92016-02-16 18:54:20 -0800471 variables = {'BASE_URL': arc_bucket_url}
Mike Frysinger0bdbc102019-06-13 15:27:29 -0400472 for build, (target, _) in build_targets.items():
David Riley73f00d92016-02-16 18:54:20 -0800473 variables[build + '_TARGET'] = '%s-%s' % (build_branch, target)
David Rileyc0da9d92016-02-01 12:11:01 -0800474
475 portage_util.EBuild.MarkAsStable(
476 unstable_ebuild.ebuild_path, new_ebuild_path,
477 variables, make_stable=True)
478 new_ebuild = portage_util.EBuild(new_ebuild_path)
479
480 # Determine whether this is ebuild is redundant.
481 if IsTheNewEBuildRedundant(new_ebuild, stable_candidate):
482 msg = 'Previous ebuild with same version found and ebuild is redundant.'
483 logging.info(msg)
484 os.unlink(new_ebuild_path)
485 return None
486
487 if stable_candidate:
488 logging.PrintBuildbotLink('Android revisions',
489 GetAndroidRevisionListLink(build_branch,
490 stable_candidate,
491 new_ebuild))
492
493 git.RunGit(package_dir, ['add', new_ebuild_path])
494 if stable_candidate and not stable_candidate.IsSticky():
495 git.RunGit(package_dir, ['rm', stable_candidate.ebuild_path])
496
497 # Update ebuild manifest and git add it.
498 gen_manifest_cmd = ['ebuild', new_ebuild_path, 'manifest', '--force']
Mike Frysinger45602c72019-09-22 02:15:11 -0400499 cros_build_lib.run(gen_manifest_cmd, extra_env=None, print_cmd=True)
David Rileyc0da9d92016-02-01 12:11:01 -0800500 git.RunGit(package_dir, ['add', 'Manifest'])
501
502 portage_util.EBuild.CommitChange(
Hidehiko Abe4fd94ae2017-01-24 18:59:55 +0900503 _GIT_COMMIT_MESSAGE % {'android_package': android_package,
David Rileyc0da9d92016-02-01 12:11:01 -0800504 'android_version': android_version},
505 package_dir)
506
507 return '%s-%s' % (new_ebuild.package, new_ebuild.version)
508
509
510def GetParser():
511 """Creates the argument parser."""
512 parser = commandline.ArgumentParser()
513 parser.add_argument('-b', '--boards')
514 parser.add_argument('--android_bucket_url',
David Riley73f00d92016-02-16 18:54:20 -0800515 default=constants.ANDROID_BUCKET_URL,
516 type='gs_path')
David Rileyc0da9d92016-02-01 12:11:01 -0800517 parser.add_argument('--android_build_branch',
Shuhei Takahashi6d02c192017-04-05 14:01:24 +0900518 required=True,
519 help='Android branch to import from. '
520 'Ex: git_mnc-dr-arc-dev')
Hidehiko Abe4fd94ae2017-01-24 18:59:55 +0900521 parser.add_argument('--android_package',
522 default=constants.ANDROID_PACKAGE_NAME)
David Riley73f00d92016-02-16 18:54:20 -0800523 parser.add_argument('--arc_bucket_url',
524 default=constants.ARC_BUCKET_URL,
525 type='gs_path')
David Rileyc0da9d92016-02-01 12:11:01 -0800526 parser.add_argument('-f', '--force_version',
527 help='Android build id to use')
528 parser.add_argument('-s', '--srcroot',
529 default=os.path.join(os.environ['HOME'], 'trunk', 'src'),
530 help='Path to the src directory')
531 parser.add_argument('-t', '--tracking_branch', default='cros/master',
532 help='Branch we are tracking changes against')
533 return parser
534
535
536def main(argv):
Hidehiko Abec9ecf262017-07-05 15:17:41 +0900537 logging.EnableBuildbotMarkers()
David Rileyc0da9d92016-02-01 12:11:01 -0800538 parser = GetParser()
539 options = parser.parse_args(argv)
540 options.Freeze()
541
542 overlay_dir = os.path.abspath(_OVERLAY_DIR % {'srcroot': options.srcroot})
Hidehiko Abe4fd94ae2017-01-24 18:59:55 +0900543 android_package_dir = os.path.join(
544 overlay_dir,
545 portage_util.GetFullAndroidPortagePackageName(options.android_package))
David Rileyc0da9d92016-02-01 12:11:01 -0800546 version_to_uprev = None
David Rileyc0da9d92016-02-01 12:11:01 -0800547
548 (unstable_ebuild, stable_ebuilds) = FindAndroidCandidates(android_package_dir)
David Riley73f00d92016-02-16 18:54:20 -0800549 acls = MakeAclDict(android_package_dir)
Qijiang Fan6588cc92019-11-20 13:26:04 +0900550 build_targets = MakeBuildTargetDict(options.android_package,
551 options.android_build_branch)
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900552 # Mirror artifacts, i.e., images and some sdk tools (e.g., adb, aapt).
553 version_to_uprev = MirrorArtifacts(options.android_bucket_url,
554 options.android_build_branch,
555 options.arc_bucket_url, acls,
Nicolas Norvez4bd854f2017-05-23 10:04:45 -0700556 build_targets,
Hidehiko Abe1ebc25d2016-07-28 02:24:37 +0900557 options.force_version)
558
David Rileyc0da9d92016-02-01 12:11:01 -0800559 stable_candidate = portage_util.BestEBuild(stable_ebuilds)
560
561 if stable_candidate:
Lann Martinffb95162018-08-28 12:02:54 -0600562 logging.info('Stable candidate found %s', stable_candidate.version)
David Rileyc0da9d92016-02-01 12:11:01 -0800563 else:
564 logging.info('No stable candidate found.')
565
566 tracking_branch = 'remotes/m/%s' % os.path.basename(options.tracking_branch)
567 existing_branch = git.GetCurrentBranch(android_package_dir)
568 work_branch = cros_mark_as_stable.GitBranch(constants.STABLE_EBUILD_BRANCH,
569 tracking_branch,
570 android_package_dir)
571 work_branch.CreateBranch()
572
573 # In the case of uprevving overlays that have patches applied to them,
574 # include the patched changes in the stabilizing branch.
575 if existing_branch:
576 git.RunGit(overlay_dir, ['rebase', existing_branch])
577
578 android_version_atom = MarkAndroidEBuildAsStable(
Hidehiko Abe4fd94ae2017-01-24 18:59:55 +0900579 stable_candidate, unstable_ebuild, options.android_package,
David Riley73f00d92016-02-16 18:54:20 -0800580 version_to_uprev, android_package_dir,
Nicolas Norvez4bd854f2017-05-23 10:04:45 -0700581 options.android_build_branch, options.arc_bucket_url, build_targets)
David Rileyc0da9d92016-02-01 12:11:01 -0800582 if android_version_atom:
583 if options.boards:
584 cros_mark_as_stable.CleanStalePackages(options.srcroot,
585 options.boards.split(':'),
586 [android_version_atom])
587
588 # Explicit print to communicate to caller.
589 print('ANDROID_VERSION_ATOM=%s' % android_version_atom)