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