David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 1 | # 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 Lee | 84bf9a2 | 2021-11-19 17:42:11 +0900 | [diff] [blame] | 7 | After calling, it prints out a JSON representing the result, with the new |
| 8 | Android version atom string included. A caller could then use this atom with |
| 9 | emerge to build the newly uprevved version of Android e.g. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 10 | |
Shuhei Takahashi | 6d02c19 | 2017-04-05 14:01:24 +0900 | [diff] [blame] | 11 | ./cros_mark_android_as_stable \ |
Shao-Chuan Lee | 9c39e0c | 2020-04-24 11:40:34 +0900 | [diff] [blame] | 12 | --android_build_branch=git_pi-arc \ |
| 13 | --android_package=android-container-pi |
Shuhei Takahashi | 6d02c19 | 2017-04-05 14:01:24 +0900 | [diff] [blame] | 14 | |
Shao-Chuan Lee | 84bf9a2 | 2021-11-19 17:42:11 +0900 | [diff] [blame] | 15 | Returns {"android_atom": "chromeos-base/android-container-pi-6417892-r1"} |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 16 | |
Shao-Chuan Lee | 9c39e0c | 2020-04-24 11:40:34 +0900 | [diff] [blame] | 17 | emerge-eve =chromeos-base/android-container-pi-6417892-r1 |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 18 | """ |
| 19 | |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 20 | import filecmp |
| 21 | import glob |
Junichi Uekawa | d21f94d | 2020-07-27 15:50:05 +0900 | [diff] [blame] | 22 | import json |
Chris McDonald | b55b703 | 2021-06-17 16:41:32 -0600 | [diff] [blame] | 23 | import logging |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 24 | import os |
| 25 | |
Chris McDonald | b55b703 | 2021-06-17 16:41:32 -0600 | [diff] [blame] | 26 | from chromite.cbuildbot import cbuildbot_alerts |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 27 | from chromite.lib import commandline |
Chris McDonald | b55b703 | 2021-06-17 16:41:32 -0600 | [diff] [blame] | 28 | from chromite.lib import constants |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 29 | from chromite.lib import cros_build_lib |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 30 | from chromite.lib import git |
| 31 | from chromite.lib import gs |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 32 | from chromite.lib import osutils |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 33 | from chromite.lib import portage_util |
Shao-Chuan Lee | 007dbe8 | 2021-02-09 14:05:39 +0900 | [diff] [blame] | 34 | from chromite.lib import repo_util |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 35 | from chromite.scripts import cros_mark_as_stable |
Shao-Chuan Lee | 7fd0ca1 | 2021-03-19 15:57:40 +0900 | [diff] [blame] | 36 | from chromite.service import android |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 37 | |
| 38 | |
| 39 | # Dir where all the action happens. |
| 40 | _OVERLAY_DIR = '%(srcroot)s/private-overlays/project-cheets-private/' |
| 41 | |
Junichi Uekawa | 6d61ab0 | 2020-04-15 14:52:28 +0900 | [diff] [blame] | 42 | _GIT_COMMIT_MESSAGE = """Marking latest for %(android_package)s ebuild with \ |
| 43 | version %(android_version)s as stable. |
| 44 | |
| 45 | BUG=None |
| 46 | TEST=CQ |
| 47 | """ |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 48 | |
Shao-Chuan Lee | 3aa7652 | 2021-12-03 17:18:58 +0900 | [diff] [blame] | 49 | _GIT_COMMIT_MESSAGE_LKGB = """%(android_package)s: Mark version \ |
| 50 | %(android_version)s as LKGB. |
| 51 | |
| 52 | Generated by Android PFQ. This change will trigger PUpr to actually submit an |
| 53 | Android uprev. |
| 54 | |
| 55 | For details, see "Migration plan" section of go/android-uprev-recipes. |
| 56 | |
| 57 | BUG=None |
| 58 | TEST=CQ |
| 59 | """ |
| 60 | |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 61 | _RUNTIME_ARTIFACTS_BUCKET_URL = 'gs://chromeos-arc-images/runtime_artifacts' |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 62 | |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 63 | |
| 64 | def 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 Martin | ffb9516 | 2018-08-28 12:02:54 -0600 | [diff] [blame] | 89 | logging.warning('Missing stable ebuild for %s', package_dir) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 90 | |
| 91 | return portage_util.BestEBuild(unstable_ebuilds), stable_ebuilds |
| 92 | |
| 93 | |
Shao-Chuan Lee | 085e3d7 | 2020-05-11 16:00:42 +0900 | [diff] [blame] | 94 | def PrintUprevMetadata(build_branch, stable_candidate, new_ebuild): |
| 95 | """Shows metadata on buildbot page at UprevAndroid step. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 96 | |
| 97 | Args: |
Shao-Chuan Lee | 085e3d7 | 2020-05-11 16:00:42 +0900 | [diff] [blame] | 98 | build_branch: The branch of Android builds. |
| 99 | stable_candidate: The existing stable ebuild. |
| 100 | new_ebuild: The newly written ebuild. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 101 | """ |
Shao-Chuan Lee | 085e3d7 | 2020-05-11 16:00:42 +0900 | [diff] [blame] | 102 | # 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 McDonald | b55b703 | 2021-06-17 16:41:32 -0600 | [diff] [blame] | 118 | cbuildbot_alerts.PrintBuildbotLink('Android changelog', ab_link) |
Shao-Chuan Lee | 085e3d7 | 2020-05-11 16:00:42 +0900 | [diff] [blame] | 119 | |
Chris McDonald | b55b703 | 2021-06-17 16:41:32 -0600 | [diff] [blame] | 120 | cbuildbot_alerts.PrintBuildbotStepText(msg) |
| 121 | cbuildbot_alerts.PrintKitchenSetBuildProperty('android_uprev', json.dumps({ |
Junichi Uekawa | d21f94d | 2020-07-27 15:50:05 +0900 | [diff] [blame] | 122 | 'branch': build_branch, |
| 123 | 'new': new_ebuild.version, |
| 124 | 'old': stable_candidate.version, |
| 125 | 'pkgname': stable_candidate.pkgname, |
| 126 | })) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 127 | |
| 128 | |
Yury Khmel | b009aeb | 2020-08-19 19:40:00 -0700 | [diff] [blame] | 129 | def FindDataCollectorArtifacts(gs_context, |
| 130 | android_version, |
| 131 | runtime_artifacts_bucket_url, |
| 132 | version_reference): |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 133 | r"""Finds and includes into variables artifacts from arc.DataCollector. |
| 134 | |
Yury Khmel | b009aeb | 2020-08-19 19:40:00 -0700 | [diff] [blame] | 135 | This is used from UpdateDataCollectorArtifacts in order to check the |
| 136 | particular version. |
| 137 | |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 138 | Args: |
Yury Khmel | b009aeb | 2020-08-19 19:40:00 -0700 | [diff] [blame] | 139 | gs_context: context to execute gsutil |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 140 | android_version: The \d+ build id of Android. |
| 141 | runtime_artifacts_bucket_url: root of runtime artifacts |
Yury Khmel | b009aeb | 2020-08-19 19:40:00 -0700 | [diff] [blame] | 142 | 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.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 146 | |
| 147 | Returns: |
Yury Khmel | b009aeb | 2020-08-19 19:40:00 -0700 | [diff] [blame] | 148 | dictionary with filled ebuild variables. This dictionary is empty in case |
| 149 | no artificats are found. |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 150 | """ |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 151 | variables = {} |
Yury Khmel | b009aeb | 2020-08-19 19:40:00 -0700 | [diff] [blame] | 152 | |
Hung Nguyen | cafdd6e | 2022-05-02 22:39:47 +0000 | [diff] [blame^] | 153 | buckets = ['ureadahead_pack', 'gms_core_cache', 'tts_cache'] |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 154 | archs = ['arm', 'arm64', 'x86', 'x86_64'] |
| 155 | build_types = ['user', 'userdebug'] |
| 156 | |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 157 | 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 Khmel | e1b7440 | 2020-05-18 08:41:35 -0700 | [diff] [blame] | 161 | f'{android_version}.tar') |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 162 | 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 Khmel | b009aeb | 2020-08-19 19:40:00 -0700 | [diff] [blame] | 170 | def 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 Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 223 | def MarkAndroidEBuildAsStable(stable_candidate, unstable_ebuild, |
| 224 | android_package, android_version, package_dir, |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 225 | build_branch, arc_bucket_url, |
Shao-Chuan Lee | 7e9eacb | 2021-03-19 16:45:21 +0900 | [diff] [blame] | 226 | runtime_artifacts_bucket_url): |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 227 | 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 Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 237 | android_package: android package name. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 238 | android_version: The \d+ build id of Android. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 239 | package_dir: Path to the android-container package dir. |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 240 | build_branch: branch of Android builds. |
| 241 | arc_bucket_url: URL of the target ARC build gs bucket. |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 242 | runtime_artifacts_bucket_url: root of runtime artifacts |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 243 | |
| 244 | Returns: |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 245 | 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 Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 249 | """ |
| 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 Riley | 676f540 | 2016-02-12 17:24:23 -0800 | [diff] [blame] | 259 | if stable_candidate.version_no_rev == new_ebuild.version_no_rev: |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 260 | return filecmp.cmp( |
| 261 | new_ebuild.ebuild_path, stable_ebuild.ebuild_path, shallow=False) |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 262 | return False |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 263 | |
| 264 | # 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] | 265 | if stable_candidate and stable_candidate.version_no_rev == android_version: |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 266 | new_ebuild_path = '%s-r%d.ebuild' % ( |
| 267 | stable_candidate.ebuild_path_no_revision, |
| 268 | stable_candidate.current_revision + 1) |
| 269 | else: |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 270 | pf = '%s-%s-r1' % (android_package, android_version) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 271 | new_ebuild_path = os.path.join(package_dir, '%s.ebuild' % pf) |
| 272 | |
Shao-Chuan Lee | 7e9eacb | 2021-03-19 16:45:21 +0900 | [diff] [blame] | 273 | build_targets = constants.ANDROID_BRANCH_TO_BUILD_TARGETS[build_branch] |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 274 | variables = {'BASE_URL': arc_bucket_url} |
Shao-Chuan Lee | 41fe352 | 2021-03-22 15:50:31 +0900 | [diff] [blame] | 275 | for var, target in build_targets.items(): |
| 276 | variables[var] = f'{build_branch}-linux-{target}' |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 277 | |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 278 | variables.update(UpdateDataCollectorArtifacts( |
Yury Khmel | b009aeb | 2020-08-19 19:40:00 -0700 | [diff] [blame] | 279 | android_version, runtime_artifacts_bucket_url, build_branch)) |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 280 | |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 281 | 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 McDonald | b55b703 | 2021-06-17 16:41:32 -0600 | [diff] [blame] | 290 | cbuildbot_alerts.PrintBuildbotStepText('%s %s not revved' |
Shao-Chuan Lee | 84bf9a2 | 2021-11-19 17:42:11 +0900 | [diff] [blame] | 291 | % (stable_candidate.pkgname, |
| 292 | stable_candidate.version)) |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 293 | osutils.SafeUnlink(new_ebuild_path) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 294 | return None |
| 295 | |
Shao-Chuan Lee | 085e3d7 | 2020-05-11 16:00:42 +0900 | [diff] [blame] | 296 | # PFQ runs should always be able to find a stable candidate. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 297 | if stable_candidate: |
Shao-Chuan Lee | 085e3d7 | 2020-05-11 16:00:42 +0900 | [diff] [blame] | 298 | PrintUprevMetadata(build_branch, stable_candidate, new_ebuild) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 299 | |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 300 | files_to_add = [new_ebuild_path] |
| 301 | files_to_remove = [] |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 302 | if stable_candidate and not stable_candidate.IsSticky(): |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 303 | osutils.SafeUnlink(stable_candidate.ebuild_path) |
| 304 | files_to_remove.append(stable_candidate.ebuild_path) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 305 | |
| 306 | # Update ebuild manifest and git add it. |
| 307 | gen_manifest_cmd = ['ebuild', new_ebuild_path, 'manifest', '--force'] |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 308 | cros_build_lib.run(gen_manifest_cmd, extra_env=None, print_cmd=True) |
Shao-Chuan Lee | 556015c | 2021-11-26 00:03:28 +0900 | [diff] [blame] | 309 | files_to_add.append(os.path.join(package_dir, 'Manifest')) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 310 | |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 311 | return ( |
| 312 | f'{new_ebuild.package}-{new_ebuild.version}', |
| 313 | files_to_add, |
| 314 | files_to_remove, |
| 315 | ) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 316 | |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 317 | |
Shao-Chuan Lee | 007dbe8 | 2021-02-09 14:05:39 +0900 | [diff] [blame] | 318 | def _PrepareGitBranch(overlay_dir): |
| 319 | """Prepares a git branch for the uprev commit. |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 320 | |
Shao-Chuan Lee | 007dbe8 | 2021-02-09 14:05:39 +0900 | [diff] [blame] | 321 | 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 Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 330 | if existing_branch: |
| 331 | git.RunGit(overlay_dir, ['rebase', existing_branch]) |
| 332 | |
| 333 | |
| 334 | def _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 Lee | 3aa7652 | 2021-12-03 17:18:58 +0900 | [diff] [blame] | 337 | if files_to_remove: |
| 338 | git.RunGit(android_package_dir, ['rm', '--'] + files_to_remove) |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 339 | |
| 340 | portage_util.EBuild.CommitChange(message, android_package_dir) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 341 | |
| 342 | |
| 343 | def 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 Lee | e394015 | 2021-03-25 14:44:36 +0900 | [diff] [blame] | 348 | default=android.ANDROID_BUCKET_URL, |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 349 | type='gs_path') |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 350 | parser.add_argument('--android_build_branch', |
Shao-Chuan Lee | 7e9eacb | 2021-03-19 16:45:21 +0900 | [diff] [blame] | 351 | choices=constants.ANDROID_BRANCH_TO_BUILD_TARGETS, |
Shao-Chuan Lee | a4b4f30 | 2021-05-12 14:40:20 +0900 | [diff] [blame] | 352 | help='Android branch to import from, overriding default') |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 353 | parser.add_argument('--android_package', |
Shao-Chuan Lee | a4b4f30 | 2021-05-12 14:40:20 +0900 | [diff] [blame] | 354 | required=True, |
| 355 | choices=constants.ANDROID_ALL_PACKAGES, |
| 356 | help='Android package to uprev') |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 357 | parser.add_argument('--arc_bucket_url', |
| 358 | default=constants.ARC_BUCKET_URL, |
| 359 | type='gs_path') |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 360 | parser.add_argument('-f', '--force_version', |
| 361 | help='Android build id to use') |
| 362 | parser.add_argument('-s', '--srcroot', |
Shao-Chuan Lee | 3aa7652 | 2021-12-03 17:18:58 +0900 | [diff] [blame] | 363 | default=os.path.join(constants.SOURCE_ROOT, 'src'), |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 364 | help='Path to the src directory') |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 365 | parser.add_argument('--runtime_artifacts_bucket_url', |
| 366 | default=_RUNTIME_ARTIFACTS_BUCKET_URL, |
| 367 | type='gs_path') |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 368 | parser.add_argument('--skip_commit', |
| 369 | action='store_true', |
| 370 | help='Skip commiting uprev changes to git') |
Shao-Chuan Lee | 3aa7652 | 2021-12-03 17:18:58 +0900 | [diff] [blame] | 371 | 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 Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 376 | return parser |
| 377 | |
| 378 | |
| 379 | def main(argv): |
Chris McDonald | b55b703 | 2021-06-17 16:41:32 -0600 | [diff] [blame] | 380 | cbuildbot_alerts.EnableBuildbotMarkers() |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 381 | 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 Lee | 0e787d2 | 2021-12-13 21:33:35 +0900 | [diff] [blame] | 386 | android_package_dir = android.GetAndroidPackageDir(options.android_package, |
| 387 | overlay_dir=overlay_dir) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 388 | |
Shao-Chuan Lee | 3aa7652 | 2021-12-03 17:18:58 +0900 | [diff] [blame] | 389 | 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 Lee | 6cd897a | 2021-12-10 14:04:32 +0900 | [diff] [blame] | 396 | # 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 Lee | 3aa7652 | 2021-12-03 17:18:58 +0900 | [diff] [blame] | 405 | # Do nothing if LKGB is already set to the requested version. |
Shao-Chuan Lee | 3aa7652 | 2021-12-03 17:18:58 +0900 | [diff] [blame] | 406 | 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 Lee | a4b4f30 | 2021-05-12 14:40:20 +0900 | [diff] [blame] | 431 | # 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 Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 436 | (unstable_ebuild, stable_ebuilds) = FindAndroidCandidates(android_package_dir) |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 437 | # Mirror artifacts, i.e., images and some sdk tools (e.g., adb, aapt). |
Shao-Chuan Lee | 7fd0ca1 | 2021-03-19 15:57:40 +0900 | [diff] [blame] | 438 | version_to_uprev = android.MirrorArtifacts(options.android_bucket_url, |
Shao-Chuan Lee | a4b4f30 | 2021-05-12 14:40:20 +0900 | [diff] [blame] | 439 | android_build_branch, |
Shao-Chuan Lee | 6865ff5 | 2021-03-19 18:29:18 +0900 | [diff] [blame] | 440 | options.arc_bucket_url, |
| 441 | android_package_dir, |
Shao-Chuan Lee | 7fd0ca1 | 2021-03-19 15:57:40 +0900 | [diff] [blame] | 442 | options.force_version) |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 443 | |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 444 | stable_candidate = portage_util.BestEBuild(stable_ebuilds) |
| 445 | |
| 446 | if stable_candidate: |
Lann Martin | ffb9516 | 2018-08-28 12:02:54 -0600 | [diff] [blame] | 447 | logging.info('Stable candidate found %s', stable_candidate.version) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 448 | else: |
| 449 | logging.info('No stable candidate found.') |
| 450 | |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 451 | revved = MarkAndroidEBuildAsStable( |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 452 | stable_candidate, unstable_ebuild, options.android_package, |
Shao-Chuan Lee | a4b4f30 | 2021-05-12 14:40:20 +0900 | [diff] [blame] | 453 | version_to_uprev, android_package_dir, android_build_branch, |
| 454 | options.arc_bucket_url, options.runtime_artifacts_bucket_url) |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 455 | |
Shao-Chuan Lee | 6badd12 | 2021-11-30 14:27:51 +0900 | [diff] [blame] | 456 | output = dict(revved=bool(revved)) |
Shao-Chuan Lee | dea458f | 2021-11-25 23:46:53 +0900 | [diff] [blame] | 457 | |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 458 | if revved: |
Shao-Chuan Lee | 84bf9a2 | 2021-11-19 17:42:11 +0900 | [diff] [blame] | 459 | android_atom, files_to_add, files_to_remove = revved |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 460 | 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 Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 468 | if options.boards: |
| 469 | cros_mark_as_stable.CleanStalePackages(options.srcroot, |
| 470 | options.boards.split(':'), |
Shao-Chuan Lee | 84bf9a2 | 2021-11-19 17:42:11 +0900 | [diff] [blame] | 471 | [android_atom]) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 472 | |
Shao-Chuan Lee | dea458f | 2021-11-25 23:46:53 +0900 | [diff] [blame] | 473 | output['android_atom'] = android_atom |
Shao-Chuan Lee | 62cfbc7 | 2021-11-30 14:15:07 +0900 | [diff] [blame] | 474 | # 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 Lee | dea458f | 2021-11-25 23:46:53 +0900 | [diff] [blame] | 480 | |
| 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 Lee | 6badd12 | 2021-11-30 14:27:51 +0900 | [diff] [blame] | 485 | print('\n' + json.dumps(output, sort_keys=True)) |