Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 2 | # 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 | |
| 8 | After calling, it prints outs ANDROID_VERSION_ATOM=(version atom string). A |
| 9 | caller could then use this atom with emerge to build the newly uprevved version |
| 10 | of Android e.g. |
| 11 | |
Shuhei Takahashi | 6d02c19 | 2017-04-05 14:01:24 +0900 | [diff] [blame] | 12 | ./cros_mark_android_as_stable \ |
Shao-Chuan Lee | 9c39e0c | 2020-04-24 11:40:34 +0900 | [diff] [blame] | 13 | --android_build_branch=git_pi-arc \ |
| 14 | --android_package=android-container-pi |
Shuhei Takahashi | 6d02c19 | 2017-04-05 14:01:24 +0900 | [diff] [blame] | 15 | |
Shao-Chuan Lee | 9c39e0c | 2020-04-24 11:40:34 +0900 | [diff] [blame] | 16 | Returns chromeos-base/android-container-pi-6417892-r1 |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 17 | |
Shao-Chuan Lee | 9c39e0c | 2020-04-24 11:40:34 +0900 | [diff] [blame] | 18 | emerge-eve =chromeos-base/android-container-pi-6417892-r1 |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 19 | """ |
| 20 | |
| 21 | from __future__ import print_function |
| 22 | |
| 23 | import filecmp |
| 24 | import glob |
| 25 | import os |
Hidehiko Abe | 12727dd | 2016-05-27 23:23:45 +0900 | [diff] [blame] | 26 | import re |
khmel@google.com | 96c193e | 2018-05-10 14:00:38 -0700 | [diff] [blame] | 27 | import time |
Mike Frysinger | 00a0229 | 2020-04-19 06:28:03 -0400 | [diff] [blame] | 28 | import sys |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 29 | |
Aviv Keshet | b7519e1 | 2016-10-04 00:50:00 -0700 | [diff] [blame] | 30 | from chromite.lib import constants |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 31 | from chromite.lib import commandline |
| 32 | from chromite.lib import cros_build_lib |
| 33 | from chromite.lib import cros_logging as logging |
| 34 | from chromite.lib import git |
| 35 | from chromite.lib import gs |
| 36 | from chromite.lib import portage_util |
| 37 | from chromite.scripts import cros_mark_as_stable |
| 38 | |
| 39 | |
Mike Frysinger | 00a0229 | 2020-04-19 06:28:03 -0400 | [diff] [blame] | 40 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 41 | |
| 42 | |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 43 | # Dir where all the action happens. |
| 44 | _OVERLAY_DIR = '%(srcroot)s/private-overlays/project-cheets-private/' |
| 45 | |
Junichi Uekawa | 6d61ab0 | 2020-04-15 14:52:28 +0900 | [diff] [blame] | 46 | _GIT_COMMIT_MESSAGE = """Marking latest for %(android_package)s ebuild with \ |
| 47 | version %(android_version)s as stable. |
| 48 | |
| 49 | BUG=None |
| 50 | TEST=CQ |
| 51 | """ |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 52 | |
| 53 | # URLs that print lists of Android revisions between two build ids. |
Shao-Chuan Lee | 561b606 | 2020-05-01 14:51:35 +0900 | [diff] [blame] | 54 | _ANDROID_VERSION_URL = ('https://android-build.googleplex.com' |
| 55 | '/builds/%(new)s/branches/%(branch)s/cls?end=%(old)s') |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 56 | |
| 57 | |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 58 | def IsBuildIdValid(bucket_url, build_branch, build_id, targets): |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 59 | """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 Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 68 | targets: Dict from build key to (targe build suffix, artifact file pattern) |
| 69 | pair. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 70 | |
| 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 Frysinger | 0bdbc10 | 2019-06-13 15:27:29 -0400 | [diff] [blame] | 77 | for build, (target, _) in targets.items(): |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 78 | 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: |
| 85 | logging.warn( |
| 86 | 'Directory [%s] does not contain any subpath, ignoring it.', |
| 87 | build_id_path) |
| 88 | return None |
| 89 | if len(subpaths) > 1: |
| 90 | logging.warn( |
| 91 | '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 Abe | 12727dd | 2016-05-27 23:23:45 +0900 | [diff] [blame] | 100 | gs_context.List(subpath_dir) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 101 | except gs.GSNoSuchKey: |
| 102 | logging.warn( |
Hidehiko Abe | 12727dd | 2016-05-27 23:23:45 +0900 | [diff] [blame] | 103 | 'Did not find a file for build id [%s] in directory [%s].', |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 104 | 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 Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 114 | def GetLatestBuild(bucket_url, build_branch, targets): |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 115 | """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 Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 120 | targets: Dict from build key to (targe build suffix, artifact file pattern) |
| 121 | pair. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 122 | |
| 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 Frysinger | 0bdbc10 | 2019-06-13 15:27:29 -0400 | [diff] [blame] | 130 | for target, _ in targets.values(): |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 131 | 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(): |
| 138 | logging.warn('Directory [%s] does not look like a valid build_id.', |
| 139 | gs_result.url) |
| 140 | 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: |
| 152 | logging.warn('Did not find a build_id common to all platforms.') |
| 153 | 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 Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 157 | subpaths = IsBuildIdValid(bucket_url, build_branch, build_id, targets) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 158 | if subpaths: |
| 159 | return build_id, subpaths |
| 160 | |
| 161 | # If not found, no build_id is valid. |
| 162 | logging.warn('Did not find a build_id valid on all platforms.') |
| 163 | return None, None |
| 164 | |
| 165 | |
| 166 | def 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 Martin | ffb9516 | 2018-08-28 12:02:54 -0600 | [diff] [blame] | 191 | logging.warning('Missing stable ebuild for %s', package_dir) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 192 | |
| 193 | return portage_util.BestEBuild(unstable_ebuilds), stable_ebuilds |
| 194 | |
| 195 | |
Nicolas Norvez | b08f54d | 2016-12-05 17:58:54 -0800 | [diff] [blame] | 196 | def _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 Ishii | ef1ada9 | 2018-03-27 15:46:15 +0900 | [diff] [blame] | 216 | if basename in constants.ARC_ARTIFACTS_RENAME_NOT_NEEDED: |
| 217 | return basename |
Nicolas Norvez | b08f54d | 2016-12-05 17:58:54 -0800 | [diff] [blame] | 218 | 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 Thompson | 63ed561 | 2017-08-16 12:27:34 -0700 | [diff] [blame] | 223 | 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 Norvez | b08f54d | 2016-12-05 17:58:54 -0800 | [diff] [blame] | 230 | |
| 231 | |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 232 | def CopyToArcBucket(android_bucket_url, build_branch, build_id, subpaths, |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 233 | targets, arc_bucket_url, acls): |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 234 | """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 Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 244 | targets: Dict from build key to (targe build suffix, artifact file pattern) |
| 245 | pair. |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 246 | 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 Frysinger | 0bdbc10 | 2019-06-13 15:27:29 -0400 | [diff] [blame] | 250 | for build, subpath in subpaths.items(): |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 251 | target, pattern = targets[build] |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 252 | 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 Abe | 12727dd | 2016-05-27 23:23:45 +0900 | [diff] [blame] | 256 | # 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 Norvez | b08f54d | 2016-12-05 17:58:54 -0800 | [diff] [blame] | 260 | arc_path = os.path.join(arc_dir, _GetArcBasename(build, basename)) |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 261 | acl = acls[build] |
| 262 | needs_copy = True |
khmel@google.com | 96c193e | 2018-05-10 14:00:38 -0700 | [diff] [blame] | 263 | retry_count = 2 |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 264 | |
khmel@google.com | 96c193e | 2018-05-10 14:00:38 -0700 | [diff] [blame] | 265 | # 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): |
| 272 | logging.warn('Removing incorrect file %s', arc_path) |
| 273 | gs_context.Remove(arc_path) |
| 274 | else: |
| 275 | logging.info('Skipping already copied file %s', arc_path) |
| 276 | needs_copy = False |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 277 | |
khmel@google.com | 96c193e | 2018-05-10 14:00:38 -0700 | [diff] [blame] | 278 | # 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 Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 301 | |
| 302 | |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 303 | def 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 Martin | ffb9516 | 2018-08-28 12:02:54 -0600 | [diff] [blame] | 330 | logging.error('Requested build %s is not valid', version) |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 331 | 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.com | 778a1cd | 2018-04-13 11:11:58 -0700 | [diff] [blame] | 337 | |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 338 | return version |
| 339 | |
| 340 | |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 341 | def 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 Fan | 6588cc9 | 2019-11-20 13:26:04 +0900 | [diff] [blame] | 356 | def MakeBuildTargetDict(package_name, build_branch): |
Nicolas Norvez | 4bd854f | 2017-05-23 10:04:45 -0700 | [diff] [blame] | 357 | """Creates a dictionary of build targets. |
| 358 | |
Bernie Thompson | 63ed561 | 2017-08-16 12:27:34 -0700 | [diff] [blame] | 359 | Not all targets are common between branches, for example |
Nicolas Norvez | 4bd854f | 2017-05-23 10:04:45 -0700 | [diff] [blame] | 360 | 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 Fan | 6588cc9 | 2019-11-20 13:26:04 +0900 | [diff] [blame] | 365 | package_name: package name of chromeos arc package. |
Nicolas Norvez | 4bd854f | 2017-05-23 10:04:45 -0700 | [diff] [blame] | 366 | 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 Fan | 6588cc9 | 2019-11-20 13:26:04 +0900 | [diff] [blame] | 374 | if constants.ANDROID_CONTAINER_PACKAGE_KEYWORD in package_name: |
Federico 'Morg' Pareschi | 041ee65 | 2020-03-10 15:09:42 +0900 | [diff] [blame] | 375 | target_list = { |
| 376 | constants.ANDROID_MST_BUILD_BRANCH: |
| 377 | constants.ANDROID_MST_BUILD_TARGETS, |
Federico 'Morg' Pareschi | 041ee65 | 2020-03-10 15:09:42 +0900 | [diff] [blame] | 378 | 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 Fan | 6588cc9 | 2019-11-20 13:26:04 +0900 | [diff] [blame] | 385 | elif constants.ANDROID_VM_PACKAGE_KEYWORD in package_name: |
Federico 'Morg' Pareschi | 041ee65 | 2020-03-10 15:09:42 +0900 | [diff] [blame] | 386 | 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 Fan | 6588cc9 | 2019-11-20 13:26:04 +0900 | [diff] [blame] | 398 | raise ValueError('Unknown branch: %s' % build_branch) |
Federico 'Morg' Pareschi | 041ee65 | 2020-03-10 15:09:42 +0900 | [diff] [blame] | 399 | return target |
Nicolas Norvez | 4bd854f | 2017-05-23 10:04:45 -0700 | [diff] [blame] | 400 | |
| 401 | |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 402 | def 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 Abe | c9ecf26 | 2017-07-05 15:17:41 +0900 | [diff] [blame] | 417 | 'old': old_android.version_no_rev, |
| 418 | 'new': new_android.version_no_rev} |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 419 | |
| 420 | |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 421 | def MarkAndroidEBuildAsStable(stable_candidate, unstable_ebuild, |
| 422 | android_package, android_version, package_dir, |
Nicolas Norvez | 4bd854f | 2017-05-23 10:04:45 -0700 | [diff] [blame] | 423 | build_branch, arc_bucket_url, build_targets): |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 424 | 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 Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 434 | android_package: android package name. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 435 | android_version: The \d+ build id of Android. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 436 | package_dir: Path to the android-container package dir. |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 437 | build_branch: branch of Android builds. |
| 438 | arc_bucket_url: URL of the target ARC build gs bucket. |
Nicolas Norvez | 4bd854f | 2017-05-23 10:04:45 -0700 | [diff] [blame] | 439 | build_targets: build targets for this particular Android branch. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 440 | |
| 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 Riley | 676f540 | 2016-02-12 17:24:23 -0800 | [diff] [blame] | 453 | if stable_candidate.version_no_rev == new_ebuild.version_no_rev: |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 454 | 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 Riley | 676f540 | 2016-02-12 17:24:23 -0800 | [diff] [blame] | 458 | if stable_candidate and stable_candidate.version_no_rev == android_version: |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 459 | new_ebuild_path = '%s-r%d.ebuild' % ( |
| 460 | stable_candidate.ebuild_path_no_revision, |
| 461 | stable_candidate.current_revision + 1) |
| 462 | else: |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 463 | pf = '%s-%s-r1' % (android_package, android_version) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 464 | new_ebuild_path = os.path.join(package_dir, '%s.ebuild' % pf) |
| 465 | |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 466 | variables = {'BASE_URL': arc_bucket_url} |
Mike Frysinger | 0bdbc10 | 2019-06-13 15:27:29 -0400 | [diff] [blame] | 467 | for build, (target, _) in build_targets.items(): |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 468 | variables[build + '_TARGET'] = '%s-%s' % (build_branch, target) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 469 | |
| 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 Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 494 | cros_build_lib.run(gen_manifest_cmd, extra_env=None, print_cmd=True) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 495 | git.RunGit(package_dir, ['add', 'Manifest']) |
| 496 | |
| 497 | portage_util.EBuild.CommitChange( |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 498 | _GIT_COMMIT_MESSAGE % {'android_package': android_package, |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 499 | 'android_version': android_version}, |
| 500 | package_dir) |
| 501 | |
| 502 | return '%s-%s' % (new_ebuild.package, new_ebuild.version) |
| 503 | |
| 504 | |
| 505 | def GetParser(): |
| 506 | """Creates the argument parser.""" |
| 507 | parser = commandline.ArgumentParser() |
| 508 | parser.add_argument('-b', '--boards') |
| 509 | parser.add_argument('--android_bucket_url', |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 510 | default=constants.ANDROID_BUCKET_URL, |
| 511 | type='gs_path') |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 512 | parser.add_argument('--android_build_branch', |
Shuhei Takahashi | 6d02c19 | 2017-04-05 14:01:24 +0900 | [diff] [blame] | 513 | required=True, |
| 514 | help='Android branch to import from. ' |
| 515 | 'Ex: git_mnc-dr-arc-dev') |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 516 | parser.add_argument('--android_package', |
| 517 | default=constants.ANDROID_PACKAGE_NAME) |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 518 | parser.add_argument('--arc_bucket_url', |
| 519 | default=constants.ARC_BUCKET_URL, |
| 520 | type='gs_path') |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 521 | 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 | |
| 531 | def main(argv): |
Hidehiko Abe | c9ecf26 | 2017-07-05 15:17:41 +0900 | [diff] [blame] | 532 | logging.EnableBuildbotMarkers() |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 533 | parser = GetParser() |
| 534 | options = parser.parse_args(argv) |
| 535 | options.Freeze() |
| 536 | |
| 537 | overlay_dir = os.path.abspath(_OVERLAY_DIR % {'srcroot': options.srcroot}) |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 538 | android_package_dir = os.path.join( |
| 539 | overlay_dir, |
| 540 | portage_util.GetFullAndroidPortagePackageName(options.android_package)) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 541 | version_to_uprev = None |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 542 | |
| 543 | (unstable_ebuild, stable_ebuilds) = FindAndroidCandidates(android_package_dir) |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 544 | acls = MakeAclDict(android_package_dir) |
Qijiang Fan | 6588cc9 | 2019-11-20 13:26:04 +0900 | [diff] [blame] | 545 | build_targets = MakeBuildTargetDict(options.android_package, |
| 546 | options.android_build_branch) |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 547 | # 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 Norvez | 4bd854f | 2017-05-23 10:04:45 -0700 | [diff] [blame] | 551 | build_targets, |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 552 | options.force_version) |
| 553 | |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 554 | stable_candidate = portage_util.BestEBuild(stable_ebuilds) |
| 555 | |
| 556 | if stable_candidate: |
Lann Martin | ffb9516 | 2018-08-28 12:02:54 -0600 | [diff] [blame] | 557 | logging.info('Stable candidate found %s', stable_candidate.version) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 558 | 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 Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 574 | stable_candidate, unstable_ebuild, options.android_package, |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 575 | version_to_uprev, android_package_dir, |
Nicolas Norvez | 4bd854f | 2017-05-23 10:04:45 -0700 | [diff] [blame] | 576 | options.android_build_branch, options.arc_bucket_url, build_targets) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 577 | 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) |