Chris Sosa | 968a106 | 2013-08-02 17:42:50 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
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 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 9 | import os |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 10 | import pickle |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 11 | import shutil |
| 12 | import subprocess |
| 13 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 14 | import artifact_info |
| 15 | import common_util |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 16 | import devserver_constants |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 17 | import gsutil_util |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 18 | import log_util |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 19 | |
| 20 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 21 | _AU_BASE = 'au' |
| 22 | _NTON_DIR_SUFFIX = '_nton' |
| 23 | _MTON_DIR_SUFFIX = '_mton' |
| 24 | |
| 25 | ############ Actual filenames of artifacts in Google Storage ############ |
| 26 | |
| 27 | AU_SUITE_FILE = 'au_control.tar.bz2' |
Chris Sosa | 968a106 | 2013-08-02 17:42:50 -0700 | [diff] [blame] | 28 | PAYGEN_AU_SUITE_FILE_TEMPLATE = 'paygen_au_%(channel)s_control.tar.bz2' |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 29 | AUTOTEST_FILE = 'autotest.tar' |
| 30 | AUTOTEST_COMPRESSED_FILE = 'autotest.tar.bz2' |
| 31 | DEBUG_SYMBOLS_FILE = 'debug.tgz' |
beeps | c3d0f87 | 2013-07-31 21:50:40 -0700 | [diff] [blame] | 32 | FACTORY_FILE = 'ChromeOS-factory.*zip' |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 33 | FIRMWARE_FILE = 'firmware_from_source.tar.bz2' |
| 34 | IMAGE_FILE = 'image.zip' |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 35 | TEST_SUITES_FILE = 'test_suites.tar.bz2' |
| 36 | |
| 37 | _build_artifact_locks = common_util.LockDict() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 38 | |
| 39 | |
| 40 | class ArtifactDownloadError(Exception): |
| 41 | """Error used to signify an issue processing an artifact.""" |
| 42 | pass |
| 43 | |
| 44 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 45 | class BuildArtifact(log_util.Loggable): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 46 | """Wrapper around an artifact to download from gsutil. |
| 47 | |
| 48 | The purpose of this class is to download objects from Google Storage |
| 49 | and install them to a local directory. There are two main functions, one to |
| 50 | download/prepare the artifacts in to a temporary staging area and the second |
| 51 | to stage it into its final destination. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 52 | |
| 53 | Class members: |
| 54 | archive_url = archive_url |
| 55 | name: Name given for artifact -- either a regexp or name of the artifact in |
| 56 | gs. If a regexp, is modified to actual name before call to _Download. |
| 57 | build: The version of the build i.e. R26-2342.0.0. |
| 58 | marker_name: Name used to define the lock marker for the artifacts to |
| 59 | prevent it from being re-downloaded. By default based on name |
| 60 | but can be overriden by children. |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 61 | exception_file_path: Path to a file containing the serialized exception, |
| 62 | which was raised in Process method. The file is located |
| 63 | in the parent folder of install_dir, since the |
| 64 | install_dir will be deleted if the build does not |
| 65 | existed. |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 66 | install_path: Path to artifact. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 67 | install_dir: The final location where the artifact should be staged to. |
| 68 | single_name: If True the name given should only match one item. Note, if not |
| 69 | True, self.name will become a list of items returned. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 70 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 71 | def __init__(self, install_dir, archive_url, name, build): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 72 | """Args: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 73 | install_dir: Where to install the artifact. |
| 74 | archive_url: The Google Storage path to find the artifact. |
| 75 | name: Identifying name to be used to find/store the artifact. |
| 76 | build: The name of the build e.g. board/release. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 77 | """ |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 78 | super(BuildArtifact, self).__init__() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 79 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 80 | # In-memory lock to keep the devserver from colliding with itself while |
| 81 | # attempting to stage the same artifact. |
| 82 | self._process_lock = None |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 83 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 84 | self.archive_url = archive_url |
| 85 | self.name = name |
| 86 | self.build = build |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 87 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 88 | self.marker_name = '.' + self._SanitizeName(name) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 89 | |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 90 | exception_file_name = ('.' + self._SanitizeName(build) + self.marker_name + |
| 91 | '.exception') |
| 92 | # The exception file needs to be located in parent folder, since the |
| 93 | # install_dir will be deleted is the build does not exist. |
| 94 | self.exception_file_path = os.path.join(os.path.dirname(install_dir), |
| 95 | exception_file_name) |
| 96 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 97 | self.install_path = None |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 98 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 99 | self.install_dir = install_dir |
| 100 | |
| 101 | self.single_name = True |
| 102 | |
| 103 | @staticmethod |
| 104 | def _SanitizeName(name): |
| 105 | """Sanitizes name to be used for creating a file on the filesystem. |
| 106 | |
| 107 | '.','/' and '*' have special meaning in FS lingo. Replace them with words. |
| 108 | """ |
| 109 | return name.replace('*', 'STAR').replace('.', 'DOT').replace('/', 'SLASH') |
| 110 | |
Dan Shi | f8eb0d1 | 2013-08-01 17:52:06 -0700 | [diff] [blame] | 111 | def ArtifactStaged(self): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 112 | """Returns True if artifact is already staged.""" |
| 113 | return os.path.exists(os.path.join(self.install_dir, self.marker_name)) |
| 114 | |
| 115 | def _MarkArtifactStaged(self): |
| 116 | """Marks the artifact as staged.""" |
| 117 | with open(os.path.join(self.install_dir, self.marker_name), 'w') as f: |
| 118 | f.write('') |
| 119 | |
Chris Sosa | c4e8784 | 2013-08-16 18:04:14 -0700 | [diff] [blame] | 120 | def WaitForArtifactToExist(self, timeout, update_name=True): |
| 121 | """Waits for artifact to exist and sets self.name to appropriate name. |
| 122 | |
| 123 | Args: |
| 124 | update_name: If False, don't actually update self.name. |
| 125 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 126 | names = gsutil_util.GetGSNamesWithWait( |
| 127 | self.name, self.archive_url, str(self), single_item=self.single_name, |
| 128 | timeout=timeout) |
| 129 | if not names: |
| 130 | raise ArtifactDownloadError('Could not find %s in Google Storage' % |
| 131 | self.name) |
| 132 | |
| 133 | if self.single_name: |
| 134 | if len(names) > 1: |
| 135 | raise ArtifactDownloadError('Too many artifacts match %s' % self.name) |
| 136 | |
Chris Sosa | c4e8784 | 2013-08-16 18:04:14 -0700 | [diff] [blame] | 137 | new_name = names[0] |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 138 | else: |
Chris Sosa | c4e8784 | 2013-08-16 18:04:14 -0700 | [diff] [blame] | 139 | new_name = names |
| 140 | |
| 141 | if update_name: |
| 142 | self.name = new_name |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 143 | |
| 144 | def _Download(self): |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 145 | """Downloads artifact from Google Storage to a local directory.""" |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 146 | gs_path = '/'.join([self.archive_url, self.name]) |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 147 | self.install_path = os.path.join(self.install_dir, self.name) |
| 148 | gsutil_util.DownloadFromGS(gs_path, self.install_path) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 149 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 150 | def _Setup(self): |
| 151 | """For tarball like artifacts, extracts and prepares contents.""" |
| 152 | pass |
| 153 | |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 154 | def _ClearException(self): |
| 155 | """Delete any existing exception saved for this artifact.""" |
| 156 | if os.path.exists(self.exception_file_path): |
| 157 | os.remove(self.exception_file_path) |
| 158 | |
| 159 | def _SaveException(self, e): |
| 160 | """Save the exception to a file for downloader.IsStaged to retrieve. |
| 161 | |
| 162 | @param e: Exception object to be saved. |
| 163 | """ |
| 164 | with open(self.exception_file_path, 'w') as f: |
| 165 | pickle.dump(e, f) |
| 166 | |
| 167 | def GetException(self): |
| 168 | """Retrieve any exception that was raised in Process method. |
| 169 | |
| 170 | @return: An Exception object that was raised when trying to process the |
| 171 | artifact. Return None if no exception was found. |
| 172 | """ |
| 173 | if not os.path.exists(self.exception_file_path): |
| 174 | return None |
| 175 | with open(self.exception_file_path, 'r') as f: |
| 176 | return pickle.load(f) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 177 | |
| 178 | def Process(self, no_wait): |
| 179 | """Main call point to all artifacts. Downloads and Stages artifact. |
| 180 | |
| 181 | Downloads and Stages artifact from Google Storage to the install directory |
| 182 | specified in the constructor. It multi-thread safe and does not overwrite |
| 183 | the artifact if it's already been downloaded or being downloaded. After |
| 184 | processing, leaves behind a marker to indicate to future invocations that |
| 185 | the artifact has already been staged based on the name of the artifact. |
| 186 | |
| 187 | Do not override as it modifies important private variables, ensures thread |
| 188 | safety, and maintains cache semantics. |
| 189 | |
| 190 | Note: this may be a blocking call when the artifact is already in the |
| 191 | process of being staged. |
| 192 | |
| 193 | Args: |
| 194 | no_wait: If True, don't block waiting for artifact to exist if we fail to |
| 195 | immediately find it. |
| 196 | |
| 197 | Raises: |
| 198 | ArtifactDownloadError: If the artifact fails to download from Google |
| 199 | Storage for any reason or that the regexp |
| 200 | defined by name is not specific enough. |
| 201 | """ |
| 202 | if not self._process_lock: |
| 203 | self._process_lock = _build_artifact_locks.lock( |
| 204 | os.path.join(self.install_dir, self.name)) |
| 205 | |
| 206 | with self._process_lock: |
| 207 | common_util.MkDirP(self.install_dir) |
Dan Shi | f8eb0d1 | 2013-08-01 17:52:06 -0700 | [diff] [blame] | 208 | if not self.ArtifactStaged(): |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 209 | try: |
| 210 | # Delete any existing exception saved for this artifact. |
| 211 | self._ClearException() |
| 212 | # If the artifact should already have been uploaded, don't waste |
| 213 | # cycles waiting around for it to exist. |
| 214 | timeout = 1 if no_wait else 10 |
| 215 | self.WaitForArtifactToExist(timeout) |
| 216 | self._Download() |
| 217 | self._Setup() |
| 218 | self._MarkArtifactStaged() |
| 219 | except Exception as e: |
| 220 | # Save the exception to a file for downloader.IsStaged to retrieve. |
| 221 | self._SaveException(e) |
| 222 | raise |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 223 | else: |
| 224 | self._Log('%s is already staged.', self) |
| 225 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 226 | def __str__(self): |
| 227 | """String representation for the download.""" |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 228 | return '->'.join(['%s/%s' % (self.archive_url, self.name), |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 229 | self.install_dir]) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 230 | |
Chris Sosa | b26b120 | 2013-08-16 16:40:55 -0700 | [diff] [blame] | 231 | def __repr__(self): |
| 232 | return str(self) |
| 233 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 234 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 235 | class AUTestPayloadBuildArtifact(BuildArtifact): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 236 | """Wrapper for AUTest delta payloads which need additional setup.""" |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 237 | def _Setup(self): |
| 238 | super(AUTestPayloadBuildArtifact, self)._Setup() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 239 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 240 | # Rename to update.gz. |
| 241 | install_path = os.path.join(self.install_dir, self.name) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 242 | new_install_path = os.path.join(self.install_dir, |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 243 | devserver_constants.UPDATE_FILE) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 244 | shutil.move(install_path, new_install_path) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 245 | |
| 246 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 247 | # TODO(sosa): Change callers to make this artifact more sane. |
| 248 | class DeltaPayloadsArtifact(BuildArtifact): |
| 249 | """Delta payloads from the archive_url. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 250 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 251 | This artifact is super strange. It custom handles directories and |
| 252 | pulls in all delta payloads. We can't specify exactly what we want |
| 253 | because unlike other artifacts, this one does not conform to something a |
| 254 | client might know. The client doesn't know the version of n-1 or whether it |
| 255 | was even generated. |
| 256 | """ |
| 257 | def __init__(self, *args): |
| 258 | super(DeltaPayloadsArtifact, self).__init__(*args) |
| 259 | self.single_name = False # Expect multiple deltas |
| 260 | nton_name = 'chromeos_%s%s' % (self.build, self.name) |
| 261 | mton_name = 'chromeos_(?!%s)%s' % (self.build, self.name) |
| 262 | nton_install_dir = os.path.join(self.install_dir, _AU_BASE, |
| 263 | self.build + _NTON_DIR_SUFFIX) |
| 264 | mton_install_dir = os.path.join(self.install_dir, _AU_BASE, |
| 265 | self.build + _MTON_DIR_SUFFIX) |
| 266 | self._sub_artifacts = [ |
| 267 | AUTestPayloadBuildArtifact(mton_install_dir, self.archive_url, |
| 268 | mton_name, self.build), |
| 269 | AUTestPayloadBuildArtifact(nton_install_dir, self.archive_url, |
| 270 | nton_name, self.build)] |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 271 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 272 | def _Download(self): |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 273 | """With sub-artifacts we do everything in _Setup().""" |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 274 | pass |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 275 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 276 | def _Setup(self): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 277 | """Process each sub-artifact. Only error out if none can be found.""" |
| 278 | for artifact in self._sub_artifacts: |
| 279 | try: |
| 280 | artifact.Process(no_wait=True) |
| 281 | # Setup symlink so that AU will work for this payload. |
| 282 | os.symlink( |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 283 | os.path.join(os.pardir, os.pardir, |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 284 | devserver_constants.STATEFUL_FILE), |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 285 | os.path.join(artifact.install_dir, |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 286 | devserver_constants.STATEFUL_FILE)) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 287 | except ArtifactDownloadError as e: |
| 288 | self._Log('Could not process %s: %s', artifact, e) |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 289 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 290 | |
| 291 | class BundledBuildArtifact(BuildArtifact): |
| 292 | """A single build artifact bundle e.g. zip file or tar file.""" |
| 293 | def __init__(self, install_dir, archive_url, name, build, |
| 294 | files_to_extract=None, exclude=None): |
| 295 | """Takes BuildArtifacts are with two additional args. |
| 296 | |
| 297 | Additional args: |
| 298 | files_to_extract: A list of files to extract. If set to None, extract |
| 299 | all files. |
| 300 | exclude: A list of files to exclude. If None, no files are excluded. |
| 301 | """ |
| 302 | super(BundledBuildArtifact, self).__init__(install_dir, archive_url, name, |
| 303 | build) |
| 304 | self._files_to_extract = files_to_extract |
| 305 | self._exclude = exclude |
| 306 | |
| 307 | # We modify the marker so that it is unique to what was staged. |
| 308 | if files_to_extract: |
| 309 | self.marker_name = self._SanitizeName( |
| 310 | '_'.join(['.' + self.name] + files_to_extract)) |
| 311 | |
| 312 | def _Extract(self): |
| 313 | """Extracts the bundle into install_dir. Must be overridden. |
| 314 | |
| 315 | If set, uses files_to_extract to only extract those items. If set, use |
| 316 | exclude to exclude specific files. |
| 317 | """ |
| 318 | raise NotImplementedError() |
| 319 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 320 | def _Setup(self): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 321 | self._Extract() |
| 322 | |
| 323 | |
| 324 | class TarballBuildArtifact(BundledBuildArtifact): |
| 325 | """Artifact for tar and tarball files.""" |
| 326 | |
| 327 | def _Extract(self): |
| 328 | """Extracts a tarball using tar. |
| 329 | |
| 330 | Detects whether the tarball is compressed or not based on the file |
| 331 | extension and extracts the tarball into the install_path. |
| 332 | """ |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 333 | try: |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 334 | common_util.ExtractTarball(self.install_path, self.install_dir, |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 335 | files_to_extract=self._files_to_extract, |
| 336 | excluded_files=self._exclude) |
| 337 | except common_util.CommonUtilError as e: |
| 338 | raise ArtifactDownloadError(str(e)) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 339 | |
| 340 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 341 | class AutotestTarballBuildArtifact(TarballBuildArtifact): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 342 | """Wrapper around the autotest tarball to download from gsutil.""" |
| 343 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 344 | def _Setup(self): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 345 | """Extracts the tarball into the install path excluding test suites.""" |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 346 | super(AutotestTarballBuildArtifact, self)._Setup() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 347 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 348 | # Deal with older autotest packages that may not be bundled. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 349 | autotest_dir = os.path.join(self.install_dir, |
| 350 | devserver_constants.AUTOTEST_DIR) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 351 | autotest_pkgs_dir = os.path.join(autotest_dir, 'packages') |
| 352 | if not os.path.exists(autotest_pkgs_dir): |
| 353 | os.makedirs(autotest_pkgs_dir) |
| 354 | |
| 355 | 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] | 356 | cmd = ['autotest/utils/packager.py', 'upload', '--repository', |
| 357 | autotest_pkgs_dir, '--all'] |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 358 | try: |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 359 | subprocess.check_call(cmd, cwd=self.install_dir) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 360 | except subprocess.CalledProcessError, e: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 361 | raise ArtifactDownloadError( |
| 362 | 'Failed to create autotest packages!:\n%s' % e) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 363 | else: |
Gilad Arnold | f584313 | 2012-09-25 00:31:20 -0700 | [diff] [blame] | 364 | self._Log('Using pre-generated packages from autotest') |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 365 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 366 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 367 | class ZipfileBuildArtifact(BundledBuildArtifact): |
| 368 | """A downloadable artifact that is a zipfile.""" |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 369 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 370 | def _Extract(self): |
| 371 | """Extracts files into the install path.""" |
| 372 | # Unzip is weird. It expects its args before any excepts and expects its |
| 373 | # excepts in a list following the -x. |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 374 | cmd = ['unzip', '-o', self.install_path, '-d', self.install_dir] |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 375 | if self._files_to_extract: |
| 376 | cmd.extend(self._files_to_extract) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 377 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 378 | if self._exclude: |
| 379 | cmd.append('-x') |
| 380 | cmd.extend(self._exclude) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 381 | |
| 382 | try: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 383 | subprocess.check_call(cmd) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 384 | except subprocess.CalledProcessError, e: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 385 | raise ArtifactDownloadError( |
| 386 | 'An error occurred when attempting to unzip %s:\n%s' % |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 387 | (self.install_path, e)) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 388 | |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 389 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 390 | class ImplDescription(object): |
| 391 | """Data wrapper that describes an artifact's implementation.""" |
| 392 | def __init__(self, artifact_class, name, *additional_args): |
| 393 | """Constructor: |
| 394 | |
| 395 | Args: |
| 396 | artifact_class: BuildArtifact class to use for the artifact. |
| 397 | name: name to use to identify artifact (see BuildArtifact.name) |
| 398 | additional_args: If sub-class uses additional args, these are passed |
| 399 | through to them. |
| 400 | """ |
| 401 | self.artifact_class = artifact_class |
| 402 | self.name = name |
| 403 | self.additional_args = additional_args |
| 404 | |
Chris Sosa | 968a106 | 2013-08-02 17:42:50 -0700 | [diff] [blame] | 405 | def __repr__(self): |
| 406 | return '%s_%s' % (self.artifact_class, self.name) |
| 407 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 408 | |
| 409 | # Maps artifact names to their implementation description. |
| 410 | # Please note, it is good practice to use constants for these names if you're |
| 411 | # going to re-use the names ANYWHERE else in the devserver code. |
| 412 | ARTIFACT_IMPLEMENTATION_MAP = { |
| 413 | artifact_info.FULL_PAYLOAD: |
| 414 | ImplDescription(AUTestPayloadBuildArtifact, '.*_full_.*'), |
| 415 | artifact_info.DELTA_PAYLOADS: |
| 416 | ImplDescription(DeltaPayloadsArtifact, '.*_delta_.*'), |
| 417 | artifact_info.STATEFUL_PAYLOAD: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 418 | ImplDescription(BuildArtifact, devserver_constants.STATEFUL_FILE), |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 419 | |
| 420 | artifact_info.BASE_IMAGE: |
| 421 | ImplDescription(ZipfileBuildArtifact, IMAGE_FILE, |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 422 | [devserver_constants.BASE_IMAGE_FILE]), |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 423 | artifact_info.RECOVERY_IMAGE: |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 424 | ImplDescription(ZipfileBuildArtifact, IMAGE_FILE, |
| 425 | [devserver_constants.RECOVERY_IMAGE_FILE]), |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 426 | artifact_info.TEST_IMAGE: |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 427 | ImplDescription(ZipfileBuildArtifact, IMAGE_FILE, |
| 428 | [devserver_constants.TEST_IMAGE_FILE]), |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 429 | |
| 430 | artifact_info.AUTOTEST: |
| 431 | ImplDescription(AutotestTarballBuildArtifact, AUTOTEST_FILE, None, |
| 432 | ['autotest/test_suites']), |
| 433 | artifact_info.TEST_SUITES: |
| 434 | ImplDescription(TarballBuildArtifact, TEST_SUITES_FILE), |
| 435 | artifact_info.AU_SUITE: |
Chris Sosa | a56c403 | 2013-03-17 21:59:54 -0700 | [diff] [blame] | 436 | ImplDescription(TarballBuildArtifact, AU_SUITE_FILE), |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 437 | |
| 438 | artifact_info.FIRMWARE: |
| 439 | ImplDescription(BuildArtifact, FIRMWARE_FILE), |
| 440 | artifact_info.SYMBOLS: |
| 441 | ImplDescription(TarballBuildArtifact, DEBUG_SYMBOLS_FILE, |
| 442 | ['debug/breakpad']), |
beeps | c3d0f87 | 2013-07-31 21:50:40 -0700 | [diff] [blame] | 443 | |
| 444 | artifact_info.FACTORY_IMAGE: |
| 445 | ImplDescription(ZipfileBuildArtifact, FACTORY_FILE, |
| 446 | [devserver_constants.FACTORY_IMAGE_FILE]) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Chris Sosa | 968a106 | 2013-08-02 17:42:50 -0700 | [diff] [blame] | 449 | # Add all the paygen_au artifacts in one go. |
| 450 | ARTIFACT_IMPLEMENTATION_MAP.update({ |
| 451 | artifact_info.PAYGEN_AU_SUITE_TEMPLATE % { 'channel': c }: ImplDescription( |
| 452 | TarballBuildArtifact, PAYGEN_AU_SUITE_FILE_TEMPLATE % { 'channel': c }) |
| 453 | for c in devserver_constants.CHANNELS |
| 454 | }) |
| 455 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 456 | |
| 457 | class ArtifactFactory(object): |
| 458 | """A factory class that generates build artifacts from artifact names.""" |
| 459 | |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 460 | def __init__(self, download_dir, archive_url, artifacts, files, |
| 461 | build): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 462 | """Initalizes the member variables for the factory. |
| 463 | |
| 464 | Args: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 465 | archive_url: the Google Storage url of the bucket where the debug |
| 466 | symbols for the desired build are stored. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 467 | artifacts: List of artifacts to stage. These artifacts must be |
| 468 | defined in artifact_info.py and have a mapping in the |
| 469 | ARTIFACT_IMPLEMENTATION_MAP. |
| 470 | files: List of files to stage. These files are just downloaded and staged |
| 471 | as files into the download_dir. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 472 | build: The name of the build. |
| 473 | """ |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 474 | self.download_dir = download_dir |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 475 | self.archive_url = archive_url |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 476 | self.artifacts = artifacts |
| 477 | self.files = files |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 478 | self.build = build |
| 479 | |
| 480 | @staticmethod |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 481 | def _GetDescriptionComponents(name, is_artifact): |
| 482 | """Returns a tuple of for BuildArtifact class, name, and additional args. |
| 483 | |
| 484 | Raises: KeyError if artifact doesn't exist in ARTIFACT_IMPLEMENTATION_MAP. |
| 485 | """ |
| 486 | |
| 487 | if is_artifact: |
| 488 | description = ARTIFACT_IMPLEMENTATION_MAP[name] |
| 489 | else: |
| 490 | description = ImplDescription(BuildArtifact, name) |
| 491 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 492 | return (description.artifact_class, description.name, |
| 493 | description.additional_args) |
| 494 | |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 495 | def _Artifacts(self, names, is_artifact): |
| 496 | """Returns an iterable of BuildArtifacts from |names|. |
| 497 | |
| 498 | If is_artifact is true, then these names define artifacts that must exist in |
| 499 | the ARTIFACT_IMPLEMENTATION_MAP. Otherwise, treat as filenames to stage as |
| 500 | basic BuildArtifacts. |
| 501 | |
| 502 | Raises: KeyError if artifact doesn't exist in ARTIFACT_IMPLEMENTATION_MAP. |
| 503 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 504 | artifacts = [] |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 505 | for name in names: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 506 | artifact_class, path, args = self._GetDescriptionComponents( |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 507 | name, is_artifact) |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 508 | artifacts.append(artifact_class(self.download_dir, self.archive_url, path, |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 509 | self.build, *args)) |
| 510 | |
| 511 | return artifacts |
| 512 | |
| 513 | def RequiredArtifacts(self): |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 514 | """Returns an iterable of BuildArtifacts for the factory's artifacts. |
| 515 | |
| 516 | Raises: KeyError if artifact doesn't exist in ARTIFACT_IMPLEMENTATION_MAP. |
| 517 | """ |
| 518 | artifacts = [] |
| 519 | if self.artifacts: |
| 520 | artifacts.extend(self._Artifacts(self.artifacts, True)) |
| 521 | if self.files: |
| 522 | artifacts.extend(self._Artifacts(self.files, False)) |
| 523 | |
| 524 | return artifacts |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 525 | |
| 526 | def OptionalArtifacts(self): |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 527 | """Returns an iterable of BuildArtifacts that should be cached. |
| 528 | |
| 529 | Raises: KeyError if an optional artifact doesn't exist in |
| 530 | ARTIFACT_IMPLEMENTATION_MAP yet defined in |
| 531 | artifact_info.REQUESTED_TO_OPTIONAL_MAP. |
| 532 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 533 | optional_names = set() |
| 534 | for artifact_name, optional_list in ( |
| 535 | artifact_info.REQUESTED_TO_OPTIONAL_MAP.iteritems()): |
| 536 | # We are already downloading it. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 537 | if artifact_name in self.artifacts: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 538 | optional_names = optional_names.union(optional_list) |
| 539 | |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 540 | return self._Artifacts(optional_names - set(self.artifacts), True) |
Chris Sosa | 968a106 | 2013-08-02 17:42:50 -0700 | [diff] [blame] | 541 | |
| 542 | |
| 543 | # A simple main to verify correctness of the artifact map when making simple |
| 544 | # name changes. |
| 545 | if __name__ == '__main__': |
| 546 | print 'ARTIFACT IMPLEMENTATION MAP (for debugging)' |
| 547 | print 'FORMAT: ARTIFACT -> IMPLEMENTATION (<class>_file)' |
| 548 | for key, value in sorted(ARTIFACT_IMPLEMENTATION_MAP.items()): |
| 549 | print '%s -> %s' % (key, value) |