Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Chris Sosa | 968a106 | 2013-08-02 17:42:50 -0700 | [diff] [blame] | 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 |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 11 | import re |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 12 | import shutil |
| 13 | import subprocess |
| 14 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 15 | import artifact_info |
| 16 | import common_util |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 17 | import devserver_constants |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 18 | import gsutil_util |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 19 | import log_util |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 20 | |
| 21 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 22 | _AU_BASE = 'au' |
| 23 | _NTON_DIR_SUFFIX = '_nton' |
| 24 | _MTON_DIR_SUFFIX = '_mton' |
| 25 | |
| 26 | ############ Actual filenames of artifacts in Google Storage ############ |
| 27 | |
| 28 | AU_SUITE_FILE = 'au_control.tar.bz2' |
Chris Sosa | 968a106 | 2013-08-02 17:42:50 -0700 | [diff] [blame] | 29 | PAYGEN_AU_SUITE_FILE_TEMPLATE = 'paygen_au_%(channel)s_control.tar.bz2' |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 30 | AUTOTEST_FILE = 'autotest.tar' |
| 31 | AUTOTEST_COMPRESSED_FILE = 'autotest.tar.bz2' |
| 32 | DEBUG_SYMBOLS_FILE = 'debug.tgz' |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 33 | FACTORY_FILE = 'ChromeOS-factory*zip' |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 34 | FIRMWARE_FILE = 'firmware_from_source.tar.bz2' |
| 35 | IMAGE_FILE = 'image.zip' |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 36 | TEST_SUITES_FILE = 'test_suites.tar.bz2' |
| 37 | |
| 38 | _build_artifact_locks = common_util.LockDict() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 39 | |
| 40 | |
| 41 | class ArtifactDownloadError(Exception): |
| 42 | """Error used to signify an issue processing an artifact.""" |
| 43 | pass |
| 44 | |
| 45 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 46 | class BuildArtifact(log_util.Loggable): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 47 | """Wrapper around an artifact to download from gsutil. |
| 48 | |
| 49 | The purpose of this class is to download objects from Google Storage |
| 50 | and install them to a local directory. There are two main functions, one to |
| 51 | download/prepare the artifacts in to a temporary staging area and the second |
| 52 | to stage it into its final destination. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 53 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 54 | IMPORTANT! (i) `name' is a glob expression by default (and not a regex), be |
| 55 | attentive when adding new artifacts; (ii) name matching semantics differ |
| 56 | between a glob (full name string match) and a regex (partial match). |
| 57 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 58 | Class members: |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 59 | archive_url: An archive URL. |
| 60 | name: Name given for artifact; in fact, it is a pattern that captures the |
| 61 | names of files contained in the artifact. This can either be an |
| 62 | ordinary shell-style glob (the default), or a regular expression (if |
| 63 | is_regex_name is True). |
| 64 | is_regex_name: Whether the name value is a regex (default: glob). |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 65 | build: The version of the build i.e. R26-2342.0.0. |
| 66 | marker_name: Name used to define the lock marker for the artifacts to |
| 67 | prevent it from being re-downloaded. By default based on name |
| 68 | but can be overriden by children. |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 69 | exception_file_path: Path to a file containing the serialized exception, |
| 70 | which was raised in Process method. The file is located |
| 71 | in the parent folder of install_dir, since the |
| 72 | install_dir will be deleted if the build does not |
| 73 | existed. |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 74 | install_path: Path to artifact. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 75 | install_dir: The final location where the artifact should be staged to. |
| 76 | single_name: If True the name given should only match one item. Note, if not |
| 77 | True, self.name will become a list of items returned. |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 78 | installed_files: A list of files that were the final result of downloading |
| 79 | and setting up the artifact. |
| 80 | store_installed_files: Whether the list of installed files is stored in the |
| 81 | marker file. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 82 | """ |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 83 | |
| 84 | def __init__(self, install_dir, archive_url, name, build, |
| 85 | is_regex_name=False): |
| 86 | """Constructor. |
| 87 | |
| 88 | Args: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 89 | install_dir: Where to install the artifact. |
| 90 | archive_url: The Google Storage path to find the artifact. |
| 91 | name: Identifying name to be used to find/store the artifact. |
| 92 | build: The name of the build e.g. board/release. |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 93 | is_regex_name: Whether the name pattern is a regex (default: glob). |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 94 | """ |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 95 | super(BuildArtifact, self).__init__() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 96 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 97 | # In-memory lock to keep the devserver from colliding with itself while |
| 98 | # attempting to stage the same artifact. |
| 99 | self._process_lock = None |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 100 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 101 | self.archive_url = archive_url |
| 102 | self.name = name |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 103 | self.is_regex_name = is_regex_name |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 104 | self.build = build |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 105 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 106 | self.marker_name = '.' + self._SanitizeName(name) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 107 | |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 108 | exception_file_name = ('.' + self._SanitizeName(build) + self.marker_name + |
| 109 | '.exception') |
| 110 | # The exception file needs to be located in parent folder, since the |
| 111 | # install_dir will be deleted is the build does not exist. |
| 112 | self.exception_file_path = os.path.join(os.path.dirname(install_dir), |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 113 | exception_file_name) |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 114 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 115 | self.install_path = None |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 116 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 117 | self.install_dir = install_dir |
| 118 | |
| 119 | self.single_name = True |
| 120 | |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 121 | self.installed_files = [] |
| 122 | self.store_installed_files = True |
| 123 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 124 | @staticmethod |
| 125 | def _SanitizeName(name): |
| 126 | """Sanitizes name to be used for creating a file on the filesystem. |
| 127 | |
| 128 | '.','/' and '*' have special meaning in FS lingo. Replace them with words. |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 129 | |
| 130 | Args: |
| 131 | name: A file name/path. |
| 132 | Returns: |
| 133 | The sanitized name/path. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 134 | """ |
| 135 | return name.replace('*', 'STAR').replace('.', 'DOT').replace('/', 'SLASH') |
| 136 | |
Dan Shi | f8eb0d1 | 2013-08-01 17:52:06 -0700 | [diff] [blame] | 137 | def ArtifactStaged(self): |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 138 | """Returns True if artifact is already staged. |
| 139 | |
| 140 | This checks for (1) presence of the artifact marker file, and (2) the |
| 141 | presence of each installed file listed in this marker. Both must hold for |
| 142 | the artifact to be considered staged. Note that this method is safe for use |
| 143 | even if the artifacts were not stageed by this instance, as it is assumed |
| 144 | that any BuildArtifact instance that did the staging wrote the list of |
| 145 | files actually installed into the marker. |
| 146 | """ |
| 147 | marker_file = os.path.join(self.install_dir, self.marker_name) |
| 148 | |
| 149 | # If the marker is missing, it's definitely not staged. |
| 150 | if not os.path.exists(marker_file): |
| 151 | return False |
| 152 | |
| 153 | # We want to ensure that every file listed in the marker is actually there. |
| 154 | if self.store_installed_files: |
| 155 | with open(marker_file) as f: |
| 156 | files = [line.strip() for line in f] |
| 157 | |
| 158 | # Check to see if any of the purportedly installed files are missing, in |
| 159 | # which case the marker is outdated and should be removed. |
| 160 | missing_files = [fname for fname in files if not os.path.exists(fname)] |
| 161 | if missing_files: |
| 162 | self._Log('***ATTENTION*** %s files listed in %s are missing:\n%s', |
| 163 | 'All' if len(files) == len(missing_files) else 'Some', |
| 164 | marker_file, '\n'.join(missing_files)) |
| 165 | os.remove(marker_file) |
| 166 | return False |
| 167 | |
| 168 | return True |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 169 | |
| 170 | def _MarkArtifactStaged(self): |
| 171 | """Marks the artifact as staged.""" |
| 172 | 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] | 173 | f.write('\n'.join(self.installed_files)) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 174 | |
Gilad Arnold | 02dc655 | 2013-11-14 11:27:54 -0800 | [diff] [blame] | 175 | def _WaitForArtifactToExist(self, timeout, update_name=True): |
Chris Sosa | c4e8784 | 2013-08-16 18:04:14 -0700 | [diff] [blame] | 176 | """Waits for artifact to exist and sets self.name to appropriate name. |
| 177 | |
| 178 | Args: |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 179 | timeout: How long to wait for artifact to become available. |
Chris Sosa | c4e8784 | 2013-08-16 18:04:14 -0700 | [diff] [blame] | 180 | update_name: If False, don't actually update self.name. |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 181 | Raises: |
| 182 | ArtifactDownloadError: An error occurred when obtaining artifact. |
Chris Sosa | c4e8784 | 2013-08-16 18:04:14 -0700 | [diff] [blame] | 183 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 184 | names = gsutil_util.GetGSNamesWithWait( |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 185 | self.name, self.archive_url, str(self), timeout=timeout, |
| 186 | is_regex_pattern=self.is_regex_name) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 187 | if not names: |
Don Garrett | ef484fb | 2013-11-22 16:56:18 -0800 | [diff] [blame] | 188 | raise ArtifactDownloadError('Could not find %s in Google Storage at %s' % |
| 189 | (self.name, self.archive_url)) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 190 | |
| 191 | if self.single_name: |
| 192 | if len(names) > 1: |
| 193 | raise ArtifactDownloadError('Too many artifacts match %s' % self.name) |
| 194 | |
Chris Sosa | c4e8784 | 2013-08-16 18:04:14 -0700 | [diff] [blame] | 195 | new_name = names[0] |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 196 | else: |
Chris Sosa | c4e8784 | 2013-08-16 18:04:14 -0700 | [diff] [blame] | 197 | new_name = names |
| 198 | |
| 199 | if update_name: |
| 200 | self.name = new_name |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 201 | |
| 202 | def _Download(self): |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 203 | """Downloads artifact from Google Storage to a local directory.""" |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 204 | gs_path = '/'.join([self.archive_url, self.name]) |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 205 | self.install_path = os.path.join(self.install_dir, self.name) |
| 206 | gsutil_util.DownloadFromGS(gs_path, self.install_path) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 207 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 208 | def _Setup(self): |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 209 | """Process the downloaded content, update the list of installed files.""" |
| 210 | # In this primitive case, what was downloaded (has to be a single file) is |
| 211 | # what's installed. |
| 212 | self.installed_files = [self.install_path] |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 213 | |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 214 | def _ClearException(self): |
| 215 | """Delete any existing exception saved for this artifact.""" |
| 216 | if os.path.exists(self.exception_file_path): |
| 217 | os.remove(self.exception_file_path) |
| 218 | |
| 219 | def _SaveException(self, e): |
| 220 | """Save the exception to a file for downloader.IsStaged to retrieve. |
| 221 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 222 | Args: |
| 223 | e: Exception object to be saved. |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 224 | """ |
| 225 | with open(self.exception_file_path, 'w') as f: |
| 226 | pickle.dump(e, f) |
| 227 | |
| 228 | def GetException(self): |
| 229 | """Retrieve any exception that was raised in Process method. |
| 230 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 231 | Returns: |
| 232 | An Exception object that was raised when trying to process the artifact. |
| 233 | Return None if no exception was found. |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 234 | """ |
| 235 | if not os.path.exists(self.exception_file_path): |
| 236 | return None |
| 237 | with open(self.exception_file_path, 'r') as f: |
| 238 | return pickle.load(f) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 239 | |
| 240 | def Process(self, no_wait): |
| 241 | """Main call point to all artifacts. Downloads and Stages artifact. |
| 242 | |
| 243 | Downloads and Stages artifact from Google Storage to the install directory |
| 244 | specified in the constructor. It multi-thread safe and does not overwrite |
| 245 | the artifact if it's already been downloaded or being downloaded. After |
| 246 | processing, leaves behind a marker to indicate to future invocations that |
| 247 | the artifact has already been staged based on the name of the artifact. |
| 248 | |
| 249 | Do not override as it modifies important private variables, ensures thread |
| 250 | safety, and maintains cache semantics. |
| 251 | |
| 252 | Note: this may be a blocking call when the artifact is already in the |
| 253 | process of being staged. |
| 254 | |
| 255 | Args: |
| 256 | no_wait: If True, don't block waiting for artifact to exist if we fail to |
| 257 | immediately find it. |
| 258 | |
| 259 | Raises: |
| 260 | ArtifactDownloadError: If the artifact fails to download from Google |
| 261 | Storage for any reason or that the regexp |
| 262 | defined by name is not specific enough. |
| 263 | """ |
| 264 | if not self._process_lock: |
| 265 | self._process_lock = _build_artifact_locks.lock( |
| 266 | os.path.join(self.install_dir, self.name)) |
| 267 | |
| 268 | with self._process_lock: |
| 269 | common_util.MkDirP(self.install_dir) |
Dan Shi | f8eb0d1 | 2013-08-01 17:52:06 -0700 | [diff] [blame] | 270 | if not self.ArtifactStaged(): |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 271 | try: |
| 272 | # Delete any existing exception saved for this artifact. |
| 273 | self._ClearException() |
| 274 | # If the artifact should already have been uploaded, don't waste |
| 275 | # cycles waiting around for it to exist. |
| 276 | timeout = 1 if no_wait else 10 |
Gilad Arnold | 02dc655 | 2013-11-14 11:27:54 -0800 | [diff] [blame] | 277 | self._WaitForArtifactToExist(timeout) |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 278 | self._Download() |
| 279 | self._Setup() |
| 280 | self._MarkArtifactStaged() |
| 281 | except Exception as e: |
| 282 | # Save the exception to a file for downloader.IsStaged to retrieve. |
| 283 | self._SaveException(e) |
Gilad Arnold | 02dc655 | 2013-11-14 11:27:54 -0800 | [diff] [blame] | 284 | |
| 285 | # Convert an unknown exception into an ArtifactDownloadError. |
| 286 | if type(e) is ArtifactDownloadError: |
| 287 | raise |
| 288 | else: |
| 289 | raise ArtifactDownloadError('An error occurred: %s' % e) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 290 | else: |
| 291 | self._Log('%s is already staged.', self) |
| 292 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 293 | def __str__(self): |
| 294 | """String representation for the download.""" |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 295 | return '->'.join(['%s/%s' % (self.archive_url, self.name), |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 296 | self.install_dir]) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 297 | |
Chris Sosa | b26b120 | 2013-08-16 16:40:55 -0700 | [diff] [blame] | 298 | def __repr__(self): |
| 299 | return str(self) |
| 300 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 301 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 302 | class AUTestPayloadBuildArtifact(BuildArtifact): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 303 | """Wrapper for AUTest delta payloads which need additional setup.""" |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 304 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 305 | def _Setup(self): |
| 306 | super(AUTestPayloadBuildArtifact, self)._Setup() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 307 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 308 | # Rename to update.gz. |
| 309 | install_path = os.path.join(self.install_dir, self.name) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 310 | new_install_path = os.path.join(self.install_dir, |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 311 | devserver_constants.UPDATE_FILE) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 312 | shutil.move(install_path, new_install_path) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 313 | |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 314 | # Reflect the rename in the list of installed files. |
| 315 | self.installed_files.remove(install_path) |
| 316 | self.installed_files = [new_install_path] |
| 317 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 318 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 319 | # TODO(sosa): Change callers to make this artifact more sane. |
| 320 | class DeltaPayloadsArtifact(BuildArtifact): |
| 321 | """Delta payloads from the archive_url. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 322 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 323 | This artifact is super strange. It custom handles directories and |
| 324 | pulls in all delta payloads. We can't specify exactly what we want |
| 325 | because unlike other artifacts, this one does not conform to something a |
| 326 | client might know. The client doesn't know the version of n-1 or whether it |
| 327 | was even generated. |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 328 | |
| 329 | IMPORTANT! Note that this artifact simply ignores the `name' argument because |
| 330 | that name is derived internally in accordance with sub-artifacts. Also note |
| 331 | the different types of names (in fact, file name patterns) used for the |
| 332 | different sub-artifacts. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 333 | """ |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 334 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 335 | def __init__(self, *args): |
| 336 | super(DeltaPayloadsArtifact, self).__init__(*args) |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 337 | # Override the name field, we know what it should be. |
| 338 | self.name = '*_delta_*' |
| 339 | self.is_regex_name = False |
| 340 | self.single_name = False # Expect multiple deltas |
| 341 | |
| 342 | # We use a regular glob for the N-to-N delta payload. |
| 343 | nton_name = 'chromeos_%s*_delta_*' % self.build |
| 344 | # We use a regular expression for the M-to-N delta payload. |
| 345 | mton_name = ('chromeos_(?!%s).*_delta_.*' % re.escape(self.build)) |
| 346 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 347 | nton_install_dir = os.path.join(self.install_dir, _AU_BASE, |
| 348 | self.build + _NTON_DIR_SUFFIX) |
| 349 | mton_install_dir = os.path.join(self.install_dir, _AU_BASE, |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 350 | self.build + _MTON_DIR_SUFFIX) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 351 | self._sub_artifacts = [ |
| 352 | AUTestPayloadBuildArtifact(mton_install_dir, self.archive_url, |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 353 | mton_name, self.build, is_regex_name=True), |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 354 | AUTestPayloadBuildArtifact(nton_install_dir, self.archive_url, |
| 355 | nton_name, self.build)] |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 356 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 357 | def _Download(self): |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 358 | """With sub-artifacts we do everything in _Setup().""" |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 359 | pass |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 360 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 361 | def _Setup(self): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 362 | """Process each sub-artifact. Only error out if none can be found.""" |
| 363 | for artifact in self._sub_artifacts: |
| 364 | try: |
| 365 | artifact.Process(no_wait=True) |
| 366 | # Setup symlink so that AU will work for this payload. |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 367 | stateful_update_symlink = os.path.join( |
| 368 | artifact.install_dir, devserver_constants.STATEFUL_FILE) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 369 | os.symlink( |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 370 | os.path.join(os.pardir, os.pardir, |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 371 | devserver_constants.STATEFUL_FILE), |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 372 | stateful_update_symlink) |
| 373 | |
| 374 | # Aggregate sub-artifact file lists, including stateful symlink. |
| 375 | self.installed_files += artifact.installed_files |
| 376 | self.installed_files.append(stateful_update_symlink) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 377 | except ArtifactDownloadError as e: |
| 378 | self._Log('Could not process %s: %s', artifact, e) |
Gilad Arnold | 02dc655 | 2013-11-14 11:27:54 -0800 | [diff] [blame] | 379 | raise |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 380 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 381 | |
| 382 | class BundledBuildArtifact(BuildArtifact): |
| 383 | """A single build artifact bundle e.g. zip file or tar file.""" |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 384 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 385 | def __init__(self, install_dir, archive_url, name, build, |
| 386 | is_regex_name=False, files_to_extract=None, exclude=None): |
| 387 | """Takes BuildArtifact args with some additional ones. |
| 388 | |
| 389 | Args: |
| 390 | install_dir: See superclass. |
| 391 | archive_url: See superclass. |
| 392 | name: See superclass. |
| 393 | build: See superclass. |
| 394 | is_regex_name: See superclass. |
| 395 | files_to_extract: A list of files to extract. If set to None, extract |
| 396 | all files. |
| 397 | 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] | 398 | """ |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 399 | super(BundledBuildArtifact, self).__init__( |
| 400 | install_dir, archive_url, name, build, is_regex_name=is_regex_name) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 401 | self._files_to_extract = files_to_extract |
| 402 | self._exclude = exclude |
| 403 | |
| 404 | # We modify the marker so that it is unique to what was staged. |
| 405 | if files_to_extract: |
| 406 | self.marker_name = self._SanitizeName( |
| 407 | '_'.join(['.' + self.name] + files_to_extract)) |
| 408 | |
| 409 | def _Extract(self): |
| 410 | """Extracts the bundle into install_dir. Must be overridden. |
| 411 | |
| 412 | If set, uses files_to_extract to only extract those items. If set, use |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 413 | exclude to exclude specific files. In any case, this must return the list |
| 414 | of files extracted (absolute paths). |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 415 | """ |
| 416 | raise NotImplementedError() |
| 417 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 418 | def _Setup(self): |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 419 | extract_result = self._Extract() |
| 420 | if self.store_installed_files: |
| 421 | # List both the archive and the extracted files. |
| 422 | self.installed_files.append(self.install_path) |
| 423 | self.installed_files.extend(extract_result) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 424 | |
| 425 | |
| 426 | class TarballBuildArtifact(BundledBuildArtifact): |
| 427 | """Artifact for tar and tarball files.""" |
| 428 | |
| 429 | def _Extract(self): |
| 430 | """Extracts a tarball using tar. |
| 431 | |
| 432 | Detects whether the tarball is compressed or not based on the file |
| 433 | extension and extracts the tarball into the install_path. |
| 434 | """ |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 435 | try: |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 436 | return common_util.ExtractTarball(self.install_path, self.install_dir, |
| 437 | files_to_extract=self._files_to_extract, |
| 438 | excluded_files=self._exclude, |
| 439 | return_extracted_files=True) |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 440 | except common_util.CommonUtilError as e: |
| 441 | raise ArtifactDownloadError(str(e)) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 442 | |
| 443 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 444 | class AutotestTarballBuildArtifact(TarballBuildArtifact): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 445 | """Wrapper around the autotest tarball to download from gsutil.""" |
| 446 | |
Gilad Arnold | e57e233 | 2013-11-14 17:07:11 -0800 | [diff] [blame] | 447 | def __init__(self, *args, **dargs): |
| 448 | super(AutotestTarballBuildArtifact, self).__init__(*args, **dargs) |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 449 | # We don't store/check explicit file lists in Autotest tarball markers; |
| 450 | # this can get huge and unwieldy, and generally make little sense. |
| 451 | self.store_installed_files = False |
| 452 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 453 | def _Setup(self): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 454 | """Extracts the tarball into the install path excluding test suites.""" |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 455 | super(AutotestTarballBuildArtifact, self)._Setup() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 456 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 457 | # Deal with older autotest packages that may not be bundled. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 458 | autotest_dir = os.path.join(self.install_dir, |
| 459 | devserver_constants.AUTOTEST_DIR) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 460 | autotest_pkgs_dir = os.path.join(autotest_dir, 'packages') |
| 461 | if not os.path.exists(autotest_pkgs_dir): |
| 462 | os.makedirs(autotest_pkgs_dir) |
| 463 | |
| 464 | 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] | 465 | cmd = ['autotest/utils/packager.py', 'upload', '--repository', |
| 466 | autotest_pkgs_dir, '--all'] |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 467 | try: |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 468 | subprocess.check_call(cmd, cwd=self.install_dir) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 469 | except subprocess.CalledProcessError, e: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 470 | raise ArtifactDownloadError( |
| 471 | 'Failed to create autotest packages!:\n%s' % e) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 472 | else: |
Gilad Arnold | f584313 | 2012-09-25 00:31:20 -0700 | [diff] [blame] | 473 | self._Log('Using pre-generated packages from autotest') |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 474 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 475 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 476 | class ZipfileBuildArtifact(BundledBuildArtifact): |
| 477 | """A downloadable artifact that is a zipfile.""" |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 478 | |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 479 | def _RunUnzip(self, list_only): |
| 480 | # Unzip is weird. It expects its args before any excludes and expects its |
| 481 | # excludes in a list following the -x. |
| 482 | cmd = ['unzip', '-qql' if list_only else '-o', self.install_path] |
| 483 | if not list_only: |
| 484 | cmd += ['-d', self.install_dir] |
| 485 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 486 | if self._files_to_extract: |
| 487 | cmd.extend(self._files_to_extract) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 488 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 489 | if self._exclude: |
| 490 | cmd.append('-x') |
| 491 | cmd.extend(self._exclude) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 492 | |
| 493 | try: |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 494 | return subprocess.check_output(cmd).strip('\n').splitlines() |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 495 | except subprocess.CalledProcessError, e: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 496 | raise ArtifactDownloadError( |
| 497 | 'An error occurred when attempting to unzip %s:\n%s' % |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 498 | (self.install_path, e)) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 499 | |
Gilad Arnold | 1638d82 | 2013-11-07 23:38:16 -0800 | [diff] [blame] | 500 | def _Extract(self): |
| 501 | """Extracts files into the install path.""" |
| 502 | file_list = [os.path.join(self.install_dir, line[30:].strip()) |
| 503 | for line in self._RunUnzip(True) |
| 504 | if not line.endswith('/')] |
| 505 | if file_list: |
| 506 | self._RunUnzip(False) |
| 507 | |
| 508 | return file_list |
| 509 | |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 510 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 511 | class ImplDescription(object): |
| 512 | """Data wrapper that describes an artifact's implementation.""" |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 513 | |
| 514 | def __init__(self, artifact_class, name, *additional_args, |
| 515 | **additional_dargs): |
| 516 | """Constructor. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 517 | |
| 518 | Args: |
| 519 | artifact_class: BuildArtifact class to use for the artifact. |
| 520 | name: name to use to identify artifact (see BuildArtifact.name) |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 521 | *additional_args: Additional arguments to pass to artifact_class. |
| 522 | **additional_dargs: Additional named arguments to pass to artifact_class. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 523 | """ |
| 524 | self.artifact_class = artifact_class |
| 525 | self.name = name |
| 526 | self.additional_args = additional_args |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 527 | self.additional_dargs = additional_dargs |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 528 | |
Chris Sosa | 968a106 | 2013-08-02 17:42:50 -0700 | [diff] [blame] | 529 | def __repr__(self): |
| 530 | return '%s_%s' % (self.artifact_class, self.name) |
| 531 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 532 | |
| 533 | # Maps artifact names to their implementation description. |
| 534 | # Please note, it is good practice to use constants for these names if you're |
| 535 | # going to re-use the names ANYWHERE else in the devserver code. |
| 536 | ARTIFACT_IMPLEMENTATION_MAP = { |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 537 | artifact_info.FULL_PAYLOAD: |
| 538 | ImplDescription(AUTestPayloadBuildArtifact, '*_full_*'), |
| 539 | artifact_info.DELTA_PAYLOADS: |
| 540 | ImplDescription(DeltaPayloadsArtifact, 'DONTCARE'), |
| 541 | artifact_info.STATEFUL_PAYLOAD: |
| 542 | ImplDescription(BuildArtifact, devserver_constants.STATEFUL_FILE), |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 543 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 544 | artifact_info.BASE_IMAGE: |
| 545 | ImplDescription(ZipfileBuildArtifact, IMAGE_FILE, |
Gilad Arnold | 69878b4 | 2013-09-18 13:39:22 -0700 | [diff] [blame] | 546 | files_to_extract=[devserver_constants.BASE_IMAGE_FILE]), |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 547 | artifact_info.RECOVERY_IMAGE: |
| 548 | ImplDescription(ZipfileBuildArtifact, IMAGE_FILE, |
Gilad Arnold | 69878b4 | 2013-09-18 13:39:22 -0700 | [diff] [blame] | 549 | files_to_extract=[devserver_constants.RECOVERY_IMAGE_FILE]), |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 550 | artifact_info.DEV_IMAGE: |
| 551 | ImplDescription(ZipfileBuildArtifact, IMAGE_FILE, |
| 552 | files_to_extract=[devserver_constants.IMAGE_FILE]), |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 553 | artifact_info.TEST_IMAGE: |
| 554 | ImplDescription(ZipfileBuildArtifact, IMAGE_FILE, |
Gilad Arnold | 69878b4 | 2013-09-18 13:39:22 -0700 | [diff] [blame] | 555 | files_to_extract=[devserver_constants.TEST_IMAGE_FILE]), |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 556 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 557 | artifact_info.AUTOTEST: |
| 558 | ImplDescription(AutotestTarballBuildArtifact, AUTOTEST_FILE, |
| 559 | files_to_extract=None, |
| 560 | exclude=['autotest/test_suites']), |
| 561 | artifact_info.TEST_SUITES: |
| 562 | ImplDescription(TarballBuildArtifact, TEST_SUITES_FILE), |
| 563 | artifact_info.AU_SUITE: |
| 564 | ImplDescription(TarballBuildArtifact, AU_SUITE_FILE), |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 565 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 566 | artifact_info.FIRMWARE: |
| 567 | ImplDescription(BuildArtifact, FIRMWARE_FILE), |
| 568 | artifact_info.SYMBOLS: |
| 569 | ImplDescription(TarballBuildArtifact, DEBUG_SYMBOLS_FILE, |
| 570 | files_to_extract=['debug/breakpad']), |
beeps | c3d0f87 | 2013-07-31 21:50:40 -0700 | [diff] [blame] | 571 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 572 | artifact_info.FACTORY_IMAGE: |
| 573 | ImplDescription(ZipfileBuildArtifact, FACTORY_FILE, |
Gilad Arnold | 69878b4 | 2013-09-18 13:39:22 -0700 | [diff] [blame] | 574 | files_to_extract=[devserver_constants.FACTORY_IMAGE_FILE]) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 575 | } |
| 576 | |
Chris Sosa | 968a106 | 2013-08-02 17:42:50 -0700 | [diff] [blame] | 577 | # Add all the paygen_au artifacts in one go. |
| 578 | ARTIFACT_IMPLEMENTATION_MAP.update({ |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 579 | artifact_info.PAYGEN_AU_SUITE_TEMPLATE % {'channel': c}: |
| 580 | ImplDescription( |
| 581 | TarballBuildArtifact, PAYGEN_AU_SUITE_FILE_TEMPLATE % {'channel': c}) |
| 582 | for c in devserver_constants.CHANNELS |
Chris Sosa | 968a106 | 2013-08-02 17:42:50 -0700 | [diff] [blame] | 583 | }) |
| 584 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 585 | |
| 586 | class ArtifactFactory(object): |
| 587 | """A factory class that generates build artifacts from artifact names.""" |
| 588 | |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 589 | def __init__(self, download_dir, archive_url, artifacts, files, |
| 590 | build): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 591 | """Initalizes the member variables for the factory. |
| 592 | |
| 593 | Args: |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 594 | download_dir: A directory to which artifacts are downloaded. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 595 | archive_url: the Google Storage url of the bucket where the debug |
| 596 | symbols for the desired build are stored. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 597 | artifacts: List of artifacts to stage. These artifacts must be |
| 598 | defined in artifact_info.py and have a mapping in the |
| 599 | ARTIFACT_IMPLEMENTATION_MAP. |
| 600 | files: List of files to stage. These files are just downloaded and staged |
| 601 | as files into the download_dir. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 602 | build: The name of the build. |
| 603 | """ |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 604 | self.download_dir = download_dir |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 605 | self.archive_url = archive_url |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 606 | self.artifacts = artifacts |
| 607 | self.files = files |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 608 | self.build = build |
| 609 | |
| 610 | @staticmethod |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 611 | def _GetDescriptionComponents(name, is_artifact): |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 612 | """Returns components for constructing a BuildArtifact. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 613 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 614 | Args: |
| 615 | name: The artifact name / file pattern. |
| 616 | is_artifact: Whether this is a named (True) or file (False) artifact. |
| 617 | Returns: |
| 618 | A tuple consisting of the BuildArtifact subclass, name, and additional |
| 619 | list- and named-arguments. |
| 620 | Raises: |
| 621 | KeyError: if artifact doesn't exist in ARTIFACT_IMPLEMENTATION_MAP. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 622 | """ |
| 623 | |
| 624 | if is_artifact: |
| 625 | description = ARTIFACT_IMPLEMENTATION_MAP[name] |
| 626 | else: |
| 627 | description = ImplDescription(BuildArtifact, name) |
| 628 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 629 | return (description.artifact_class, description.name, |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 630 | description.additional_args, description.additional_dargs) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 631 | |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 632 | def _Artifacts(self, names, is_artifact): |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 633 | """Returns the BuildArtifacts from |names|. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 634 | |
| 635 | If is_artifact is true, then these names define artifacts that must exist in |
| 636 | the ARTIFACT_IMPLEMENTATION_MAP. Otherwise, treat as filenames to stage as |
| 637 | basic BuildArtifacts. |
| 638 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 639 | Args: |
| 640 | names: A sequence of artifact names. |
| 641 | is_artifact: Whether this is a named (True) or file (False) artifact. |
| 642 | Returns: |
| 643 | An iterable of BuildArtifacts. |
| 644 | Raises: |
| 645 | KeyError: if artifact doesn't exist in ARTIFACT_IMPLEMENTATION_MAP. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 646 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 647 | artifacts = [] |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 648 | for name in names: |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 649 | artifact_class, path, args, dargs = self._GetDescriptionComponents( |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 650 | name, is_artifact) |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 651 | artifacts.append(artifact_class(self.download_dir, self.archive_url, path, |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 652 | self.build, *args, **dargs)) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 653 | |
| 654 | return artifacts |
| 655 | |
| 656 | def RequiredArtifacts(self): |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 657 | """Returns BuildArtifacts for the factory's artifacts. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 658 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 659 | Returns: |
| 660 | An iterable of BuildArtifacts. |
| 661 | Raises: |
| 662 | KeyError: if artifact doesn't exist in ARTIFACT_IMPLEMENTATION_MAP. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 663 | """ |
| 664 | artifacts = [] |
| 665 | if self.artifacts: |
| 666 | artifacts.extend(self._Artifacts(self.artifacts, True)) |
| 667 | if self.files: |
| 668 | artifacts.extend(self._Artifacts(self.files, False)) |
| 669 | |
| 670 | return artifacts |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 671 | |
| 672 | def OptionalArtifacts(self): |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 673 | """Returns BuildArtifacts that should be cached. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 674 | |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 675 | Returns: |
| 676 | An iterable of BuildArtifacts. |
| 677 | Raises: |
| 678 | KeyError: if an optional artifact doesn't exist in |
| 679 | ARTIFACT_IMPLEMENTATION_MAP yet defined in |
| 680 | artifact_info.REQUESTED_TO_OPTIONAL_MAP. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 681 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 682 | optional_names = set() |
| 683 | for artifact_name, optional_list in ( |
| 684 | artifact_info.REQUESTED_TO_OPTIONAL_MAP.iteritems()): |
| 685 | # We are already downloading it. |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 686 | if artifact_name in self.artifacts: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 687 | optional_names = optional_names.union(optional_list) |
| 688 | |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 689 | return self._Artifacts(optional_names - set(self.artifacts), True) |
Chris Sosa | 968a106 | 2013-08-02 17:42:50 -0700 | [diff] [blame] | 690 | |
| 691 | |
| 692 | # A simple main to verify correctness of the artifact map when making simple |
| 693 | # name changes. |
| 694 | if __name__ == '__main__': |
| 695 | print 'ARTIFACT IMPLEMENTATION MAP (for debugging)' |
| 696 | print 'FORMAT: ARTIFACT -> IMPLEMENTATION (<class>_file)' |
| 697 | for key, value in sorted(ARTIFACT_IMPLEMENTATION_MAP.items()): |
| 698 | print '%s -> %s' % (key, value) |