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 | |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 49 | _RUNTIME_ARTIFACTS_BUCKET_URL = 'gs://chromeos-arc-images/runtime_artifacts' |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 50 | |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 51 | |
| 52 | def FindAndroidCandidates(package_dir): |
| 53 | """Return a tuple of Android's unstable ebuild and stable ebuilds. |
| 54 | |
| 55 | Args: |
| 56 | package_dir: The path to where the package ebuild is stored. |
| 57 | |
| 58 | Returns: |
| 59 | Tuple [unstable_ebuild, stable_ebuilds]. |
| 60 | |
| 61 | Raises: |
| 62 | Exception: if no unstable ebuild exists for Android. |
| 63 | """ |
| 64 | stable_ebuilds = [] |
| 65 | unstable_ebuilds = [] |
| 66 | for path in glob.glob(os.path.join(package_dir, '*.ebuild')): |
| 67 | ebuild = portage_util.EBuild(path) |
| 68 | if ebuild.version == '9999': |
| 69 | unstable_ebuilds.append(ebuild) |
| 70 | else: |
| 71 | stable_ebuilds.append(ebuild) |
| 72 | |
| 73 | # Apply some sanity checks. |
| 74 | if not unstable_ebuilds: |
| 75 | raise Exception('Missing 9999 ebuild for %s' % package_dir) |
| 76 | if not stable_ebuilds: |
Lann Martin | ffb9516 | 2018-08-28 12:02:54 -0600 | [diff] [blame] | 77 | logging.warning('Missing stable ebuild for %s', package_dir) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 78 | |
| 79 | return portage_util.BestEBuild(unstable_ebuilds), stable_ebuilds |
| 80 | |
| 81 | |
Shao-Chuan Lee | 085e3d7 | 2020-05-11 16:00:42 +0900 | [diff] [blame] | 82 | def PrintUprevMetadata(build_branch, stable_candidate, new_ebuild): |
| 83 | """Shows metadata on buildbot page at UprevAndroid step. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 84 | |
| 85 | Args: |
Shao-Chuan Lee | 085e3d7 | 2020-05-11 16:00:42 +0900 | [diff] [blame] | 86 | build_branch: The branch of Android builds. |
| 87 | stable_candidate: The existing stable ebuild. |
| 88 | new_ebuild: The newly written ebuild. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 89 | """ |
Shao-Chuan Lee | 085e3d7 | 2020-05-11 16:00:42 +0900 | [diff] [blame] | 90 | # Examples: |
| 91 | # "android-container-pi revved 6461825-r1 -> 6468247-r1" |
| 92 | # "android-container-pi revved 6461825-r1 -> 6461825-r2 (ebuild update only)" |
| 93 | msg = '%s revved %s -> %s' % (stable_candidate.pkgname, |
| 94 | stable_candidate.version, |
| 95 | new_ebuild.version) |
| 96 | |
| 97 | old_android = stable_candidate.version_no_rev |
| 98 | new_android = new_ebuild.version_no_rev |
| 99 | |
| 100 | if old_android == new_android: |
| 101 | msg += ' (ebuild update only)' |
| 102 | else: |
| 103 | ab_link = ('https://android-build.googleplex.com' |
| 104 | '/builds/%s/branches/%s/cls?end=%s' |
| 105 | % (new_android, build_branch, old_android)) |
Chris McDonald | b55b703 | 2021-06-17 16:41:32 -0600 | [diff] [blame] | 106 | cbuildbot_alerts.PrintBuildbotLink('Android changelog', ab_link) |
Shao-Chuan Lee | 085e3d7 | 2020-05-11 16:00:42 +0900 | [diff] [blame] | 107 | |
Chris McDonald | b55b703 | 2021-06-17 16:41:32 -0600 | [diff] [blame] | 108 | cbuildbot_alerts.PrintBuildbotStepText(msg) |
| 109 | cbuildbot_alerts.PrintKitchenSetBuildProperty('android_uprev', json.dumps({ |
Junichi Uekawa | d21f94d | 2020-07-27 15:50:05 +0900 | [diff] [blame] | 110 | 'branch': build_branch, |
| 111 | 'new': new_ebuild.version, |
| 112 | 'old': stable_candidate.version, |
| 113 | 'pkgname': stable_candidate.pkgname, |
| 114 | })) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 115 | |
| 116 | |
Yury Khmel | b009aeb | 2020-08-19 19:40:00 -0700 | [diff] [blame] | 117 | def FindDataCollectorArtifacts(gs_context, |
| 118 | android_version, |
| 119 | runtime_artifacts_bucket_url, |
| 120 | version_reference): |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 121 | r"""Finds and includes into variables artifacts from arc.DataCollector. |
| 122 | |
Yury Khmel | b009aeb | 2020-08-19 19:40:00 -0700 | [diff] [blame] | 123 | This is used from UpdateDataCollectorArtifacts in order to check the |
| 124 | particular version. |
| 125 | |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 126 | Args: |
Yury Khmel | b009aeb | 2020-08-19 19:40:00 -0700 | [diff] [blame] | 127 | gs_context: context to execute gsutil |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 128 | android_version: The \d+ build id of Android. |
| 129 | runtime_artifacts_bucket_url: root of runtime artifacts |
Yury Khmel | b009aeb | 2020-08-19 19:40:00 -0700 | [diff] [blame] | 130 | build_branch: build branch. Used to determine the pinned version if exists. |
| 131 | version_reference: which version to use as a reference. Could be '${PV}' in |
| 132 | case version of data collector artifacts matches the |
| 133 | Android version or direct version in case of override. |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 134 | |
| 135 | Returns: |
Yury Khmel | b009aeb | 2020-08-19 19:40:00 -0700 | [diff] [blame] | 136 | dictionary with filled ebuild variables. This dictionary is empty in case |
| 137 | no artificats are found. |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 138 | """ |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 139 | variables = {} |
Yury Khmel | b009aeb | 2020-08-19 19:40:00 -0700 | [diff] [blame] | 140 | |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 141 | buckets = ['ureadahead_pack', 'gms_core_cache'] |
| 142 | archs = ['arm', 'arm64', 'x86', 'x86_64'] |
| 143 | build_types = ['user', 'userdebug'] |
| 144 | |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 145 | for bucket in buckets: |
| 146 | for arch in archs: |
| 147 | for build_type in build_types: |
| 148 | path = (f'{runtime_artifacts_bucket_url}/{bucket}_{arch}_{build_type}_' |
Yury Khmel | e1b7440 | 2020-05-18 08:41:35 -0700 | [diff] [blame] | 149 | f'{android_version}.tar') |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 150 | if gs_context.Exists(path): |
| 151 | variables[(f'{arch}_{build_type}_{bucket}').upper()] = ( |
| 152 | f'{runtime_artifacts_bucket_url}/{bucket}_{arch}_{build_type}_' |
| 153 | f'{version_reference}.tar') |
| 154 | |
| 155 | return variables |
| 156 | |
| 157 | |
Yury Khmel | b009aeb | 2020-08-19 19:40:00 -0700 | [diff] [blame] | 158 | def UpdateDataCollectorArtifacts(android_version, |
| 159 | runtime_artifacts_bucket_url, |
| 160 | build_branch): |
| 161 | r"""Finds and includes into variables artifacts from arc.DataCollector. |
| 162 | |
| 163 | This verifies default android version. In case artificts are not found for |
| 164 | default Android version it tries to find artifacts for pinned version. If |
| 165 | pinned version is provided, it is required artifacts exist for the pinned |
| 166 | version. |
| 167 | |
| 168 | Args: |
| 169 | android_version: The \d+ build id of Android. |
| 170 | runtime_artifacts_bucket_url: root of runtime artifacts |
| 171 | build_branch: build branch. Used to determine the pinned version if exists. |
| 172 | |
| 173 | Returns: |
| 174 | dictionary with filled ebuild variables. |
| 175 | """ |
| 176 | |
| 177 | gs_context = gs.GSContext() |
| 178 | # Check the existing version. If we find any artifacts, use them. |
| 179 | variables = FindDataCollectorArtifacts(gs_context, |
| 180 | android_version, |
| 181 | runtime_artifacts_bucket_url, |
| 182 | '${PV}') |
| 183 | if variables: |
| 184 | # Data artificts were found. |
| 185 | return variables |
| 186 | |
| 187 | # Check pinned version for the current branch. |
| 188 | pin_path = (f'{runtime_artifacts_bucket_url}/{build_branch}_pin_version') |
| 189 | if not gs_context.Exists(pin_path): |
| 190 | # No pinned version. |
| 191 | logging.warning( |
| 192 | 'No data collector artifacts were found for %s', |
| 193 | android_version) |
| 194 | return variables |
| 195 | |
| 196 | pin_version = gs_context.Cat(pin_path, encoding='utf-8').rstrip() |
| 197 | logging.info('Pinned version %s overrides %s', |
| 198 | pin_version, android_version) |
| 199 | variables = FindDataCollectorArtifacts(gs_context, |
| 200 | pin_version, |
| 201 | runtime_artifacts_bucket_url, |
| 202 | pin_version) |
| 203 | if not variables: |
| 204 | # If pin version set it must contain data. |
| 205 | raise Exception('Pinned version %s:%s does not contain artificats' % ( |
| 206 | build_branch, pin_version)) |
| 207 | |
| 208 | return variables |
| 209 | |
| 210 | |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 211 | def MarkAndroidEBuildAsStable(stable_candidate, unstable_ebuild, |
| 212 | android_package, android_version, package_dir, |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 213 | build_branch, arc_bucket_url, |
Shao-Chuan Lee | 7e9eacb | 2021-03-19 16:45:21 +0900 | [diff] [blame] | 214 | runtime_artifacts_bucket_url): |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 215 | r"""Uprevs the Android ebuild. |
| 216 | |
| 217 | This is the main function that uprevs from a stable candidate |
| 218 | to its new version. |
| 219 | |
| 220 | Args: |
| 221 | stable_candidate: ebuild that corresponds to the stable ebuild we are |
| 222 | revving from. If None, builds the a new ebuild given the version |
| 223 | with revision set to 1. |
| 224 | unstable_ebuild: ebuild corresponding to the unstable ebuild for Android. |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 225 | android_package: android package name. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 226 | android_version: The \d+ build id of Android. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 227 | package_dir: Path to the android-container package dir. |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 228 | build_branch: branch of Android builds. |
| 229 | arc_bucket_url: URL of the target ARC build gs bucket. |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 230 | runtime_artifacts_bucket_url: root of runtime artifacts |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 231 | |
| 232 | Returns: |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 233 | Tuple[str, List[str], List[str]] if revved, or None |
| 234 | 1. Full portage version atom (including rc's, etc) that was revved. |
| 235 | 2. List of files to be `git add`ed. |
| 236 | 3. List of files to be `git rm`ed. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 237 | """ |
| 238 | def IsTheNewEBuildRedundant(new_ebuild, stable_ebuild): |
| 239 | """Returns True if the new ebuild is redundant. |
| 240 | |
| 241 | This is True if there if the current stable ebuild is the exact same copy |
| 242 | of the new one. |
| 243 | """ |
| 244 | if not stable_ebuild: |
| 245 | return False |
| 246 | |
David Riley | 676f540 | 2016-02-12 17:24:23 -0800 | [diff] [blame] | 247 | if stable_candidate.version_no_rev == new_ebuild.version_no_rev: |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 248 | return filecmp.cmp( |
| 249 | new_ebuild.ebuild_path, stable_ebuild.ebuild_path, shallow=False) |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 250 | return False |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 251 | |
| 252 | # 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] | 253 | if stable_candidate and stable_candidate.version_no_rev == android_version: |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 254 | new_ebuild_path = '%s-r%d.ebuild' % ( |
| 255 | stable_candidate.ebuild_path_no_revision, |
| 256 | stable_candidate.current_revision + 1) |
| 257 | else: |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 258 | pf = '%s-%s-r1' % (android_package, android_version) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 259 | new_ebuild_path = os.path.join(package_dir, '%s.ebuild' % pf) |
| 260 | |
Shao-Chuan Lee | 7e9eacb | 2021-03-19 16:45:21 +0900 | [diff] [blame] | 261 | build_targets = constants.ANDROID_BRANCH_TO_BUILD_TARGETS[build_branch] |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 262 | variables = {'BASE_URL': arc_bucket_url} |
Shao-Chuan Lee | 41fe352 | 2021-03-22 15:50:31 +0900 | [diff] [blame] | 263 | for var, target in build_targets.items(): |
| 264 | variables[var] = f'{build_branch}-linux-{target}' |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 265 | |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 266 | variables.update(UpdateDataCollectorArtifacts( |
Yury Khmel | b009aeb | 2020-08-19 19:40:00 -0700 | [diff] [blame] | 267 | android_version, runtime_artifacts_bucket_url, build_branch)) |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 268 | |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 269 | portage_util.EBuild.MarkAsStable( |
| 270 | unstable_ebuild.ebuild_path, new_ebuild_path, |
| 271 | variables, make_stable=True) |
| 272 | new_ebuild = portage_util.EBuild(new_ebuild_path) |
| 273 | |
| 274 | # Determine whether this is ebuild is redundant. |
| 275 | if IsTheNewEBuildRedundant(new_ebuild, stable_candidate): |
| 276 | msg = 'Previous ebuild with same version found and ebuild is redundant.' |
| 277 | logging.info(msg) |
Chris McDonald | b55b703 | 2021-06-17 16:41:32 -0600 | [diff] [blame] | 278 | cbuildbot_alerts.PrintBuildbotStepText('%s %s not revved' |
Shao-Chuan Lee | 84bf9a2 | 2021-11-19 17:42:11 +0900 | [diff] [blame] | 279 | % (stable_candidate.pkgname, |
| 280 | stable_candidate.version)) |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 281 | osutils.SafeUnlink(new_ebuild_path) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 282 | return None |
| 283 | |
Shao-Chuan Lee | 085e3d7 | 2020-05-11 16:00:42 +0900 | [diff] [blame] | 284 | # PFQ runs should always be able to find a stable candidate. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 285 | if stable_candidate: |
Shao-Chuan Lee | 085e3d7 | 2020-05-11 16:00:42 +0900 | [diff] [blame] | 286 | PrintUprevMetadata(build_branch, stable_candidate, new_ebuild) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 287 | |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 288 | files_to_add = [new_ebuild_path] |
| 289 | files_to_remove = [] |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 290 | if stable_candidate and not stable_candidate.IsSticky(): |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 291 | osutils.SafeUnlink(stable_candidate.ebuild_path) |
| 292 | files_to_remove.append(stable_candidate.ebuild_path) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 293 | |
| 294 | # Update ebuild manifest and git add it. |
| 295 | gen_manifest_cmd = ['ebuild', new_ebuild_path, 'manifest', '--force'] |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 296 | 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] | 297 | files_to_add.append(os.path.join(package_dir, 'Manifest')) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 298 | |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 299 | return ( |
| 300 | f'{new_ebuild.package}-{new_ebuild.version}', |
| 301 | files_to_add, |
| 302 | files_to_remove, |
| 303 | ) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 304 | |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 305 | |
Shao-Chuan Lee | 007dbe8 | 2021-02-09 14:05:39 +0900 | [diff] [blame] | 306 | def _PrepareGitBranch(overlay_dir): |
| 307 | """Prepares a git branch for the uprev commit. |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 308 | |
Shao-Chuan Lee | 007dbe8 | 2021-02-09 14:05:39 +0900 | [diff] [blame] | 309 | If the overlay project is currently on a branch (e.g. patches are being |
| 310 | applied), rebase the new branch on top of it. |
| 311 | |
| 312 | Args: |
| 313 | overlay_dir: The overlay directory. |
| 314 | """ |
| 315 | existing_branch = git.GetCurrentBranch(overlay_dir) |
| 316 | repo_util.Repository.MustFind(overlay_dir).StartBranch( |
| 317 | constants.STABLE_EBUILD_BRANCH, projects=['.'], cwd=overlay_dir) |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 318 | if existing_branch: |
| 319 | git.RunGit(overlay_dir, ['rebase', existing_branch]) |
| 320 | |
| 321 | |
| 322 | def _CommitChange(message, android_package_dir, files_to_add, files_to_remove): |
| 323 | """Commit changes to git with list of files to add/remove.""" |
| 324 | git.RunGit(android_package_dir, ['add', '--'] + files_to_add) |
| 325 | git.RunGit(android_package_dir, ['rm', '--'] + files_to_remove) |
| 326 | |
| 327 | portage_util.EBuild.CommitChange(message, android_package_dir) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 328 | |
| 329 | |
| 330 | def GetParser(): |
| 331 | """Creates the argument parser.""" |
| 332 | parser = commandline.ArgumentParser() |
| 333 | parser.add_argument('-b', '--boards') |
| 334 | parser.add_argument('--android_bucket_url', |
Shao-Chuan Lee | e394015 | 2021-03-25 14:44:36 +0900 | [diff] [blame] | 335 | default=android.ANDROID_BUCKET_URL, |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 336 | type='gs_path') |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 337 | parser.add_argument('--android_build_branch', |
Shao-Chuan Lee | 7e9eacb | 2021-03-19 16:45:21 +0900 | [diff] [blame] | 338 | choices=constants.ANDROID_BRANCH_TO_BUILD_TARGETS, |
Shao-Chuan Lee | a4b4f30 | 2021-05-12 14:40:20 +0900 | [diff] [blame] | 339 | help='Android branch to import from, overriding default') |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 340 | parser.add_argument('--android_package', |
Shao-Chuan Lee | a4b4f30 | 2021-05-12 14:40:20 +0900 | [diff] [blame] | 341 | required=True, |
| 342 | choices=constants.ANDROID_ALL_PACKAGES, |
| 343 | help='Android package to uprev') |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 344 | parser.add_argument('--arc_bucket_url', |
| 345 | default=constants.ARC_BUCKET_URL, |
| 346 | type='gs_path') |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 347 | parser.add_argument('-f', '--force_version', |
| 348 | help='Android build id to use') |
| 349 | parser.add_argument('-s', '--srcroot', |
| 350 | default=os.path.join(os.environ['HOME'], 'trunk', 'src'), |
| 351 | help='Path to the src directory') |
khmel@chromium.org | d3ec3d7 | 2020-04-29 15:57:35 -0700 | [diff] [blame] | 352 | parser.add_argument('--runtime_artifacts_bucket_url', |
| 353 | default=_RUNTIME_ARTIFACTS_BUCKET_URL, |
| 354 | type='gs_path') |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 355 | parser.add_argument('--skip_commit', |
| 356 | action='store_true', |
| 357 | help='Skip commiting uprev changes to git') |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 358 | return parser |
| 359 | |
| 360 | |
| 361 | def main(argv): |
Chris McDonald | b55b703 | 2021-06-17 16:41:32 -0600 | [diff] [blame] | 362 | cbuildbot_alerts.EnableBuildbotMarkers() |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 363 | parser = GetParser() |
| 364 | options = parser.parse_args(argv) |
| 365 | options.Freeze() |
| 366 | |
| 367 | overlay_dir = os.path.abspath(_OVERLAY_DIR % {'srcroot': options.srcroot}) |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 368 | android_package_dir = os.path.join( |
| 369 | overlay_dir, |
| 370 | portage_util.GetFullAndroidPortagePackageName(options.android_package)) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 371 | |
Shao-Chuan Lee | a4b4f30 | 2021-05-12 14:40:20 +0900 | [diff] [blame] | 372 | # Use default Android branch if not overridden. |
| 373 | android_build_branch = ( |
| 374 | options.android_build_branch or |
| 375 | android.GetAndroidBranchForPackage(options.android_package)) |
| 376 | |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 377 | (unstable_ebuild, stable_ebuilds) = FindAndroidCandidates(android_package_dir) |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 378 | # 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] | 379 | version_to_uprev = android.MirrorArtifacts(options.android_bucket_url, |
Shao-Chuan Lee | a4b4f30 | 2021-05-12 14:40:20 +0900 | [diff] [blame] | 380 | android_build_branch, |
Shao-Chuan Lee | 6865ff5 | 2021-03-19 18:29:18 +0900 | [diff] [blame] | 381 | options.arc_bucket_url, |
| 382 | android_package_dir, |
Shao-Chuan Lee | 7fd0ca1 | 2021-03-19 15:57:40 +0900 | [diff] [blame] | 383 | options.force_version) |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 384 | |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 385 | stable_candidate = portage_util.BestEBuild(stable_ebuilds) |
| 386 | |
| 387 | if stable_candidate: |
Lann Martin | ffb9516 | 2018-08-28 12:02:54 -0600 | [diff] [blame] | 388 | logging.info('Stable candidate found %s', stable_candidate.version) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 389 | else: |
| 390 | logging.info('No stable candidate found.') |
| 391 | |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 392 | if not options.skip_commit: |
Shao-Chuan Lee | 007dbe8 | 2021-02-09 14:05:39 +0900 | [diff] [blame] | 393 | _PrepareGitBranch(overlay_dir) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 394 | |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 395 | revved = MarkAndroidEBuildAsStable( |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 396 | stable_candidate, unstable_ebuild, options.android_package, |
Shao-Chuan Lee | a4b4f30 | 2021-05-12 14:40:20 +0900 | [diff] [blame] | 397 | version_to_uprev, android_package_dir, android_build_branch, |
| 398 | options.arc_bucket_url, options.runtime_artifacts_bucket_url) |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 399 | |
Shao-Chuan Lee | dea458f | 2021-11-25 23:46:53 +0900 | [diff] [blame] | 400 | output = dict(revved=revved) |
| 401 | |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 402 | if revved: |
Shao-Chuan Lee | 84bf9a2 | 2021-11-19 17:42:11 +0900 | [diff] [blame] | 403 | android_atom, files_to_add, files_to_remove = revved |
Shao-Chuan Lee | 301a419 | 2021-02-08 11:53:49 +0900 | [diff] [blame] | 404 | if not options.skip_commit: |
| 405 | _CommitChange( |
| 406 | _GIT_COMMIT_MESSAGE % {'android_package': options.android_package, |
| 407 | 'android_version': version_to_uprev}, |
| 408 | android_package_dir, |
| 409 | files_to_add, |
| 410 | files_to_remove, |
| 411 | ) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 412 | if options.boards: |
| 413 | cros_mark_as_stable.CleanStalePackages(options.srcroot, |
| 414 | options.boards.split(':'), |
Shao-Chuan Lee | 84bf9a2 | 2021-11-19 17:42:11 +0900 | [diff] [blame] | 415 | [android_atom]) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 416 | |
Shao-Chuan Lee | dea458f | 2021-11-25 23:46:53 +0900 | [diff] [blame] | 417 | output['android_atom'] = android_atom |
Shao-Chuan Lee | 62cfbc7 | 2021-11-30 14:15:07 +0900 | [diff] [blame^] | 418 | # This field is read by the PUpr uprev handler for creating CLs. We cannot |
| 419 | # return absolute paths because this script runs inside chroot but the uprev |
| 420 | # handler runs outside. |
| 421 | # Here we return paths relative to |overlay_dir|. |
| 422 | output['modified_files'] = [os.path.relpath(f, overlay_dir) |
| 423 | for f in files_to_add + files_to_remove] |
Shao-Chuan Lee | dea458f | 2021-11-25 23:46:53 +0900 | [diff] [blame] | 424 | |
| 425 | # The output is being parsed by service.packages.uprev_android and has to be |
| 426 | # in its own single line. When invoked from chromite API endpoints, entering |
| 427 | # chroot can generate junk messages on stdout, so we prefix our output with a |
| 428 | # line break to further ensure that. |
| 429 | print('\n' + json.dumps(output)) |