xixuan | 44b5545 | 2016-09-06 22:35:56 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Luis Hector Chavez | dca9dd7 | 2018-06-12 19:56:30 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 3 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 7 | """Downloaders used to download artifacts and files from a given source.""" |
| 8 | |
| 9 | from __future__ import print_function |
| 10 | |
Prashanth B | a06d2d2 | 2014-03-07 23:35:19 | [diff] [blame] | 11 | import collections |
Eric Caruso | e76d187 | 2019-02-21 19:17:45 | [diff] [blame] | 12 | import errno |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 13 | import glob |
Chris Sosa | 9164ca3 | 2012-03-28 18:04:50 | [diff] [blame] | 14 | import os |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 15 | import re |
| 16 | import shutil |
Eric Caruso | e76d187 | 2019-02-21 19:17:45 | [diff] [blame] | 17 | import subprocess |
Gilad Arnold | 0b8c3f3 | 2012-09-19 21:35:44 | [diff] [blame] | 18 | import threading |
Prashanth B | a06d2d2 | 2014-03-07 23:35:19 | [diff] [blame] | 19 | from datetime import datetime |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 20 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 21 | import build_artifact |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 22 | import common_util |
| 23 | import log_util |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 24 | |
xixuan | 44b5545 | 2016-09-06 22:35:56 | [diff] [blame] | 25 | # Make sure that chromite is available to import. |
| 26 | import setup_chromite # pylint: disable=unused-import |
| 27 | |
| 28 | try: |
| 29 | from chromite.lib import gs |
| 30 | except ImportError as e: |
| 31 | gs = None |
| 32 | |
Dan Shi | 72b1613 | 2015-10-08 19:10:33 | [diff] [blame] | 33 | try: |
| 34 | import android_build |
| 35 | except ImportError as e: |
| 36 | # Ignore android_build import failure. This is to support devserver running |
| 37 | # inside a ChromeOS device triggered by cros flash. Most ChromeOS test images |
| 38 | # do not have google-api-python-client module and they don't need to support |
| 39 | # Android updating, therefore, ignore the import failure here. |
| 40 | android_build = None |
| 41 | |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 42 | |
Dan Shi | 6e50c72 | 2013-08-19 22:05:06 | [diff] [blame] | 43 | class DownloaderException(Exception): |
| 44 | """Exception that aggregates all exceptions raised during async download. |
| 45 | |
| 46 | Exceptions could be raised in artifact.Process method, and saved to files. |
| 47 | When caller calls IsStaged to check the downloading progress, devserver can |
| 48 | retrieve the persisted exceptions from the files, wrap them into a |
| 49 | DownloaderException, and raise it. |
| 50 | """ |
| 51 | def __init__(self, exceptions): |
| 52 | """Initialize a DownloaderException instance with a list of exceptions. |
| 53 | |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 54 | Args: |
| 55 | exceptions: Exceptions raised when downloading artifacts. |
Dan Shi | 6e50c72 | 2013-08-19 22:05:06 | [diff] [blame] | 56 | """ |
| 57 | message = 'Exceptions were raised when downloading artifacts.' |
| 58 | Exception.__init__(self, message) |
| 59 | self.exceptions = exceptions |
| 60 | |
| 61 | def __repr__(self): |
| 62 | return self.__str__() |
| 63 | |
| 64 | def __str__(self): |
| 65 | """Return a custom exception message with all exceptions merged.""" |
| 66 | return '--------\n'.join([str(exception) for exception in self.exceptions]) |
| 67 | |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 68 | class Downloader(log_util.Loggable): |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 69 | """Downloader of images to the devsever. |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 70 | |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 71 | This is the base class for different types of downloaders, including |
Dan Shi | 72b1613 | 2015-10-08 19:10:33 | [diff] [blame] | 72 | GoogleStorageDownloader, LocalDownloader and AndroidBuildDownloader. |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 73 | |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 74 | Given a URL to a build on the archive server: |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 75 | - Caches that build and the given artifacts onto the devserver. |
| 76 | - May also initiate caching of related artifacts in the background. |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 77 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 78 | Private class members: |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 79 | static_dir: local filesystem directory to store all artifacts. |
| 80 | build_dir: the local filesystem directory to store artifacts for the given |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 81 | build based on the remote source. |
| 82 | |
| 83 | Public methods must be overridden: |
| 84 | Wait: Verifies the local artifact exists and returns the appropriate names. |
| 85 | Fetch: Downloads artifact from given source to a local directory. |
| 86 | DescribeSource: Gets the source of the download, e.g., a url to GS. |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 87 | """ |
| 88 | |
Alex Miller | a44d502 | 2012-07-27 18:34:16 | [diff] [blame] | 89 | # This filename must be kept in sync with clean_staged_images.py |
| 90 | _TIMESTAMP_FILENAME = 'staged.timestamp' |
Chris Masone | a22d938 | 2012-05-18 19:38:51 | [diff] [blame] | 91 | |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 92 | def __init__(self, static_dir, build_dir, build): |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 93 | super(Downloader, self).__init__() |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 94 | self._static_dir = static_dir |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 95 | self._build_dir = build_dir |
| 96 | self._build = build |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 97 | |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 98 | def GetBuildDir(self): |
| 99 | """Returns the path to where the artifacts will be staged.""" |
| 100 | return self._build_dir |
Simran Basi | 4243a86 | 2014-12-12 20:48:33 | [diff] [blame] | 101 | |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 102 | def GetBuild(self): |
| 103 | """Returns the path to where the artifacts will be staged.""" |
| 104 | return self._build |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 105 | |
Chris Sosa | 9164ca3 | 2012-03-28 18:04:50 | [diff] [blame] | 106 | @staticmethod |
Simran Basi | ef83d6a | 2014-08-28 21:32:01 | [diff] [blame] | 107 | def TouchTimestampForStaged(directory_path): |
Alex Miller | a44d502 | 2012-07-27 18:34:16 | [diff] [blame] | 108 | file_name = os.path.join(directory_path, Downloader._TIMESTAMP_FILENAME) |
| 109 | # Easiest python version of |touch file_name| |
| 110 | with file(file_name, 'a'): |
| 111 | os.utime(file_name, None) |
| 112 | |
Dan Shi | ba0e674 | 2013-06-27 00:39:05 | [diff] [blame] | 113 | @staticmethod |
| 114 | def _TryRemoveStageDir(directory_path): |
Gilad Arnold | 02dc655 | 2013-11-14 19:27:54 | [diff] [blame] | 115 | """If download failed, try to remove the stage dir. |
Dan Shi | ba0e674 | 2013-06-27 00:39:05 | [diff] [blame] | 116 | |
Gilad Arnold | 02dc655 | 2013-11-14 19:27:54 | [diff] [blame] | 117 | If the download attempt failed (ArtifactDownloadError) and staged.timestamp |
| 118 | is the only file in that directory. The build could be non-existing, and |
| 119 | the directory should be removed. |
Dan Shi | ba0e674 | 2013-06-27 00:39:05 | [diff] [blame] | 120 | |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 121 | Args: |
| 122 | directory_path: directory used to stage the image. |
Dan Shi | ba0e674 | 2013-06-27 00:39:05 | [diff] [blame] | 123 | """ |
| 124 | file_name = os.path.join(directory_path, Downloader._TIMESTAMP_FILENAME) |
| 125 | if os.path.exists(file_name) and len(os.listdir(directory_path)) == 1: |
| 126 | os.remove(file_name) |
| 127 | os.rmdir(directory_path) |
| 128 | |
Prashanth B | a06d2d2 | 2014-03-07 23:35:19 | [diff] [blame] | 129 | def ListBuildDir(self): |
| 130 | """List the files in the build directory. |
| 131 | |
| 132 | Only lists files a single level into the build directory. Includes |
| 133 | timestamp information in the listing. |
| 134 | |
| 135 | Returns: |
| 136 | A string with information about the files in the build directory. |
| 137 | None if the build directory doesn't exist. |
| 138 | |
| 139 | Raises: |
| 140 | build_artifact.ArtifactDownloadError: If the build_dir path exists |
| 141 | but is not a directory. |
| 142 | """ |
| 143 | if not os.path.exists(self._build_dir): |
| 144 | return None |
| 145 | if not os.path.isdir(self._build_dir): |
| 146 | raise build_artifact.ArtifactDownloadError( |
| 147 | 'Artifacts %s improperly staged to build_dir path %s. The path is ' |
| 148 | 'not a directory.' % (self._archive_url, self._build_dir)) |
| 149 | |
| 150 | ls_format = collections.namedtuple( |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 151 | 'ls', ['name', 'accessed', 'modified', 'size']) |
Prashanth B | a06d2d2 | 2014-03-07 23:35:19 | [diff] [blame] | 152 | output_format = ('Name: %(name)s Accessed: %(accessed)s ' |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 153 | 'Modified: %(modified)s Size: %(size)s bytes.\n') |
Prashanth B | a06d2d2 | 2014-03-07 23:35:19 | [diff] [blame] | 154 | |
| 155 | build_dir_info = 'Listing contents of :%s \n' % self._build_dir |
| 156 | for file_name in os.listdir(self._build_dir): |
| 157 | file_path = os.path.join(self._build_dir, file_name) |
| 158 | file_info = os.stat(file_path) |
| 159 | ls_info = ls_format(file_path, |
| 160 | datetime.fromtimestamp(file_info.st_atime), |
| 161 | datetime.fromtimestamp(file_info.st_mtime), |
| 162 | file_info.st_size) |
| 163 | build_dir_info += output_format % ls_info._asdict() |
| 164 | return build_dir_info |
| 165 | |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 166 | def Download(self, factory, async=False): |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 167 | """Downloads and caches the |artifacts|. |
Chris Sosa | 9164ca3 | 2012-03-28 18:04:50 | [diff] [blame] | 168 | |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 169 | Downloads and caches the |artifacts|. Returns once these are present on the |
| 170 | devserver. A call to this will attempt to cache non-specified artifacts in |
| 171 | the background following the principle of spatial locality. |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 172 | |
Chris Sosa | 7549080 | 2013-10-01 00:21:45 | [diff] [blame] | 173 | Args: |
Eric Caruso | e76d187 | 2019-02-21 19:17:45 | [diff] [blame] | 174 | factory: The artifact factory. |
| 175 | async: If True, return without waiting for download to complete. |
Chris Sosa | 7549080 | 2013-10-01 00:21:45 | [diff] [blame] | 176 | |
| 177 | Raises: |
Gilad Arnold | 02dc655 | 2013-11-14 19:27:54 | [diff] [blame] | 178 | build_artifact.ArtifactDownloadError: If failed to download the artifact. |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 179 | """ |
Eric Caruso | e76d187 | 2019-02-21 19:17:45 | [diff] [blame] | 180 | try: |
| 181 | common_util.MkDirP(self._build_dir) |
| 182 | except OSError as e: |
| 183 | if e.errno != errno.EACCES: |
| 184 | raise |
| 185 | self._Log('Could not create build dir due to permissions issue. ' |
| 186 | 'Attempting to fix permissions.') |
| 187 | subprocess.Popen(['sudo', 'chown', '-R', |
| 188 | '%s:%s' % (os.getuid(), os.getgid()), |
| 189 | self._static_dir]).wait() |
| 190 | # Then try to create the build dir again. |
| 191 | common_util.MkDirP(self._build_dir) |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 192 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 193 | # We are doing some work on this build -- let's touch it to indicate that |
| 194 | # we shouldn't be cleaning it up anytime soon. |
Simran Basi | ef83d6a | 2014-08-28 21:32:01 | [diff] [blame] | 195 | Downloader.TouchTimestampForStaged(self._build_dir) |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 196 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 197 | # Create factory to create build_artifacts from artifact names. |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 198 | background_artifacts = factory.OptionalArtifacts() |
| 199 | if background_artifacts: |
| 200 | self._DownloadArtifactsInBackground(background_artifacts) |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 201 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 202 | required_artifacts = factory.RequiredArtifacts() |
| 203 | str_repr = [str(a) for a in required_artifacts] |
| 204 | self._Log('Downloading artifacts %s.', ' '.join(str_repr)) |
Dan Shi | e37f8fe | 2013-08-09 23:10:29 | [diff] [blame] | 205 | |
Dan Shi | 6e50c72 | 2013-08-19 22:05:06 | [diff] [blame] | 206 | if async: |
| 207 | self._DownloadArtifactsInBackground(required_artifacts) |
| 208 | else: |
| 209 | self._DownloadArtifactsSerially(required_artifacts, no_wait=True) |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 210 | |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 211 | def IsStaged(self, factory): |
Dan Shi | f8eb0d1 | 2013-08-02 00:52:06 | [diff] [blame] | 212 | """Check if all artifacts have been downloaded. |
| 213 | |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 214 | Args: |
| 215 | factory: An instance of BaseArtifactFactory to be used to check if desired |
| 216 | artifacts or files are staged. |
| 217 | |
| 218 | Returns: |
| 219 | True if all artifacts are staged. |
| 220 | |
| 221 | Raises: |
| 222 | DownloaderException: A wrapper for exceptions raised by any artifact when |
| 223 | calling Process. |
Dan Shi | f8eb0d1 | 2013-08-02 00:52:06 | [diff] [blame] | 224 | """ |
Dan Shi | f8eb0d1 | 2013-08-02 00:52:06 | [diff] [blame] | 225 | required_artifacts = factory.RequiredArtifacts() |
Dan Shi | 6e50c72 | 2013-08-19 22:05:06 | [diff] [blame] | 226 | exceptions = [artifact.GetException() for artifact in required_artifacts if |
| 227 | artifact.GetException()] |
| 228 | if exceptions: |
| 229 | raise DownloaderException(exceptions) |
| 230 | |
Dan Shi | f8eb0d1 | 2013-08-02 00:52:06 | [diff] [blame] | 231 | return all([artifact.ArtifactStaged() for artifact in required_artifacts]) |
| 232 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 233 | def _DownloadArtifactsSerially(self, artifacts, no_wait): |
| 234 | """Simple function to download all the given artifacts serially. |
| 235 | |
Chris Sosa | 7549080 | 2013-10-01 00:21:45 | [diff] [blame] | 236 | Args: |
| 237 | artifacts: A list of build_artifact.BuildArtifact instances to |
| 238 | download. |
| 239 | no_wait: If True, don't block waiting for artifact to exist if we |
| 240 | fail to immediately find it. |
| 241 | |
| 242 | Raises: |
| 243 | build_artifact.ArtifactDownloadError: If we failed to download the |
| 244 | artifact. |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 245 | """ |
Dan Shi | 6e50c72 | 2013-08-19 22:05:06 | [diff] [blame] | 246 | try: |
| 247 | for artifact in artifacts: |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 248 | artifact.Process(self, no_wait) |
Gilad Arnold | 02dc655 | 2013-11-14 19:27:54 | [diff] [blame] | 249 | except build_artifact.ArtifactDownloadError: |
Dan Shi | 6e50c72 | 2013-08-19 22:05:06 | [diff] [blame] | 250 | Downloader._TryRemoveStageDir(self._build_dir) |
| 251 | raise |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 252 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 253 | def _DownloadArtifactsInBackground(self, artifacts): |
| 254 | """Downloads |artifacts| in the background. |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 255 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 256 | Downloads |artifacts| in the background. As these are backgrounded |
| 257 | artifacts, they are done best effort and may not exist. |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 258 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 259 | Args: |
| 260 | artifacts: List of build_artifact.BuildArtifact instances to download. |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 261 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 262 | self._Log('Invoking background download of artifacts for %r', artifacts) |
| 263 | thread = threading.Thread(target=self._DownloadArtifactsSerially, |
| 264 | args=(artifacts, False)) |
| 265 | thread.start() |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 266 | |
| 267 | def Wait(self, name, is_regex_name, timeout): |
| 268 | """Waits for artifact to exist and returns the appropriate names. |
| 269 | |
| 270 | Args: |
| 271 | name: Name to look at. |
| 272 | is_regex_name: True if the name is a regex pattern. |
| 273 | timeout: How long to wait for the artifact to become available. |
| 274 | |
| 275 | Returns: |
| 276 | A list of names that match. |
| 277 | """ |
| 278 | raise NotImplementedError() |
| 279 | |
| 280 | def Fetch(self, remote_name, local_path): |
| 281 | """Downloads artifact from given source to a local directory. |
| 282 | |
| 283 | Args: |
| 284 | remote_name: Remote name of the file to fetch. |
| 285 | local_path: Local path to the folder to store fetched file. |
| 286 | |
| 287 | Returns: |
| 288 | The path to fetched file. |
| 289 | """ |
| 290 | raise NotImplementedError() |
| 291 | |
| 292 | def DescribeSource(self): |
| 293 | """Gets the source of the download, e.g., a url to GS.""" |
| 294 | raise NotImplementedError() |
| 295 | |
| 296 | |
| 297 | class GoogleStorageDownloader(Downloader): |
| 298 | """Downloader of images to the devserver from Google Storage. |
| 299 | |
| 300 | Given a URL to a build on the archive server: |
| 301 | - Caches that build and the given artifacts onto the devserver. |
| 302 | - May also initiate caching of related artifacts in the background. |
| 303 | |
| 304 | This is intended to be used with ChromeOS. |
| 305 | |
| 306 | Private class members: |
| 307 | archive_url: Google Storage URL to download build artifacts from. |
| 308 | """ |
| 309 | |
Luis Hector Chavez | dca9dd7 | 2018-06-12 19:56:30 | [diff] [blame] | 310 | def __init__(self, static_dir, archive_url, build_id): |
| 311 | build = build_id.split('/')[-1] |
| 312 | build_dir = os.path.join(static_dir, build_id) |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 313 | |
| 314 | super(GoogleStorageDownloader, self).__init__(static_dir, build_dir, build) |
| 315 | |
| 316 | self._archive_url = archive_url |
| 317 | |
xixuan | 178263c | 2017-03-22 16:10:25 | [diff] [blame] | 318 | if common_util.IsRunningOnMoblab(): |
| 319 | self._ctx = gs.GSContext(cache_user='chronos') if gs else None |
| 320 | else: |
| 321 | self._ctx = gs.GSContext() if gs else None |
xixuan | 44b5545 | 2016-09-06 22:35:56 | [diff] [blame] | 322 | |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 323 | def Wait(self, name, is_regex_name, timeout): |
| 324 | """Waits for artifact to exist and returns the appropriate names. |
| 325 | |
| 326 | Args: |
| 327 | name: Name to look at. |
| 328 | is_regex_name: True if the name is a regex pattern. |
| 329 | timeout: How long to wait for the artifact to become available. |
| 330 | |
| 331 | Returns: |
| 332 | A list of names that match. |
| 333 | |
| 334 | Raises: |
| 335 | ArtifactDownloadError: An error occurred when obtaining artifact. |
| 336 | """ |
xixuan | 44b5545 | 2016-09-06 22:35:56 | [diff] [blame] | 337 | names = self._ctx.GetGsNamesWithWait( |
| 338 | name, self._archive_url, timeout=timeout, |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 339 | is_regex_pattern=is_regex_name) |
| 340 | if not names: |
| 341 | raise build_artifact.ArtifactDownloadError( |
| 342 | 'Could not find %s in Google Storage at %s' % |
| 343 | (name, self._archive_url)) |
| 344 | return names |
| 345 | |
| 346 | def Fetch(self, remote_name, local_path): |
| 347 | """Downloads artifact from Google Storage to a local directory.""" |
| 348 | install_path = os.path.join(local_path, remote_name) |
| 349 | gs_path = '/'.join([self._archive_url, remote_name]) |
xixuan | 44b5545 | 2016-09-06 22:35:56 | [diff] [blame] | 350 | self._ctx.Copy(gs_path, local_path) |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 351 | return install_path |
| 352 | |
| 353 | def DescribeSource(self): |
| 354 | return self._archive_url |
| 355 | |
Luis Hector Chavez | dca9dd7 | 2018-06-12 19:56:30 | [diff] [blame] | 356 | @staticmethod |
| 357 | def GetBuildIdFromArchiveURL(archive_url): |
| 358 | """Extracts the build ID from the archive URL. |
| 359 | |
| 360 | The archive_url is of the form gs://server/[some_path/target]/...]/build |
| 361 | This function discards 'gs://server/' and extracts the [some_path/target] |
| 362 | as rel_path and the build as build. |
| 363 | """ |
| 364 | sub_url = archive_url.partition('://')[2] |
| 365 | split_sub_url = sub_url.split('/') |
| 366 | return '/'.join(split_sub_url[1:]) |
| 367 | |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 368 | |
| 369 | class LocalDownloader(Downloader): |
| 370 | """Downloader of images to the devserver from local storage. |
| 371 | |
| 372 | Given a local path: |
| 373 | - Caches that build and the given artifacts onto the devserver. |
| 374 | - May also initiate caching of related artifacts in the background. |
| 375 | |
| 376 | Private class members: |
| 377 | archive_params: parameters for where to download build artifacts from. |
| 378 | """ |
| 379 | |
Prathmesh Prabhu | 58d0893 | 2018-01-19 23:08:19 | [diff] [blame] | 380 | def __init__(self, static_dir, source_path, delete_source=False): |
| 381 | """Initialize us. |
| 382 | |
| 383 | Args: |
| 384 | static_dir: The directory where artifacts are to be staged. |
| 385 | source_path: The source path to copy artifacts from. |
| 386 | delete_source: If True, delete the source files. This mode is faster than |
| 387 | actually copying because it allows us to simply move the files. |
| 388 | """ |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 389 | # The local path is of the form /{path to static dir}/{rel_path}/{build}. |
| 390 | # local_path must be a subpath of the static directory. |
| 391 | self.source_path = source_path |
Prathmesh Prabhu | 58d0893 | 2018-01-19 23:08:19 | [diff] [blame] | 392 | self._move_files = delete_source |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 393 | rel_path = os.path.basename(os.path.dirname(source_path)) |
| 394 | build = os.path.basename(source_path) |
| 395 | build_dir = os.path.join(static_dir, rel_path, build) |
| 396 | |
| 397 | super(LocalDownloader, self).__init__(static_dir, build_dir, build) |
| 398 | |
| 399 | def Wait(self, name, is_regex_name, timeout): |
| 400 | """Verifies the local artifact exists and returns the appropriate names. |
| 401 | |
| 402 | Args: |
| 403 | name: Name to look at. |
| 404 | is_regex_name: True if the name is a regex pattern. |
| 405 | timeout: How long to wait for the artifact to become available. |
| 406 | |
| 407 | Returns: |
| 408 | A list of names that match. |
| 409 | |
| 410 | Raises: |
| 411 | ArtifactDownloadError: An error occurred when obtaining artifact. |
| 412 | """ |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 413 | if is_regex_name: |
| 414 | filter_re = re.compile(name) |
Prathmesh Prabhu | bee63be | 2018-02-10 07:28:24 | [diff] [blame] | 415 | artifacts = [f for f in os.listdir(self.source_path) if |
| 416 | filter_re.match(f)] |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 417 | else: |
Prathmesh Prabhu | bee63be | 2018-02-10 07:28:24 | [diff] [blame] | 418 | glob_search = glob.glob(os.path.join(self.source_path, name)) |
| 419 | artifacts = [os.path.basename(g) for g in glob_search] |
| 420 | |
| 421 | if not artifacts: |
| 422 | raise build_artifact.ArtifactDownloadError( |
| 423 | 'Artifact %s not found at %s(regex_match: %s)' |
| 424 | % (name, self.source_path, is_regex_name)) |
| 425 | return artifacts |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 426 | |
| 427 | def Fetch(self, remote_name, local_path): |
| 428 | """Downloads artifact from Google Storage to a local directory.""" |
| 429 | install_path = os.path.join(local_path, remote_name) |
Prathmesh Prabhu | 58d0893 | 2018-01-19 23:08:19 | [diff] [blame] | 430 | src_path = os.path.join(self.source_path, remote_name) |
| 431 | if self._move_files: |
| 432 | shutil.move(src_path, install_path) |
| 433 | else: |
| 434 | shutil.copyfile(src_path, install_path) |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 435 | return install_path |
| 436 | |
| 437 | def DescribeSource(self): |
| 438 | return self.source_path |
| 439 | |
| 440 | |
Dan Shi | 72b1613 | 2015-10-08 19:10:33 | [diff] [blame] | 441 | class AndroidBuildDownloader(Downloader): |
| 442 | """Downloader of images to the devserver from Android's build server.""" |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 443 | |
Dan Shi | 72b1613 | 2015-10-08 19:10:33 | [diff] [blame] | 444 | def __init__(self, static_dir, branch, build_id, target): |
| 445 | """Initialize AndroidBuildDownloader. |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 446 | |
| 447 | Args: |
| 448 | static_dir: Root directory to store the build. |
Dan Shi | 72b1613 | 2015-10-08 19:10:33 | [diff] [blame] | 449 | branch: Branch for the build. Download will always verify if the given |
| 450 | build id is for the branch. |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 451 | build_id: Build id of the Android build, e.g., 2155602. |
| 452 | target: Target of the Android build, e.g., shamu-userdebug. |
| 453 | """ |
Dan Shi | 72b1613 | 2015-10-08 19:10:33 | [diff] [blame] | 454 | build = '%s/%s/%s' % (branch, target, build_id) |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 455 | build_dir = os.path.join(static_dir, '', build) |
| 456 | |
Dan Shi | 72b1613 | 2015-10-08 19:10:33 | [diff] [blame] | 457 | self.branch = branch |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 458 | self.build_id = build_id |
| 459 | self.target = target |
| 460 | |
Dan Shi | 72b1613 | 2015-10-08 19:10:33 | [diff] [blame] | 461 | super(AndroidBuildDownloader, self).__init__(static_dir, build_dir, build) |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 462 | |
| 463 | def Wait(self, name, is_regex_name, timeout): |
| 464 | """Verifies the local artifact exists and returns the appropriate names. |
| 465 | |
| 466 | Args: |
| 467 | name: Name to look at. |
| 468 | is_regex_name: True if the name is a regex pattern. |
| 469 | timeout: How long to wait for the artifact to become available. |
| 470 | |
| 471 | Returns: |
| 472 | A list of names that match. |
| 473 | |
| 474 | Raises: |
| 475 | ArtifactDownloadError: An error occurred when obtaining artifact. |
| 476 | """ |
Dan Shi | 72b1613 | 2015-10-08 19:10:33 | [diff] [blame] | 477 | artifacts = android_build.BuildAccessor.GetArtifacts( |
| 478 | branch=self.branch, build_id=self.build_id, target=self.target) |
| 479 | |
| 480 | names = [] |
| 481 | for artifact_name in [a['name'] for a in artifacts]: |
| 482 | match = (re.match(name, artifact_name) if is_regex_name |
| 483 | else name == artifact_name) |
| 484 | if match: |
| 485 | names.append(artifact_name) |
| 486 | |
| 487 | if not names: |
| 488 | raise build_artifact.ArtifactDownloadError( |
Dan Shi | 9ee5dc2 | 2017-06-27 18:53:07 | [diff] [blame] | 489 | 'No artifact found with given name: %s for %s-%s. All available ' |
| 490 | 'artifacts are: %s' % |
| 491 | (name, self.target, self.build_id, |
| 492 | ','.join([a['name'] for a in artifacts]))) |
Dan Shi | 72b1613 | 2015-10-08 19:10:33 | [diff] [blame] | 493 | |
| 494 | return names |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 495 | |
| 496 | def Fetch(self, remote_name, local_path): |
Dan Shi | 72b1613 | 2015-10-08 19:10:33 | [diff] [blame] | 497 | """Downloads artifact from Android's build server to a local directory.""" |
| 498 | dest_file = os.path.join(local_path, remote_name) |
| 499 | android_build.BuildAccessor.Download( |
| 500 | branch=self.branch, build_id=self.build_id, target=self.target, |
| 501 | resource_id=remote_name, dest_file=dest_file) |
| 502 | return dest_file |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 503 | |
| 504 | def DescribeSource(self): |
Dan Shi | 72b1613 | 2015-10-08 19:10:33 | [diff] [blame] | 505 | return '%s/%s/%s/%s' % (android_build.DEFAULT_BUILDER, self.branch, |
| 506 | self.target, self.build_id) |