Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Module containing classes that wrap artifact downloads.""" |
| 6 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 7 | import os |
| 8 | import shutil |
| 9 | import subprocess |
| 10 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 11 | import artifact_info |
| 12 | import common_util |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 13 | import devserver_constants |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 14 | import gsutil_util |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 15 | import log_util |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 16 | |
| 17 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 18 | _AU_BASE = 'au' |
| 19 | _NTON_DIR_SUFFIX = '_nton' |
| 20 | _MTON_DIR_SUFFIX = '_mton' |
| 21 | |
| 22 | ############ Actual filenames of artifacts in Google Storage ############ |
| 23 | |
| 24 | AU_SUITE_FILE = 'au_control.tar.bz2' |
| 25 | AUTOTEST_FILE = 'autotest.tar' |
| 26 | AUTOTEST_COMPRESSED_FILE = 'autotest.tar.bz2' |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 27 | BASE_IMAGE_FILE = 'chromiumos_base_image.bin' |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 28 | DEBUG_SYMBOLS_FILE = 'debug.tgz' |
| 29 | FIRMWARE_FILE = 'firmware_from_source.tar.bz2' |
| 30 | IMAGE_FILE = 'image.zip' |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 31 | RECOVERY_IMAGE_FILE = 'recovery_image.bin' |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 32 | STATEFUL_UPDATE_FILE = 'stateful.tgz' |
| 33 | TEST_IMAGE_FILE = 'chromiumos_test_image.bin' |
| 34 | TEST_SUITES_FILE = 'test_suites.tar.bz2' |
| 35 | |
| 36 | _build_artifact_locks = common_util.LockDict() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 37 | |
| 38 | |
| 39 | class ArtifactDownloadError(Exception): |
| 40 | """Error used to signify an issue processing an artifact.""" |
| 41 | pass |
| 42 | |
| 43 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 44 | class BuildArtifact(log_util.Loggable): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 45 | """Wrapper around an artifact to download from gsutil. |
| 46 | |
| 47 | The purpose of this class is to download objects from Google Storage |
| 48 | and install them to a local directory. There are two main functions, one to |
| 49 | download/prepare the artifacts in to a temporary staging area and the second |
| 50 | to stage it into its final destination. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 51 | |
| 52 | Class members: |
| 53 | archive_url = archive_url |
| 54 | name: Name given for artifact -- either a regexp or name of the artifact in |
| 55 | gs. If a regexp, is modified to actual name before call to _Download. |
| 56 | build: The version of the build i.e. R26-2342.0.0. |
| 57 | marker_name: Name used to define the lock marker for the artifacts to |
| 58 | prevent it from being re-downloaded. By default based on name |
| 59 | but can be overriden by children. |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 60 | install_path: Path to artifact. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 61 | install_dir: The final location where the artifact should be staged to. |
| 62 | single_name: If True the name given should only match one item. Note, if not |
| 63 | True, self.name will become a list of items returned. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 64 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 65 | def __init__(self, install_dir, archive_url, name, build): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 66 | """Args: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 67 | install_dir: Where to install the artifact. |
| 68 | archive_url: The Google Storage path to find the artifact. |
| 69 | name: Identifying name to be used to find/store the artifact. |
| 70 | build: The name of the build e.g. board/release. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 71 | """ |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 72 | super(BuildArtifact, self).__init__() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 73 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 74 | # In-memory lock to keep the devserver from colliding with itself while |
| 75 | # attempting to stage the same artifact. |
| 76 | self._process_lock = None |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 77 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 78 | self.archive_url = archive_url |
| 79 | self.name = name |
| 80 | self.build = build |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 81 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 82 | self.marker_name = '.' + self._SanitizeName(name) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 83 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 84 | self.install_path = None |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 85 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 86 | self.install_dir = install_dir |
| 87 | |
| 88 | self.single_name = True |
| 89 | |
| 90 | @staticmethod |
| 91 | def _SanitizeName(name): |
| 92 | """Sanitizes name to be used for creating a file on the filesystem. |
| 93 | |
| 94 | '.','/' and '*' have special meaning in FS lingo. Replace them with words. |
| 95 | """ |
| 96 | return name.replace('*', 'STAR').replace('.', 'DOT').replace('/', 'SLASH') |
| 97 | |
| 98 | def _ArtifactStaged(self): |
| 99 | """Returns True if artifact is already staged.""" |
| 100 | return os.path.exists(os.path.join(self.install_dir, self.marker_name)) |
| 101 | |
| 102 | def _MarkArtifactStaged(self): |
| 103 | """Marks the artifact as staged.""" |
| 104 | with open(os.path.join(self.install_dir, self.marker_name), 'w') as f: |
| 105 | f.write('') |
| 106 | |
| 107 | def _WaitForArtifactToExist(self, timeout): |
| 108 | """Waits for artifact to exist and sets self.name to appropriate name.""" |
| 109 | names = gsutil_util.GetGSNamesWithWait( |
| 110 | self.name, self.archive_url, str(self), single_item=self.single_name, |
| 111 | timeout=timeout) |
| 112 | if not names: |
| 113 | raise ArtifactDownloadError('Could not find %s in Google Storage' % |
| 114 | self.name) |
| 115 | |
| 116 | if self.single_name: |
| 117 | if len(names) > 1: |
| 118 | raise ArtifactDownloadError('Too many artifacts match %s' % self.name) |
| 119 | |
| 120 | self.name = names[0] |
| 121 | else: |
| 122 | self.name = names |
| 123 | |
| 124 | def _Download(self): |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 125 | """Downloads artifact from Google Storage to a local directory.""" |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 126 | gs_path = '/'.join([self.archive_url, self.name]) |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 127 | self.install_path = os.path.join(self.install_dir, self.name) |
| 128 | gsutil_util.DownloadFromGS(gs_path, self.install_path) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 129 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 130 | def _Setup(self): |
| 131 | """For tarball like artifacts, extracts and prepares contents.""" |
| 132 | pass |
| 133 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 134 | |
| 135 | def Process(self, no_wait): |
| 136 | """Main call point to all artifacts. Downloads and Stages artifact. |
| 137 | |
| 138 | Downloads and Stages artifact from Google Storage to the install directory |
| 139 | specified in the constructor. It multi-thread safe and does not overwrite |
| 140 | the artifact if it's already been downloaded or being downloaded. After |
| 141 | processing, leaves behind a marker to indicate to future invocations that |
| 142 | the artifact has already been staged based on the name of the artifact. |
| 143 | |
| 144 | Do not override as it modifies important private variables, ensures thread |
| 145 | safety, and maintains cache semantics. |
| 146 | |
| 147 | Note: this may be a blocking call when the artifact is already in the |
| 148 | process of being staged. |
| 149 | |
| 150 | Args: |
| 151 | no_wait: If True, don't block waiting for artifact to exist if we fail to |
| 152 | immediately find it. |
| 153 | |
| 154 | Raises: |
| 155 | ArtifactDownloadError: If the artifact fails to download from Google |
| 156 | Storage for any reason or that the regexp |
| 157 | defined by name is not specific enough. |
| 158 | """ |
| 159 | if not self._process_lock: |
| 160 | self._process_lock = _build_artifact_locks.lock( |
| 161 | os.path.join(self.install_dir, self.name)) |
| 162 | |
| 163 | with self._process_lock: |
| 164 | common_util.MkDirP(self.install_dir) |
| 165 | if not self._ArtifactStaged(): |
| 166 | # If the artifact should already have been uploaded, don't waste |
| 167 | # cycles waiting around for it to exist. |
| 168 | timeout = 1 if no_wait else 10 |
| 169 | self._WaitForArtifactToExist(timeout) |
| 170 | self._Download() |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 171 | self._Setup() |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 172 | self._MarkArtifactStaged() |
| 173 | else: |
| 174 | self._Log('%s is already staged.', self) |
| 175 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 176 | def __str__(self): |
| 177 | """String representation for the download.""" |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 178 | return '->'.join(['%s/%s' % (self.archive_url, self.name), |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 179 | self.install_dir]) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 180 | |
| 181 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 182 | class AUTestPayloadBuildArtifact(BuildArtifact): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 183 | """Wrapper for AUTest delta payloads which need additional setup.""" |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 184 | def _Setup(self): |
| 185 | super(AUTestPayloadBuildArtifact, self)._Setup() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 186 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 187 | # Rename to update.gz. |
| 188 | install_path = os.path.join(self.install_dir, self.name) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 189 | new_install_path = os.path.join(self.install_dir, |
| 190 | devserver_constants.ROOT_UPDATE_FILE) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 191 | shutil.move(install_path, new_install_path) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 192 | |
| 193 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 194 | # TODO(sosa): Change callers to make this artifact more sane. |
| 195 | class DeltaPayloadsArtifact(BuildArtifact): |
| 196 | """Delta payloads from the archive_url. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 197 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 198 | This artifact is super strange. It custom handles directories and |
| 199 | pulls in all delta payloads. We can't specify exactly what we want |
| 200 | because unlike other artifacts, this one does not conform to something a |
| 201 | client might know. The client doesn't know the version of n-1 or whether it |
| 202 | was even generated. |
| 203 | """ |
| 204 | def __init__(self, *args): |
| 205 | super(DeltaPayloadsArtifact, self).__init__(*args) |
| 206 | self.single_name = False # Expect multiple deltas |
| 207 | nton_name = 'chromeos_%s%s' % (self.build, self.name) |
| 208 | mton_name = 'chromeos_(?!%s)%s' % (self.build, self.name) |
| 209 | nton_install_dir = os.path.join(self.install_dir, _AU_BASE, |
| 210 | self.build + _NTON_DIR_SUFFIX) |
| 211 | mton_install_dir = os.path.join(self.install_dir, _AU_BASE, |
| 212 | self.build + _MTON_DIR_SUFFIX) |
| 213 | self._sub_artifacts = [ |
| 214 | AUTestPayloadBuildArtifact(mton_install_dir, self.archive_url, |
| 215 | mton_name, self.build), |
| 216 | AUTestPayloadBuildArtifact(nton_install_dir, self.archive_url, |
| 217 | nton_name, self.build)] |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 218 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 219 | def _Download(self): |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 220 | """With sub-artifacts we do everything in _Setup().""" |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 221 | pass |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 222 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 223 | def _Setup(self): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 224 | """Process each sub-artifact. Only error out if none can be found.""" |
| 225 | for artifact in self._sub_artifacts: |
| 226 | try: |
| 227 | artifact.Process(no_wait=True) |
| 228 | # Setup symlink so that AU will work for this payload. |
| 229 | os.symlink( |
| 230 | os.path.join(os.pardir, os.pardir, STATEFUL_UPDATE_FILE), |
| 231 | os.path.join(artifact.install_dir, STATEFUL_UPDATE_FILE)) |
| 232 | except ArtifactDownloadError as e: |
| 233 | self._Log('Could not process %s: %s', artifact, e) |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 234 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 235 | |
| 236 | class BundledBuildArtifact(BuildArtifact): |
| 237 | """A single build artifact bundle e.g. zip file or tar file.""" |
| 238 | def __init__(self, install_dir, archive_url, name, build, |
| 239 | files_to_extract=None, exclude=None): |
| 240 | """Takes BuildArtifacts are with two additional args. |
| 241 | |
| 242 | Additional args: |
| 243 | files_to_extract: A list of files to extract. If set to None, extract |
| 244 | all files. |
| 245 | exclude: A list of files to exclude. If None, no files are excluded. |
| 246 | """ |
| 247 | super(BundledBuildArtifact, self).__init__(install_dir, archive_url, name, |
| 248 | build) |
| 249 | self._files_to_extract = files_to_extract |
| 250 | self._exclude = exclude |
| 251 | |
| 252 | # We modify the marker so that it is unique to what was staged. |
| 253 | if files_to_extract: |
| 254 | self.marker_name = self._SanitizeName( |
| 255 | '_'.join(['.' + self.name] + files_to_extract)) |
| 256 | |
| 257 | def _Extract(self): |
| 258 | """Extracts the bundle into install_dir. Must be overridden. |
| 259 | |
| 260 | If set, uses files_to_extract to only extract those items. If set, use |
| 261 | exclude to exclude specific files. |
| 262 | """ |
| 263 | raise NotImplementedError() |
| 264 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 265 | def _Setup(self): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 266 | self._Extract() |
| 267 | |
| 268 | |
| 269 | class TarballBuildArtifact(BundledBuildArtifact): |
| 270 | """Artifact for tar and tarball files.""" |
| 271 | |
| 272 | def _Extract(self): |
| 273 | """Extracts a tarball using tar. |
| 274 | |
| 275 | Detects whether the tarball is compressed or not based on the file |
| 276 | extension and extracts the tarball into the install_path. |
| 277 | """ |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 278 | try: |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 279 | common_util.ExtractTarball(self.install_path, self.install_dir, |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 280 | files_to_extract=self._files_to_extract, |
| 281 | excluded_files=self._exclude) |
| 282 | except common_util.CommonUtilError as e: |
| 283 | raise ArtifactDownloadError(str(e)) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 284 | |
| 285 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 286 | class AutotestTarballBuildArtifact(TarballBuildArtifact): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 287 | """Wrapper around the autotest tarball to download from gsutil.""" |
| 288 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 289 | def _Setup(self): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 290 | """Extracts the tarball into the install path excluding test suites.""" |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 291 | super(AutotestTarballBuildArtifact, self)._Setup() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 292 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 293 | # Deal with older autotest packages that may not be bundled. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 294 | autotest_dir = os.path.join(self.install_dir, |
| 295 | devserver_constants.AUTOTEST_DIR) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 296 | autotest_pkgs_dir = os.path.join(autotest_dir, 'packages') |
| 297 | if not os.path.exists(autotest_pkgs_dir): |
| 298 | os.makedirs(autotest_pkgs_dir) |
| 299 | |
| 300 | if not os.path.exists(os.path.join(autotest_pkgs_dir, 'packages.checksum')): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 301 | cmd = ['autotest/utils/packager.py', 'upload', '--repository', |
| 302 | autotest_pkgs_dir, '--all'] |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 303 | try: |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 304 | subprocess.check_call(cmd, cwd=self.install_dir) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 305 | except subprocess.CalledProcessError, e: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 306 | raise ArtifactDownloadError( |
| 307 | 'Failed to create autotest packages!:\n%s' % e) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 308 | else: |
Gilad Arnold | f584313 | 2012-09-25 00:31:20 -0700 | [diff] [blame] | 309 | self._Log('Using pre-generated packages from autotest') |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 310 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 311 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 312 | class ZipfileBuildArtifact(BundledBuildArtifact): |
| 313 | """A downloadable artifact that is a zipfile.""" |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 314 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 315 | def _Extract(self): |
| 316 | """Extracts files into the install path.""" |
| 317 | # Unzip is weird. It expects its args before any excepts and expects its |
| 318 | # excepts in a list following the -x. |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 319 | cmd = ['unzip', '-o', self.install_path, '-d', self.install_dir] |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 320 | if self._files_to_extract: |
| 321 | cmd.extend(self._files_to_extract) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 322 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 323 | if self._exclude: |
| 324 | cmd.append('-x') |
| 325 | cmd.extend(self._exclude) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 326 | |
| 327 | try: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 328 | subprocess.check_call(cmd) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 329 | except subprocess.CalledProcessError, e: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 330 | raise ArtifactDownloadError( |
| 331 | 'An error occurred when attempting to unzip %s:\n%s' % |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 332 | (self.install_path, e)) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 333 | |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 334 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 335 | class ImplDescription(object): |
| 336 | """Data wrapper that describes an artifact's implementation.""" |
| 337 | def __init__(self, artifact_class, name, *additional_args): |
| 338 | """Constructor: |
| 339 | |
| 340 | Args: |
| 341 | artifact_class: BuildArtifact class to use for the artifact. |
| 342 | name: name to use to identify artifact (see BuildArtifact.name) |
| 343 | additional_args: If sub-class uses additional args, these are passed |
| 344 | through to them. |
| 345 | """ |
| 346 | self.artifact_class = artifact_class |
| 347 | self.name = name |
| 348 | self.additional_args = additional_args |
| 349 | |
| 350 | |
| 351 | # Maps artifact names to their implementation description. |
| 352 | # Please note, it is good practice to use constants for these names if you're |
| 353 | # going to re-use the names ANYWHERE else in the devserver code. |
| 354 | ARTIFACT_IMPLEMENTATION_MAP = { |
| 355 | artifact_info.FULL_PAYLOAD: |
| 356 | ImplDescription(AUTestPayloadBuildArtifact, '.*_full_.*'), |
| 357 | artifact_info.DELTA_PAYLOADS: |
| 358 | ImplDescription(DeltaPayloadsArtifact, '.*_delta_.*'), |
| 359 | artifact_info.STATEFUL_PAYLOAD: |
| 360 | ImplDescription(BuildArtifact, STATEFUL_UPDATE_FILE), |
| 361 | |
| 362 | artifact_info.BASE_IMAGE: |
| 363 | ImplDescription(ZipfileBuildArtifact, IMAGE_FILE, |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 364 | [BASE_IMAGE_FILE]), |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 365 | artifact_info.RECOVERY_IMAGE: |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 366 | ImplDescription(ZipfileBuildArtifact, IMAGE_FILE, [RECOVERY_IMAGE_FILE]), |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 367 | artifact_info.TEST_IMAGE: |
| 368 | ImplDescription(ZipfileBuildArtifact, IMAGE_FILE, [TEST_IMAGE_FILE]), |
| 369 | |
| 370 | artifact_info.AUTOTEST: |
| 371 | ImplDescription(AutotestTarballBuildArtifact, AUTOTEST_FILE, None, |
| 372 | ['autotest/test_suites']), |
| 373 | artifact_info.TEST_SUITES: |
| 374 | ImplDescription(TarballBuildArtifact, TEST_SUITES_FILE), |
| 375 | artifact_info.AU_SUITE: |
Chris Sosa | a56c403 | 2013-03-17 21:59:54 -0700 | [diff] [blame] | 376 | ImplDescription(TarballBuildArtifact, AU_SUITE_FILE), |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 377 | |
| 378 | artifact_info.FIRMWARE: |
| 379 | ImplDescription(BuildArtifact, FIRMWARE_FILE), |
| 380 | artifact_info.SYMBOLS: |
| 381 | ImplDescription(TarballBuildArtifact, DEBUG_SYMBOLS_FILE, |
| 382 | ['debug/breakpad']), |
| 383 | } |
| 384 | |
| 385 | |
| 386 | class ArtifactFactory(object): |
| 387 | """A factory class that generates build artifacts from artifact names.""" |
| 388 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 389 | def __init__(self, download_dir, archive_url, artifact_names, build): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 390 | """Initalizes the member variables for the factory. |
| 391 | |
| 392 | Args: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 393 | archive_url: the Google Storage url of the bucket where the debug |
| 394 | symbols for the desired build are stored. |
| 395 | artifact_names: List of artifact names to stage. |
| 396 | build: The name of the build. |
| 397 | """ |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 398 | self.download_dir = download_dir |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 399 | self.archive_url = archive_url |
| 400 | self.artifact_names = artifact_names |
| 401 | self.build = build |
| 402 | |
| 403 | @staticmethod |
| 404 | def _GetDescriptionComponents(artifact_name): |
| 405 | """Returns a tuple of for BuildArtifact class, name, and additional args.""" |
| 406 | description = ARTIFACT_IMPLEMENTATION_MAP[artifact_name] |
| 407 | return (description.artifact_class, description.name, |
| 408 | description.additional_args) |
| 409 | |
| 410 | def _Artifacts(self, artifact_names): |
| 411 | """Returns an iterable of BuildArtifacts from |artifact_names|.""" |
| 412 | artifacts = [] |
| 413 | for artifact_name in artifact_names: |
| 414 | artifact_class, path, args = self._GetDescriptionComponents( |
| 415 | artifact_name) |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame^] | 416 | artifacts.append(artifact_class(self.download_dir, self.archive_url, path, |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 417 | self.build, *args)) |
| 418 | |
| 419 | return artifacts |
| 420 | |
| 421 | def RequiredArtifacts(self): |
| 422 | """Returns an iterable of BuildArtifacts for the factory's artifacts.""" |
| 423 | return self._Artifacts(self.artifact_names) |
| 424 | |
| 425 | def OptionalArtifacts(self): |
| 426 | """Returns an iterable of BuildArtifacts that should be cached.""" |
| 427 | optional_names = set() |
| 428 | for artifact_name, optional_list in ( |
| 429 | artifact_info.REQUESTED_TO_OPTIONAL_MAP.iteritems()): |
| 430 | # We are already downloading it. |
| 431 | if artifact_name in self.artifact_names: |
| 432 | optional_names = optional_names.union(optional_list) |
| 433 | |
| 434 | return self._Artifacts(optional_names - set(self.artifact_names)) |