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