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