Kuang-che Wu | 6e4beca | 2018-06-27 17:45:02 +0800 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 2 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | """Android utility. |
| 6 | |
| 7 | Terminology used in this module: |
| 8 | "product-variant" is sometimes called "target" and sometimes "flavor". |
| 9 | I prefer to use "flavor" in the code because |
| 10 | - "target" is too general |
| 11 | - sometimes, it is not in the form of "product-variant", for example, |
| 12 | "cts_arm_64" |
| 13 | """ |
| 14 | |
| 15 | from __future__ import print_function |
| 16 | import json |
| 17 | import logging |
| 18 | import os |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 19 | import tempfile |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 20 | |
| 21 | from bisect_kit import cli |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 22 | from bisect_kit import codechange |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 23 | from bisect_kit import errors |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 24 | from bisect_kit import git_util |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 25 | from bisect_kit import repo_util |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 26 | from bisect_kit import util |
| 27 | |
| 28 | logger = logging.getLogger(__name__) |
| 29 | |
| 30 | ANDROID_URL_BASE = ('https://android-build.googleplex.com/' |
| 31 | 'builds/branch/{branch}') |
| 32 | BUILD_IDS_BETWEEN_URL_TEMPLATE = ( |
| 33 | ANDROID_URL_BASE + '/build-ids/between/{end}/{start}') |
| 34 | BUILD_INFO_URL_TEMPLATE = ANDROID_URL_BASE + '/builds?id={build_id}' |
| 35 | |
| 36 | |
| 37 | def is_android_build_id(s): |
| 38 | """Is an Android build id.""" |
| 39 | # Build ID is always a number |
| 40 | return s.isdigit() |
| 41 | |
| 42 | |
| 43 | def argtype_android_build_id(s): |
| 44 | if not is_android_build_id(s): |
| 45 | msg = 'invalid android build id (%s)' % s |
| 46 | raise cli.ArgTypeError(msg, '9876543') |
| 47 | return s |
| 48 | |
| 49 | |
| 50 | def fetch_android_build_data(url): |
| 51 | """Fetches file from android build server. |
| 52 | |
| 53 | Args: |
| 54 | url: URL to fetch |
| 55 | |
| 56 | Returns: |
| 57 | file content (str). None if failed. |
| 58 | """ |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 59 | # Fetching android build data directly will fail without authentication. |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 60 | # Following code is just serving as demo purpose. You should modify or hook |
| 61 | # it with your own implementation. |
| 62 | logger.warn('fetch_android_build_data need to be hooked') |
| 63 | import urllib2 |
| 64 | try: |
| 65 | return urllib2.urlopen(url).read() |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 66 | except urllib2.URLError as e: |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 67 | logger.exception('failed to fetch "%s"', url) |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 68 | raise errors.ExternalError(str(e)) |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 69 | |
| 70 | |
| 71 | def is_good_build(branch, flavor, build_id): |
| 72 | """Determine a build_id was succeeded. |
| 73 | |
| 74 | Args: |
| 75 | branch: The Android branch from which to retrieve the builds. |
| 76 | flavor: Target name of the Android image in question. |
| 77 | E.g. "cheets_x86-userdebug" or "cheets_arm-user". |
| 78 | build_id: Android build id |
| 79 | |
| 80 | Returns: |
| 81 | True if the given build was successful. |
| 82 | """ |
| 83 | |
| 84 | url = BUILD_INFO_URL_TEMPLATE.format(branch=branch, build_id=build_id) |
| 85 | build = json.loads(fetch_android_build_data(url).decode('utf-8')) |
| 86 | for target in build[0]['targets']: |
| 87 | if target['target']['name'] == flavor and target.get('successful'): |
| 88 | return True |
| 89 | return False |
| 90 | |
| 91 | |
Kuang-che Wu | 0a902f4 | 2019-01-21 18:58:32 +0800 | [diff] [blame] | 92 | def get_build_ids_between(branch, flavor, start, end): |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 93 | """Returns a list of build IDs. |
| 94 | |
| 95 | Args: |
| 96 | branch: The Android branch from which to retrieve the builds. |
Kuang-che Wu | 0a902f4 | 2019-01-21 18:58:32 +0800 | [diff] [blame] | 97 | flavor: Target name of the Android image in question. |
| 98 | E.g. "cheets_x86-userdebug" or "cheets_arm-user". |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 99 | start: The starting point build ID. (inclusive) |
| 100 | end: The ending point build ID. (inclusive) |
| 101 | |
| 102 | Returns: |
| 103 | A list of build IDs. (str) |
| 104 | """ |
Kuang-che Wu | 0a902f4 | 2019-01-21 18:58:32 +0800 | [diff] [blame] | 105 | # Do not remove this argument because our internal implementation need it. |
| 106 | del flavor # not used |
| 107 | |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 108 | # TODO(kcwu): remove pagination hack after b/68239878 fixed |
| 109 | build_id_set = set() |
| 110 | tmp_end = end |
| 111 | while True: |
| 112 | url = BUILD_IDS_BETWEEN_URL_TEMPLATE.format( |
| 113 | branch=branch, start=start, end=tmp_end) |
| 114 | query_result = json.loads(fetch_android_build_data(url).decode('utf-8')) |
| 115 | found_new = set(map(int, query_result['ids'])) - build_id_set |
| 116 | if not found_new: |
| 117 | break |
| 118 | build_id_set.update(found_new) |
| 119 | tmp_end = min(build_id_set) |
| 120 | |
| 121 | logger.debug('Found %d builds in the range.', len(build_id_set)) |
| 122 | |
| 123 | return map(str, sorted(build_id_set)) |
| 124 | |
| 125 | |
| 126 | def lunch(android_root, flavor, *args, **kwargs): |
| 127 | """Helper to run commands with Android lunch env. |
| 128 | |
| 129 | Args: |
| 130 | android_root: root path of Android tree |
| 131 | flavor: lunch flavor |
| 132 | args: command to run |
| 133 | kwargs: extra arguments passed to util.Popen |
| 134 | """ |
| 135 | util.check_call('./android_lunch_helper.sh', android_root, flavor, *args, |
| 136 | **kwargs) |
| 137 | |
| 138 | |
| 139 | def fetch_artifact(flavor, build_id, filename, dest): |
| 140 | """Fetches Android build artifact. |
| 141 | |
| 142 | Args: |
| 143 | flavor: Android build flavor |
| 144 | build_id: Android build id |
| 145 | filename: artifact name |
| 146 | dest: local path to store the fetched artifact |
| 147 | """ |
Kuang-che Wu | c7ee6c8 | 2019-01-16 17:05:49 +0800 | [diff] [blame] | 148 | service_account_args = [] |
| 149 | # These arguments are for bisector service and not required for users of |
| 150 | # bisect-kit using personal account. |
| 151 | if os.environ.get('ANDROID_CLOUD_SERVICE_ACCOUNT_EMAIL'): |
| 152 | assert os.environ.get('ANDROID_CLOUD_SERVICE_ACCOUNT_KEY') |
| 153 | service_account_args += [ |
| 154 | '--email', |
| 155 | os.environ.get('ANDROID_CLOUD_SERVICE_ACCOUNT_EMAIL'), |
| 156 | '--apiary_service_account_private_key_path', |
| 157 | os.environ.get('ANDROID_CLOUD_SERVICE_ACCOUNT_KEY'), |
| 158 | ] |
| 159 | |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 160 | util.check_call('/google/data/ro/projects/android/fetch_artifact', '--target', |
Kuang-che Wu | c7ee6c8 | 2019-01-16 17:05:49 +0800 | [diff] [blame] | 161 | flavor, '--bid', build_id, filename, dest, |
| 162 | *service_account_args) |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 163 | |
| 164 | |
| 165 | def fetch_manifest(android_root, flavor, build_id): |
| 166 | """Fetches Android repo manifest of given build. |
| 167 | |
| 168 | Args: |
| 169 | android_root: root path of Android tree. Fetched manifest file will be |
| 170 | stored inside. |
| 171 | flavor: Android build flavor |
| 172 | build_id: Android build id |
| 173 | |
| 174 | Returns: |
| 175 | the local filename of manifest (relative to android_root/.repo/manifests/) |
| 176 | """ |
| 177 | # Assume manifest is flavor independent, thus not encoded into the file name. |
| 178 | manifest_name = 'manifest_%s.xml' % build_id |
| 179 | manifest_path = os.path.join(android_root, '.repo', 'manifests', |
| 180 | manifest_name) |
| 181 | if not os.path.exists(manifest_path): |
| 182 | fetch_artifact(flavor, build_id, manifest_name, manifest_path) |
| 183 | return manifest_name |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 184 | |
| 185 | |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 186 | def lookup_build_timestamp(flavor, build_id): |
| 187 | """Lookup timestamp of Android prebuilt. |
| 188 | |
| 189 | Args: |
| 190 | flavor: Android build flavor |
| 191 | build_id: Android build id |
| 192 | |
| 193 | Returns: |
| 194 | timestamp |
| 195 | """ |
| 196 | tmp_fn = tempfile.mktemp() |
| 197 | try: |
| 198 | fetch_artifact(flavor, build_id, 'BUILD_INFO', tmp_fn) |
| 199 | data = json.load(open(tmp_fn)) |
| 200 | return int(data['sync_start_time']) |
| 201 | finally: |
| 202 | if os.path.exists(tmp_fn): |
| 203 | os.unlink(tmp_fn) |
| 204 | |
| 205 | |
| 206 | class AndroidSpecManager(codechange.SpecManager): |
| 207 | """Repo manifest related operations. |
| 208 | |
| 209 | This class fetches and enumerates android manifest files, parses them, |
| 210 | and sync to disk state according to them. |
| 211 | """ |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 212 | |
| 213 | def __init__(self, config): |
| 214 | self.config = config |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 215 | self.manifest_dir = os.path.join(self.config['android_root'], '.repo', |
| 216 | 'manifests') |
| 217 | |
| 218 | def collect_float_spec(self, old, new): |
| 219 | result = [] |
| 220 | path = 'default.xml' |
| 221 | |
| 222 | commits = [] |
| 223 | old_timestamp = lookup_build_timestamp(self.config['flavor'], old) |
| 224 | new_timestamp = lookup_build_timestamp(self.config['flavor'], new) |
| 225 | for timestamp, git_rev in git_util.get_history(self.manifest_dir, path): |
| 226 | if timestamp < old_timestamp: |
| 227 | commits = [] |
| 228 | commits.append((timestamp, git_rev)) |
| 229 | if timestamp > new_timestamp: |
| 230 | break |
| 231 | |
| 232 | for timestamp, git_rev in commits: |
| 233 | result.append( |
| 234 | codechange.Spec(codechange.SPEC_FLOAT, git_rev, timestamp, path)) |
| 235 | return result |
| 236 | |
| 237 | def collect_fixed_spec(self, old, new): |
| 238 | result = [] |
Kuang-che Wu | 0a902f4 | 2019-01-21 18:58:32 +0800 | [diff] [blame] | 239 | revlist = get_build_ids_between(self.config['branch'], |
| 240 | self.config['flavor'], int(old), int(new)) |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 241 | for rev in revlist: |
| 242 | manifest_name = fetch_manifest(self.config['android_root'], |
| 243 | self.config['flavor'], rev) |
| 244 | path = os.path.join(self.manifest_dir, manifest_name) |
| 245 | timestamp = lookup_build_timestamp(self.config['flavor'], rev) |
| 246 | result.append( |
| 247 | codechange.Spec(codechange.SPEC_FIXED, rev, timestamp, path)) |
| 248 | return result |
| 249 | |
| 250 | def _load_manifest_content(self, spec): |
| 251 | if spec.spec_type == codechange.SPEC_FIXED: |
| 252 | manifest_name = fetch_manifest(self.config['branch'], |
| 253 | self.config['flavor'], spec.name) |
Kuang-che Wu | 89ac2e7 | 2018-07-25 17:39:07 +0800 | [diff] [blame] | 254 | content = open(os.path.join(self.manifest_dir, manifest_name)).read() |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 255 | else: |
Kuang-che Wu | 89ac2e7 | 2018-07-25 17:39:07 +0800 | [diff] [blame] | 256 | content = git_util.get_file_from_revision(self.manifest_dir, spec.name, |
| 257 | spec.path) |
| 258 | return content |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 259 | |
| 260 | def parse_spec(self, spec): |
| 261 | logging.debug('parse_spec %s', spec.name) |
| 262 | manifest_content = self._load_manifest_content(spec) |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 263 | parser = repo_util.ManifestParser(self.manifest_dir) |
| 264 | root = parser.parse_single_xml(manifest_content, allow_include=False) |
| 265 | spec.entries = parser.process_parsed_result(root) |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 266 | if spec.spec_type == codechange.SPEC_FIXED: |
| 267 | assert spec.is_static() |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 268 | |
| 269 | def sync_disk_state(self, rev): |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 270 | manifest_name = fetch_manifest(self.config['android_root'], |
| 271 | self.config['flavor'], rev) |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 272 | repo_util.sync( |
| 273 | self.config['android_root'], |
| 274 | manifest_name=manifest_name, |
| 275 | current_branch=True) |