David Riley | 0655bab | 2017-11-02 10:44:26 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Luis Hector Chavez | dca9dd7 | 2018-06-12 12:56:30 -0700 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Module containing classes that wrap artifact downloads.""" |
| 8 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 9 | from __future__ import print_function |
| 10 | |
| 11 | import itertools |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 12 | import os |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 13 | import pickle |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 14 | import re |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 15 | import shutil |
| 16 | import subprocess |
xixuan | 56252ff | 2017-03-09 15:40:31 -0800 | [diff] [blame] | 17 | import traceback |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 18 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 19 | import artifact_info |
| 20 | import common_util |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 21 | import devserver_constants |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 22 | import log_util |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 23 | |
Don Garrett | fb15e32 | 2016-06-21 19:12:08 -0700 | [diff] [blame] | 24 | # We do a number of things with args/kwargs arguments that confuse pylint. |
| 25 | # pylint: disable=docstring-misnamed-args |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 26 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 27 | _AU_BASE = 'au' |
| 28 | _NTON_DIR_SUFFIX = '_nton' |
| 29 | _MTON_DIR_SUFFIX = '_mton' |
| 30 | |
| 31 | ############ Actual filenames of artifacts in Google Storage ############ |
| 32 | |
| 33 | AU_SUITE_FILE = 'au_control.tar.bz2' |
Chris Sosa | 968a106 | 2013-08-02 17:42:50 -0700 | [diff] [blame] | 34 | PAYGEN_AU_SUITE_FILE_TEMPLATE = 'paygen_au_%(channel)s_control.tar.bz2' |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 35 | AUTOTEST_FILE = 'autotest.tar' |
Simran Basi | ea0590d | 2014-10-29 11:31:26 -0700 | [diff] [blame] | 36 | CONTROL_FILES_FILE = 'control_files.tar' |
| 37 | AUTOTEST_PACKAGES_FILE = 'autotest_packages.tar' |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 38 | AUTOTEST_COMPRESSED_FILE = 'autotest.tar.bz2' |
Simran Basi | 6459bba | 2015-02-04 14:47:23 -0800 | [diff] [blame] | 39 | AUTOTEST_SERVER_PACKAGE_FILE = 'autotest_server_package.tar.bz2' |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 40 | DEBUG_SYMBOLS_FILE = 'debug.tgz' |
Dan Shi | 55d0f97 | 2016-10-04 11:45:00 -0700 | [diff] [blame] | 41 | DEBUG_SYMBOLS_ONLY_FILE = 'debug_breakpad.tar.xz' |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 42 | FACTORY_FILE = 'ChromeOS-factory*.zip' |
Mike Frysinger | a0e6a28 | 2016-09-01 17:29:08 -0400 | [diff] [blame] | 43 | FACTORY_SHIM_FILE = 'factory_image.zip' |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 44 | FIRMWARE_FILE = 'firmware_from_source.tar.bz2' |
| 45 | IMAGE_FILE = 'image.zip' |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 46 | TEST_SUITES_FILE = 'test_suites.tar.bz2' |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 47 | BASE_IMAGE_FILE = 'chromiumos_base_image.tar.xz' |
| 48 | TEST_IMAGE_FILE = 'chromiumos_test_image.tar.xz' |
| 49 | RECOVERY_IMAGE_FILE = 'recovery_image.tar.xz' |
Nick Sanders | a995c9d | 2019-05-07 17:45:43 -0700 | [diff] [blame] | 50 | SIGNED_RECOVERY_IMAGE_FILE = 'chromeos_*recovery*mp*.bin' |
Justin Giorgi | b759052 | 2017-02-07 13:36:24 -0800 | [diff] [blame] | 51 | LIBIOTA_TEST_BINARIES_FILE = 'test_binaries.tar.gz' |
| 52 | LIBIOTA_BOARD_UTILS_FILE = 'board_utils.tar.gz' |
David Riley | 0655bab | 2017-11-02 10:44:26 -0700 | [diff] [blame] | 53 | QUICK_PROVISION_FILE = 'full_dev_part_*.bin.gz' |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 54 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 55 | ############ Actual filenames of Android build artifacts ############ |
| 56 | |
Dan Shi | ba4e00f | 2015-10-27 12:03:53 -0700 | [diff] [blame] | 57 | ANDROID_IMAGE_ZIP = r'.*-img-[^-]*\.zip' |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 58 | ANDROID_RADIO_IMAGE = 'radio.img' |
| 59 | ANDROID_BOOTLOADER_IMAGE = 'bootloader.img' |
| 60 | ANDROID_FASTBOOT = 'fastboot' |
| 61 | ANDROID_TEST_ZIP = r'[^-]*-tests-.*\.zip' |
Dan Shi | 74136ae | 2015-12-01 14:40:06 -0800 | [diff] [blame] | 62 | ANDROID_VENDOR_PARTITION_ZIP = r'[^-]*-vendor_partitions-.*\.zip' |
Dan Shi | 6c2b2a2 | 2016-03-04 15:52:19 -0800 | [diff] [blame] | 63 | ANDROID_AUTOTEST_SERVER_PACKAGE = r'[^-]*-autotest_server_package-.*\.tar.bz2' |
| 64 | ANDROID_TEST_SUITES = r'[^-]*-test_suites-.*\.tar.bz2' |
| 65 | ANDROID_CONTROL_FILES = r'[^-]*-autotest_control_files-.*\.tar' |
Simran Basi | 05be721 | 2016-03-16 13:08:23 -0700 | [diff] [blame] | 66 | ANDROID_NATIVETESTS_FILE = r'[^-]*-brillo-tests-.*\.zip' |
Ralph Nathan | 6c7fe5e | 2016-06-22 15:50:10 -0700 | [diff] [blame] | 67 | ANDROID_CONTINUOUS_NATIVE_TESTS_FILE = r'[^-]*-continuous_native_tests-.*\.zip' |
Justin Giorgi | bb32ac4 | 2016-08-04 10:34:35 -0700 | [diff] [blame] | 68 | ANDROID_CONTINUOUS_INSTRUMENTATION_TESTS_FILE = ( |
| 69 | r'[^-]*-continuous_instrumentation_tests-.*\.zip') |
Justin Giorgi | 4faa890 | 2016-08-19 19:54:30 -0700 | [diff] [blame] | 70 | ANDROID_CTS_FILE = 'android-cts.zip' |
Justin Giorgi | 576c154 | 2016-06-24 08:24:20 -0700 | [diff] [blame] | 71 | ANDROID_TARGET_FILES_ZIP = r'[^-]*-target_files-.*\.zip' |
| 72 | ANDROID_DTB_ZIP = r'[^-]*-dtb-.*\.zip' |
Satoshi Niwa | 5e3ed5e | 2018-06-25 16:03:33 +0900 | [diff] [blame] | 73 | ANDROID_PUSH_TO_DEVICE_ZIP = 'push_to_device.zip' |
Qijiang Fan | 31beb7f | 2018-06-26 17:09:00 +0900 | [diff] [blame] | 74 | ANDROID_SEPOLICY_ZIP = 'sepolicy.zip' |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 75 | |
| 76 | _build_artifact_locks = common_util.LockDict() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 77 | |
| 78 | |
| 79 | class ArtifactDownloadError(Exception): |
| 80 | """Error used to signify an issue processing an artifact.""" |
| 81 | pass |
| 82 | |
| 83 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 84 | class ArtifactMeta(type): |
| 85 | """metaclass for an artifact type. |
| 86 | |
| 87 | This metaclass is for class Artifact and its subclasses to have a meaningful |
| 88 | string composed of class name and the corresponding artifact name, e.g., |
| 89 | `Artifact_full_payload`. This helps to better logging, refer to logging in |
| 90 | method Downloader.Download. |
| 91 | """ |
| 92 | |
| 93 | ARTIFACT_NAME = None |
| 94 | |
| 95 | def __str__(cls): |
| 96 | return '%s_%s' % (cls.__name__, cls.ARTIFACT_NAME) |
| 97 | |
| 98 | def __repr__(cls): |
| 99 | return str(cls) |
| 100 | |
| 101 | |
| 102 | class Artifact(log_util.Loggable): |
| 103 | """Wrapper around an artifact to download using a fetcher. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 104 | |
| 105 | The purpose of this class is to download objects from Google Storage |
| 106 | and install them to a local directory. There are two main functions, one to |
| 107 | download/prepare the artifacts in to a temporary staging area and the second |
| 108 | to stage it into its final destination. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 109 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 110 | IMPORTANT! (i) `name' is a glob expression by default (and not a regex), be |
| 111 | attentive when adding new artifacts; (ii) name matching semantics differ |
| 112 | between a glob (full name string match) and a regex (partial match). |
| 113 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 114 | Class members: |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 115 | fetcher: An object which knows how to fetch the artifact. |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 116 | name: Name given for artifact; in fact, it is a pattern that captures the |
| 117 | names of files contained in the artifact. This can either be an |
| 118 | ordinary shell-style glob (the default), or a regular expression (if |
| 119 | is_regex_name is True). |
| 120 | is_regex_name: Whether the name value is a regex (default: glob). |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 121 | build: The version of the build i.e. R26-2342.0.0. |
| 122 | marker_name: Name used to define the lock marker for the artifacts to |
| 123 | prevent it from being re-downloaded. By default based on name |
| 124 | but can be overriden by children. |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 125 | exception_file_path: Path to a file containing the serialized exception, |
| 126 | which was raised in Process method. The file is located |
| 127 | in the parent folder of install_dir, since the |
| 128 | install_dir will be deleted if the build does not |
| 129 | existed. |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 130 | install_path: Path to artifact. |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 131 | install_subdir: Directory within install_path where the artifact is actually |
| 132 | stored. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 133 | install_dir: The final location where the artifact should be staged to. |
| 134 | single_name: If True the name given should only match one item. Note, if not |
| 135 | True, self.name will become a list of items returned. |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 136 | installed_files: A list of files that were the final result of downloading |
| 137 | and setting up the artifact. |
| 138 | store_installed_files: Whether the list of installed files is stored in the |
| 139 | marker file. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 140 | """ |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 141 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 142 | __metaclass__ = ArtifactMeta |
| 143 | |
| 144 | def __init__(self, name, install_dir, build, install_subdir='', |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 145 | is_regex_name=False, optional_name=None): |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 146 | """Constructor. |
| 147 | |
| 148 | Args: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 149 | install_dir: Where to install the artifact. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 150 | name: Identifying name to be used to find/store the artifact. |
| 151 | build: The name of the build e.g. board/release. |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 152 | install_subdir: Directory within install_path where the artifact is |
| 153 | actually stored. |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 154 | is_regex_name: Whether the name pattern is a regex (default: glob). |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 155 | optional_name: An alternative name to find the artifact, which can lead |
| 156 | to faster download. Unlike |name|, there is no guarantee that an |
| 157 | artifact named |optional_name| is/will be on Google Storage. If it |
| 158 | exists, we download it. Otherwise, we fall back to wait for |name|. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 159 | """ |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 160 | super(Artifact, self).__init__() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 161 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 162 | # In-memory lock to keep the devserver from colliding with itself while |
| 163 | # attempting to stage the same artifact. |
| 164 | self._process_lock = None |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 165 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 166 | self.name = name |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 167 | self.optional_name = optional_name |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 168 | self.is_regex_name = is_regex_name |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 169 | self.build = build |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 170 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 171 | self.marker_name = '.' + self._SanitizeName(name) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 172 | |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 173 | exception_file_name = ('.' + self._SanitizeName(build) + self.marker_name + |
| 174 | '.exception') |
| 175 | # The exception file needs to be located in parent folder, since the |
| 176 | # install_dir will be deleted is the build does not exist. |
| 177 | self.exception_file_path = os.path.join(os.path.dirname(install_dir), |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 178 | exception_file_name) |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 179 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 180 | self.install_path = None |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 181 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 182 | self.install_dir = install_dir |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 183 | self.install_subdir = install_subdir |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 184 | |
| 185 | self.single_name = True |
| 186 | |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 187 | self.installed_files = [] |
| 188 | self.store_installed_files = True |
| 189 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 190 | @staticmethod |
| 191 | def _SanitizeName(name): |
| 192 | """Sanitizes name to be used for creating a file on the filesystem. |
| 193 | |
| 194 | '.','/' and '*' have special meaning in FS lingo. Replace them with words. |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 195 | |
| 196 | Args: |
| 197 | name: A file name/path. |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 198 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 199 | Returns: |
| 200 | The sanitized name/path. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 201 | """ |
| 202 | return name.replace('*', 'STAR').replace('.', 'DOT').replace('/', 'SLASH') |
| 203 | |
Dan Shi | f8eb0d1 | 2013-08-01 17:52:06 -0700 | [diff] [blame] | 204 | def ArtifactStaged(self): |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 205 | """Returns True if artifact is already staged. |
| 206 | |
| 207 | This checks for (1) presence of the artifact marker file, and (2) the |
| 208 | presence of each installed file listed in this marker. Both must hold for |
| 209 | the artifact to be considered staged. Note that this method is safe for use |
| 210 | even if the artifacts were not stageed by this instance, as it is assumed |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 211 | that any Artifact instance that did the staging wrote the list of |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 212 | files actually installed into the marker. |
| 213 | """ |
| 214 | marker_file = os.path.join(self.install_dir, self.marker_name) |
| 215 | |
| 216 | # If the marker is missing, it's definitely not staged. |
| 217 | if not os.path.exists(marker_file): |
Aviv Keshet | 57d1817 | 2016-06-18 20:39:09 -0700 | [diff] [blame] | 218 | self._Log('No marker file, %s is not staged.', self) |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 219 | return False |
| 220 | |
| 221 | # We want to ensure that every file listed in the marker is actually there. |
| 222 | if self.store_installed_files: |
| 223 | with open(marker_file) as f: |
| 224 | files = [line.strip() for line in f] |
| 225 | |
| 226 | # Check to see if any of the purportedly installed files are missing, in |
| 227 | # which case the marker is outdated and should be removed. |
| 228 | missing_files = [fname for fname in files if not os.path.exists(fname)] |
| 229 | if missing_files: |
| 230 | self._Log('***ATTENTION*** %s files listed in %s are missing:\n%s', |
| 231 | 'All' if len(files) == len(missing_files) else 'Some', |
| 232 | marker_file, '\n'.join(missing_files)) |
| 233 | os.remove(marker_file) |
Aviv Keshet | 57d1817 | 2016-06-18 20:39:09 -0700 | [diff] [blame] | 234 | self._Log('Missing files, %s is not staged.', self) |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 235 | return False |
| 236 | |
Aviv Keshet | 57d1817 | 2016-06-18 20:39:09 -0700 | [diff] [blame] | 237 | self._Log('ArtifactStaged() -> yes, %s is staged.', self) |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 238 | return True |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 239 | |
| 240 | def _MarkArtifactStaged(self): |
| 241 | """Marks the artifact as staged.""" |
| 242 | with open(os.path.join(self.install_dir, self.marker_name), 'w') as f: |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 243 | f.write('\n'.join(self.installed_files)) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 244 | |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 245 | def _UpdateName(self, names): |
| 246 | if self.single_name and len(names) > 1: |
| 247 | raise ArtifactDownloadError('Too many artifacts match %s' % self.name) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 248 | |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 249 | self.name = names[0] |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 250 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 251 | def _Setup(self): |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 252 | """Process the downloaded content, update the list of installed files.""" |
| 253 | # In this primitive case, what was downloaded (has to be a single file) is |
| 254 | # what's installed. |
| 255 | self.installed_files = [self.install_path] |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 256 | |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 257 | def _ClearException(self): |
| 258 | """Delete any existing exception saved for this artifact.""" |
| 259 | if os.path.exists(self.exception_file_path): |
| 260 | os.remove(self.exception_file_path) |
| 261 | |
| 262 | def _SaveException(self, e): |
xixuan | 56252ff | 2017-03-09 15:40:31 -0800 | [diff] [blame] | 263 | """Save the exception and traceback to a file for downloader.IsStaged. |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 264 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 265 | Args: |
| 266 | e: Exception object to be saved. |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 267 | """ |
| 268 | with open(self.exception_file_path, 'w') as f: |
xixuan | 56252ff | 2017-03-09 15:40:31 -0800 | [diff] [blame] | 269 | pickle.dump('%s\n%s' % (e, str(traceback.format_exc())), f) |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 270 | |
| 271 | def GetException(self): |
| 272 | """Retrieve any exception that was raised in Process method. |
| 273 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 274 | Returns: |
| 275 | An Exception object that was raised when trying to process the artifact. |
| 276 | Return None if no exception was found. |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 277 | """ |
| 278 | if not os.path.exists(self.exception_file_path): |
| 279 | return None |
| 280 | with open(self.exception_file_path, 'r') as f: |
| 281 | return pickle.load(f) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 282 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 283 | def Process(self, downloader, no_wait): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 284 | """Main call point to all artifacts. Downloads and Stages artifact. |
| 285 | |
| 286 | Downloads and Stages artifact from Google Storage to the install directory |
| 287 | specified in the constructor. It multi-thread safe and does not overwrite |
| 288 | the artifact if it's already been downloaded or being downloaded. After |
| 289 | processing, leaves behind a marker to indicate to future invocations that |
| 290 | the artifact has already been staged based on the name of the artifact. |
| 291 | |
| 292 | Do not override as it modifies important private variables, ensures thread |
| 293 | safety, and maintains cache semantics. |
| 294 | |
| 295 | Note: this may be a blocking call when the artifact is already in the |
| 296 | process of being staged. |
| 297 | |
| 298 | Args: |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 299 | downloader: A downloader instance containing the logic to download |
| 300 | artifacts. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 301 | no_wait: If True, don't block waiting for artifact to exist if we fail to |
| 302 | immediately find it. |
| 303 | |
| 304 | Raises: |
| 305 | ArtifactDownloadError: If the artifact fails to download from Google |
| 306 | Storage for any reason or that the regexp |
| 307 | defined by name is not specific enough. |
| 308 | """ |
| 309 | if not self._process_lock: |
| 310 | self._process_lock = _build_artifact_locks.lock( |
| 311 | os.path.join(self.install_dir, self.name)) |
| 312 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 313 | real_install_dir = os.path.join(self.install_dir, self.install_subdir) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 314 | with self._process_lock: |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 315 | common_util.MkDirP(real_install_dir) |
Dan Shi | f8eb0d1 | 2013-08-01 17:52:06 -0700 | [diff] [blame] | 316 | if not self.ArtifactStaged(): |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 317 | # Delete any existing exception saved for this artifact. |
| 318 | self._ClearException() |
| 319 | found_artifact = False |
| 320 | if self.optional_name: |
| 321 | try: |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 322 | # Check if the artifact named |optional_name| exists. |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 323 | # Because this artifact may not always exist, don't bother |
| 324 | # to wait for it (set timeout=1). |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 325 | new_names = downloader.Wait( |
| 326 | self.optional_name, self.is_regex_name, timeout=1) |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 327 | self._UpdateName(new_names) |
| 328 | |
| 329 | except ArtifactDownloadError: |
| 330 | self._Log('Unable to download %s; fall back to download %s', |
| 331 | self.optional_name, self.name) |
| 332 | else: |
| 333 | found_artifact = True |
| 334 | |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 335 | try: |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 336 | # If the artifact should already have been uploaded, don't waste |
| 337 | # cycles waiting around for it to exist. |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 338 | if not found_artifact: |
| 339 | timeout = 1 if no_wait else 10 |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 340 | new_names = downloader.Wait( |
| 341 | self.name, self.is_regex_name, timeout) |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 342 | self._UpdateName(new_names) |
| 343 | |
David Riley | e131a0f | 2017-11-02 10:42:34 -0700 | [diff] [blame] | 344 | |
| 345 | files = self.name if isinstance(self.name, list) else [self.name] |
| 346 | for filename in files: |
| 347 | self._Log('Downloading file %s', filename) |
| 348 | self.install_path = downloader.Fetch(filename, real_install_dir) |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 349 | self._Setup() |
| 350 | self._MarkArtifactStaged() |
| 351 | except Exception as e: |
| 352 | # Save the exception to a file for downloader.IsStaged to retrieve. |
| 353 | self._SaveException(e) |
Gilad Arnold | 02dc655 | 2013-11-14 11:27:54 -0800 | [diff] [blame] | 354 | |
| 355 | # Convert an unknown exception into an ArtifactDownloadError. |
| 356 | if type(e) is ArtifactDownloadError: |
| 357 | raise |
| 358 | else: |
| 359 | raise ArtifactDownloadError('An error occurred: %s' % e) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 360 | else: |
| 361 | self._Log('%s is already staged.', self) |
| 362 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 363 | def __str__(self): |
| 364 | """String representation for the download.""" |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 365 | return '%s->%s' % (self.name, self.install_dir) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 366 | |
Chris Sosa | b26b120 | 2013-08-16 16:40:55 -0700 | [diff] [blame] | 367 | def __repr__(self): |
| 368 | return str(self) |
| 369 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 370 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 371 | class AUTestPayload(Artifact): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 372 | """Wrapper for AUTest delta payloads which need additional setup.""" |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 373 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 374 | def _Setup(self): |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 375 | super(AUTestPayload, self)._Setup() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 376 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 377 | # Rename to update.gz. |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 378 | install_path = os.path.join(self.install_dir, self.install_subdir, |
| 379 | self.name) |
| 380 | new_install_path = os.path.join(self.install_dir, self.install_subdir, |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 381 | devserver_constants.UPDATE_FILE) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 382 | shutil.move(install_path, new_install_path) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 383 | |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 384 | # Reflect the rename in the list of installed files. |
| 385 | self.installed_files.remove(install_path) |
| 386 | self.installed_files = [new_install_path] |
| 387 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 388 | |
David Riley | e131a0f | 2017-11-02 10:42:34 -0700 | [diff] [blame] | 389 | class MultiArtifact(Artifact): |
| 390 | """Wrapper for artifacts where name matches multiple items..""" |
| 391 | |
| 392 | def __init__(self, *args, **kwargs): |
| 393 | """Takes Artifact args. |
| 394 | |
| 395 | Args: |
| 396 | *args: See Artifact documentation. |
| 397 | **kwargs: See Artifact documentation. |
| 398 | """ |
| 399 | super(MultiArtifact, self).__init__(*args, **kwargs) |
| 400 | self.single_name = False |
| 401 | |
| 402 | def _UpdateName(self, names): |
| 403 | self.name = names if isinstance(names, list) else [names] |
| 404 | |
| 405 | def _Setup(self): |
| 406 | super(MultiArtifact, self)._Setup() |
| 407 | |
| 408 | self.installed_files = [os.path.join(self.install_dir, self.install_subdir, |
| 409 | name) for name in self.name] |
| 410 | |
| 411 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 412 | class DeltaPayloadBase(AUTestPayload): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 413 | """Delta payloads from the archive_url. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 414 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 415 | These artifacts are super strange. They custom handle directories and |
| 416 | pull in all delta payloads. We can't specify exactly what we want |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 417 | because unlike other artifacts, this one does not conform to something a |
| 418 | client might know. The client doesn't know the version of n-1 or whether it |
| 419 | was even generated. |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 420 | |
| 421 | IMPORTANT! Note that this artifact simply ignores the `name' argument because |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 422 | that name is derived internally. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 423 | """ |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 424 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 425 | def _Setup(self): |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 426 | super(DeltaPayloadBase, self)._Setup() |
| 427 | # Setup symlink so that AU will work for this payload. |
| 428 | stateful_update_symlink = os.path.join( |
| 429 | self.install_dir, self.install_subdir, |
| 430 | devserver_constants.STATEFUL_FILE) |
| 431 | os.symlink(os.path.join(os.pardir, os.pardir, |
| 432 | devserver_constants.STATEFUL_FILE), |
| 433 | stateful_update_symlink) |
| 434 | self.installed_files.append(stateful_update_symlink) |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 435 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 436 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 437 | class BundledArtifact(Artifact): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 438 | """A single build artifact bundle e.g. zip file or tar file.""" |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 439 | |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 440 | def __init__(self, *args, **kwargs): |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 441 | """Takes Artifact args with some additional ones. |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 442 | |
| 443 | Args: |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 444 | *args: See Artifact documentation. |
| 445 | **kwargs: See Artifact documentation. |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 446 | files_to_extract: A list of files to extract. If set to None, extract |
| 447 | all files. |
| 448 | exclude: A list of files to exclude. If None, no files are excluded. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 449 | """ |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 450 | self._files_to_extract = kwargs.pop('files_to_extract', None) |
| 451 | self._exclude = kwargs.pop('exclude', None) |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 452 | super(BundledArtifact, self).__init__(*args, **kwargs) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 453 | |
| 454 | # We modify the marker so that it is unique to what was staged. |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 455 | if self._files_to_extract: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 456 | self.marker_name = self._SanitizeName( |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 457 | '_'.join(['.' + self.name] + self._files_to_extract)) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 458 | |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 459 | def _RunUnzip(self, list_only): |
| 460 | # Unzip is weird. It expects its args before any excludes and expects its |
| 461 | # excludes in a list following the -x. |
| 462 | cmd = ['unzip', '-qql' if list_only else '-o', self.install_path] |
| 463 | if not list_only: |
| 464 | cmd += ['-d', self.install_dir] |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 465 | |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 466 | if self._files_to_extract: |
| 467 | cmd.extend(self._files_to_extract) |
| 468 | |
| 469 | if self._exclude: |
| 470 | cmd.append('-x') |
| 471 | cmd.extend(self._exclude) |
| 472 | |
| 473 | try: |
| 474 | return subprocess.check_output(cmd).strip('\n').splitlines() |
| 475 | except subprocess.CalledProcessError, e: |
| 476 | raise ArtifactDownloadError( |
| 477 | 'An error occurred when attempting to unzip %s:\n%s' % |
| 478 | (self.install_path, e)) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 479 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 480 | def _Setup(self): |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 481 | extract_result = self._Extract() |
| 482 | if self.store_installed_files: |
| 483 | # List both the archive and the extracted files. |
| 484 | self.installed_files.append(self.install_path) |
| 485 | self.installed_files.extend(extract_result) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 486 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 487 | def _Extract(self): |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 488 | """Extracts files into the install path.""" |
| 489 | if self.name.endswith('.zip'): |
| 490 | return self._ExtractZipfile() |
| 491 | else: |
| 492 | return self._ExtractTarball() |
| 493 | |
| 494 | def _ExtractZipfile(self): |
| 495 | """Extracts a zip file using unzip.""" |
| 496 | file_list = [os.path.join(self.install_dir, line[30:].strip()) |
| 497 | for line in self._RunUnzip(True) |
| 498 | if not line.endswith('/')] |
| 499 | if file_list: |
| 500 | self._RunUnzip(False) |
| 501 | |
| 502 | return file_list |
| 503 | |
| 504 | def _ExtractTarball(self): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 505 | """Extracts a tarball using tar. |
| 506 | |
| 507 | Detects whether the tarball is compressed or not based on the file |
| 508 | extension and extracts the tarball into the install_path. |
| 509 | """ |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 510 | try: |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 511 | return common_util.ExtractTarball(self.install_path, self.install_dir, |
| 512 | files_to_extract=self._files_to_extract, |
| 513 | excluded_files=self._exclude, |
| 514 | return_extracted_files=True) |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 515 | except common_util.CommonUtilError as e: |
| 516 | raise ArtifactDownloadError(str(e)) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 517 | |
| 518 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 519 | class AutotestTarball(BundledArtifact): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 520 | """Wrapper around the autotest tarball to download from gsutil.""" |
| 521 | |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 522 | def __init__(self, *args, **kwargs): |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 523 | super(AutotestTarball, self).__init__(*args, **kwargs) |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 524 | # We don't store/check explicit file lists in Autotest tarball markers; |
| 525 | # this can get huge and unwieldy, and generally make little sense. |
| 526 | self.store_installed_files = False |
| 527 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 528 | def _Setup(self): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 529 | """Extracts the tarball into the install path excluding test suites.""" |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 530 | super(AutotestTarball, self)._Setup() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 531 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 532 | # Deal with older autotest packages that may not be bundled. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 533 | autotest_dir = os.path.join(self.install_dir, |
| 534 | devserver_constants.AUTOTEST_DIR) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 535 | autotest_pkgs_dir = os.path.join(autotest_dir, 'packages') |
| 536 | if not os.path.exists(autotest_pkgs_dir): |
| 537 | os.makedirs(autotest_pkgs_dir) |
| 538 | |
| 539 | if not os.path.exists(os.path.join(autotest_pkgs_dir, 'packages.checksum')): |
Prashant Malani | 20e8371 | 2017-02-28 01:30:41 -0800 | [diff] [blame] | 540 | cmd = ['autotest/utils/packager.py', '--action=upload', '--repository', |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 541 | autotest_pkgs_dir, '--all'] |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 542 | try: |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 543 | subprocess.check_call(cmd, cwd=self.install_dir) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 544 | except subprocess.CalledProcessError, e: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 545 | raise ArtifactDownloadError( |
| 546 | 'Failed to create autotest packages!:\n%s' % e) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 547 | else: |
Gilad Arnold | f584313 | 2012-09-25 00:31:20 -0700 | [diff] [blame] | 548 | self._Log('Using pre-generated packages from autotest') |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 549 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 550 | |
Luis Hector Chavez | dca9dd7 | 2018-06-12 12:56:30 -0700 | [diff] [blame] | 551 | class SignedArtifact(Artifact): |
| 552 | """Wrapper for signed artifacts which need a path translation.""" |
| 553 | |
| 554 | def _Setup(self): |
| 555 | super(SignedArtifact, self)._Setup() |
| 556 | |
| 557 | # Rename to signed_image.bin. |
| 558 | install_path = os.path.join(self.install_dir, self.install_subdir, |
| 559 | self.name) |
| 560 | new_install_path = os.path.join(self.install_dir, self.install_subdir, |
| 561 | devserver_constants.SIGNED_IMAGE_FILE) |
| 562 | shutil.move(install_path, new_install_path) |
| 563 | |
| 564 | # Reflect the rename in the list of installed files. |
| 565 | self.installed_files.remove(install_path) |
| 566 | self.installed_files = [new_install_path] |
| 567 | |
| 568 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 569 | def _CreateNewArtifact(tag, base, name, *fixed_args, **fixed_kwargs): |
| 570 | """Get a data wrapper that describes an artifact's implementation. |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 571 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 572 | Args: |
| 573 | tag: Tag of the artifact, defined in artifact_info. |
| 574 | base: Class of the artifact, e.g., BundledArtifact. |
| 575 | name: Name of the artifact, e.g., image.zip. |
| 576 | *fixed_args: Fixed arguments that are additional to the one used in base |
| 577 | class. |
| 578 | **fixed_kwargs: Fixed keyword arguments that are additional to the one used |
| 579 | in base class. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 580 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 581 | Returns: |
| 582 | A data wrapper that describes an artifact's implementation. |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 583 | """ |
Don Garrett | fb15e32 | 2016-06-21 19:12:08 -0700 | [diff] [blame] | 584 | # pylint: disable=super-on-old-class |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 585 | class NewArtifact(base): |
| 586 | """A data wrapper that describes an artifact's implementation.""" |
| 587 | ARTIFACT_TAG = tag |
| 588 | ARTIFACT_NAME = name |
| 589 | |
| 590 | def __init__(self, *args, **kwargs): |
| 591 | all_args = fixed_args + args |
| 592 | all_kwargs = {} |
| 593 | all_kwargs.update(fixed_kwargs) |
| 594 | all_kwargs.update(kwargs) |
| 595 | super(NewArtifact, self).__init__(self.ARTIFACT_NAME, |
| 596 | *all_args, **all_kwargs) |
| 597 | |
| 598 | NewArtifact.__name__ = base.__name__ |
| 599 | return NewArtifact |
Chris Sosa | 968a106 | 2013-08-02 17:42:50 -0700 | [diff] [blame] | 600 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 601 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 602 | # TODO(dshi): Refactor the code here to split out the logic of creating the |
| 603 | # artifacts mapping to a different module. |
| 604 | chromeos_artifact_map = {} |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 605 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 606 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 607 | def _AddCrOSArtifact(tag, base, name, *fixed_args, **fixed_kwargs): |
Don Garrett | fb15e32 | 2016-06-21 19:12:08 -0700 | [diff] [blame] | 608 | """Add a data wrapper for ChromeOS artifacts. |
| 609 | |
| 610 | Add a data wrapper that describes a ChromeOS artifact's implementation to |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 611 | chromeos_artifact_map. |
| 612 | """ |
| 613 | artifact = _CreateNewArtifact(tag, base, name, *fixed_args, **fixed_kwargs) |
| 614 | chromeos_artifact_map.setdefault(tag, []).append(artifact) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 615 | |
beeps | c3d0f87 | 2013-07-31 21:50:40 -0700 | [diff] [blame] | 616 | |
Amin Hassani | e9596af | 2019-02-13 13:48:30 -0800 | [diff] [blame] | 617 | _AddCrOSArtifact(artifact_info.FULL_PAYLOAD, AUTestPayload, '*_full_*bin') |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 618 | |
| 619 | |
| 620 | class DeltaPayloadNtoN(DeltaPayloadBase): |
| 621 | """ChromeOS Delta payload artifact for updating from version N to N.""" |
| 622 | ARTIFACT_TAG = artifact_info.DELTA_PAYLOADS |
| 623 | ARTIFACT_NAME = 'NOT_APPLICABLE' |
| 624 | |
| 625 | def __init__(self, install_dir, build, *args, **kwargs): |
| 626 | name = 'chromeos_%s*_delta_*' % build |
| 627 | install_subdir = os.path.join(_AU_BASE, build + _NTON_DIR_SUFFIX) |
| 628 | super(DeltaPayloadNtoN, self).__init__(name, install_dir, build, *args, |
| 629 | install_subdir=install_subdir, |
| 630 | **kwargs) |
| 631 | |
| 632 | |
| 633 | class DeltaPayloadMtoN(DeltaPayloadBase): |
| 634 | """ChromeOS Delta payload artifact for updating from version M to N.""" |
| 635 | ARTIFACT_TAG = artifact_info.DELTA_PAYLOADS |
| 636 | ARTIFACT_NAME = 'NOT_APPLICABLE' |
| 637 | |
| 638 | def __init__(self, install_dir, build, *args, **kwargs): |
| 639 | name = ('chromeos_(?!%s).*_delta_.*' % re.escape(build)) |
| 640 | install_subdir = os.path.join(_AU_BASE, build + _MTON_DIR_SUFFIX) |
| 641 | super(DeltaPayloadMtoN, self).__init__(name, install_dir, build, *args, |
| 642 | install_subdir=install_subdir, |
| 643 | is_regex_name=True, **kwargs) |
| 644 | |
| 645 | |
| 646 | chromeos_artifact_map[artifact_info.DELTA_PAYLOADS] = [DeltaPayloadNtoN, |
| 647 | DeltaPayloadMtoN] |
| 648 | |
| 649 | |
| 650 | _AddCrOSArtifact(artifact_info.STATEFUL_PAYLOAD, Artifact, |
| 651 | devserver_constants.STATEFUL_FILE) |
| 652 | _AddCrOSArtifact(artifact_info.BASE_IMAGE, BundledArtifact, IMAGE_FILE, |
| 653 | optional_name=BASE_IMAGE_FILE, |
| 654 | files_to_extract=[devserver_constants.BASE_IMAGE_FILE]) |
| 655 | _AddCrOSArtifact(artifact_info.RECOVERY_IMAGE, BundledArtifact, IMAGE_FILE, |
| 656 | optional_name=RECOVERY_IMAGE_FILE, |
| 657 | files_to_extract=[devserver_constants.RECOVERY_IMAGE_FILE]) |
Nick Sanders | a995c9d | 2019-05-07 17:45:43 -0700 | [diff] [blame] | 658 | _AddCrOSArtifact(artifact_info.SIGNED_IMAGE, SignedArtifact, |
| 659 | SIGNED_RECOVERY_IMAGE_FILE) |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 660 | _AddCrOSArtifact(artifact_info.DEV_IMAGE, BundledArtifact, IMAGE_FILE, |
| 661 | files_to_extract=[devserver_constants.IMAGE_FILE]) |
| 662 | _AddCrOSArtifact(artifact_info.TEST_IMAGE, BundledArtifact, IMAGE_FILE, |
| 663 | optional_name=TEST_IMAGE_FILE, |
| 664 | files_to_extract=[devserver_constants.TEST_IMAGE_FILE]) |
| 665 | _AddCrOSArtifact(artifact_info.AUTOTEST, AutotestTarball, AUTOTEST_FILE, |
| 666 | files_to_extract=None, exclude=['autotest/test_suites']) |
| 667 | _AddCrOSArtifact(artifact_info.CONTROL_FILES, BundledArtifact, |
| 668 | CONTROL_FILES_FILE) |
| 669 | _AddCrOSArtifact(artifact_info.AUTOTEST_PACKAGES, AutotestTarball, |
| 670 | AUTOTEST_PACKAGES_FILE) |
| 671 | _AddCrOSArtifact(artifact_info.TEST_SUITES, BundledArtifact, TEST_SUITES_FILE) |
| 672 | _AddCrOSArtifact(artifact_info.AU_SUITE, BundledArtifact, AU_SUITE_FILE) |
| 673 | _AddCrOSArtifact(artifact_info.AUTOTEST_SERVER_PACKAGE, Artifact, |
| 674 | AUTOTEST_SERVER_PACKAGE_FILE) |
| 675 | _AddCrOSArtifact(artifact_info.FIRMWARE, Artifact, FIRMWARE_FILE) |
| 676 | _AddCrOSArtifact(artifact_info.SYMBOLS, BundledArtifact, DEBUG_SYMBOLS_FILE, |
| 677 | files_to_extract=['debug/breakpad']) |
Dan Shi | 55d0f97 | 2016-10-04 11:45:00 -0700 | [diff] [blame] | 678 | _AddCrOSArtifact(artifact_info.SYMBOLS_ONLY, BundledArtifact, |
| 679 | DEBUG_SYMBOLS_ONLY_FILE, |
| 680 | files_to_extract=['debug/breakpad']) |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 681 | _AddCrOSArtifact(artifact_info.FACTORY_IMAGE, BundledArtifact, FACTORY_FILE, |
| 682 | files_to_extract=[devserver_constants.FACTORY_IMAGE_FILE]) |
Mike Frysinger | a0e6a28 | 2016-09-01 17:29:08 -0400 | [diff] [blame] | 683 | _AddCrOSArtifact(artifact_info.FACTORY_SHIM_IMAGE, BundledArtifact, |
| 684 | FACTORY_SHIM_FILE, |
| 685 | files_to_extract=[devserver_constants.FACTORY_SHIM_IMAGE_FILE]) |
David Riley | 0655bab | 2017-11-02 10:44:26 -0700 | [diff] [blame] | 686 | _AddCrOSArtifact(artifact_info.QUICK_PROVISION, MultiArtifact, |
| 687 | QUICK_PROVISION_FILE) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 688 | |
Chris Sosa | 968a106 | 2013-08-02 17:42:50 -0700 | [diff] [blame] | 689 | # Add all the paygen_au artifacts in one go. |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 690 | for c in devserver_constants.CHANNELS: |
| 691 | _AddCrOSArtifact(artifact_info.PAYGEN_AU_SUITE_TEMPLATE % {'channel': c}, |
| 692 | BundledArtifact, |
| 693 | PAYGEN_AU_SUITE_FILE_TEMPLATE % {'channel': c}) |
| 694 | |
Justin Giorgi | b759052 | 2017-02-07 13:36:24 -0800 | [diff] [blame] | 695 | #### Libiota Artifacts #### |
| 696 | _AddCrOSArtifact(artifact_info.LIBIOTA_TEST_BINARIES, Artifact, |
| 697 | LIBIOTA_TEST_BINARIES_FILE) |
| 698 | _AddCrOSArtifact(artifact_info.LIBIOTA_BOARD_UTILS, Artifact, |
| 699 | LIBIOTA_BOARD_UTILS_FILE) |
| 700 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 701 | android_artifact_map = {} |
Chris Sosa | 968a106 | 2013-08-02 17:42:50 -0700 | [diff] [blame] | 702 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 703 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 704 | def _AddAndroidArtifact(tag, base, name, *fixed_args, **fixed_kwargs): |
Don Garrett | fb15e32 | 2016-06-21 19:12:08 -0700 | [diff] [blame] | 705 | """Add a data wrapper for android artifacts. |
| 706 | |
| 707 | Add a data wrapper that describes an Android artifact's implementation to |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 708 | android_artifact_map. |
| 709 | """ |
| 710 | artifact = _CreateNewArtifact(tag, base, name, *fixed_args, **fixed_kwargs) |
| 711 | android_artifact_map.setdefault(tag, []).append(artifact) |
| 712 | |
| 713 | |
Dan Shi | ba4e00f | 2015-10-27 12:03:53 -0700 | [diff] [blame] | 714 | _AddAndroidArtifact(artifact_info.ANDROID_ZIP_IMAGES, Artifact, |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 715 | ANDROID_IMAGE_ZIP, is_regex_name=True) |
| 716 | _AddAndroidArtifact(artifact_info.ANDROID_RADIO_IMAGE, Artifact, |
| 717 | ANDROID_RADIO_IMAGE) |
| 718 | _AddAndroidArtifact(artifact_info.ANDROID_BOOTLOADER_IMAGE, Artifact, |
| 719 | ANDROID_BOOTLOADER_IMAGE) |
| 720 | _AddAndroidArtifact(artifact_info.ANDROID_FASTBOOT, Artifact, ANDROID_FASTBOOT) |
| 721 | _AddAndroidArtifact(artifact_info.ANDROID_TEST_ZIP, BundledArtifact, |
| 722 | ANDROID_TEST_ZIP, is_regex_name=True) |
Dan Shi | 74136ae | 2015-12-01 14:40:06 -0800 | [diff] [blame] | 723 | _AddAndroidArtifact(artifact_info.ANDROID_VENDOR_PARTITION_ZIP, Artifact, |
| 724 | ANDROID_VENDOR_PARTITION_ZIP, is_regex_name=True) |
Dan Shi | 6c2b2a2 | 2016-03-04 15:52:19 -0800 | [diff] [blame] | 725 | _AddAndroidArtifact(artifact_info.AUTOTEST_SERVER_PACKAGE, Artifact, |
| 726 | ANDROID_AUTOTEST_SERVER_PACKAGE, is_regex_name=True) |
| 727 | _AddAndroidArtifact(artifact_info.TEST_SUITES, BundledArtifact, |
| 728 | ANDROID_TEST_SUITES, is_regex_name=True) |
| 729 | _AddAndroidArtifact(artifact_info.CONTROL_FILES, BundledArtifact, |
| 730 | ANDROID_CONTROL_FILES, is_regex_name=True) |
Simran Basi | 05be721 | 2016-03-16 13:08:23 -0700 | [diff] [blame] | 731 | _AddAndroidArtifact(artifact_info.ANDROID_NATIVETESTS_ZIP, BundledArtifact, |
| 732 | ANDROID_NATIVETESTS_FILE, is_regex_name=True) |
Ralph Nathan | 6c7fe5e | 2016-06-22 15:50:10 -0700 | [diff] [blame] | 733 | _AddAndroidArtifact(artifact_info.ANDROID_CONTINUOUS_NATIVE_TESTS_ZIP, |
| 734 | BundledArtifact, |
| 735 | ANDROID_CONTINUOUS_NATIVE_TESTS_FILE, |
| 736 | is_regex_name=True) |
Justin Giorgi | bb32ac4 | 2016-08-04 10:34:35 -0700 | [diff] [blame] | 737 | _AddAndroidArtifact(artifact_info.ANDROID_CONTINUOUS_INSTRUMENTATION_TESTS_ZIP, |
| 738 | BundledArtifact, |
| 739 | ANDROID_CONTINUOUS_INSTRUMENTATION_TESTS_FILE, |
| 740 | is_regex_name=True) |
Justin Giorgi | 4faa890 | 2016-08-19 19:54:30 -0700 | [diff] [blame] | 741 | _AddAndroidArtifact(artifact_info.ANDROID_CTS_ZIP, BundledArtifact, |
| 742 | ANDROID_CTS_FILE) |
Justin Giorgi | 576c154 | 2016-06-24 08:24:20 -0700 | [diff] [blame] | 743 | _AddAndroidArtifact(artifact_info.ANDROID_TARGET_FILES_ZIP, Artifact, |
| 744 | ANDROID_TARGET_FILES_ZIP, is_regex_name=True) |
| 745 | _AddAndroidArtifact(artifact_info.ANDROID_DTB_ZIP, Artifact, |
| 746 | ANDROID_DTB_ZIP, is_regex_name=True) |
Satoshi Niwa | 5e3ed5e | 2018-06-25 16:03:33 +0900 | [diff] [blame] | 747 | _AddAndroidArtifact(artifact_info.ANDROID_PUSH_TO_DEVICE_ZIP, |
| 748 | Artifact, ANDROID_PUSH_TO_DEVICE_ZIP) |
Qijiang Fan | 31beb7f | 2018-06-26 17:09:00 +0900 | [diff] [blame] | 749 | _AddAndroidArtifact(artifact_info.ANDROID_SEPOLICY_ZIP, |
| 750 | Artifact, ANDROID_SEPOLICY_ZIP) |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 751 | |
| 752 | class BaseArtifactFactory(object): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 753 | """A factory class that generates build artifacts from artifact names.""" |
| 754 | |
Dan Shi | 6c2b2a2 | 2016-03-04 15:52:19 -0800 | [diff] [blame] | 755 | def __init__(self, artifact_map, download_dir, artifacts, files, build, |
| 756 | requested_to_optional_map): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 757 | """Initalizes the member variables for the factory. |
| 758 | |
| 759 | Args: |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 760 | artifact_map: A map from artifact names to ImplDescription objects. |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 761 | download_dir: A directory to which artifacts are downloaded. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 762 | artifacts: List of artifacts to stage. These artifacts must be |
| 763 | defined in artifact_info.py and have a mapping in the |
| 764 | ARTIFACT_IMPLEMENTATION_MAP. |
| 765 | files: List of files to stage. These files are just downloaded and staged |
| 766 | as files into the download_dir. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 767 | build: The name of the build. |
Dan Shi | 6c2b2a2 | 2016-03-04 15:52:19 -0800 | [diff] [blame] | 768 | requested_to_optional_map: A map between an artifact X to a list of |
| 769 | artifacts Y. If X is requested, all items in Y should also get |
| 770 | triggered for download. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 771 | """ |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 772 | self.artifact_map = artifact_map |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 773 | self.download_dir = download_dir |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 774 | self.artifacts = artifacts |
| 775 | self.files = files |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 776 | self.build = build |
Dan Shi | 6c2b2a2 | 2016-03-04 15:52:19 -0800 | [diff] [blame] | 777 | self.requested_to_optional_map = requested_to_optional_map |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 778 | |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 779 | def _Artifacts(self, names, is_artifact): |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 780 | """Returns the Artifacts from |names|. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 781 | |
| 782 | If is_artifact is true, then these names define artifacts that must exist in |
| 783 | the ARTIFACT_IMPLEMENTATION_MAP. Otherwise, treat as filenames to stage as |
| 784 | basic BuildArtifacts. |
| 785 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 786 | Args: |
| 787 | names: A sequence of artifact names. |
| 788 | is_artifact: Whether this is a named (True) or file (False) artifact. |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 789 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 790 | Returns: |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 791 | An iterable of Artifacts. |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 792 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 793 | Raises: |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 794 | KeyError: if artifact doesn't exist in the artifact map. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 795 | """ |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 796 | if is_artifact: |
| 797 | classes = itertools.chain(*(self.artifact_map[name] for name in names)) |
| 798 | return list(cls(self.download_dir, self.build) for cls in classes) |
| 799 | else: |
| 800 | return list(Artifact(name, self.download_dir, self.build) |
| 801 | for name in names) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 802 | |
| 803 | def RequiredArtifacts(self): |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 804 | """Returns BuildArtifacts for the factory's artifacts. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 805 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 806 | Returns: |
| 807 | An iterable of BuildArtifacts. |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 808 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 809 | Raises: |
| 810 | KeyError: if artifact doesn't exist in ARTIFACT_IMPLEMENTATION_MAP. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 811 | """ |
| 812 | artifacts = [] |
| 813 | if self.artifacts: |
| 814 | artifacts.extend(self._Artifacts(self.artifacts, True)) |
| 815 | if self.files: |
| 816 | artifacts.extend(self._Artifacts(self.files, False)) |
| 817 | |
| 818 | return artifacts |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 819 | |
| 820 | def OptionalArtifacts(self): |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 821 | """Returns BuildArtifacts that should be cached. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 822 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 823 | Returns: |
| 824 | An iterable of BuildArtifacts. |
Yu-Ju Hong | 5d5bf0d | 2014-02-11 21:38:20 -0800 | [diff] [blame] | 825 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 826 | Raises: |
| 827 | KeyError: if an optional artifact doesn't exist in |
| 828 | ARTIFACT_IMPLEMENTATION_MAP yet defined in |
| 829 | artifact_info.REQUESTED_TO_OPTIONAL_MAP. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 830 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 831 | optional_names = set() |
Dan Shi | 6c2b2a2 | 2016-03-04 15:52:19 -0800 | [diff] [blame] | 832 | for artifact_name, optional_list in self.requested_to_optional_map.items(): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 833 | # We are already downloading it. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 834 | if artifact_name in self.artifacts: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 835 | optional_names = optional_names.union(optional_list) |
| 836 | |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 837 | return self._Artifacts(optional_names - set(self.artifacts), True) |
Chris Sosa | 968a106 | 2013-08-02 17:42:50 -0700 | [diff] [blame] | 838 | |
| 839 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 840 | class ChromeOSArtifactFactory(BaseArtifactFactory): |
| 841 | """A factory class that generates ChromeOS build artifacts from names.""" |
| 842 | |
| 843 | def __init__(self, download_dir, artifacts, files, build): |
| 844 | """Pass the ChromeOS artifact map to the base class.""" |
| 845 | super(ChromeOSArtifactFactory, self).__init__( |
Dan Shi | 6c2b2a2 | 2016-03-04 15:52:19 -0800 | [diff] [blame] | 846 | chromeos_artifact_map, download_dir, artifacts, files, build, |
| 847 | artifact_info.CROS_REQUESTED_TO_OPTIONAL_MAP) |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 848 | |
| 849 | |
| 850 | class AndroidArtifactFactory(BaseArtifactFactory): |
| 851 | """A factory class that generates Android build artifacts from names.""" |
| 852 | |
| 853 | def __init__(self, download_dir, artifacts, files, build): |
| 854 | """Pass the Android artifact map to the base class.""" |
| 855 | super(AndroidArtifactFactory, self).__init__( |
Dan Shi | 6c2b2a2 | 2016-03-04 15:52:19 -0800 | [diff] [blame] | 856 | android_artifact_map, download_dir, artifacts, files, build, |
| 857 | artifact_info.ANDROID_REQUESTED_TO_OPTIONAL_MAP) |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 858 | |
| 859 | |
Chris Sosa | 968a106 | 2013-08-02 17:42:50 -0700 | [diff] [blame] | 860 | # A simple main to verify correctness of the artifact map when making simple |
| 861 | # name changes. |
| 862 | if __name__ == '__main__': |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 863 | print('ARTIFACT IMPLEMENTATION MAPs (for debugging)') |
| 864 | print('FORMAT: ARTIFACT -> IMPLEMENTATION (<type>_file)') |
| 865 | for label, mapping in (('CHROMEOS', chromeos_artifact_map), |
| 866 | ('ANDROID', android_artifact_map)): |
| 867 | print('%s:' % label) |
| 868 | for key, value in sorted(mapping.items()): |
| 869 | print(' %s -> %s' % (key, ', '.join(str(val) for val in value))) |