joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 1 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | import datetime |
| 6 | import operator |
| 7 | import os |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 8 | import re |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 9 | import shutil |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 10 | import time |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 11 | import threading |
| 12 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 13 | import build_util |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 14 | import artifact_info |
| 15 | import build_artifact |
| 16 | import common_util |
| 17 | import devserver_constants |
| 18 | import downloader |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 19 | import gsutil_util |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 20 | import log_util |
| 21 | |
| 22 | # Module-local log function. |
| 23 | def _Log(message, *args): |
| 24 | return log_util.LogWithTag('XBUDDY', message, *args) |
| 25 | |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 26 | _XBUDDY_CAPACITY = 5 |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 27 | |
| 28 | # Local build constants |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 29 | LATEST = "latest" |
| 30 | LOCAL = "local" |
| 31 | REMOTE = "remote" |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 32 | LOCAL_ALIASES = [ |
| 33 | 'test', |
| 34 | 'base', |
| 35 | 'dev', |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 36 | 'full_payload', |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 37 | ] |
| 38 | |
| 39 | LOCAL_FILE_NAMES = [ |
| 40 | devserver_constants.TEST_IMAGE_FILE, |
| 41 | devserver_constants.BASE_IMAGE_FILE, |
| 42 | devserver_constants.IMAGE_FILE, |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 43 | devserver_constants.ROOT_UPDATE_FILE, |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 44 | ] |
| 45 | |
| 46 | LOCAL_ALIAS_TO_FILENAME = dict(zip(LOCAL_ALIASES, LOCAL_FILE_NAMES)) |
| 47 | |
| 48 | # Google Storage constants |
| 49 | GS_ALIASES = [ |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 50 | 'test', |
| 51 | 'base', |
| 52 | 'recovery', |
| 53 | 'full_payload', |
| 54 | 'stateful', |
| 55 | 'autotest', |
| 56 | ] |
| 57 | |
| 58 | # TODO(joyc) these should become devserver constants. |
| 59 | # currently, storage locations are embedded in the artifact classes defined in |
| 60 | # build_artifact |
| 61 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 62 | GS_FILE_NAMES = [ |
| 63 | devserver_constants.TEST_IMAGE_FILE, |
| 64 | devserver_constants.BASE_IMAGE_FILE, |
| 65 | devserver_constants.RECOVERY_IMAGE_FILE, |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 66 | devserver_constants.ROOT_UPDATE_FILE, |
| 67 | build_artifact.STATEFUL_UPDATE_FILE, |
| 68 | devserver_constants.AUTOTEST_DIR, |
| 69 | ] |
| 70 | |
| 71 | ARTIFACTS = [ |
| 72 | artifact_info.TEST_IMAGE, |
| 73 | artifact_info.BASE_IMAGE, |
| 74 | artifact_info.RECOVERY_IMAGE, |
| 75 | artifact_info.FULL_PAYLOAD, |
| 76 | artifact_info.STATEFUL_PAYLOAD, |
| 77 | artifact_info.AUTOTEST, |
| 78 | ] |
| 79 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 80 | GS_ALIAS_TO_FILENAME = dict(zip(GS_ALIASES, GS_FILE_NAMES)) |
| 81 | GS_ALIAS_TO_ARTIFACT = dict(zip(GS_ALIASES, ARTIFACTS)) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 82 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 83 | LATEST_OFFICIAL = "latest-official" |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 84 | |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 85 | RELEASE = "release" |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 86 | |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 87 | |
| 88 | class XBuddyException(Exception): |
| 89 | """Exception classes used by this module.""" |
| 90 | pass |
| 91 | |
| 92 | |
| 93 | # no __init__ method |
| 94 | #pylint: disable=W0232 |
| 95 | class Timestamp(): |
| 96 | """Class to translate build path strings and timestamp filenames.""" |
| 97 | |
| 98 | _TIMESTAMP_DELIMITER = 'SLASH' |
| 99 | XBUDDY_TIMESTAMP_DIR = 'xbuddy_UpdateTimestamps' |
| 100 | |
| 101 | @staticmethod |
| 102 | def TimestampToBuild(timestamp_filename): |
| 103 | return timestamp_filename.replace(Timestamp._TIMESTAMP_DELIMITER, '/') |
| 104 | |
| 105 | @staticmethod |
| 106 | def BuildToTimestamp(build_path): |
| 107 | return build_path.replace('/', Timestamp._TIMESTAMP_DELIMITER) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 108 | |
| 109 | @staticmethod |
| 110 | def UpdateTimestamp(timestamp_dir, build_id): |
| 111 | """Update timestamp file of build with build_id.""" |
| 112 | common_util.MkDirP(timestamp_dir) |
| 113 | time_file = os.path.join(timestamp_dir, |
| 114 | Timestamp.BuildToTimestamp(build_id)) |
| 115 | with file(time_file, 'a'): |
| 116 | os.utime(time_file, None) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 117 | #pylint: enable=W0232 |
| 118 | |
| 119 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 120 | class XBuddy(build_util.BuildObject): |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 121 | """Class that manages image retrieval and caching by the devserver. |
| 122 | |
| 123 | Image retrieval by xBuddy path: |
| 124 | XBuddy accesses images and artifacts that it stores using an xBuddy |
| 125 | path of the form: board/version/alias |
| 126 | The primary xbuddy.Get call retrieves the correct artifact or url to where |
| 127 | the artifacts can be found. |
| 128 | |
| 129 | Image caching: |
| 130 | Images and other artifacts are stored identically to how they would have |
| 131 | been if devserver's stage rpc was called and the xBuddy cache replaces |
| 132 | build versions on a LRU basis. Timestamps are maintained by last accessed |
| 133 | times of representative files in the a directory in the static serve |
| 134 | directory (XBUDDY_TIMESTAMP_DIR). |
| 135 | |
| 136 | Private class members: |
| 137 | _true_values - used for interpreting boolean values |
| 138 | _staging_thread_count - track download requests |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 139 | _timestamp_folder - directory with empty files standing in as timestamps |
| 140 | for each image currently cached by xBuddy |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 141 | """ |
| 142 | _true_values = ['true', 't', 'yes', 'y'] |
| 143 | |
| 144 | # Number of threads that are staging images. |
| 145 | _staging_thread_count = 0 |
| 146 | # Lock used to lock increasing/decreasing count. |
| 147 | _staging_thread_count_lock = threading.Lock() |
| 148 | |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 149 | def __init__(self, manage_builds=False, **kwargs): |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 150 | super(XBuddy, self).__init__(**kwargs) |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 151 | self._manage_builds = manage_builds |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 152 | self._timestamp_folder = os.path.join(self.static_dir, |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 153 | Timestamp.XBUDDY_TIMESTAMP_DIR) |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 154 | common_util.MkDirP(self._timestamp_folder) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 155 | |
| 156 | @classmethod |
| 157 | def ParseBoolean(cls, boolean_string): |
| 158 | """Evaluate a string to a boolean value""" |
| 159 | if boolean_string: |
| 160 | return boolean_string.lower() in cls._true_values |
| 161 | else: |
| 162 | return False |
| 163 | |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 164 | def _LookupOfficial(self, board, suffix=RELEASE): |
| 165 | """Check LATEST-master for the version number of interest.""" |
| 166 | _Log("Checking gs for latest %s-%s image", board, suffix) |
| 167 | latest_addr = devserver_constants.GS_LATEST_MASTER % {'board':board, |
| 168 | 'suffix':suffix} |
| 169 | cmd = 'gsutil cat %s' % latest_addr |
| 170 | msg = 'Failed to find build at %s' % latest_addr |
| 171 | # Full release + version is in the LATEST file |
| 172 | version = gsutil_util.GSUtilRun(cmd, msg) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 173 | |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 174 | return devserver_constants.IMAGE_DIR % {'board':board, |
| 175 | 'suffix':suffix, |
| 176 | 'version':version} |
| 177 | |
| 178 | def _LookupChannel(self, board, channel='stable'): |
| 179 | """Check the channel folder for the version number of interest.""" |
| 180 | # Get all names in channel dir. Get 10 highest directories by version |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 181 | _Log("Checking channel '%s' for latest '%s' image", channel, board) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 182 | channel_dir = devserver_constants.GS_CHANNEL_DIR % {'channel':channel, |
| 183 | 'board':board} |
| 184 | latest_version = gsutil_util.GetLatestVersionFromGSDir(channel_dir) |
| 185 | |
| 186 | # Figure out release number from the version number |
| 187 | image_url = devserver_constants.IMAGE_DIR % {'board':board, |
| 188 | 'suffix':RELEASE, |
| 189 | 'version':'R*'+latest_version} |
| 190 | image_dir = os.path.join(devserver_constants.GS_IMAGE_DIR, image_url) |
| 191 | |
| 192 | # There should only be one match on cros-image-archive. |
| 193 | full_version = gsutil_util.GetLatestVersionFromGSDir(image_dir) |
| 194 | |
| 195 | return devserver_constants.IMAGE_DIR % {'board':board, |
| 196 | 'suffix':RELEASE, |
| 197 | 'version':full_version} |
| 198 | |
| 199 | def _LookupVersion(self, board, version): |
| 200 | """Search GS image releases for the highest match to a version prefix.""" |
| 201 | # Build the pattern for GS to match |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 202 | _Log("Checking gs for latest '%s' image with prefix '%s'", board, version) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 203 | image_url = devserver_constants.IMAGE_DIR % {'board':board, |
| 204 | 'suffix':RELEASE, |
| 205 | 'version':version + '*'} |
| 206 | image_dir = os.path.join(devserver_constants.GS_IMAGE_DIR, image_url) |
| 207 | |
| 208 | # grab the newest version of the ones matched |
| 209 | full_version = gsutil_util.GetLatestVersionFromGSDir(image_dir) |
| 210 | return devserver_constants.IMAGE_DIR % {'board':board, |
| 211 | 'suffix':RELEASE, |
| 212 | 'version':full_version} |
| 213 | |
| 214 | def _ResolveVersionToUrl(self, board, version): |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 215 | """ |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 216 | Handle version aliases for remote payloads in GS. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 217 | |
| 218 | Args: |
| 219 | board: as specified in the original call. (i.e. x86-generic, parrot) |
| 220 | version: as entered in the original call. can be |
| 221 | {TBD, 0. some custom alias as defined in a config file} |
| 222 | 1. latest |
| 223 | 2. latest-{channel} |
| 224 | 3. latest-official-{board suffix} |
| 225 | 4. version prefix (i.e. RX-Y.X, RX-Y, RX) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 226 | |
| 227 | Returns: |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 228 | image_url is where the image dir is actually found on GS |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 229 | |
| 230 | """ |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 231 | # TODO (joychen) Convert separate calls to a dict + error out bad paths |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 232 | |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 233 | # Only the last segment of the alias is variable relative to the rest. |
| 234 | version_tuple = version.rsplit('-', 1) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 235 | |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 236 | if re.match(devserver_constants.VERSION_RE, version): |
| 237 | # This is supposed to be a complete version number on GS. Return it. |
| 238 | return devserver_constants.IMAGE_DIR % {'board':board, |
| 239 | 'suffix':RELEASE, |
| 240 | 'version':version} |
| 241 | elif version == LATEST_OFFICIAL: |
| 242 | # latest-official --> LATEST build in board-release |
| 243 | return self._LookupOfficial(board) |
| 244 | elif version_tuple[0] == LATEST_OFFICIAL: |
| 245 | # latest-official-{suffix} --> LATEST build in board-{suffix} |
| 246 | return self._LookupOfficial(board, version_tuple[1]) |
| 247 | elif version == LATEST: |
| 248 | # latest --> latest build on stable channel |
| 249 | return self._LookupChannel(board) |
| 250 | elif version_tuple[0] == LATEST: |
| 251 | if re.match(devserver_constants.VERSION_RE, version_tuple[1]): |
| 252 | # latest-R* --> most recent qualifying build |
| 253 | return self._LookupVersion(board, version_tuple[1]) |
| 254 | else: |
| 255 | # latest-{channel} --> latest build within that channel |
| 256 | return self._LookupChannel(board, version_tuple[1]) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 257 | else: |
| 258 | # The given version doesn't match any known patterns. |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 259 | raise XBuddyException("Version %s unknown. Can't find on GS." % version) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 260 | |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 261 | @staticmethod |
| 262 | def _Symlink(link, target): |
| 263 | """Symlinks link to target, and removes whatever link was there before.""" |
| 264 | _Log("Linking to %s from %s", link, target) |
| 265 | if os.path.lexists(link): |
| 266 | os.unlink(link) |
| 267 | os.symlink(target, link) |
| 268 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 269 | def _GetLatestLocalVersion(self, board, file_name): |
| 270 | """Get the version of the latest image built for board by build_image |
| 271 | |
| 272 | Updates the symlink reference within the xBuddy static dir to point to |
| 273 | the real image dir in the local /build/images directory. |
| 274 | |
| 275 | Args: |
| 276 | board - board-suffix |
| 277 | file_name - the filename of the image we have cached |
| 278 | |
| 279 | Returns: |
| 280 | version - the discovered version of the image. |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 281 | found - True if file was found |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 282 | """ |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 283 | latest_local_dir = self.GetLatestImageDir(board) |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 284 | if not latest_local_dir and os.path.exists(latest_local_dir): |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 285 | raise XBuddyException('No builds found for %s. Did you run build_image?' % |
| 286 | board) |
| 287 | |
| 288 | # assume that the version number is the name of the directory |
| 289 | version = os.path.basename(latest_local_dir) |
| 290 | |
| 291 | path_to_image = os.path.join(latest_local_dir, file_name) |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 292 | if os.path.exists(path_to_image): |
| 293 | return version, True |
| 294 | else: |
| 295 | return version, False |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 296 | |
| 297 | def _InterpretPath(self, path_list): |
| 298 | """ |
| 299 | Split and return the pieces of an xBuddy path name |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 300 | |
| 301 | input: |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 302 | path_list: the segments of the path xBuddy Get was called with. |
| 303 | Documentation of path_list can be found in devserver.py:xbuddy |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 304 | |
| 305 | Return: |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 306 | tuple of (image_type, board, version) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 307 | |
| 308 | Raises: |
| 309 | XBuddyException: if the path can't be resolved into valid components |
| 310 | """ |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 311 | path_list = list(path_list) |
| 312 | |
| 313 | # Required parts of path parsing. |
| 314 | try: |
| 315 | # Determine if image is explicitly local or remote. |
| 316 | is_local = False |
| 317 | if path_list[0] == LOCAL: |
| 318 | path_list.pop(0) |
| 319 | is_local = True |
| 320 | elif path_list[0] == REMOTE: |
| 321 | path_list.pop(0) |
| 322 | _Log(str(path_list)) |
| 323 | |
| 324 | # Set board |
| 325 | board = path_list.pop(0) |
| 326 | _Log(str(path_list)) |
| 327 | |
| 328 | # Set defaults |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 329 | version = LATEST |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 330 | image_type = GS_ALIASES[0] |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 331 | except IndexError: |
| 332 | msg = "Specify at least the board in your xBuddy call. Your path: %s" |
| 333 | raise XBuddyException(msg % os.path.join(path_list)) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 334 | |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 335 | # Read as much of the xBuddy path as possible |
| 336 | try: |
| 337 | # Override default if terminal is a valid artifact alias or a version |
| 338 | terminal = path_list[-1] |
| 339 | if terminal in GS_ALIASES + LOCAL_ALIASES: |
| 340 | image_type = terminal |
| 341 | version = path_list[-2] |
| 342 | else: |
| 343 | version = terminal |
| 344 | except IndexError: |
| 345 | # This path doesn't have an alias or a version. That's fine. |
| 346 | _Log("Some parts of the path not specified. Using defaults.") |
| 347 | |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 348 | _Log("Get artifact '%s' in '%s/%s'. Locally? %s", |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 349 | image_type, board, version, is_local) |
| 350 | |
| 351 | return image_type, board, version, is_local |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 352 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 353 | def _SyncRegistryWithBuildImages(self): |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 354 | """ Crawl images_dir for build_ids of images generated from build_image. |
| 355 | |
| 356 | This will find images and symlink them in xBuddy's static dir so that |
| 357 | xBuddy's cache can serve them. |
| 358 | If xBuddy's _manage_builds option is on, then a timestamp will also be |
| 359 | generated, and xBuddy will clear them from the directory they are in, as |
| 360 | necessary. |
| 361 | """ |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 362 | build_ids = [] |
| 363 | for b in os.listdir(self.images_dir): |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 364 | # Ensure we have directories to track all boards in build/images |
| 365 | common_util.MkDirP(os.path.join(self.static_dir, b)) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 366 | board_dir = os.path.join(self.images_dir, b) |
| 367 | build_ids.extend(['/'.join([b, v]) for v |
| 368 | in os.listdir(board_dir) if not v==LATEST]) |
| 369 | |
| 370 | # Check currently registered images |
| 371 | for f in os.listdir(self._timestamp_folder): |
| 372 | build_id = Timestamp.TimestampToBuild(f) |
| 373 | if build_id in build_ids: |
| 374 | build_ids.remove(build_id) |
| 375 | |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 376 | # Symlink undiscovered images, and update timestamps if manage_builds is on |
| 377 | for build_id in build_ids: |
| 378 | link = os.path.join(self.static_dir, build_id) |
| 379 | target = os.path.join(self.images_dir, build_id) |
| 380 | XBuddy._Symlink(link, target) |
| 381 | if self._manage_builds: |
| 382 | Timestamp.UpdateTimestamp(self._timestamp_folder, build_id) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 383 | |
| 384 | def _ListBuildTimes(self): |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 385 | """ Returns the currently cached builds and their last access timestamp. |
| 386 | |
| 387 | Returns: |
| 388 | list of tuples that matches xBuddy build/version to timestamps in long |
| 389 | """ |
| 390 | # update currently cached builds |
| 391 | build_dict = {} |
| 392 | |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 393 | for f in os.listdir(self._timestamp_folder): |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 394 | last_accessed = os.path.getmtime(os.path.join(self._timestamp_folder, f)) |
| 395 | build_id = Timestamp.TimestampToBuild(f) |
| 396 | stale_time = datetime.timedelta(seconds = (time.time()-last_accessed)) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 397 | build_dict[build_id] = stale_time |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 398 | return_tup = sorted(build_dict.iteritems(), key=operator.itemgetter(1)) |
| 399 | return return_tup |
| 400 | |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 401 | def _Download(self, gs_url, artifact): |
| 402 | """Download the single artifact from the given gs_url.""" |
| 403 | with XBuddy._staging_thread_count_lock: |
| 404 | XBuddy._staging_thread_count += 1 |
| 405 | try: |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 406 | _Log("Downloading '%s' from '%s'", artifact, gs_url) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 407 | downloader.Downloader(self.static_dir, gs_url).Download( |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 408 | [artifact]) |
| 409 | finally: |
| 410 | with XBuddy._staging_thread_count_lock: |
| 411 | XBuddy._staging_thread_count -= 1 |
| 412 | |
| 413 | def _CleanCache(self): |
| 414 | """Delete all builds besides the first _XBUDDY_CAPACITY builds""" |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 415 | cached_builds = [e[0] for e in self._ListBuildTimes()] |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 416 | _Log('In cache now: %s', cached_builds) |
| 417 | |
| 418 | for b in range(_XBUDDY_CAPACITY, len(cached_builds)): |
| 419 | b_path = cached_builds[b] |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 420 | _Log("Clearing '%s' from cache", b_path) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 421 | |
| 422 | time_file = os.path.join(self._timestamp_folder, |
| 423 | Timestamp.BuildToTimestamp(b_path)) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 424 | os.unlink(time_file) |
| 425 | clear_dir = os.path.join(self.static_dir, b_path) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 426 | try: |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 427 | # handle symlinks, in the case of links to local builds if enabled |
| 428 | if self._manage_builds and os.path.islink(clear_dir): |
| 429 | target = os.readlink(clear_dir) |
| 430 | _Log('Deleting locally built image at %s', target) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 431 | |
| 432 | os.unlink(clear_dir) |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 433 | if os.path.exists(target): |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 434 | shutil.rmtree(target) |
| 435 | elif os.path.exists(clear_dir): |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 436 | _Log('Deleting downloaded image at %s', clear_dir) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 437 | shutil.rmtree(clear_dir) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 438 | |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 439 | except Exception: |
| 440 | raise XBuddyException('Failed to clear build in %s.' % clear_dir) |
| 441 | |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 442 | def _GetFromGS(self, build_id, image_type, lookup_only): |
| 443 | """Check if the artifact is available locally. Download from GS if not. |
| 444 | |
| 445 | Return: |
| 446 | boolean - True if cached. |
| 447 | """ |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 448 | gs_url = os.path.join(devserver_constants.GS_IMAGE_DIR, |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 449 | build_id) |
| 450 | |
| 451 | # stage image if not found in cache |
| 452 | file_name = GS_ALIAS_TO_FILENAME[image_type] |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 453 | file_loc = os.path.join(self.static_dir, build_id, file_name) |
| 454 | cached = os.path.exists(file_loc) |
| 455 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 456 | if not cached: |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 457 | if lookup_only: |
| 458 | return False |
| 459 | else: |
| 460 | artifact = GS_ALIAS_TO_ARTIFACT[image_type] |
| 461 | self._Download(gs_url, artifact) |
| 462 | return True |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 463 | else: |
| 464 | _Log('Image already cached.') |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 465 | return True |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 466 | |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 467 | def _GetArtifact(self, path, lookup_only=False): |
| 468 | """Interpret an xBuddy path and return directory/file_name to resource. |
| 469 | |
| 470 | Returns: |
| 471 | image_url to the directory |
| 472 | file_name of the artifact |
| 473 | found = True if the artifact is cached |
| 474 | |
| 475 | Raises: |
| 476 | XBuddyException if the path could not be translated |
| 477 | """ |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 478 | image_type, board, version, is_local = self._InterpretPath(path) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 479 | |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 480 | found = False |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 481 | if is_local: |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 482 | # Get a local image |
| 483 | if image_type not in LOCAL_ALIASES: |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 484 | raise XBuddyException('Bad local image type: %s. Use one of: %s' % |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 485 | (image_type, LOCAL_ALIASES)) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 486 | file_name = LOCAL_ALIAS_TO_FILENAME[image_type] |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 487 | |
| 488 | if version == LATEST: |
| 489 | # Get the latest local image for the given board |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 490 | version, found = self._GetLatestLocalVersion(board, file_name) |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 491 | else: |
| 492 | # An exact version path in build/images was specified for this board |
| 493 | local_file = os.path.join(self.images_dir, board, version, file_name) |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 494 | if os.path.exists(local_file): |
| 495 | found = True |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 496 | |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 497 | image_url = os.path.join(board, version) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 498 | else: |
| 499 | # Get a remote image |
| 500 | if image_type not in GS_ALIASES: |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 501 | raise XBuddyException('Bad remote image type: %s. Use one of: %s' % |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 502 | (image_type, GS_ALIASES)) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 503 | file_name = GS_ALIAS_TO_FILENAME[image_type] |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 504 | |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 505 | # Interpret the version (alias), and get gs address |
| 506 | image_url = self._ResolveVersionToUrl(board, version) |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 507 | found = self._GetFromGS(image_url, image_type, lookup_only) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 508 | |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 509 | return image_url, file_name, found |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 510 | |
| 511 | ############################ BEGIN PUBLIC METHODS |
| 512 | |
| 513 | def List(self): |
| 514 | """Lists the currently available images & time since last access.""" |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 515 | self._SyncRegistryWithBuildImages() |
| 516 | builds = self._ListBuildTimes() |
| 517 | return_string = '' |
| 518 | for build, timestamp in builds: |
| 519 | return_string += '<b>' + build + '</b> ' |
| 520 | return_string += '(time since last access: ' + str(timestamp) + ')<br>' |
| 521 | return return_string |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 522 | |
| 523 | def Capacity(self): |
| 524 | """Returns the number of images cached by xBuddy.""" |
| 525 | return str(_XBUDDY_CAPACITY) |
| 526 | |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 527 | def Translate(self, path_list): |
| 528 | """Translates an xBuddy path to a real path to artifact if it exists. |
| 529 | |
| 530 | Equivalent to the Get call, minus downloading and updating timestamps. |
| 531 | The returned path is always the path to the directory. |
| 532 | |
| 533 | Throws: |
| 534 | XBuddyException - if the path couldn't be translated |
| 535 | """ |
| 536 | self._SyncRegistryWithBuildImages() |
| 537 | |
| 538 | build_id, _file_name, found = self._GetArtifact(path_list, lookup_only=True) |
| 539 | |
| 540 | _Log('Returning path to payload: %s', build_id) |
| 541 | return build_id, found |
| 542 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 543 | def Get(self, path_list, return_dir=False): |
| 544 | """The full xBuddy call, returns resource specified by path_list. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 545 | |
| 546 | Please see devserver.py:xbuddy for full documentation. |
| 547 | Args: |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 548 | path_list: [board, version, alias] as split from the xbuddy call url |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 549 | return_dir: boolean, if set to true, returns the dir name instead. |
| 550 | |
| 551 | Returns: |
| 552 | Path to the image or update directory on the devserver. |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 553 | e.g. http://host/static/x86-generic/ |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 554 | R26-4000.0.0/chromium-test-image.bin |
| 555 | or |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 556 | http://host/static/x86-generic/R26-4000.0.0/ |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 557 | |
| 558 | Raises: |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 559 | XBuddyException if path is invalid |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 560 | """ |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 561 | self._SyncRegistryWithBuildImages() |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 562 | build_id, file_name, _found = self._GetArtifact(path_list) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 563 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 564 | Timestamp.UpdateTimestamp(self._timestamp_folder, build_id) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 565 | |
| 566 | #TODO (joyc): run in sep thread |
| 567 | self._CleanCache() |
| 568 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 569 | return_url = os.path.join('static', build_id) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 570 | if not return_dir: |
| 571 | return_url = os.path.join(return_url, file_name) |
| 572 | |
| 573 | _Log('Returning path to payload: %s', return_url) |
| 574 | return return_url |