Kuang-che Wu | 6e4beca | 2018-06-27 17:45:02 +0800 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 2 | # Copyright 2017 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 | """ChromeOS utility. |
| 6 | |
| 7 | Terminology used in this module. |
| 8 | short_version: ChromeOS version number without milestone, like "9876.0.0". |
| 9 | full_version: ChromeOS version number with milestone, like "R62-9876.0.0". |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 10 | snapshot_version: ChromeOS version number with milestone and snapshot id, |
| 11 | like "R62-9876.0.0-12345". |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 12 | version: if not specified, it could be in short or full format. |
| 13 | """ |
| 14 | |
| 15 | from __future__ import print_function |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 16 | import ast |
Kuang-che Wu | 72b5a57 | 2019-10-29 20:37:57 +0800 | [diff] [blame] | 17 | import calendar |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 18 | import datetime |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 19 | import errno |
| 20 | import json |
| 21 | import logging |
| 22 | import os |
| 23 | import re |
| 24 | import subprocess |
| 25 | import time |
| 26 | |
Zheng-Jie Chang | 4fabff6 | 2019-12-08 21:54:35 +0800 | [diff] [blame] | 27 | from google.protobuf import json_format |
| 28 | |
Zheng-Jie Chang | 2b6d147 | 2019-11-13 12:40:17 +0800 | [diff] [blame] | 29 | from bisect_kit import buildbucket_util |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 30 | from bisect_kit import cli |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 31 | from bisect_kit import codechange |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 32 | from bisect_kit import cr_util |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 33 | from bisect_kit import errors |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 34 | from bisect_kit import git_util |
Kuang-che Wu | fb55310 | 2018-10-02 18:14:29 +0800 | [diff] [blame] | 35 | from bisect_kit import locking |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 36 | from bisect_kit import repo_util |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 37 | from bisect_kit import util |
| 38 | |
| 39 | logger = logging.getLogger(__name__) |
| 40 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 41 | re_chromeos_full_version = r'^R\d+-\d+\.\d+\.\d+$' |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 42 | re_chromeos_localbuild_version = r'^\d+\.\d+\.\d{4}_\d\d_\d\d_\d{4}$' |
| 43 | re_chromeos_short_version = r'^\d+\.\d+\.\d+$' |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 44 | re_chromeos_snapshot_version = r'^R\d+-\d+\.\d+\.\d+-\d+$' |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 45 | |
| 46 | gs_archive_path = 'gs://chromeos-image-archive/{board}-release' |
| 47 | gs_release_path = ( |
Kuang-che Wu | 80bf6a5 | 2019-05-31 12:48:06 +0800 | [diff] [blame] | 48 | 'gs://chromeos-releases/{channel}-channel/{boardpath}/{short_version}') |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 49 | |
| 50 | # Assume gsutil is in PATH. |
| 51 | gsutil_bin = 'gsutil' |
Zheng-Jie Chang | b869704 | 2019-10-29 16:03:26 +0800 | [diff] [blame] | 52 | |
| 53 | # Since snapshots with version >= 12618.0.0 have android and chrome version |
| 54 | # info. |
| 55 | snapshot_cutover_version = '12618.0.0' |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 56 | |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 57 | chromeos_root_inside_chroot = '/mnt/host/source' |
| 58 | # relative to chromeos_root |
Kuang-che Wu | 7f82c6f | 2019-08-12 14:29:28 +0800 | [diff] [blame] | 59 | prebuilt_autotest_dir = 'tmp/autotest-prebuilt' |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 60 | # Relative to chromeos root. Images are cached_images_dir/$board/$image_name. |
| 61 | cached_images_dir = 'src/build/images' |
| 62 | test_image_filename = 'chromiumos_test_image.bin' |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 63 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 64 | VERSION_KEY_CROS_SHORT_VERSION = 'cros_short_version' |
| 65 | VERSION_KEY_CROS_FULL_VERSION = 'cros_full_version' |
| 66 | VERSION_KEY_MILESTONE = 'milestone' |
| 67 | VERSION_KEY_CR_VERSION = 'cr_version' |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 68 | VERSION_KEY_ANDROID_BUILD_ID = 'android_build_id' |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 69 | VERSION_KEY_ANDROID_BRANCH = 'android_branch' |
| 70 | |
| 71 | |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 72 | class NeedRecreateChrootException(Exception): |
| 73 | """Failed to build ChromeOS because of chroot mismatch or corruption""" |
| 74 | |
| 75 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 76 | def is_cros_short_version(s): |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 77 | """Determines if `s` is chromeos short version. |
| 78 | |
| 79 | This function doesn't accept version number of local build. |
| 80 | """ |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 81 | return bool(re.match(re_chromeos_short_version, s)) |
| 82 | |
| 83 | |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 84 | def is_cros_localbuild_version(s): |
| 85 | """Determines if `s` is chromeos local build version.""" |
| 86 | return bool(re.match(re_chromeos_localbuild_version, s)) |
| 87 | |
| 88 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 89 | def is_cros_full_version(s): |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 90 | """Determines if `s` is chromeos full version. |
| 91 | |
| 92 | This function doesn't accept version number of local build. |
| 93 | """ |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 94 | return bool(re.match(re_chromeos_full_version, s)) |
| 95 | |
| 96 | |
| 97 | def is_cros_version(s): |
| 98 | """Determines if `s` is chromeos version (either short or full)""" |
| 99 | return is_cros_short_version(s) or is_cros_full_version(s) |
| 100 | |
| 101 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 102 | def is_cros_snapshot_version(s): |
| 103 | """Determines if `s` is chromeos snapshot version""" |
| 104 | return bool(re.match(re_chromeos_snapshot_version, s)) |
| 105 | |
| 106 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 107 | def make_cros_full_version(milestone, short_version): |
| 108 | """Makes full_version from milestone and short_version""" |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 109 | assert milestone |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 110 | return 'R%s-%s' % (milestone, short_version) |
| 111 | |
| 112 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 113 | def make_cros_snapshot_version(milestone, short_version, snapshot_id): |
| 114 | """Makes snapshot version from milestone, short_version and snapshot id""" |
| 115 | return 'R%s-%s-%s' % (milestone, short_version, snapshot_id) |
| 116 | |
| 117 | |
| 118 | def version_split(version): |
| 119 | """Splits full_version or snapshot_version into milestone and short_version""" |
| 120 | assert is_cros_full_version(version) or is_cros_snapshot_version(version) |
| 121 | if is_cros_snapshot_version(version): |
| 122 | return snapshot_version_split(version)[0:2] |
| 123 | milestone, short_version = version.split('-') |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 124 | return milestone[1:], short_version |
| 125 | |
| 126 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 127 | def snapshot_version_split(snapshot_version): |
| 128 | """Splits snapshot_version into milestone, short_version and snapshot_id""" |
| 129 | assert is_cros_snapshot_version(snapshot_version) |
| 130 | milestone, shot_version, snapshot_id = snapshot_version.split('-') |
| 131 | return milestone[1:], shot_version, snapshot_id |
| 132 | |
| 133 | |
Zheng-Jie Chang | b869704 | 2019-10-29 16:03:26 +0800 | [diff] [blame] | 134 | def query_snapshot_buildbucket_id(board, snapshot_version): |
| 135 | """Query buildbucket id of a snapshot""" |
| 136 | assert is_cros_snapshot_version(snapshot_version) |
| 137 | path = ('gs://chromeos-image-archive/{board}-postsubmit' |
| 138 | '/{snapshot_version}-*/image.zip') |
| 139 | output = gsutil_ls( |
| 140 | '-d', |
| 141 | path.format(board=board, snapshot_version=snapshot_version), |
| 142 | ignore_errors=True) |
| 143 | for line in output: |
| 144 | m = re.match(r'.*-postsubmit/R\d+-\d+\.\d+\.\d+-\d+-(.+)/image\.zip', line) |
| 145 | if m: |
| 146 | return m.group(1) |
| 147 | return None |
| 148 | |
| 149 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 150 | def argtype_cros_version(s): |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 151 | if (not is_cros_version(s)) and (not is_cros_snapshot_version(s)): |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 152 | msg = 'invalid cros version' |
Kuang-che Wu | ce2f3be | 2019-10-28 19:44:54 +0800 | [diff] [blame] | 153 | raise cli.ArgTypeError(msg, '9876.0.0, R62-9876.0.0 or R77-12369.0.0-11681') |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 154 | return s |
| 155 | |
| 156 | |
| 157 | def query_dut_lsb_release(host): |
| 158 | """Query /etc/lsb-release of given DUT |
| 159 | |
| 160 | Args: |
| 161 | host: the DUT address |
| 162 | |
| 163 | Returns: |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 164 | dict for keys and values of /etc/lsb-release. |
| 165 | |
| 166 | Raises: |
Kuang-che Wu | 4427814 | 2019-03-04 11:33:57 +0800 | [diff] [blame] | 167 | errors.SshConnectionError: cannot connect to host |
| 168 | errors.ExternalError: lsb-release file doesn't exist |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 169 | """ |
| 170 | try: |
Kuang-che Wu | 4427814 | 2019-03-04 11:33:57 +0800 | [diff] [blame] | 171 | output = util.ssh_cmd(host, 'cat', '/etc/lsb-release') |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 172 | except subprocess.CalledProcessError: |
Kuang-che Wu | 4427814 | 2019-03-04 11:33:57 +0800 | [diff] [blame] | 173 | raise errors.ExternalError('unable to read /etc/lsb-release; not a DUT') |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 174 | return dict(re.findall(r'^(\w+)=(.*)$', output, re.M)) |
| 175 | |
| 176 | |
| 177 | def is_dut(host): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 178 | """Determines whether a host is a chromeos device. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 179 | |
| 180 | Args: |
| 181 | host: the DUT address |
| 182 | |
| 183 | Returns: |
| 184 | True if the host is a chromeos device. |
| 185 | """ |
Kuang-che Wu | 4427814 | 2019-03-04 11:33:57 +0800 | [diff] [blame] | 186 | try: |
| 187 | return query_dut_lsb_release(host).get('DEVICETYPE') in [ |
| 188 | 'CHROMEBASE', |
| 189 | 'CHROMEBIT', |
| 190 | 'CHROMEBOOK', |
| 191 | 'CHROMEBOX', |
| 192 | 'REFERENCE', |
| 193 | ] |
| 194 | except (errors.ExternalError, errors.SshConnectionError): |
| 195 | return False |
| 196 | |
| 197 | |
| 198 | def is_good_dut(host): |
| 199 | if not is_dut(host): |
| 200 | return False |
| 201 | |
| 202 | # Sometimes python is broken after 'cros flash'. |
| 203 | try: |
| 204 | util.ssh_cmd(host, 'python', '-c', '1') |
| 205 | return True |
| 206 | except (subprocess.CalledProcessError, errors.SshConnectionError): |
| 207 | return False |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 208 | |
| 209 | |
| 210 | def query_dut_board(host): |
| 211 | """Query board name of a given DUT""" |
| 212 | return query_dut_lsb_release(host).get('CHROMEOS_RELEASE_BOARD') |
| 213 | |
| 214 | |
| 215 | def query_dut_short_version(host): |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 216 | """Query short version of a given DUT. |
| 217 | |
| 218 | This function may return version of local build, which |
| 219 | is_cros_short_version() is false. |
| 220 | """ |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 221 | return query_dut_lsb_release(host).get('CHROMEOS_RELEASE_VERSION') |
| 222 | |
| 223 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 224 | def query_dut_is_snapshot(host): |
| 225 | """Query if given DUT is a snapshot version.""" |
| 226 | path = query_dut_lsb_release(host).get('CHROMEOS_RELEASE_BUILDER_PATH', '') |
| 227 | return '-postsubmit' in path |
| 228 | |
| 229 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 230 | def query_dut_boot_id(host, connect_timeout=None): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 231 | """Query boot id. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 232 | |
| 233 | Args: |
| 234 | host: DUT address |
| 235 | connect_timeout: connection timeout |
| 236 | |
| 237 | Returns: |
| 238 | boot uuid |
| 239 | """ |
Kuang-che Wu | 4427814 | 2019-03-04 11:33:57 +0800 | [diff] [blame] | 240 | return util.ssh_cmd( |
| 241 | host, |
| 242 | 'cat', |
| 243 | '/proc/sys/kernel/random/boot_id', |
| 244 | connect_timeout=connect_timeout).strip() |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 245 | |
| 246 | |
| 247 | def reboot(host): |
| 248 | """Reboot a DUT and verify""" |
| 249 | logger.debug('reboot %s', host) |
| 250 | boot_id = query_dut_boot_id(host) |
| 251 | |
Kuang-che Wu | 4427814 | 2019-03-04 11:33:57 +0800 | [diff] [blame] | 252 | try: |
| 253 | util.ssh_cmd(host, 'reboot') |
Kuang-che Wu | 5f662e8 | 2019-03-05 11:49:56 +0800 | [diff] [blame] | 254 | except errors.SshConnectionError: |
| 255 | # Depends on timing, ssh may return failure due to broken pipe, which is |
| 256 | # working as intended. Ignore such kind of errors. |
Kuang-che Wu | 4427814 | 2019-03-04 11:33:57 +0800 | [diff] [blame] | 257 | pass |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 258 | wait_reboot_done(host, boot_id) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 259 | |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 260 | |
| 261 | def wait_reboot_done(host, boot_id): |
Kuang-che Wu | 4fe945b | 2018-03-31 16:46:38 +0800 | [diff] [blame] | 262 | # For dev-mode test image, the reboot time is roughly at least 16 seconds |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 263 | # (dev screen short delay) or more (long delay). |
| 264 | time.sleep(15) |
| 265 | for _ in range(100): |
| 266 | try: |
| 267 | # During boot, DUT does not response and thus ssh may hang a while. So |
| 268 | # set a connect timeout. 3 seconds are enough and 2 are not. It's okay to |
| 269 | # set tight limit because it's inside retry loop. |
| 270 | assert boot_id != query_dut_boot_id(host, connect_timeout=3) |
| 271 | return |
Kuang-che Wu | 5f662e8 | 2019-03-05 11:49:56 +0800 | [diff] [blame] | 272 | except errors.SshConnectionError: |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 273 | logger.debug('reboot not ready? sleep wait 1 sec') |
| 274 | time.sleep(1) |
| 275 | |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 276 | raise errors.ExternalError('reboot failed?') |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 277 | |
| 278 | |
Kuang-che Wu | 80bf6a5 | 2019-05-31 12:48:06 +0800 | [diff] [blame] | 279 | def gs_release_boardpath(board): |
| 280 | """Normalizes board name for gs://chromeos-releases/ |
| 281 | |
| 282 | This follows behavior of PushImage() in chromite/scripts/pushimage.py |
| 283 | Note, only gs://chromeos-releases/ needs normalization, |
| 284 | gs://chromeos-image-archive does not. |
| 285 | |
| 286 | Args: |
| 287 | board: ChromeOS board name |
| 288 | |
| 289 | Returns: |
| 290 | normalized board name |
| 291 | """ |
| 292 | return board.replace('_', '-') |
| 293 | |
| 294 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 295 | def gsutil(*args, **kwargs): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 296 | """gsutil command line wrapper. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 297 | |
| 298 | Args: |
| 299 | args: command line arguments passed to gsutil |
| 300 | kwargs: |
| 301 | ignore_errors: if true, return '' for failures, for example 'gsutil ls' |
| 302 | but the path not found. |
| 303 | |
| 304 | Returns: |
| 305 | stdout of gsutil |
| 306 | |
| 307 | Raises: |
Zheng-Jie Chang | b869704 | 2019-10-29 16:03:26 +0800 | [diff] [blame] | 308 | errors.ExternalError: gsutil failed to run |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 309 | subprocess.CalledProcessError: command failed |
| 310 | """ |
| 311 | stderr_lines = [] |
| 312 | try: |
| 313 | return util.check_output( |
| 314 | gsutil_bin, *args, stderr_callback=stderr_lines.append) |
| 315 | except subprocess.CalledProcessError as e: |
| 316 | stderr = ''.join(stderr_lines) |
| 317 | if re.search(r'ServiceException:.* does not have .*access', stderr): |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 318 | raise errors.ExternalError( |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 319 | 'gsutil failed due to permission. ' + |
| 320 | 'Run "%s config" and follow its instruction. ' % gsutil_bin + |
| 321 | 'Fill any string if it asks for project-id') |
| 322 | if kwargs.get('ignore_errors'): |
| 323 | return '' |
| 324 | raise |
| 325 | except OSError as e: |
| 326 | if e.errno == errno.ENOENT: |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 327 | raise errors.ExternalError( |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 328 | 'Unable to run %s. gsutil is not installed or not in PATH?' % |
| 329 | gsutil_bin) |
| 330 | raise |
| 331 | |
| 332 | |
| 333 | def gsutil_ls(*args, **kwargs): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 334 | """gsutil ls. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 335 | |
| 336 | Args: |
| 337 | args: arguments passed to 'gsutil ls' |
| 338 | kwargs: extra parameters, where |
Kuang-che Wu | 4fe945b | 2018-03-31 16:46:38 +0800 | [diff] [blame] | 339 | ignore_errors: if true, return empty list instead of raising |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 340 | exception, ex. path not found. |
| 341 | |
| 342 | Returns: |
| 343 | list of 'gsutil ls' result. One element for one line of gsutil output. |
| 344 | |
| 345 | Raises: |
| 346 | subprocess.CalledProcessError: gsutil failed, usually means path not found |
| 347 | """ |
| 348 | return gsutil('ls', *args, **kwargs).splitlines() |
| 349 | |
| 350 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 351 | def gsutil_stat_update_time(*args, **kwargs): |
| 352 | """Returns the last modified time of a file or multiple files. |
| 353 | |
| 354 | Args: |
| 355 | args: arguments passed to 'gsutil stat'. |
| 356 | kwargs: extra parameters for gsutil. |
| 357 | |
| 358 | Returns: |
| 359 | A integer indicates the last modified timestamp. |
| 360 | |
| 361 | Raises: |
| 362 | subprocess.CalledProcessError: gsutil failed, usually means path not found |
| 363 | errors.ExternalError: update time is not found |
| 364 | """ |
| 365 | result = -1 |
| 366 | # Currently we believe stat always returns a UTC time, and strptime also |
| 367 | # parses a UTC time by default. |
| 368 | time_format = '%a, %d %b %Y %H:%M:%S GMT' |
| 369 | |
| 370 | for line in gsutil('stat', *args, **kwargs).splitlines(): |
| 371 | if ':' not in line: |
| 372 | continue |
Kuang-che Wu | c89f2a2 | 2019-11-26 15:30:50 +0800 | [diff] [blame] | 373 | key, value = line.split(':', 1) |
| 374 | key, value = key.strip(), value.strip() |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 375 | if key != 'Update time': |
| 376 | continue |
| 377 | dt = datetime.datetime.strptime(value, time_format) |
Kuang-che Wu | 72b5a57 | 2019-10-29 20:37:57 +0800 | [diff] [blame] | 378 | unixtime = int(calendar.timegm(dt.utctimetuple())) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 379 | result = max(result, unixtime) |
| 380 | |
| 381 | if result == -1: |
| 382 | raise errors.ExternalError("didn't find update time") |
| 383 | return result |
| 384 | |
| 385 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 386 | def query_milestone_by_version(board, short_version): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 387 | """Query milestone by ChromeOS version number. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 388 | |
| 389 | Args: |
| 390 | board: ChromeOS board name |
| 391 | short_version: ChromeOS version number in short format, ex. 9300.0.0 |
| 392 | |
| 393 | Returns: |
| 394 | ChromeOS milestone number (string). For example, '58' for '9300.0.0'. |
| 395 | None if failed. |
| 396 | """ |
| 397 | path = gs_archive_path.format(board=board) + '/R*-' + short_version |
| 398 | for line in gsutil_ls('-d', path, ignore_errors=True): |
| 399 | m = re.search(r'/R(\d+)-', line) |
| 400 | if not m: |
| 401 | continue |
| 402 | return m.group(1) |
| 403 | |
| 404 | for channel in ['canary', 'dev', 'beta', 'stable']: |
| 405 | path = gs_release_path.format( |
Kuang-che Wu | 80bf6a5 | 2019-05-31 12:48:06 +0800 | [diff] [blame] | 406 | channel=channel, |
| 407 | boardpath=gs_release_boardpath(board), |
| 408 | short_version=short_version) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 409 | for line in gsutil_ls(path, ignore_errors=True): |
| 410 | m = re.search(r'\bR(\d+)-' + short_version, line) |
| 411 | if not m: |
| 412 | continue |
| 413 | return m.group(1) |
| 414 | |
Zheng-Jie Chang | 4e25a8d | 2020-01-08 16:00:13 +0800 | [diff] [blame] | 415 | logger.debug('unable to query milestone of %s for %s', short_version, board) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 416 | return None |
| 417 | |
| 418 | |
Kuang-che Wu | 80bf6a5 | 2019-05-31 12:48:06 +0800 | [diff] [blame] | 419 | def list_board_names(chromeos_root): |
| 420 | """List board names. |
| 421 | |
| 422 | Args: |
| 423 | chromeos_root: chromeos tree root |
| 424 | |
| 425 | Returns: |
| 426 | list of board names |
| 427 | """ |
| 428 | # Following logic is simplified from chromite/lib/portage_util.py |
| 429 | cros_list_overlays = os.path.join(chromeos_root, |
| 430 | 'chromite/bin/cros_list_overlays') |
| 431 | overlays = util.check_output(cros_list_overlays).splitlines() |
| 432 | result = set() |
| 433 | for overlay in overlays: |
| 434 | conf_file = os.path.join(overlay, 'metadata', 'layout.conf') |
| 435 | name = None |
| 436 | if os.path.exists(conf_file): |
| 437 | for line in open(conf_file): |
| 438 | m = re.match(r'^repo-name\s*=\s*(\S+)\s*$', line) |
| 439 | if m: |
| 440 | name = m.group(1) |
| 441 | break |
| 442 | |
| 443 | if not name: |
| 444 | name_file = os.path.join(overlay, 'profiles', 'repo_name') |
| 445 | if os.path.exists(name_file): |
Kuang-che Wu | a572349 | 2019-11-25 20:59:34 +0800 | [diff] [blame] | 446 | with open(name_file) as f: |
| 447 | name = f.read().strip() |
Kuang-che Wu | 80bf6a5 | 2019-05-31 12:48:06 +0800 | [diff] [blame] | 448 | |
| 449 | if name: |
| 450 | name = re.sub(r'-private$', '', name) |
| 451 | result.add(name) |
| 452 | |
| 453 | return list(result) |
| 454 | |
| 455 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 456 | def recognize_version(board, version): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 457 | """Recognize ChromeOS version. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 458 | |
| 459 | Args: |
| 460 | board: ChromeOS board name |
| 461 | version: ChromeOS version number in short or full format |
| 462 | |
| 463 | Returns: |
| 464 | (milestone, version in short format) |
| 465 | """ |
| 466 | if is_cros_short_version(version): |
| 467 | milestone = query_milestone_by_version(board, version) |
| 468 | short_version = version |
| 469 | else: |
| 470 | milestone, short_version = version_split(version) |
| 471 | return milestone, short_version |
| 472 | |
| 473 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 474 | def extract_major_version(version): |
| 475 | """Converts a version to its major version. |
| 476 | |
| 477 | Args: |
Kuang-che Wu | 9501f34 | 2019-11-15 17:15:21 +0800 | [diff] [blame] | 478 | version: ChromeOS version number or snapshot version |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 479 | |
| 480 | Returns: |
| 481 | major version number in string format |
| 482 | """ |
| 483 | version = version_to_short(version) |
| 484 | m = re.match(r'^(\d+)\.\d+\.\d+$', version) |
| 485 | return m.group(1) |
| 486 | |
| 487 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 488 | def version_to_short(version): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 489 | """Convert ChromeOS version number to short format. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 490 | |
| 491 | Args: |
| 492 | version: ChromeOS version number in short or full format |
| 493 | |
| 494 | Returns: |
| 495 | version number in short format |
| 496 | """ |
| 497 | if is_cros_short_version(version): |
| 498 | return version |
| 499 | _, short_version = version_split(version) |
| 500 | return short_version |
| 501 | |
| 502 | |
| 503 | def version_to_full(board, version): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 504 | """Convert ChromeOS version number to full format. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 505 | |
| 506 | Args: |
| 507 | board: ChromeOS board name |
| 508 | version: ChromeOS version number in short or full format |
| 509 | |
| 510 | Returns: |
| 511 | version number in full format |
| 512 | """ |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 513 | if is_cros_snapshot_version(version): |
| 514 | milestone, short_version, _ = snapshot_version_split(version) |
| 515 | return make_cros_full_version(milestone, short_version) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 516 | if is_cros_full_version(version): |
| 517 | return version |
| 518 | milestone = query_milestone_by_version(board, version) |
Kuang-che Wu | 0205f05 | 2019-05-23 12:48:37 +0800 | [diff] [blame] | 519 | assert milestone, 'incorrect board=%s or version=%s ?' % (board, version) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 520 | return make_cros_full_version(milestone, version) |
| 521 | |
| 522 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 523 | def list_snapshots_from_image_archive(board, major_version): |
Kuang-che Wu | 9501f34 | 2019-11-15 17:15:21 +0800 | [diff] [blame] | 524 | """List ChromeOS snapshot image available from gs://chromeos-image-archive. |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 525 | |
| 526 | Args: |
| 527 | board: ChromeOS board |
| 528 | major_version: ChromeOS major version |
| 529 | |
| 530 | Returns: |
| 531 | list of (version, gs_path): |
| 532 | version: Chrome OS snapshot version |
| 533 | gs_path: gs path of test image |
| 534 | """ |
| 535 | |
Zheng-Jie Chang | 43a0841 | 2019-12-05 17:05:45 +0800 | [diff] [blame] | 536 | def extract_snapshot_id(result): |
| 537 | m = re.match(r'^R\d+-\d+\.\d+\.\d+-(\d+)', result[0]) |
| 538 | assert m |
| 539 | return int(m.group(1)) |
| 540 | |
Zheng-Jie Chang | a331b87 | 2020-01-06 17:09:32 +0800 | [diff] [blame] | 541 | short_version = '%s.0.0' % major_version |
| 542 | milestone = query_milestone_by_version(board, short_version) |
| 543 | if not milestone: |
| 544 | milestone = '*' |
| 545 | |
| 546 | path = ('gs://chromeos-image-archive/{board}-postsubmit/R{milestone}-' |
| 547 | '{short_version}-*/image.zip') |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 548 | result = [] |
| 549 | output = gsutil_ls( |
Zheng-Jie Chang | a331b87 | 2020-01-06 17:09:32 +0800 | [diff] [blame] | 550 | path.format( |
| 551 | board=board, milestone=milestone, short_version=short_version), |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 552 | ignore_errors=True) |
| 553 | |
Zheng-Jie Chang | a331b87 | 2020-01-06 17:09:32 +0800 | [diff] [blame] | 554 | for gs_path in sorted(output): |
| 555 | m = re.match(r'^gs:\S+(R\d+-\d+\.\d+\.\d+-\d+)', gs_path) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 556 | if m: |
| 557 | snapshot_version = m.group(1) |
Zheng-Jie Chang | 43a0841 | 2019-12-05 17:05:45 +0800 | [diff] [blame] | 558 | # we should skip if there is duplicate snapshot |
| 559 | if result and result[-1][0] == snapshot_version: |
| 560 | continue |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 561 | result.append((snapshot_version, gs_path)) |
Zheng-Jie Chang | 43a0841 | 2019-12-05 17:05:45 +0800 | [diff] [blame] | 562 | |
| 563 | # sort by its snapshot_id |
| 564 | result.sort(key=extract_snapshot_id) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 565 | return result |
| 566 | |
| 567 | |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 568 | def list_prebuilt_from_image_archive(board): |
| 569 | """Lists ChromeOS prebuilt image available from gs://chromeos-image-archive. |
| 570 | |
| 571 | gs://chromeos-image-archive contains only recent builds (in two years). |
| 572 | We prefer this function to list_prebuilt_from_chromeos_releases() because |
| 573 | - this is what "cros flash" supports directly. |
| 574 | - the paths have milestone information, so we don't need to do slow query |
| 575 | by ourselves. |
| 576 | |
| 577 | Args: |
| 578 | board: ChromeOS board name |
| 579 | |
| 580 | Returns: |
| 581 | list of (version, gs_path): |
| 582 | version: Chrome OS version in full format |
| 583 | gs_path: gs path of test image |
| 584 | """ |
| 585 | result = [] |
| 586 | for line in gsutil_ls(gs_archive_path.format(board=board)): |
| 587 | m = re.match(r'^gs:\S+(R\d+-\d+\.\d+\.\d+)', line) |
| 588 | if m: |
| 589 | full_version = m.group(1) |
| 590 | test_image = 'chromiumos_test_image.tar.xz' |
| 591 | assert line.endswith('/') |
| 592 | gs_path = line + test_image |
| 593 | result.append((full_version, gs_path)) |
| 594 | return result |
| 595 | |
| 596 | |
| 597 | def list_prebuilt_from_chromeos_releases(board): |
| 598 | """Lists ChromeOS versions available from gs://chromeos-releases. |
| 599 | |
| 600 | gs://chromeos-releases contains more builds. However, 'cros flash' doesn't |
| 601 | support it. |
| 602 | |
| 603 | Args: |
| 604 | board: ChromeOS board name |
| 605 | |
| 606 | Returns: |
| 607 | list of (version, gs_path): |
| 608 | version: Chrome OS version in short format |
| 609 | gs_path: gs path of test image (with wildcard) |
| 610 | """ |
| 611 | result = [] |
| 612 | for line in gsutil_ls( |
Kuang-che Wu | 80bf6a5 | 2019-05-31 12:48:06 +0800 | [diff] [blame] | 613 | gs_release_path.format( |
| 614 | channel='*', boardpath=gs_release_boardpath(board), short_version=''), |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 615 | ignore_errors=True): |
| 616 | m = re.match(r'gs:\S+/(\d+\.\d+\.\d+)/$', line) |
| 617 | if m: |
| 618 | short_version = m.group(1) |
| 619 | test_image = 'ChromeOS-test-R*-{short_version}-{board}.tar.xz'.format( |
| 620 | short_version=short_version, board=board) |
| 621 | gs_path = line + test_image |
| 622 | result.append((short_version, gs_path)) |
| 623 | return result |
| 624 | |
| 625 | |
Kuang-che Wu | e180840 | 2020-01-06 20:27:45 +0800 | [diff] [blame] | 626 | def has_test_image(board, version): |
| 627 | if is_cros_snapshot_version(version): |
| 628 | return bool(query_snapshot_buildbucket_id(board, version)) |
| 629 | |
| 630 | full_version = version_to_full(board, version) |
| 631 | short_version = version_to_short(version) |
| 632 | paths = [ |
| 633 | gs_archive_path.format(board=board) + |
| 634 | '/%s/chromiumos_test_image.tar.xz' % full_version, |
| 635 | gs_release_path.format( |
| 636 | channel='*', |
| 637 | boardpath=gs_release_boardpath(board), |
| 638 | short_version=short_version), |
| 639 | ] |
| 640 | |
| 641 | for path in paths: |
| 642 | if gsutil_ls(path, ignore_errors=True): |
| 643 | return True |
| 644 | return False |
| 645 | |
| 646 | |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 647 | def list_chromeos_prebuilt_versions(board, |
| 648 | old, |
| 649 | new, |
| 650 | only_good_build=True, |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 651 | include_older_build=True, |
| 652 | use_snapshot=False): |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 653 | """Lists ChromeOS version numbers with prebuilt between given range |
| 654 | |
| 655 | Args: |
| 656 | board: ChromeOS board name |
| 657 | old: start version (inclusive) |
| 658 | new: end version (inclusive) |
| 659 | only_good_build: only if test image is available |
| 660 | include_older_build: include prebuilt in gs://chromeos-releases |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 661 | use_snapshot: return snapshot versions if found |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 662 | |
| 663 | Returns: |
| 664 | list of sorted version numbers (in full format) between [old, new] range |
| 665 | (inclusive). |
| 666 | """ |
| 667 | old = version_to_short(old) |
| 668 | new = version_to_short(new) |
| 669 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 670 | rev_map = { |
| 671 | } # dict: short version -> list of (short/full or snapshot version, gs path) |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 672 | for full_version, gs_path in list_prebuilt_from_image_archive(board): |
| 673 | short_version = version_to_short(full_version) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 674 | rev_map[short_version] = [(full_version, gs_path)] |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 675 | |
| 676 | if include_older_build and old not in rev_map: |
| 677 | for short_version, gs_path in list_prebuilt_from_chromeos_releases(board): |
| 678 | if short_version not in rev_map: |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 679 | rev_map[short_version] = [(short_version, gs_path)] |
| 680 | |
| 681 | if use_snapshot: |
| 682 | for major_version in range( |
| 683 | int(extract_major_version(old)), |
| 684 | int(extract_major_version(new)) + 1): |
| 685 | short_version = '%s.0.0' % major_version |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 686 | next_short_version = '%s.0.0' % (major_version + 1) |
Zheng-Jie Chang | b869704 | 2019-10-29 16:03:26 +0800 | [diff] [blame] | 687 | # If current version is smaller than cutover, ignore it as it might not |
| 688 | # contain enough information for continuing android and chrome bisection. |
| 689 | if not util.is_version_lesseq(snapshot_cutover_version, short_version): |
| 690 | continue |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 691 | |
| 692 | # Given the fact that snapshots are images between two release versions. |
| 693 | # Adding snapshots of 12345.0.0 should be treated as adding commits |
| 694 | # between [12345.0.0, 12346.0.0). |
| 695 | # So in the following lines we check two facts: |
| 696 | # 1) If 12346.0.0(next_short_version) is a version between old and new |
| 697 | if not util.is_direct_relative_version(next_short_version, old): |
| 698 | continue |
| 699 | if not util.is_direct_relative_version(next_short_version, new): |
| 700 | continue |
| 701 | # 2) If 12345.0.0(short_version) is a version between old and new |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 702 | if not util.is_direct_relative_version(short_version, old): |
| 703 | continue |
| 704 | if not util.is_direct_relative_version(short_version, new): |
| 705 | continue |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 706 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 707 | snapshots = list_snapshots_from_image_archive(board, str(major_version)) |
| 708 | if snapshots: |
| 709 | # if snapshots found, we can append them after the release version, |
| 710 | # so the prebuilt image list of this version will be |
| 711 | # release_image, snapshot1, snapshot2,... |
Zheng-Jie Chang | b869704 | 2019-10-29 16:03:26 +0800 | [diff] [blame] | 712 | if short_version not in rev_map: |
| 713 | rev_map[short_version] = [] |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 714 | rev_map[short_version] += snapshots |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 715 | |
| 716 | result = [] |
| 717 | for rev in sorted(rev_map, key=util.version_key_func): |
| 718 | if not util.is_direct_relative_version(new, rev): |
| 719 | continue |
| 720 | if not util.is_version_lesseq(old, rev): |
| 721 | continue |
| 722 | if not util.is_version_lesseq(rev, new): |
| 723 | continue |
| 724 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 725 | for version, gs_path in rev_map[rev]: |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 726 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 727 | # version_to_full() and gsutil_ls() may take long time if versions are a |
| 728 | # lot. This is acceptable because we usually bisect only short range. |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 729 | |
Zheng-Jie Chang | a331b87 | 2020-01-06 17:09:32 +0800 | [diff] [blame] | 730 | if only_good_build and not is_cros_snapshot_version(version): |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 731 | gs_result = gsutil_ls(gs_path, ignore_errors=True) |
| 732 | if not gs_result: |
| 733 | logger.warning('%s is not a good build, ignore', version) |
| 734 | continue |
| 735 | assert len(gs_result) == 1 |
| 736 | m = re.search(r'(R\d+-\d+\.\d+\.\d+)', gs_result[0]) |
| 737 | if not m: |
| 738 | logger.warning('format of image path is unexpected: %s', gs_result[0]) |
| 739 | continue |
Zheng-Jie Chang | a331b87 | 2020-01-06 17:09:32 +0800 | [diff] [blame] | 740 | version = m.group(1) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 741 | elif is_cros_short_version(version): |
| 742 | version = version_to_full(board, version) |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 743 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 744 | result.append(version) |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 745 | |
| 746 | return result |
| 747 | |
| 748 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 749 | def prepare_snapshot_image(chromeos_root, board, snapshot_version): |
| 750 | """Prepare chromeos snapshot image. |
| 751 | |
| 752 | Args: |
| 753 | chromeos_root: chromeos tree root |
| 754 | board: ChromeOS board name |
| 755 | snapshot_version: ChromeOS snapshot version number |
| 756 | |
| 757 | Returns: |
| 758 | local file path of test image relative to chromeos_root |
| 759 | """ |
| 760 | assert is_cros_snapshot_version(snapshot_version) |
| 761 | milestone, short_version, snapshot_id = snapshot_version_split( |
| 762 | snapshot_version) |
| 763 | full_version = make_cros_full_version(milestone, short_version) |
| 764 | tmp_dir = os.path.join( |
| 765 | chromeos_root, 'tmp', |
| 766 | 'ChromeOS-test-%s-%s-%s' % (full_version, board, snapshot_id)) |
| 767 | if not os.path.exists(tmp_dir): |
| 768 | os.makedirs(tmp_dir) |
| 769 | |
| 770 | gs_path = ('gs://chromeos-image-archive/{board}-postsubmit/' + |
| 771 | '{snapshot_version}-*/image.zip') |
| 772 | gs_path = gs_path.format(board=board, snapshot_version=snapshot_version) |
| 773 | |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 774 | full_path = os.path.join(tmp_dir, test_image_filename) |
| 775 | rel_path = os.path.relpath(full_path, chromeos_root) |
| 776 | if os.path.exists(full_path): |
| 777 | return rel_path |
| 778 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 779 | files = gsutil_ls(gs_path, ignore_errors=True) |
Zheng-Jie Chang | eb7308f | 2019-12-05 14:25:05 +0800 | [diff] [blame] | 780 | if len(files) >= 1: |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 781 | gs_path = files[0] |
| 782 | gsutil('cp', gs_path, tmp_dir) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 783 | util.check_call( |
| 784 | 'unzip', '-j', 'image.zip', test_image_filename, cwd=tmp_dir) |
| 785 | os.remove(os.path.join(tmp_dir, 'image.zip')) |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 786 | return rel_path |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 787 | |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 788 | assert False |
Kuang-che Wu | a7ddf9b | 2019-11-25 18:59:57 +0800 | [diff] [blame] | 789 | return None |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 790 | |
| 791 | |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 792 | def prepare_prebuilt_image(chromeos_root, board, version): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 793 | """Prepare chromeos prebuilt image. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 794 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 795 | It searches for xbuddy image which "cros flash" can use, or fetch image to |
| 796 | local disk. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 797 | |
| 798 | Args: |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 799 | chromeos_root: chromeos tree root |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 800 | board: ChromeOS board name |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 801 | version: ChromeOS version number in short or full format |
| 802 | |
| 803 | Returns: |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 804 | xbuddy path or file path (relative to chromeos_root) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 805 | """ |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 806 | assert is_cros_version(version) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 807 | full_version = version_to_full(board, version) |
| 808 | short_version = version_to_short(full_version) |
| 809 | |
| 810 | image_path = None |
| 811 | gs_path = gs_archive_path.format(board=board) + '/' + full_version |
| 812 | if gsutil_ls('-d', gs_path, ignore_errors=True): |
| 813 | image_path = 'xbuddy://remote/{board}/{full_version}/test'.format( |
| 814 | board=board, full_version=full_version) |
| 815 | else: |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 816 | tmp_dir = os.path.join(chromeos_root, 'tmp', |
| 817 | 'ChromeOS-test-%s-%s' % (full_version, board)) |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 818 | full_path = os.path.join(tmp_dir, test_image_filename) |
| 819 | rel_path = os.path.relpath(full_path, chromeos_root) |
| 820 | if os.path.exists(full_path): |
| 821 | return rel_path |
| 822 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 823 | if not os.path.exists(tmp_dir): |
| 824 | os.makedirs(tmp_dir) |
| 825 | # gs://chromeos-releases may have more old images than |
Kuang-che Wu | 4fe945b | 2018-03-31 16:46:38 +0800 | [diff] [blame] | 826 | # gs://chromeos-image-archive, but 'cros flash' doesn't support it. We have |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 827 | # to fetch the image by ourselves |
| 828 | for channel in ['canary', 'dev', 'beta', 'stable']: |
| 829 | fn = 'ChromeOS-test-{full_version}-{board}.tar.xz'.format( |
| 830 | full_version=full_version, board=board) |
| 831 | gs_path = gs_release_path.format( |
Kuang-che Wu | 80bf6a5 | 2019-05-31 12:48:06 +0800 | [diff] [blame] | 832 | channel=channel, |
| 833 | boardpath=gs_release_boardpath(board), |
| 834 | short_version=short_version) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 835 | gs_path += '/' + fn |
| 836 | if gsutil_ls(gs_path, ignore_errors=True): |
| 837 | # TODO(kcwu): delete tmp |
| 838 | gsutil('cp', gs_path, tmp_dir) |
| 839 | util.check_call('tar', 'Jxvf', fn, cwd=tmp_dir) |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 840 | image_path = os.path.relpath(full_path, chromeos_root) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 841 | break |
| 842 | |
| 843 | assert image_path |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 844 | return image_path |
| 845 | |
| 846 | |
| 847 | def cros_flash(chromeos_root, |
| 848 | host, |
| 849 | board, |
| 850 | image_path, |
| 851 | version=None, |
| 852 | clobber_stateful=False, |
Kuang-che Wu | 155fb6e | 2018-11-29 16:00:41 +0800 | [diff] [blame] | 853 | disable_rootfs_verification=True): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 854 | """Flash a DUT with given ChromeOS image. |
| 855 | |
| 856 | This is implemented by 'cros flash' command line. |
| 857 | |
| 858 | Args: |
| 859 | chromeos_root: use 'cros flash' of which chromeos tree |
| 860 | host: DUT address |
| 861 | board: ChromeOS board name |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 862 | image_path: chromeos image xbuddy path or file path. For relative |
| 863 | path, it should be relative to chromeos_root. |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 864 | version: ChromeOS version in short or full format |
| 865 | clobber_stateful: Clobber stateful partition when performing update |
| 866 | disable_rootfs_verification: Disable rootfs verification after update |
| 867 | is completed |
Kuang-che Wu | 414d67f | 2019-05-28 11:28:57 +0800 | [diff] [blame] | 868 | |
| 869 | Raises: |
| 870 | errors.ExternalError: cros flash failed |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 871 | """ |
| 872 | logger.info('cros_flash %s %s %s %s', host, board, version, image_path) |
| 873 | |
| 874 | # Reboot is necessary because sometimes previous 'cros flash' failed and |
| 875 | # entered a bad state. |
| 876 | reboot(host) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 877 | |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 878 | # Handle relative path. |
| 879 | if '://' not in image_path and not os.path.isabs(image_path): |
| 880 | assert os.path.exists(os.path.join(chromeos_root, image_path)) |
| 881 | image_path = os.path.join(chromeos_root_inside_chroot, image_path) |
| 882 | |
Kuang-che Wu | f3d03ca | 2019-03-11 17:31:40 +0800 | [diff] [blame] | 883 | args = [ |
| 884 | '--debug', '--no-ping', '--send-payload-in-parallel', host, image_path |
| 885 | ] |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 886 | if clobber_stateful: |
| 887 | args.append('--clobber-stateful') |
| 888 | if disable_rootfs_verification: |
| 889 | args.append('--disable-rootfs-verification') |
| 890 | |
Kuang-che Wu | 414d67f | 2019-05-28 11:28:57 +0800 | [diff] [blame] | 891 | try: |
| 892 | cros_sdk(chromeos_root, 'cros', 'flash', *args) |
| 893 | except subprocess.CalledProcessError: |
| 894 | raise errors.ExternalError('cros flash failed') |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 895 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 896 | if version: |
| 897 | # In the past, cros flash may fail with returncode=0 |
| 898 | # So let's have an extra check. |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 899 | if is_cros_snapshot_version(version): |
| 900 | builder_path = query_dut_lsb_release(host).get( |
| 901 | 'CHROMEOS_RELEASE_BUILDER_PATH', '') |
| 902 | expect_prefix = '%s-postsubmit/%s-' % (board, version) |
| 903 | if not builder_path.startswith(expect_prefix): |
| 904 | raise errors.ExternalError( |
| 905 | 'although cros flash succeeded, the OS builder path is ' |
| 906 | 'unexpected: actual=%s expect=%s' % (builder_path, expect_prefix)) |
| 907 | else: |
| 908 | expect_version = version_to_short(version) |
| 909 | dut_version = query_dut_short_version(host) |
| 910 | if dut_version != expect_version: |
| 911 | raise errors.ExternalError( |
| 912 | 'although cros flash succeeded, the OS version is unexpected: ' |
| 913 | 'actual=%s expect=%s' % (dut_version, expect_version)) |
Kuang-che Wu | 414d67f | 2019-05-28 11:28:57 +0800 | [diff] [blame] | 914 | |
Kuang-che Wu | 4a81ea7 | 2019-10-05 15:35:17 +0800 | [diff] [blame] | 915 | # "cros flash" may terminate successfully but the DUT starts self-repairing |
Kuang-che Wu | 414d67f | 2019-05-28 11:28:57 +0800 | [diff] [blame] | 916 | # (b/130786578), so it's necessary to do sanity check. |
| 917 | if not is_good_dut(host): |
| 918 | raise errors.ExternalError( |
| 919 | 'although cros flash succeeded, the DUT is in bad state') |
| 920 | |
| 921 | |
| 922 | def cros_flash_with_retry(chromeos_root, |
| 923 | host, |
| 924 | board, |
| 925 | image_path, |
| 926 | version=None, |
| 927 | clobber_stateful=False, |
| 928 | disable_rootfs_verification=True, |
| 929 | repair_callback=None): |
| 930 | # 'cros flash' is not 100% reliable, retry if necessary. |
| 931 | for attempt in range(2): |
| 932 | if attempt > 0: |
| 933 | logger.info('will retry 60 seconds later') |
| 934 | time.sleep(60) |
| 935 | |
| 936 | try: |
| 937 | cros_flash( |
| 938 | chromeos_root, |
| 939 | host, |
| 940 | board, |
| 941 | image_path, |
| 942 | version=version, |
| 943 | clobber_stateful=clobber_stateful, |
| 944 | disable_rootfs_verification=disable_rootfs_verification) |
| 945 | return True |
| 946 | except errors.ExternalError: |
| 947 | logger.exception('cros flash failed') |
| 948 | if repair_callback and not repair_callback(host): |
| 949 | logger.warning('not repaired, assume it is harmless') |
| 950 | continue |
| 951 | return False |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 952 | |
| 953 | |
| 954 | def version_info(board, version): |
| 955 | """Query subcomponents version info of given version of ChromeOS |
| 956 | |
| 957 | Args: |
| 958 | board: ChromeOS board name |
| 959 | version: ChromeOS version number in short or full format |
| 960 | |
| 961 | Returns: |
| 962 | dict of component and version info, including (if available): |
| 963 | cros_short_version: ChromeOS version |
| 964 | cros_full_version: ChromeOS version |
| 965 | milestone: milestone of ChromeOS |
| 966 | cr_version: Chrome version |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 967 | android_build_id: Android build id |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 968 | android_branch: Android branch, in format like 'git_nyc-mr1-arc' |
| 969 | """ |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 970 | if is_cros_snapshot_version(version): |
Zheng-Jie Chang | 2b6d147 | 2019-11-13 12:40:17 +0800 | [diff] [blame] | 971 | api = buildbucket_util.BuildbucketApi() |
Zheng-Jie Chang | b869704 | 2019-10-29 16:03:26 +0800 | [diff] [blame] | 972 | milestone, short_version, _ = snapshot_version_split(version) |
| 973 | buildbucket_id = query_snapshot_buildbucket_id(board, version) |
Zheng-Jie Chang | 2b6d147 | 2019-11-13 12:40:17 +0800 | [diff] [blame] | 974 | data = api.get(int(buildbucket_id)).output.properties |
Zheng-Jie Chang | 4fabff6 | 2019-12-08 21:54:35 +0800 | [diff] [blame] | 975 | target_versions = json_format.MessageToDict(data['target_versions']) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 976 | return { |
Zheng-Jie Chang | b869704 | 2019-10-29 16:03:26 +0800 | [diff] [blame] | 977 | VERSION_KEY_MILESTONE: milestone, |
| 978 | VERSION_KEY_CROS_FULL_VERSION: version, |
| 979 | VERSION_KEY_CROS_SHORT_VERSION: short_version, |
Zheng-Jie Chang | 2d1dd9b | 2019-12-07 23:30:20 +0800 | [diff] [blame] | 980 | VERSION_KEY_CR_VERSION: target_versions.get('chromeVersion'), |
| 981 | VERSION_KEY_ANDROID_BUILD_ID: target_versions.get('androidVersion'), |
| 982 | VERSION_KEY_ANDROID_BRANCH: target_versions.get('androidBranchVersion'), |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 983 | } |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 984 | info = {} |
| 985 | full_version = version_to_full(board, version) |
| 986 | |
| 987 | # Some boards may have only partial-metadata.json but no metadata.json. |
| 988 | # e.g. caroline R60-9462.0.0 |
| 989 | # Let's try both. |
| 990 | metadata = None |
| 991 | for metadata_filename in ['metadata.json', 'partial-metadata.json']: |
Kuang-che Wu | 0768b97 | 2019-10-05 15:18:59 +0800 | [diff] [blame] | 992 | path = gs_archive_path.format( |
| 993 | board=board) + '/%s/%s' % (full_version, metadata_filename) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 994 | metadata = gsutil('cat', path, ignore_errors=True) |
| 995 | if metadata: |
| 996 | o = json.loads(metadata) |
| 997 | v = o['version'] |
| 998 | board_metadata = o['board-metadata'][board] |
| 999 | info.update({ |
| 1000 | VERSION_KEY_CROS_SHORT_VERSION: v['platform'], |
| 1001 | VERSION_KEY_CROS_FULL_VERSION: v['full'], |
| 1002 | VERSION_KEY_MILESTONE: v['milestone'], |
| 1003 | VERSION_KEY_CR_VERSION: v['chrome'], |
| 1004 | }) |
| 1005 | |
| 1006 | if 'android' in v: |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 1007 | info[VERSION_KEY_ANDROID_BUILD_ID] = v['android'] |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 1008 | if 'android-branch' in v: # this appears since R58-9317.0.0 |
| 1009 | info[VERSION_KEY_ANDROID_BRANCH] = v['android-branch'] |
| 1010 | elif 'android-container-branch' in board_metadata: |
| 1011 | info[VERSION_KEY_ANDROID_BRANCH] = v['android-container-branch'] |
| 1012 | break |
| 1013 | else: |
| 1014 | logger.error('Failed to read metadata from gs://chromeos-image-archive') |
| 1015 | logger.error( |
| 1016 | 'Note, so far no quick way to look up version info for too old builds') |
| 1017 | |
| 1018 | return info |
Kuang-che Wu | 848b1af | 2018-02-01 20:59:36 +0800 | [diff] [blame] | 1019 | |
| 1020 | |
| 1021 | def query_chrome_version(board, version): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1022 | """Queries chrome version of chromeos build. |
Kuang-che Wu | 848b1af | 2018-02-01 20:59:36 +0800 | [diff] [blame] | 1023 | |
| 1024 | Args: |
| 1025 | board: ChromeOS board name |
| 1026 | version: ChromeOS version number in short or full format |
| 1027 | |
| 1028 | Returns: |
| 1029 | Chrome version number |
| 1030 | """ |
| 1031 | info = version_info(board, version) |
| 1032 | return info['cr_version'] |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 1033 | |
| 1034 | |
| 1035 | def query_android_build_id(board, rev): |
| 1036 | info = version_info(board, rev) |
| 1037 | rev = info['android_build_id'] |
| 1038 | return rev |
| 1039 | |
| 1040 | |
| 1041 | def query_android_branch(board, rev): |
| 1042 | info = version_info(board, rev) |
| 1043 | rev = info['android_branch'] |
| 1044 | return rev |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1045 | |
| 1046 | |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 1047 | def guess_chrome_version(board, rev): |
| 1048 | """Guess chrome version number. |
| 1049 | |
| 1050 | Args: |
| 1051 | board: chromeos board name |
| 1052 | rev: chrome or chromeos version |
| 1053 | |
| 1054 | Returns: |
| 1055 | chrome version number |
| 1056 | """ |
| 1057 | if is_cros_version(rev): |
| 1058 | assert board, 'need to specify BOARD for cros version' |
| 1059 | rev = query_chrome_version(board, rev) |
| 1060 | assert cr_util.is_chrome_version(rev) |
| 1061 | |
| 1062 | return rev |
| 1063 | |
| 1064 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1065 | def is_inside_chroot(): |
| 1066 | """Returns True if we are inside chroot.""" |
| 1067 | return os.path.exists('/etc/cros_chroot_version') |
| 1068 | |
| 1069 | |
| 1070 | def cros_sdk(chromeos_root, *args, **kwargs): |
| 1071 | """Run commands inside chromeos chroot. |
| 1072 | |
| 1073 | Args: |
| 1074 | chromeos_root: chromeos tree root |
| 1075 | *args: command to run |
| 1076 | **kwargs: |
Kuang-che Wu | d4603d7 | 2018-11-29 17:51:21 +0800 | [diff] [blame] | 1077 | chrome_root: pass to cros_sdk; mount this path into the SDK chroot |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1078 | env: (dict) environment variables for the command |
Kuang-che Wu | bcafc55 | 2019-08-15 15:27:02 +0800 | [diff] [blame] | 1079 | log_stdout: Whether write the stdout output of the child process to log. |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1080 | stdin: standard input file handle for the command |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 1081 | stderr_callback: Callback function for stderr. Called once per line. |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1082 | goma_dir: Goma installed directory to mount into the chroot |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1083 | """ |
| 1084 | envs = [] |
| 1085 | for k, v in kwargs.get('env', {}).items(): |
| 1086 | assert re.match(r'^[A-Za-z_][A-Za-z0-9_]*$', k) |
| 1087 | envs.append('%s=%s' % (k, v)) |
| 1088 | |
| 1089 | # Use --no-ns-pid to prevent cros_sdk change our pgid, otherwise subsequent |
| 1090 | # commands would be considered as background process. |
Kuang-che Wu | 399d466 | 2019-06-06 15:23:37 +0800 | [diff] [blame] | 1091 | prefix = ['chromite/bin/cros_sdk', '--no-ns-pid'] |
Kuang-che Wu | d4603d7 | 2018-11-29 17:51:21 +0800 | [diff] [blame] | 1092 | |
| 1093 | if kwargs.get('chrome_root'): |
Kuang-che Wu | 399d466 | 2019-06-06 15:23:37 +0800 | [diff] [blame] | 1094 | prefix += ['--chrome_root', kwargs['chrome_root']] |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1095 | if kwargs.get('goma_dir'): |
| 1096 | prefix += ['--goma_dir', kwargs['goma_dir']] |
Kuang-che Wu | d4603d7 | 2018-11-29 17:51:21 +0800 | [diff] [blame] | 1097 | |
Kuang-che Wu | 399d466 | 2019-06-06 15:23:37 +0800 | [diff] [blame] | 1098 | prefix += envs + ['--'] |
Kuang-che Wu | d4603d7 | 2018-11-29 17:51:21 +0800 | [diff] [blame] | 1099 | |
Kuang-che Wu | 399d466 | 2019-06-06 15:23:37 +0800 | [diff] [blame] | 1100 | # In addition to the output of command we are interested, cros_sdk may |
| 1101 | # generate its own messages. For example, chroot creation messages if we run |
| 1102 | # cros_sdk the first time. |
| 1103 | # This is the hack to run dummy command once, so we can get clean output for |
| 1104 | # the command we are interested. |
| 1105 | cmd = prefix + ['true'] |
| 1106 | util.check_call(*cmd, cwd=chromeos_root) |
| 1107 | |
| 1108 | cmd = prefix + list(args) |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 1109 | return util.check_output( |
| 1110 | *cmd, |
| 1111 | cwd=chromeos_root, |
Kuang-che Wu | bcafc55 | 2019-08-15 15:27:02 +0800 | [diff] [blame] | 1112 | log_stdout=kwargs.get('log_stdout', True), |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 1113 | stdin=kwargs.get('stdin'), |
| 1114 | stderr_callback=kwargs.get('stderr_callback')) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1115 | |
| 1116 | |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1117 | def create_chroot(chromeos_root): |
| 1118 | """Creates ChromeOS chroot. |
| 1119 | |
| 1120 | Args: |
| 1121 | chromeos_root: chromeos tree root |
| 1122 | """ |
| 1123 | if os.path.exists(os.path.join(chromeos_root, 'chroot')): |
| 1124 | return |
| 1125 | if os.path.exists(os.path.join(chromeos_root, 'chroot.img')): |
| 1126 | return |
| 1127 | |
| 1128 | util.check_output('chromite/bin/cros_sdk', '--create', cwd=chromeos_root) |
| 1129 | |
| 1130 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1131 | def copy_into_chroot(chromeos_root, src, dst): |
| 1132 | """Copies file into chromeos chroot. |
| 1133 | |
| 1134 | Args: |
| 1135 | chromeos_root: chromeos tree root |
| 1136 | src: path outside chroot |
| 1137 | dst: path inside chroot |
| 1138 | """ |
| 1139 | # chroot may be an image, so we cannot copy to corresponding path |
| 1140 | # directly. |
| 1141 | cros_sdk(chromeos_root, 'sh', '-c', 'cat > %s' % dst, stdin=open(src)) |
| 1142 | |
| 1143 | |
| 1144 | def exists_in_chroot(chromeos_root, path): |
| 1145 | """Determine whether a path exists in the chroot. |
| 1146 | |
| 1147 | Args: |
| 1148 | chromeos_root: chromeos tree root |
| 1149 | path: path inside chroot, relative to src/scripts |
| 1150 | |
| 1151 | Returns: |
| 1152 | True if a path exists |
| 1153 | """ |
| 1154 | try: |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 1155 | cros_sdk(chromeos_root, 'test', '-e', path) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1156 | except subprocess.CalledProcessError: |
| 1157 | return False |
| 1158 | return True |
| 1159 | |
| 1160 | |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 1161 | def check_if_need_recreate_chroot(stdout, stderr): |
| 1162 | """Analyze build log and determine if chroot should be recreated. |
| 1163 | |
| 1164 | Args: |
| 1165 | stdout: stdout output of build |
| 1166 | stderr: stderr output of build |
| 1167 | |
| 1168 | Returns: |
| 1169 | the reason if chroot needs recreated; None otherwise |
| 1170 | """ |
Kuang-che Wu | 74768d3 | 2018-09-07 12:03:24 +0800 | [diff] [blame] | 1171 | if re.search( |
| 1172 | r"The current version of portage supports EAPI '\d+'. " |
Kuang-che Wu | ae6824b | 2019-08-27 22:20:01 +0800 | [diff] [blame] | 1173 | 'You must upgrade', stderr): |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 1174 | return 'EAPI version mismatch' |
| 1175 | |
Kuang-che Wu | 5ac8132 | 2018-11-26 14:04:06 +0800 | [diff] [blame] | 1176 | if 'Chroot is too new. Consider running:' in stderr: |
| 1177 | return 'chroot version is too new' |
| 1178 | |
| 1179 | # old message before Oct 2018 |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 1180 | if 'Chroot version is too new. Consider running cros_sdk --replace' in stderr: |
| 1181 | return 'chroot version is too new' |
| 1182 | |
Kuang-che Wu | 6fe987f | 2018-08-28 15:24:20 +0800 | [diff] [blame] | 1183 | # https://groups.google.com/a/chromium.org/forum/#!msg/chromium-os-dev/uzwT5APspB4/NFakFyCIDwAJ |
| 1184 | if "undefined reference to 'std::__1::basic_string" in stdout: |
| 1185 | return 'might be due to compiler change' |
| 1186 | |
Kuang-che Wu | 94e3b45 | 2019-11-21 12:49:18 +0800 | [diff] [blame] | 1187 | # Detect failures due to file collisions. |
| 1188 | # For example, kernel uprev from 3.x to 4.x, they are two separate packages |
| 1189 | # and conflict with each other. Other possible cases are package renaming or |
| 1190 | # refactoring. Let's recreate chroot to work around them. |
| 1191 | if 'Detected file collision' in stdout: |
| 1192 | # Using wildcard between words because the text wraps to the next line |
| 1193 | # depending on length of package name and each line is prefixed with |
| 1194 | # package name. |
| 1195 | # Using ".{,100}" instead of ".*" to prevent regex matching time explodes |
| 1196 | # exponentially. 100 is chosen arbitrarily. It should be longer than any |
| 1197 | # package name (65 now). |
| 1198 | m = re.search( |
| 1199 | r'Package (\S+).{,100}NOT.{,100}merged.{,100}' |
| 1200 | r'due.{,100}to.{,100}file.{,100}collisions', stdout, re.S) |
| 1201 | if m: |
| 1202 | return 'failed to install package due to file collision: ' + m.group(1) |
Kuang-che Wu | 356c352 | 2019-11-19 16:11:05 +0800 | [diff] [blame] | 1203 | |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 1204 | return None |
| 1205 | |
| 1206 | |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1207 | def build_packages(chromeos_root, |
| 1208 | board, |
| 1209 | chrome_root=None, |
| 1210 | goma_dir=None, |
| 1211 | afdo_use=False): |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 1212 | """Build ChromeOS packages. |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1213 | |
| 1214 | Args: |
| 1215 | chromeos_root: chromeos tree root |
| 1216 | board: ChromeOS board name |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1217 | chrome_root: Chrome tree root. If specified, build chrome using the |
| 1218 | provided tree |
| 1219 | goma_dir: Goma installed directory to mount into the chroot. If specified, |
| 1220 | build chrome with goma. |
| 1221 | afdo_use: build chrome with AFDO optimization |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1222 | """ |
Zheng-Jie Chang | ffd4946 | 2019-12-16 12:15:18 +0800 | [diff] [blame] | 1223 | |
| 1224 | def has_build_package_argument(argument): |
| 1225 | stderr_lines = [] |
| 1226 | try: |
| 1227 | util.check_call( |
| 1228 | 'src/scripts/build_packages', |
| 1229 | '--help', |
| 1230 | cwd=chromeos_root, |
| 1231 | stderr_callback=stderr_lines.append) |
| 1232 | except subprocess.CalledProcessError: |
| 1233 | help_output = ''.join(stderr_lines) |
| 1234 | return '--[no]%s' % argument in help_output |
| 1235 | |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1236 | common_env = { |
| 1237 | 'USE': '-cros-debug chrome_internal', |
| 1238 | 'FEATURES': 'separatedebug', |
| 1239 | } |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 1240 | stderr_lines = [] |
| 1241 | try: |
Kuang-che Wu | fb55310 | 2018-10-02 18:14:29 +0800 | [diff] [blame] | 1242 | with locking.lock_file(locking.LOCK_FILE_FOR_BUILD): |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1243 | env = common_env.copy() |
| 1244 | env['FEATURES'] += ' -separatedebug splitdebug' |
Kuang-che Wu | fb55310 | 2018-10-02 18:14:29 +0800 | [diff] [blame] | 1245 | cros_sdk( |
| 1246 | chromeos_root, |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 1247 | './update_chroot', |
| 1248 | '--toolchain_boards', |
Kuang-che Wu | fb55310 | 2018-10-02 18:14:29 +0800 | [diff] [blame] | 1249 | board, |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1250 | env=env, |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 1251 | stderr_callback=stderr_lines.append) |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1252 | |
| 1253 | env = common_env.copy() |
| 1254 | cmd = [ |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 1255 | './build_packages', |
| 1256 | '--board', |
| 1257 | board, |
| 1258 | '--withdev', |
| 1259 | '--noworkon', |
| 1260 | '--skip_chroot_upgrade', |
| 1261 | '--accept_licenses=@CHROMEOS', |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1262 | ] |
Zheng-Jie Chang | ffd4946 | 2019-12-16 12:15:18 +0800 | [diff] [blame] | 1263 | |
| 1264 | # `use_any_chrome` flag is default on and will force to use a chrome |
| 1265 | # prebuilt even if the version doesn't match. |
| 1266 | |
| 1267 | # As this argument is landed in 12681, we should check if the argument |
| 1268 | # exists before adding this. |
| 1269 | if has_build_package_argument('use_any_chrome'): |
| 1270 | cmd.append('--nouse_any_chrome') |
| 1271 | |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1272 | if goma_dir: |
| 1273 | # Tell build_packages to start and stop goma |
| 1274 | cmd.append('--run_goma') |
| 1275 | env['USE_GOMA'] = 'true' |
| 1276 | if afdo_use: |
| 1277 | env['USE'] += ' afdo_use' |
| 1278 | cros_sdk( |
| 1279 | chromeos_root, |
| 1280 | *cmd, |
| 1281 | env=env, |
| 1282 | chrome_root=chrome_root, |
| 1283 | stderr_callback=stderr_lines.append, |
| 1284 | goma_dir=goma_dir) |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 1285 | except subprocess.CalledProcessError as e: |
| 1286 | # Detect failures due to incompatibility between chroot and source tree. If |
| 1287 | # so, notify the caller to recreate chroot and retry. |
| 1288 | reason = check_if_need_recreate_chroot(e.output, ''.join(stderr_lines)) |
| 1289 | if reason: |
| 1290 | raise NeedRecreateChrootException(reason) |
| 1291 | |
| 1292 | # For other failures, don't know how to handle. Just bail out. |
| 1293 | raise |
| 1294 | |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 1295 | |
| 1296 | def build_image(chromeos_root, board): |
| 1297 | """Build ChromeOS image. |
| 1298 | |
| 1299 | Args: |
| 1300 | chromeos_root: chromeos tree root |
| 1301 | board: ChromeOS board name |
| 1302 | |
| 1303 | Returns: |
| 1304 | image folder; relative to chromeos_root |
| 1305 | """ |
| 1306 | stderr_lines = [] |
| 1307 | try: |
| 1308 | with locking.lock_file(locking.LOCK_FILE_FOR_BUILD): |
| 1309 | cros_sdk( |
| 1310 | chromeos_root, |
| 1311 | './build_image', |
| 1312 | '--board', |
| 1313 | board, |
| 1314 | '--noenable_rootfs_verification', |
| 1315 | 'test', |
| 1316 | env={ |
| 1317 | 'USE': '-cros-debug chrome_internal', |
| 1318 | 'FEATURES': 'separatedebug', |
| 1319 | }, |
| 1320 | stderr_callback=stderr_lines.append) |
| 1321 | except subprocess.CalledProcessError as e: |
| 1322 | # Detect failures due to incompatibility between chroot and source tree. If |
| 1323 | # so, notify the caller to recreate chroot and retry. |
| 1324 | reason = check_if_need_recreate_chroot(e.output, ''.join(stderr_lines)) |
| 1325 | if reason: |
| 1326 | raise NeedRecreateChrootException(reason) |
| 1327 | |
| 1328 | # For other failures, don't know how to handle. Just bail out. |
| 1329 | raise |
| 1330 | |
| 1331 | image_symlink = os.path.join(chromeos_root, cached_images_dir, board, |
| 1332 | 'latest') |
| 1333 | assert os.path.exists(image_symlink) |
| 1334 | image_name = os.readlink(image_symlink) |
| 1335 | image_folder = os.path.join(cached_images_dir, board, image_name) |
| 1336 | assert os.path.exists( |
| 1337 | os.path.join(chromeos_root, image_folder, test_image_filename)) |
| 1338 | return image_folder |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1339 | |
| 1340 | |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 1341 | class AutotestControlInfo(object): |
| 1342 | """Parsed content of autotest control file. |
| 1343 | |
| 1344 | Attributes: |
| 1345 | name: test name |
| 1346 | path: control file path |
| 1347 | variables: dict of top-level control variables. Sample keys: NAME, AUTHOR, |
| 1348 | DOC, ATTRIBUTES, DEPENDENCIES, etc. |
| 1349 | """ |
| 1350 | |
| 1351 | def __init__(self, path, variables): |
| 1352 | self.name = variables['NAME'] |
| 1353 | self.path = path |
| 1354 | self.variables = variables |
| 1355 | |
| 1356 | |
| 1357 | def parse_autotest_control_file(path): |
| 1358 | """Parses autotest control file. |
| 1359 | |
| 1360 | This only parses simple top-level string assignments. |
| 1361 | |
| 1362 | Returns: |
| 1363 | AutotestControlInfo object |
| 1364 | """ |
| 1365 | variables = {} |
Kuang-che Wu | a572349 | 2019-11-25 20:59:34 +0800 | [diff] [blame] | 1366 | with open(path) as f: |
| 1367 | code = ast.parse(f.read()) |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 1368 | for stmt in code.body: |
| 1369 | # Skip if not simple "NAME = *" assignment. |
| 1370 | if not (isinstance(stmt, ast.Assign) and len(stmt.targets) == 1 and |
| 1371 | isinstance(stmt.targets[0], ast.Name)): |
| 1372 | continue |
| 1373 | |
| 1374 | # Only support string value. |
| 1375 | if isinstance(stmt.value, ast.Str): |
| 1376 | variables[stmt.targets[0].id] = stmt.value.s |
| 1377 | |
| 1378 | return AutotestControlInfo(path, variables) |
| 1379 | |
| 1380 | |
| 1381 | def enumerate_autotest_control_files(autotest_dir): |
| 1382 | """Enumerate autotest control files. |
| 1383 | |
| 1384 | Args: |
| 1385 | autotest_dir: autotest folder |
| 1386 | |
| 1387 | Returns: |
| 1388 | list of paths to control files |
| 1389 | """ |
| 1390 | # Where to find control files. Relative to autotest_dir. |
| 1391 | subpaths = [ |
| 1392 | 'server/site_tests', |
| 1393 | 'client/site_tests', |
| 1394 | 'server/tests', |
| 1395 | 'client/tests', |
| 1396 | ] |
| 1397 | |
| 1398 | blacklist = ['site-packages', 'venv', 'results', 'logs', 'containers'] |
| 1399 | result = [] |
| 1400 | for subpath in subpaths: |
| 1401 | path = os.path.join(autotest_dir, subpath) |
| 1402 | for root, dirs, files in os.walk(path): |
| 1403 | |
| 1404 | for black in blacklist: |
| 1405 | if black in dirs: |
| 1406 | dirs.remove(black) |
| 1407 | |
| 1408 | for filename in files: |
| 1409 | if filename == 'control' or filename.startswith('control.'): |
| 1410 | result.append(os.path.join(root, filename)) |
| 1411 | |
| 1412 | return result |
| 1413 | |
| 1414 | |
| 1415 | def get_autotest_test_info(autotest_dir, test_name): |
| 1416 | """Get metadata of given test. |
| 1417 | |
| 1418 | Args: |
| 1419 | autotest_dir: autotest folder |
| 1420 | test_name: test name |
| 1421 | |
| 1422 | Returns: |
| 1423 | AutotestControlInfo object. None if test not found. |
| 1424 | """ |
| 1425 | for control_file in enumerate_autotest_control_files(autotest_dir): |
| 1426 | info = parse_autotest_control_file(control_file) |
| 1427 | if info.name == test_name: |
| 1428 | return info |
| 1429 | return None |
| 1430 | |
| 1431 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1432 | class ChromeOSSpecManager(codechange.SpecManager): |
| 1433 | """Repo manifest related operations. |
| 1434 | |
| 1435 | This class enumerates chromeos manifest files, parses them, |
| 1436 | and sync to disk state according to them. |
| 1437 | """ |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1438 | |
| 1439 | def __init__(self, config): |
| 1440 | self.config = config |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1441 | self.manifest_dir = os.path.join(self.config['chromeos_root'], '.repo', |
| 1442 | 'manifests') |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1443 | self.manifest_internal_dir = os.path.join(self.config['chromeos_mirror'], |
| 1444 | 'manifest-internal.git') |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1445 | self.historical_manifest_git_dir = os.path.join( |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 1446 | self.config['chromeos_mirror'], 'chromeos/manifest-versions.git') |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1447 | if not os.path.exists(self.historical_manifest_git_dir): |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 1448 | raise errors.InternalError('Manifest snapshots should be cloned into %s' % |
| 1449 | self.historical_manifest_git_dir) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1450 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1451 | def lookup_snapshot_manifest_revisions(self, old, new): |
| 1452 | """Get manifest commits between snapshot versions. |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1453 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1454 | Returns: |
| 1455 | list of (timestamp, commit_id, snapshot_id): |
| 1456 | timestamp: integer unix timestamp |
| 1457 | commit_id: a string indicates commit hash |
| 1458 | snapshot_id: a string indicates snapshot id |
| 1459 | """ |
| 1460 | assert is_cros_snapshot_version(old) |
| 1461 | assert is_cros_snapshot_version(new) |
| 1462 | |
| 1463 | gs_path = ( |
| 1464 | 'gs://chromeos-image-archive/{board}-postsubmit/{version}-*/image.zip') |
| 1465 | # Try to guess the commit time of a snapshot manifest, it is usually a few |
| 1466 | # minutes different between snapshot manifest commit and image.zip |
| 1467 | # generate. |
| 1468 | try: |
| 1469 | old_timestamp = gsutil_stat_update_time( |
| 1470 | gs_path.format(board=self.config['board'], version=old)) - 86400 |
| 1471 | except subprocess.CalledProcessError: |
| 1472 | old_timestamp = None |
| 1473 | try: |
| 1474 | new_timestamp = gsutil_stat_update_time( |
| 1475 | gs_path.format(board=self.config['board'], version=new)) + 86400 |
| 1476 | # 1558657989 is snapshot_id 5982's commit time, this ensures every time |
| 1477 | # we can find snapshot 5982 |
| 1478 | # snapshot_id <= 5982 has different commit message format, so we need |
| 1479 | # to identify its id in different ways, see below comment for more info. |
| 1480 | new_timestamp = max(new_timestamp, 1558657989 + 1) |
| 1481 | except subprocess.CalledProcessError: |
| 1482 | new_timestamp = None |
| 1483 | result = [] |
| 1484 | _, _, old_snapshot_id = snapshot_version_split(old) |
| 1485 | _, _, new_snapshot_id = snapshot_version_split(new) |
| 1486 | repo = self.manifest_internal_dir |
| 1487 | path = 'snapshot.xml' |
| 1488 | branch = 'snapshot' |
| 1489 | commits = git_util.get_history( |
| 1490 | repo, |
| 1491 | path, |
| 1492 | branch, |
| 1493 | after=old_timestamp, |
| 1494 | before=new_timestamp, |
| 1495 | with_subject=True) |
| 1496 | |
| 1497 | # Unfortunately, we can not identify snapshot_id <= 5982 from its commit |
| 1498 | # subject, as their subjects are all `Annealing manifest snapshot.`. |
| 1499 | # So instead we count the snapshot_id manually. |
| 1500 | count = 5982 |
| 1501 | # There are two snapshot_id = 2633 in commit history, ignore the former |
| 1502 | # one. |
| 1503 | ignore_list = ['95c8526a7f0798d02f692010669dcbd5a152439a'] |
| 1504 | # We examine the commits in reverse order as there are some testing |
| 1505 | # commits before snapshot_id=2, this method works fine after |
| 1506 | # snapshot 2, except snapshot 2633 |
| 1507 | for commit in reversed(commits): |
| 1508 | msg = commit[2] |
| 1509 | if commit[1] in ignore_list: |
| 1510 | continue |
| 1511 | |
| 1512 | match = re.match(r'^annealing manifest snapshot (\d+)', msg) |
| 1513 | if match: |
| 1514 | snapshot_id = match.group(1) |
| 1515 | elif 'Annealing manifest snapshot' in msg: |
| 1516 | snapshot_id = str(count) |
| 1517 | count -= 1 |
| 1518 | else: |
| 1519 | continue |
| 1520 | if int(old_snapshot_id) <= int(snapshot_id) <= int(new_snapshot_id): |
| 1521 | result.append((commit[0], commit[1], snapshot_id)) |
| 1522 | # We find commits in reversed order, now reverse it again to chronological |
| 1523 | # order. |
| 1524 | return list(reversed(result)) |
| 1525 | |
| 1526 | def lookup_build_timestamp(self, rev): |
| 1527 | assert is_cros_full_version(rev) or is_cros_snapshot_version(rev) |
| 1528 | if is_cros_full_version(rev): |
| 1529 | return self.lookup_release_build_timestamp(rev) |
Kuang-che Wu | a7ddf9b | 2019-11-25 18:59:57 +0800 | [diff] [blame] | 1530 | return self.lookup_snapshot_build_timestamp(rev) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1531 | |
| 1532 | def lookup_snapshot_build_timestamp(self, rev): |
| 1533 | assert is_cros_snapshot_version(rev) |
| 1534 | return int(self.lookup_snapshot_manifest_revisions(rev, rev)[0][0]) |
| 1535 | |
| 1536 | def lookup_release_build_timestamp(self, rev): |
| 1537 | assert is_cros_full_version(rev) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1538 | milestone, short_version = version_split(rev) |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1539 | path = os.path.join('buildspecs', milestone, short_version + '.xml') |
| 1540 | try: |
| 1541 | timestamp = git_util.get_commit_time(self.historical_manifest_git_dir, |
| 1542 | 'refs/heads/master', path) |
| 1543 | except ValueError: |
Kuang-che Wu | ce2f3be | 2019-10-28 19:44:54 +0800 | [diff] [blame] | 1544 | raise errors.InternalError( |
| 1545 | '%s does not have %s' % (self.historical_manifest_git_dir, path)) |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1546 | return timestamp |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1547 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1548 | def collect_float_spec(self, old, new): |
| 1549 | old_timestamp = self.lookup_build_timestamp(old) |
| 1550 | new_timestamp = self.lookup_build_timestamp(new) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1551 | # snapshot time is different from commit time |
| 1552 | # usually it's a few minutes different |
| 1553 | # 30 minutes should be safe in most cases |
| 1554 | if is_cros_snapshot_version(old): |
| 1555 | old_timestamp = old_timestamp - 1800 |
| 1556 | if is_cros_snapshot_version(new): |
| 1557 | new_timestamp = new_timestamp + 1800 |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1558 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1559 | path = os.path.join(self.manifest_dir, 'default.xml') |
| 1560 | if not os.path.islink(path) or os.readlink(path) != 'full.xml': |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 1561 | raise errors.InternalError( |
| 1562 | 'default.xml not symlink to full.xml is not supported') |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1563 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1564 | result = [] |
| 1565 | path = 'full.xml' |
| 1566 | parser = repo_util.ManifestParser(self.manifest_dir) |
| 1567 | for timestamp, git_rev in parser.enumerate_manifest_commits( |
| 1568 | old_timestamp, new_timestamp, path): |
| 1569 | result.append( |
| 1570 | codechange.Spec(codechange.SPEC_FLOAT, git_rev, timestamp, path)) |
| 1571 | return result |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1572 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1573 | def collect_fixed_spec(self, old, new): |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1574 | assert is_cros_full_version(old) or is_cros_snapshot_version(old) |
| 1575 | assert is_cros_full_version(new) or is_cros_snapshot_version(new) |
| 1576 | |
| 1577 | # case 1: if both are snapshot, return a list of snapshot |
| 1578 | if is_cros_snapshot_version(old) and is_cros_snapshot_version(new): |
| 1579 | return self.collect_snapshot_specs(old, new) |
| 1580 | |
| 1581 | # case 2: if both are release version |
| 1582 | # return a list of release version |
| 1583 | if is_cros_full_version(old) and is_cros_full_version(new): |
| 1584 | return self.collect_release_specs(old, new) |
| 1585 | |
| 1586 | # case 3: return a list of release version and append a snapshot |
| 1587 | # before or at the end |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 1588 | result = self.collect_release_specs( |
| 1589 | version_to_full(self.config['board'], old), |
| 1590 | version_to_full(self.config['board'], new)) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1591 | if is_cros_snapshot_version(old): |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 1592 | result = self.collect_snapshot_specs(old, old) + result[1:] |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1593 | elif is_cros_snapshot_version(new): |
Zheng-Jie Chang | 5cd62dd | 2019-12-09 13:21:12 +0800 | [diff] [blame] | 1594 | result += self.collect_snapshot_specs(new, new) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1595 | return result |
| 1596 | |
| 1597 | def collect_snapshot_specs(self, old, new): |
| 1598 | assert is_cros_snapshot_version(old) |
| 1599 | assert is_cros_snapshot_version(new) |
| 1600 | |
| 1601 | def guess_snapshot_version(board, snapshot_id, old, new): |
| 1602 | if old.endswith('-' + snapshot_id): |
| 1603 | return old |
| 1604 | if new.endswith('-' + snapshot_id): |
| 1605 | return new |
Kuang-che Wu | f791afa | 2019-10-28 19:53:26 +0800 | [diff] [blame] | 1606 | gs_path = ('gs://chromeos-image-archive/{board}-postsubmit/' |
| 1607 | 'R*-{snapshot_id}-*'.format( |
| 1608 | board=board, snapshot_id=snapshot_id)) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1609 | for line in gsutil_ls(gs_path, ignore_errors=True): |
| 1610 | m = re.match(r'^gs:\S+(R\d+-\d+\.\d+\.\d+-\d+)\S+', line) |
| 1611 | if m: |
| 1612 | return m.group(1) |
Zheng-Jie Chang | 026cd5d | 2019-12-04 12:13:01 +0800 | [diff] [blame] | 1613 | return None |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1614 | |
| 1615 | result = [] |
| 1616 | path = 'snapshot.xml' |
| 1617 | revisions = self.lookup_snapshot_manifest_revisions(old, new) |
Kuang-che Wu | f791afa | 2019-10-28 19:53:26 +0800 | [diff] [blame] | 1618 | for timestamp, _git_rev, snapshot_id in revisions: |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1619 | snapshot_version = guess_snapshot_version(self.config['board'], |
| 1620 | snapshot_id, old, new) |
Zheng-Jie Chang | 026cd5d | 2019-12-04 12:13:01 +0800 | [diff] [blame] | 1621 | if snapshot_version: |
| 1622 | result.append( |
| 1623 | codechange.Spec(codechange.SPEC_FIXED, snapshot_version, timestamp, |
| 1624 | path)) |
| 1625 | else: |
| 1626 | logger.warning('snapshot id %s is not found, ignore', snapshot_id) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1627 | return result |
| 1628 | |
| 1629 | def collect_release_specs(self, old, new): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1630 | assert is_cros_full_version(old) |
| 1631 | assert is_cros_full_version(new) |
| 1632 | old_milestone, old_short_version = version_split(old) |
| 1633 | new_milestone, new_short_version = version_split(new) |
| 1634 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1635 | result = [] |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1636 | for milestone in git_util.list_dir_from_revision( |
| 1637 | self.historical_manifest_git_dir, 'refs/heads/master', 'buildspecs'): |
| 1638 | if not milestone.isdigit(): |
| 1639 | continue |
| 1640 | if not int(old_milestone) <= int(milestone) <= int(new_milestone): |
| 1641 | continue |
| 1642 | |
Kuang-che Wu | 74768d3 | 2018-09-07 12:03:24 +0800 | [diff] [blame] | 1643 | files = git_util.list_dir_from_revision( |
| 1644 | self.historical_manifest_git_dir, 'refs/heads/master', |
| 1645 | os.path.join('buildspecs', milestone)) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1646 | |
| 1647 | for fn in files: |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1648 | path = os.path.join('buildspecs', milestone, fn) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1649 | short_version, ext = os.path.splitext(fn) |
| 1650 | if ext != '.xml': |
| 1651 | continue |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1652 | if (util.is_version_lesseq(old_short_version, short_version) and |
| 1653 | util.is_version_lesseq(short_version, new_short_version) and |
| 1654 | util.is_direct_relative_version(short_version, new_short_version)): |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1655 | rev = make_cros_full_version(milestone, short_version) |
| 1656 | timestamp = git_util.get_commit_time(self.historical_manifest_git_dir, |
| 1657 | 'refs/heads/master', path) |
| 1658 | result.append( |
| 1659 | codechange.Spec(codechange.SPEC_FIXED, rev, timestamp, path)) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1660 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1661 | def version_key_func(spec): |
| 1662 | _milestone, short_version = version_split(spec.name) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1663 | return util.version_key_func(short_version) |
| 1664 | |
| 1665 | result.sort(key=version_key_func) |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1666 | assert result[0].name == old |
| 1667 | assert result[-1].name == new |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1668 | return result |
| 1669 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1670 | def get_manifest(self, rev): |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1671 | assert is_cros_full_version(rev) or is_cros_snapshot_version(rev) |
| 1672 | if is_cros_full_version(rev): |
| 1673 | milestone, short_version = version_split(rev) |
| 1674 | path = os.path.join('buildspecs', milestone, '%s.xml' % short_version) |
| 1675 | manifest = git_util.get_file_from_revision( |
| 1676 | self.historical_manifest_git_dir, 'refs/heads/master', path) |
| 1677 | else: |
| 1678 | revisions = self.lookup_snapshot_manifest_revisions(rev, rev) |
| 1679 | commit_id = revisions[0][1] |
| 1680 | manifest = git_util.get_file_from_revision(self.manifest_internal_dir, |
| 1681 | commit_id, 'snapshot.xml') |
Zheng-Jie Chang | 0fc704b | 2019-12-09 18:43:38 +0800 | [diff] [blame] | 1682 | return manifest |
| 1683 | |
| 1684 | def get_manifest_file(self, rev): |
| 1685 | assert is_cros_full_version(rev) or is_cros_snapshot_version(rev) |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1686 | manifest_name = 'manifest_%s.xml' % rev |
| 1687 | manifest_path = os.path.join(self.manifest_dir, manifest_name) |
| 1688 | with open(manifest_path, 'w') as f: |
Zheng-Jie Chang | 0fc704b | 2019-12-09 18:43:38 +0800 | [diff] [blame] | 1689 | f.write(self.get_manifest(rev)) |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1690 | return manifest_name |
| 1691 | |
| 1692 | def parse_spec(self, spec): |
| 1693 | parser = repo_util.ManifestParser(self.manifest_dir) |
| 1694 | if spec.spec_type == codechange.SPEC_FIXED: |
Zheng-Jie Chang | 0fc704b | 2019-12-09 18:43:38 +0800 | [diff] [blame] | 1695 | manifest_name = self.get_manifest_file(spec.name) |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1696 | manifest_path = os.path.join(self.manifest_dir, manifest_name) |
Kuang-che Wu | a572349 | 2019-11-25 20:59:34 +0800 | [diff] [blame] | 1697 | with open(manifest_path) as f: |
| 1698 | content = f.read() |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1699 | root = parser.parse_single_xml(content, allow_include=False) |
| 1700 | else: |
| 1701 | root = parser.parse_xml_recursive(spec.name, spec.path) |
| 1702 | |
| 1703 | spec.entries = parser.process_parsed_result(root) |
| 1704 | if spec.spec_type == codechange.SPEC_FIXED: |
Kuang-che Wu | fe1e88a | 2019-09-10 21:52:25 +0800 | [diff] [blame] | 1705 | if not spec.is_static(): |
| 1706 | raise ValueError( |
| 1707 | 'fixed spec %r has unexpected floating entries' % spec.name) |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1708 | |
| 1709 | def sync_disk_state(self, rev): |
Zheng-Jie Chang | 0fc704b | 2019-12-09 18:43:38 +0800 | [diff] [blame] | 1710 | manifest_name = self.get_manifest_file(rev) |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1711 | |
| 1712 | # For ChromeOS, mark_as_stable step requires 'repo init -m', which sticks |
| 1713 | # manifest. 'repo sync -m' is not enough |
| 1714 | repo_util.init( |
| 1715 | self.config['chromeos_root'], |
| 1716 | 'https://chrome-internal.googlesource.com/chromeos/manifest-internal', |
| 1717 | manifest_name=manifest_name, |
| 1718 | repo_url='https://chromium.googlesource.com/external/repo.git', |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 1719 | reference=self.config['chromeos_mirror'], |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1720 | ) |
| 1721 | |
| 1722 | # Note, don't sync with current_branch=True for chromeos. One of its |
| 1723 | # build steps (inside mark_as_stable) executes "git describe" which |
| 1724 | # needs git tag information. |
| 1725 | repo_util.sync(self.config['chromeos_root']) |