Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2016 The ChromiumOS Authors |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 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. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 40 | _OVERLAY_DIR = "%(srcroot)s/private-overlays/project-cheets-private/" |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 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 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [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): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 65 | """Return a tuple of Android's unstable ebuild and stable ebuilds. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 66 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 67 | Args: |
| 68 | package_dir: The path to where the package ebuild is stored. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 69 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 70 | Returns: |
| 71 | Tuple [unstable_ebuild, stable_ebuilds]. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 72 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 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) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 84 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 85 | # Apply some confidence checks. |
| 86 | if not unstable_ebuilds: |
| 87 | raise Exception("Missing 9999 ebuild for %s" % package_dir) |
| 88 | if not stable_ebuilds: |
| 89 | logging.warning("Missing stable ebuild for %s", package_dir) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 90 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 91 | return portage_util.BestEBuild(unstable_ebuilds), stable_ebuilds |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 92 | |
| 93 | |
Shao-Chuan Lee | 085e3d7 | 2020-05-11 16:00:42 +0900 | [diff] [blame] | 94 | def PrintUprevMetadata(build_branch, stable_candidate, new_ebuild): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 95 | """Shows metadata on buildbot page at UprevAndroid step. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 96 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 97 | Args: |
| 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 | """ |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [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" % ( |
| 106 | stable_candidate.pkgname, |
| 107 | stable_candidate.version, |
| 108 | new_ebuild.version, |
| 109 | ) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 110 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 111 | old_android = stable_candidate.version_no_rev |
| 112 | new_android = new_ebuild.version_no_rev |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 113 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 114 | if old_android == new_android: |
| 115 | msg += " (ebuild update only)" |
| 116 | else: |
| 117 | ab_link = ( |
| 118 | "https://android-build.googleplex.com" |
| 119 | "/builds/%s/branches/%s/cls?end=%s" |
| 120 | % (new_android, build_branch, old_android) |
| 121 | ) |
| 122 | cbuildbot_alerts.PrintBuildbotLink("Android changelog", ab_link) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 123 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 124 | cbuildbot_alerts.PrintBuildbotStepText(msg) |
| 125 | cbuildbot_alerts.PrintKitchenSetBuildProperty( |
| 126 | "android_uprev", |
| 127 | json.dumps( |
| 128 | { |
| 129 | "branch": build_branch, |
| 130 | "new": new_ebuild.version, |
| 131 | "old": stable_candidate.version, |
| 132 | "pkgname": stable_candidate.pkgname, |
| 133 | } |
| 134 | ), |
| 135 | ) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 136 | |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 137 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 138 | def FindDataCollectorArtifacts( |
| 139 | gs_context, android_version, runtime_artifacts_bucket_url, version_reference |
| 140 | ): |
| 141 | r"""Finds and includes into variables artifacts from arc.DataCollector. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 142 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 143 | This is used from UpdateDataCollectorArtifacts in order to check the |
| 144 | particular version. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 145 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 146 | Args: |
| 147 | gs_context: context to execute gsutil |
| 148 | android_version: The \d+ build id of Android. |
| 149 | runtime_artifacts_bucket_url: root of runtime artifacts |
| 150 | build_branch: build branch. Used to determine the pinned version if exists. |
| 151 | version_reference: which version to use as a reference. Could be '${PV}' in |
| 152 | case version of data collector artifacts matches the |
| 153 | Android version or direct version in case of override. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 154 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 155 | Returns: |
| 156 | dictionary with filled ebuild variables. This dictionary is empty in case |
| 157 | no artificats are found. |
| 158 | """ |
| 159 | variables = {} |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 160 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 161 | buckets = ["ureadahead_pack", "gms_core_cache", "tts_cache"] |
| 162 | archs = ["arm", "arm64", "x86", "x86_64"] |
| 163 | build_types = ["user", "userdebug"] |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 164 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 165 | for bucket in buckets: |
| 166 | for arch in archs: |
| 167 | for build_type in build_types: |
| 168 | path = ( |
| 169 | f"{runtime_artifacts_bucket_url}/{bucket}_{arch}_{build_type}_" |
| 170 | f"{android_version}.tar" |
| 171 | ) |
| 172 | if gs_context.Exists(path): |
| 173 | variables[(f"{arch}_{build_type}_{bucket}").upper()] = ( |
| 174 | f"{runtime_artifacts_bucket_url}/{bucket}_{arch}_{build_type}_" |
| 175 | f"{version_reference}.tar" |
| 176 | ) |
| 177 | |
| 178 | return variables |
| 179 | |
| 180 | |
| 181 | def UpdateDataCollectorArtifacts( |
| 182 | android_version, runtime_artifacts_bucket_url, build_branch |
| 183 | ): |
| 184 | r"""Finds and includes into variables artifacts from arc.DataCollector. |
| 185 | |
| 186 | This verifies default android version. In case artificts are not found for |
| 187 | default Android version it tries to find artifacts for pinned version. If |
| 188 | pinned version is provided, it is required artifacts exist for the pinned |
| 189 | version. |
| 190 | |
| 191 | Args: |
| 192 | android_version: The \d+ build id of Android. |
| 193 | runtime_artifacts_bucket_url: root of runtime artifacts |
| 194 | build_branch: build branch. Used to determine the pinned version if exists. |
| 195 | |
| 196 | Returns: |
| 197 | dictionary with filled ebuild variables. |
| 198 | """ |
| 199 | |
| 200 | gs_context = gs.GSContext() |
| 201 | # Check the existing version. If we find any artifacts, use them. |
| 202 | variables = FindDataCollectorArtifacts( |
| 203 | gs_context, android_version, runtime_artifacts_bucket_url, "${PV}" |
| 204 | ) |
| 205 | if variables: |
| 206 | # Data artificts were found. |
| 207 | return variables |
| 208 | |
| 209 | # Check pinned version for the current branch. |
| 210 | pin_path = f"{runtime_artifacts_bucket_url}/{build_branch}_pin_version" |
| 211 | if not gs_context.Exists(pin_path): |
| 212 | # No pinned version. |
| 213 | logging.warning( |
| 214 | "No data collector artifacts were found for %s", android_version |
| 215 | ) |
| 216 | return variables |
| 217 | |
| 218 | pin_version = gs_context.Cat(pin_path, encoding="utf-8").rstrip() |
| 219 | logging.info("Pinned version %s overrides %s", pin_version, android_version) |
| 220 | variables = FindDataCollectorArtifacts( |
| 221 | gs_context, pin_version, runtime_artifacts_bucket_url, pin_version |
| 222 | ) |
| 223 | if not variables: |
| 224 | # If pin version set it must contain data. |
| 225 | raise Exception( |
| 226 | "Pinned version %s:%s does not contain artificats" |
| 227 | % (build_branch, pin_version) |
| 228 | ) |
| 229 | |
| 230 | return variables |
| 231 | |
| 232 | |
| 233 | def MarkAndroidEBuildAsStable( |
| 234 | stable_candidate, |
| 235 | unstable_ebuild, |
| 236 | android_package, |
| 237 | android_version, |
| 238 | package_dir, |
| 239 | build_branch, |
| 240 | arc_bucket_url, |
| 241 | runtime_artifacts_bucket_url, |
| 242 | ): |
| 243 | r"""Uprevs the Android ebuild. |
| 244 | |
| 245 | This is the main function that uprevs from a stable candidate |
| 246 | to its new version. |
| 247 | |
| 248 | Args: |
| 249 | stable_candidate: ebuild that corresponds to the stable ebuild we are |
| 250 | revving from. If None, builds the a new ebuild given the version |
| 251 | with revision set to 1. |
| 252 | unstable_ebuild: ebuild corresponding to the unstable ebuild for Android. |
| 253 | android_package: android package name. |
| 254 | android_version: The \d+ build id of Android. |
| 255 | package_dir: Path to the android-container package dir. |
| 256 | build_branch: branch of Android builds. |
| 257 | arc_bucket_url: URL of the target ARC build gs bucket. |
| 258 | runtime_artifacts_bucket_url: root of runtime artifacts |
| 259 | |
| 260 | Returns: |
| 261 | Tuple[str, List[str], List[str]] if revved, or None |
| 262 | 1. Full portage version atom (including rc's, etc) that was revved. |
| 263 | 2. List of files to be `git add`ed. |
| 264 | 3. List of files to be `git rm`ed. |
| 265 | """ |
| 266 | |
| 267 | def IsTheNewEBuildRedundant(new_ebuild, stable_ebuild): |
| 268 | """Returns True if the new ebuild is redundant. |
| 269 | |
| 270 | This is True if there if the current stable ebuild is the exact same copy |
| 271 | of the new one. |
| 272 | """ |
| 273 | if not stable_ebuild: |
| 274 | return False |
| 275 | |
| 276 | if stable_candidate.version_no_rev == new_ebuild.version_no_rev: |
| 277 | return filecmp.cmp( |
| 278 | new_ebuild.ebuild_path, stable_ebuild.ebuild_path, shallow=False |
| 279 | ) |
| 280 | return False |
| 281 | |
| 282 | # Case where we have the last stable candidate with same version just rev. |
| 283 | if stable_candidate and stable_candidate.version_no_rev == android_version: |
| 284 | new_ebuild_path = "%s-r%d.ebuild" % ( |
| 285 | stable_candidate.ebuild_path_no_revision, |
| 286 | stable_candidate.current_revision + 1, |
| 287 | ) |
| 288 | else: |
| 289 | pf = "%s-%s-r1" % (android_package, android_version) |
| 290 | new_ebuild_path = os.path.join(package_dir, "%s.ebuild" % pf) |
| 291 | |
| 292 | build_targets = constants.ANDROID_BRANCH_TO_BUILD_TARGETS[build_branch] |
| 293 | variables = {"BASE_URL": arc_bucket_url} |
| 294 | for var, target in build_targets.items(): |
| 295 | variables[var] = f"{build_branch}-linux-{target}" |
| 296 | |
| 297 | variables.update( |
| 298 | UpdateDataCollectorArtifacts( |
| 299 | android_version, runtime_artifacts_bucket_url, build_branch |
| 300 | ) |
| 301 | ) |
| 302 | |
| 303 | portage_util.EBuild.MarkAsStable( |
| 304 | unstable_ebuild.ebuild_path, |
| 305 | new_ebuild_path, |
| 306 | variables, |
| 307 | make_stable=True, |
| 308 | ) |
| 309 | new_ebuild = portage_util.EBuild(new_ebuild_path) |
| 310 | |
| 311 | # Determine whether this is ebuild is redundant. |
| 312 | if IsTheNewEBuildRedundant(new_ebuild, stable_candidate): |
| 313 | msg = "Previous ebuild with same version found and ebuild is redundant." |
| 314 | logging.info(msg) |
| 315 | cbuildbot_alerts.PrintBuildbotStepText( |
| 316 | "%s %s not revved" |
| 317 | % (stable_candidate.pkgname, stable_candidate.version) |
| 318 | ) |
| 319 | osutils.SafeUnlink(new_ebuild_path) |
| 320 | return None |
| 321 | |
| 322 | # PFQ runs should always be able to find a stable candidate. |
| 323 | if stable_candidate: |
| 324 | PrintUprevMetadata(build_branch, stable_candidate, new_ebuild) |
| 325 | |
| 326 | files_to_add = [new_ebuild_path] |
| 327 | files_to_remove = [] |
| 328 | if stable_candidate and not stable_candidate.IsSticky(): |
| 329 | osutils.SafeUnlink(stable_candidate.ebuild_path) |
| 330 | files_to_remove.append(stable_candidate.ebuild_path) |
| 331 | |
| 332 | # Update ebuild manifest and git add it. |
| 333 | gen_manifest_cmd = ["ebuild", new_ebuild_path, "manifest", "--force"] |
| 334 | cros_build_lib.run(gen_manifest_cmd, extra_env=None, print_cmd=True) |
| 335 | files_to_add.append(os.path.join(package_dir, "Manifest")) |
| 336 | |
| 337 | return ( |
| 338 | f"{new_ebuild.package}-{new_ebuild.version}", |
| 339 | files_to_add, |
| 340 | files_to_remove, |
| 341 | ) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 342 | |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 343 | |
Shao-Chuan Lee | 007dbe8 | 2021-02-09 14:05:39 +0900 | [diff] [blame] | 344 | def _PrepareGitBranch(overlay_dir): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 345 | """Prepares a git branch for the uprev commit. |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 346 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 347 | If the overlay project is currently on a branch (e.g. patches are being |
| 348 | applied), rebase the new branch on top of it. |
Shao-Chuan Lee | 007dbe8 | 2021-02-09 14:05:39 +0900 | [diff] [blame] | 349 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 350 | Args: |
| 351 | overlay_dir: The overlay directory. |
| 352 | """ |
| 353 | existing_branch = git.GetCurrentBranch(overlay_dir) |
| 354 | repo_util.Repository.MustFind(overlay_dir).StartBranch( |
| 355 | constants.STABLE_EBUILD_BRANCH, projects=["."], cwd=overlay_dir |
| 356 | ) |
| 357 | if existing_branch: |
| 358 | git.RunGit(overlay_dir, ["rebase", existing_branch]) |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 359 | |
| 360 | |
| 361 | def _CommitChange(message, android_package_dir, files_to_add, files_to_remove): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 362 | """Commit changes to git with list of files to add/remove.""" |
| 363 | git.RunGit(android_package_dir, ["add", "--"] + files_to_add) |
| 364 | if files_to_remove: |
| 365 | git.RunGit(android_package_dir, ["rm", "--"] + files_to_remove) |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 366 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 367 | portage_util.EBuild.CommitChange(message, android_package_dir) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 368 | |
| 369 | |
| 370 | def GetParser(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 371 | """Creates the argument parser.""" |
| 372 | parser = commandline.ArgumentParser() |
| 373 | parser.add_argument("-b", "--boards") |
| 374 | parser.add_argument( |
| 375 | "--android_bucket_url", |
| 376 | default=android.ANDROID_BUCKET_URL, |
| 377 | type="gs_path", |
| 378 | ) |
| 379 | parser.add_argument( |
| 380 | "--android_build_branch", |
| 381 | choices=constants.ANDROID_BRANCH_TO_BUILD_TARGETS, |
| 382 | help="Android branch to import from, overriding default", |
| 383 | ) |
| 384 | parser.add_argument( |
| 385 | "--android_package", |
| 386 | required=True, |
| 387 | choices=constants.ANDROID_ALL_PACKAGES, |
| 388 | help="Android package to uprev", |
| 389 | ) |
| 390 | parser.add_argument( |
| 391 | "--arc_bucket_url", default=constants.ARC_BUCKET_URL, type="gs_path" |
| 392 | ) |
| 393 | parser.add_argument("-f", "--force_version", help="Android build id to use") |
| 394 | parser.add_argument( |
| 395 | "-s", |
| 396 | "--srcroot", |
| 397 | default=os.path.join(constants.SOURCE_ROOT, "src"), |
| 398 | help="Path to the src directory", |
| 399 | ) |
| 400 | parser.add_argument( |
| 401 | "--runtime_artifacts_bucket_url", |
| 402 | default=_RUNTIME_ARTIFACTS_BUCKET_URL, |
| 403 | type="gs_path", |
| 404 | ) |
| 405 | parser.add_argument( |
| 406 | "--skip_commit", |
| 407 | action="store_true", |
| 408 | help="Skip commiting uprev changes to git", |
| 409 | ) |
| 410 | parser.add_argument( |
| 411 | "--update_lkgb", |
| 412 | action="store_true", |
| 413 | help=( |
| 414 | "Update the LKGB file instead of uprevving ebuilds " |
| 415 | "and populating artifacts. " |
| 416 | "Requires --force_version be set." |
| 417 | ), |
| 418 | ) |
| 419 | return parser |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 420 | |
| 421 | |
| 422 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 423 | cbuildbot_alerts.EnableBuildbotMarkers() |
| 424 | parser = GetParser() |
| 425 | options = parser.parse_args(argv) |
| 426 | options.Freeze() |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 427 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 428 | overlay_dir = os.path.abspath(_OVERLAY_DIR % {"srcroot": options.srcroot}) |
| 429 | android_package_dir = android.GetAndroidPackageDir( |
| 430 | options.android_package, overlay_dir=overlay_dir |
| 431 | ) |
Shao-Chuan Lee | 3aa7652 | 2021-12-03 17:18:58 +0900 | [diff] [blame] | 432 | |
| 433 | if not options.skip_commit: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 434 | _PrepareGitBranch(overlay_dir) |
Shao-Chuan Lee | 3aa7652 | 2021-12-03 17:18:58 +0900 | [diff] [blame] | 435 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 436 | if options.update_lkgb: |
| 437 | if not options.force_version: |
| 438 | raise Exception("--force_version is required with --update_lkgb") |
Shao-Chuan Lee | a4b4f30 | 2021-05-12 14:40:20 +0900 | [diff] [blame] | 439 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 440 | # Attempt to read current LKGB, if available. |
| 441 | current_lkgb = None |
| 442 | try: |
| 443 | current_lkgb = android.ReadLKGB(android_package_dir) |
| 444 | except android.MissingLKGBError: |
| 445 | logging.info("LKGB file is missing, creating a new one.") |
| 446 | except android.InvalidLKGBError: |
| 447 | logging.warning("Current LKGB file is invalid, overwriting.") |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 448 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 449 | # Do nothing if LKGB is already set to the requested version. |
| 450 | if current_lkgb == options.force_version: |
| 451 | logging.warning( |
| 452 | "LKGB of %s is already %s, doing nothing.", |
| 453 | options.android_package, |
| 454 | options.force_version, |
| 455 | ) |
| 456 | cbuildbot_alerts.PrintBuildbotStepText( |
| 457 | "LKGB of %s unchanged @ %s" |
| 458 | % (options.android_package, options.force_version) |
| 459 | ) |
| 460 | return |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 461 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 462 | # Actually update the LKGB. |
| 463 | path = android.WriteLKGB(android_package_dir, options.force_version) |
| 464 | cbuildbot_alerts.PrintBuildbotStepText( |
| 465 | "LKGB of %s updated %s -> %s" |
| 466 | % (options.android_package, current_lkgb, options.force_version) |
| 467 | ) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 468 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 469 | if not options.skip_commit: |
| 470 | _CommitChange( |
| 471 | _GIT_COMMIT_MESSAGE_LKGB |
| 472 | % { |
| 473 | "android_package": options.android_package, |
| 474 | "android_version": options.force_version, |
| 475 | }, |
| 476 | android_package_dir, |
| 477 | [path], |
| 478 | [], |
| 479 | ) |
| 480 | return |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 481 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 482 | # Use default Android branch if not overridden. |
| 483 | android_build_branch = ( |
| 484 | options.android_build_branch |
| 485 | or android.GetAndroidBranchForPackage(options.android_package) |
| 486 | ) |
Shao-Chuan Lee | dea458f | 2021-11-25 23:46:53 +0900 | [diff] [blame] | 487 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 488 | (unstable_ebuild, stable_ebuilds) = FindAndroidCandidates( |
| 489 | android_package_dir |
| 490 | ) |
| 491 | # Mirror artifacts, i.e., images and some sdk tools (e.g., adb, aapt). |
| 492 | version_to_uprev = android.MirrorArtifacts( |
| 493 | options.android_bucket_url, |
| 494 | android_build_branch, |
| 495 | options.arc_bucket_url, |
| 496 | android_package_dir, |
| 497 | options.force_version, |
| 498 | ) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 499 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 500 | stable_candidate = portage_util.BestEBuild(stable_ebuilds) |
Shao-Chuan Lee | dea458f | 2021-11-25 23:46:53 +0900 | [diff] [blame] | 501 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 502 | if stable_candidate: |
| 503 | logging.info("Stable candidate found %s", stable_candidate.version) |
| 504 | else: |
| 505 | logging.info("No stable candidate found.") |
| 506 | |
| 507 | revved = MarkAndroidEBuildAsStable( |
| 508 | stable_candidate, |
| 509 | unstable_ebuild, |
| 510 | options.android_package, |
| 511 | version_to_uprev, |
| 512 | android_package_dir, |
| 513 | android_build_branch, |
| 514 | options.arc_bucket_url, |
| 515 | options.runtime_artifacts_bucket_url, |
| 516 | ) |
| 517 | |
| 518 | output = dict(revved=bool(revved)) |
| 519 | |
| 520 | if revved: |
| 521 | android_atom, files_to_add, files_to_remove = revved |
| 522 | if not options.skip_commit: |
| 523 | _CommitChange( |
| 524 | _GIT_COMMIT_MESSAGE |
| 525 | % { |
| 526 | "android_package": options.android_package, |
| 527 | "android_version": version_to_uprev, |
| 528 | }, |
| 529 | android_package_dir, |
| 530 | files_to_add, |
| 531 | files_to_remove, |
| 532 | ) |
| 533 | if options.boards: |
| 534 | cros_mark_as_stable.CleanStalePackages( |
| 535 | options.srcroot, options.boards.split(":"), [android_atom] |
| 536 | ) |
| 537 | |
| 538 | output["android_atom"] = android_atom |
| 539 | # This field is read by the PUpr uprev handler for creating CLs. We cannot |
| 540 | # return absolute paths because this script runs inside chroot but the uprev |
| 541 | # handler runs outside. |
| 542 | # Here we return paths relative to |overlay_dir|. |
| 543 | output["modified_files"] = [ |
| 544 | os.path.relpath(f, overlay_dir) |
| 545 | for f in files_to_add + files_to_remove |
| 546 | ] |
| 547 | |
| 548 | # The output is being parsed by service.packages.uprev_android and has to be |
| 549 | # in its own single line. When invoked from chromite API endpoints, entering |
| 550 | # chroot can generate junk messages on stdout, so we prefix our output with a |
| 551 | # line break to further ensure that. |
| 552 | print("\n" + json.dumps(output, sort_keys=True)) |