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