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 | |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 5 | import ConfigParser |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 6 | import datetime |
| 7 | import operator |
| 8 | import os |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 9 | import re |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 10 | import shutil |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 11 | import time |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 12 | import threading |
| 13 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 14 | import build_util |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 15 | import artifact_info |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 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 | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 26 | # xBuddy config constants |
| 27 | CONFIG_FILE = 'xbuddy_config.ini' |
| 28 | SHADOW_CONFIG_FILE = 'shadow_xbuddy_config.ini' |
| 29 | PATH_REWRITES = 'PATH_REWRITES' |
| 30 | GENERAL = 'GENERAL' |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 31 | |
Chris Sosa | c2abc72 | 2013-08-26 17:11:22 -0700 | [diff] [blame] | 32 | # Path for shadow config in chroot. |
| 33 | CHROOT_SHADOW_DIR = '/mnt/host/source/src/platform/dev' |
| 34 | |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 35 | # XBuddy aliases |
| 36 | TEST = 'test' |
| 37 | BASE = 'base' |
| 38 | DEV = 'dev' |
| 39 | FULL = 'full_payload' |
| 40 | RECOVERY = 'recovery' |
| 41 | STATEFUL = 'stateful' |
| 42 | AUTOTEST = 'autotest' |
| 43 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 44 | # Local build constants |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 45 | ANY = "ANY" |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 46 | LATEST = "latest" |
| 47 | LOCAL = "local" |
| 48 | REMOTE = "remote" |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 49 | |
| 50 | # TODO(sosa): Fix a lot of assumptions about these aliases. There is too much |
| 51 | # implicit logic here that's unnecessary. What should be done: |
| 52 | # 1) Collapse Alias logic to one set of aliases for xbuddy (not local/remote). |
| 53 | # 2) Do not use zip when creating these dicts. Better to not rely on ordering. |
| 54 | # 3) Move alias/artifact mapping to a central module rather than having it here. |
| 55 | # 4) Be explicit when things are missing i.e. no dev images in image.zip. |
| 56 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 57 | LOCAL_ALIASES = [ |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 58 | TEST, |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 59 | DEV, |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 60 | BASE, |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 61 | FULL, |
| 62 | ANY, |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 63 | ] |
| 64 | |
| 65 | LOCAL_FILE_NAMES = [ |
| 66 | devserver_constants.TEST_IMAGE_FILE, |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 67 | devserver_constants.IMAGE_FILE, |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 68 | devserver_constants.BASE_IMAGE_FILE, |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 69 | devserver_constants.UPDATE_FILE, |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 70 | None, # For ANY. |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 71 | ] |
| 72 | |
| 73 | LOCAL_ALIAS_TO_FILENAME = dict(zip(LOCAL_ALIASES, LOCAL_FILE_NAMES)) |
| 74 | |
| 75 | # Google Storage constants |
| 76 | GS_ALIASES = [ |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 77 | TEST, |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 78 | DEV, |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 79 | BASE, |
| 80 | RECOVERY, |
| 81 | FULL, |
| 82 | STATEFUL, |
| 83 | AUTOTEST, |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 84 | ] |
| 85 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 86 | GS_FILE_NAMES = [ |
| 87 | devserver_constants.TEST_IMAGE_FILE, |
| 88 | devserver_constants.BASE_IMAGE_FILE, |
| 89 | devserver_constants.RECOVERY_IMAGE_FILE, |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 90 | devserver_constants.UPDATE_FILE, |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 91 | devserver_constants.STATEFUL_FILE, |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 92 | devserver_constants.AUTOTEST_DIR, |
| 93 | ] |
| 94 | |
| 95 | ARTIFACTS = [ |
| 96 | artifact_info.TEST_IMAGE, |
| 97 | artifact_info.BASE_IMAGE, |
| 98 | artifact_info.RECOVERY_IMAGE, |
| 99 | artifact_info.FULL_PAYLOAD, |
| 100 | artifact_info.STATEFUL_PAYLOAD, |
| 101 | artifact_info.AUTOTEST, |
| 102 | ] |
| 103 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 104 | GS_ALIAS_TO_FILENAME = dict(zip(GS_ALIASES, GS_FILE_NAMES)) |
| 105 | GS_ALIAS_TO_ARTIFACT = dict(zip(GS_ALIASES, ARTIFACTS)) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 106 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 107 | LATEST_OFFICIAL = "latest-official" |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 108 | |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 109 | RELEASE = "release" |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 110 | |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 111 | |
| 112 | class XBuddyException(Exception): |
| 113 | """Exception classes used by this module.""" |
| 114 | pass |
| 115 | |
| 116 | |
| 117 | # no __init__ method |
| 118 | #pylint: disable=W0232 |
| 119 | class Timestamp(): |
| 120 | """Class to translate build path strings and timestamp filenames.""" |
| 121 | |
| 122 | _TIMESTAMP_DELIMITER = 'SLASH' |
| 123 | XBUDDY_TIMESTAMP_DIR = 'xbuddy_UpdateTimestamps' |
| 124 | |
| 125 | @staticmethod |
| 126 | def TimestampToBuild(timestamp_filename): |
| 127 | return timestamp_filename.replace(Timestamp._TIMESTAMP_DELIMITER, '/') |
| 128 | |
| 129 | @staticmethod |
| 130 | def BuildToTimestamp(build_path): |
| 131 | return build_path.replace('/', Timestamp._TIMESTAMP_DELIMITER) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 132 | |
| 133 | @staticmethod |
| 134 | def UpdateTimestamp(timestamp_dir, build_id): |
| 135 | """Update timestamp file of build with build_id.""" |
| 136 | common_util.MkDirP(timestamp_dir) |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 137 | _Log("Updating timestamp for %s", build_id) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 138 | time_file = os.path.join(timestamp_dir, |
| 139 | Timestamp.BuildToTimestamp(build_id)) |
| 140 | with file(time_file, 'a'): |
| 141 | os.utime(time_file, None) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 142 | #pylint: enable=W0232 |
| 143 | |
| 144 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 145 | class XBuddy(build_util.BuildObject): |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 146 | """Class that manages image retrieval and caching by the devserver. |
| 147 | |
| 148 | Image retrieval by xBuddy path: |
| 149 | XBuddy accesses images and artifacts that it stores using an xBuddy |
| 150 | path of the form: board/version/alias |
| 151 | The primary xbuddy.Get call retrieves the correct artifact or url to where |
| 152 | the artifacts can be found. |
| 153 | |
| 154 | Image caching: |
| 155 | Images and other artifacts are stored identically to how they would have |
| 156 | been if devserver's stage rpc was called and the xBuddy cache replaces |
| 157 | build versions on a LRU basis. Timestamps are maintained by last accessed |
| 158 | times of representative files in the a directory in the static serve |
| 159 | directory (XBUDDY_TIMESTAMP_DIR). |
| 160 | |
| 161 | Private class members: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 162 | _true_values: used for interpreting boolean values |
| 163 | _staging_thread_count: track download requests |
| 164 | _timestamp_folder: directory with empty files standing in as timestamps |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 165 | for each image currently cached by xBuddy |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 166 | """ |
| 167 | _true_values = ['true', 't', 'yes', 'y'] |
| 168 | |
| 169 | # Number of threads that are staging images. |
| 170 | _staging_thread_count = 0 |
| 171 | # Lock used to lock increasing/decreasing count. |
| 172 | _staging_thread_count_lock = threading.Lock() |
| 173 | |
joychen | b0dfe55 | 2013-07-30 10:02:06 -0700 | [diff] [blame] | 174 | def __init__(self, manage_builds=False, board=None, **kwargs): |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 175 | super(XBuddy, self).__init__(**kwargs) |
joychen | b0dfe55 | 2013-07-30 10:02:06 -0700 | [diff] [blame] | 176 | |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 177 | self.config = self._ReadConfig() |
| 178 | self._manage_builds = manage_builds or self._ManageBuilds() |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 179 | self._board = board |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 180 | self._timestamp_folder = os.path.join(self.static_dir, |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 181 | Timestamp.XBUDDY_TIMESTAMP_DIR) |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 182 | common_util.MkDirP(self._timestamp_folder) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 183 | |
| 184 | @classmethod |
| 185 | def ParseBoolean(cls, boolean_string): |
| 186 | """Evaluate a string to a boolean value""" |
| 187 | if boolean_string: |
| 188 | return boolean_string.lower() in cls._true_values |
| 189 | else: |
| 190 | return False |
| 191 | |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 192 | def _ReadConfig(self): |
| 193 | """Read xbuddy config from ini files. |
| 194 | |
| 195 | Reads the base config from xbuddy_config.ini, and then merges in the |
| 196 | shadow config from shadow_xbuddy_config.ini |
| 197 | |
| 198 | Returns: |
| 199 | The merged configuration. |
| 200 | Raises: |
| 201 | XBuddyException if the config file is missing. |
| 202 | """ |
| 203 | xbuddy_config = ConfigParser.ConfigParser() |
| 204 | config_file = os.path.join(self.devserver_dir, CONFIG_FILE) |
| 205 | if os.path.exists(config_file): |
| 206 | xbuddy_config.read(config_file) |
| 207 | else: |
| 208 | raise XBuddyException('%s not found' % (CONFIG_FILE)) |
| 209 | |
| 210 | # Read the shadow file if there is one. |
Chris Sosa | c2abc72 | 2013-08-26 17:11:22 -0700 | [diff] [blame] | 211 | if os.path.isdir(CHROOT_SHADOW_DIR): |
| 212 | shadow_config_file = os.path.join(CHROOT_SHADOW_DIR, SHADOW_CONFIG_FILE) |
| 213 | else: |
| 214 | shadow_config_file = os.path.join(self.devserver_dir, SHADOW_CONFIG_FILE) |
| 215 | |
| 216 | _Log('Using shadow config file stored at %s', shadow_config_file) |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 217 | if os.path.exists(shadow_config_file): |
| 218 | shadow_xbuddy_config = ConfigParser.ConfigParser() |
| 219 | shadow_xbuddy_config.read(shadow_config_file) |
| 220 | |
| 221 | # Merge shadow config in. |
| 222 | sections = shadow_xbuddy_config.sections() |
| 223 | for s in sections: |
| 224 | if not xbuddy_config.has_section(s): |
| 225 | xbuddy_config.add_section(s) |
| 226 | options = shadow_xbuddy_config.options(s) |
| 227 | for o in options: |
| 228 | val = shadow_xbuddy_config.get(s, o) |
| 229 | xbuddy_config.set(s, o, val) |
| 230 | |
| 231 | return xbuddy_config |
| 232 | |
| 233 | def _ManageBuilds(self): |
| 234 | """Checks if xBuddy is managing local builds using the current config.""" |
| 235 | try: |
| 236 | return self.ParseBoolean(self.config.get(GENERAL, 'manage_builds')) |
| 237 | except ConfigParser.Error: |
| 238 | return False |
| 239 | |
| 240 | def _Capacity(self): |
| 241 | """Gets the xbuddy capacity from the current config.""" |
| 242 | try: |
| 243 | return int(self.config.get(GENERAL, 'capacity')) |
| 244 | except ConfigParser.Error: |
| 245 | return 5 |
| 246 | |
| 247 | def _LookupAlias(self, alias, board): |
| 248 | """Given the full xbuddy config, look up an alias for path rewrite. |
| 249 | |
| 250 | Args: |
| 251 | alias: The xbuddy path that could be one of the aliases in the |
| 252 | rewrite table. |
| 253 | board: The board to fill in with when paths are rewritten. Can be from |
| 254 | the update request xml or the default board from devserver. |
| 255 | Returns: |
| 256 | If a rewrite is found, a string with the current board substituted in. |
| 257 | If no rewrite is found, just return the original string. |
| 258 | """ |
| 259 | if alias == '': |
| 260 | alias = 'update_default' |
| 261 | |
| 262 | try: |
| 263 | val = self.config.get(PATH_REWRITES, alias) |
| 264 | except ConfigParser.Error: |
| 265 | # No alias lookup found. Return original path. |
| 266 | return alias |
| 267 | |
| 268 | if not val.strip(): |
| 269 | # The found value was an empty string. |
| 270 | return alias |
| 271 | else: |
| 272 | # Fill in the board. |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 273 | rewrite = val.replace("BOARD", "%(board)s") % { |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 274 | 'board': board} |
| 275 | _Log("Path was rewritten to %s", rewrite) |
| 276 | return rewrite |
| 277 | |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 278 | def _LookupOfficial(self, board, suffix=RELEASE): |
| 279 | """Check LATEST-master for the version number of interest.""" |
| 280 | _Log("Checking gs for latest %s-%s image", board, suffix) |
| 281 | latest_addr = devserver_constants.GS_LATEST_MASTER % {'board':board, |
| 282 | 'suffix':suffix} |
| 283 | cmd = 'gsutil cat %s' % latest_addr |
| 284 | msg = 'Failed to find build at %s' % latest_addr |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 285 | # Full release + version is in the LATEST file. |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 286 | version = gsutil_util.GSUtilRun(cmd, msg) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 287 | |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 288 | return devserver_constants.IMAGE_DIR % {'board':board, |
| 289 | 'suffix':suffix, |
| 290 | 'version':version} |
| 291 | |
| 292 | def _LookupChannel(self, board, channel='stable'): |
| 293 | """Check the channel folder for the version number of interest.""" |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 294 | # Get all names in channel dir. Get 10 highest directories by version. |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 295 | _Log("Checking channel '%s' for latest '%s' image", channel, board) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 296 | channel_dir = devserver_constants.GS_CHANNEL_DIR % {'channel':channel, |
| 297 | 'board':board} |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 298 | latest_version = gsutil_util.GetLatestVersionFromGSDir( |
| 299 | channel_dir, with_release=False) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 300 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 301 | # Figure out release number from the version number. |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 302 | image_url = devserver_constants.IMAGE_DIR % { |
| 303 | 'board':board, |
| 304 | 'suffix':RELEASE, |
| 305 | 'version':'R*' + latest_version} |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 306 | image_dir = os.path.join(devserver_constants.GS_IMAGE_DIR, image_url) |
| 307 | |
| 308 | # There should only be one match on cros-image-archive. |
| 309 | full_version = gsutil_util.GetLatestVersionFromGSDir(image_dir) |
| 310 | |
| 311 | return devserver_constants.IMAGE_DIR % {'board':board, |
| 312 | 'suffix':RELEASE, |
| 313 | 'version':full_version} |
| 314 | |
| 315 | def _LookupVersion(self, board, version): |
| 316 | """Search GS image releases for the highest match to a version prefix.""" |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 317 | # Build the pattern for GS to match. |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 318 | _Log("Checking gs for latest '%s' image with prefix '%s'", board, version) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 319 | image_url = devserver_constants.IMAGE_DIR % {'board':board, |
| 320 | 'suffix':RELEASE, |
| 321 | 'version':version + '*'} |
| 322 | image_dir = os.path.join(devserver_constants.GS_IMAGE_DIR, image_url) |
| 323 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 324 | # Grab the newest version of the ones matched. |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 325 | full_version = gsutil_util.GetLatestVersionFromGSDir(image_dir) |
| 326 | return devserver_constants.IMAGE_DIR % {'board':board, |
| 327 | 'suffix':RELEASE, |
| 328 | 'version':full_version} |
| 329 | |
| 330 | def _ResolveVersionToUrl(self, board, version): |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 331 | """Handle version aliases for remote payloads in GS. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 332 | |
| 333 | Args: |
| 334 | board: as specified in the original call. (i.e. x86-generic, parrot) |
| 335 | version: as entered in the original call. can be |
| 336 | {TBD, 0. some custom alias as defined in a config file} |
| 337 | 1. latest |
| 338 | 2. latest-{channel} |
| 339 | 3. latest-official-{board suffix} |
| 340 | 4. version prefix (i.e. RX-Y.X, RX-Y, RX) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 341 | |
| 342 | Returns: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 343 | Location where the image dir is actually found on GS |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 344 | |
| 345 | """ |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 346 | # TODO(joychen): Convert separate calls to a dict + error out bad paths. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 347 | |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 348 | # Only the last segment of the alias is variable relative to the rest. |
| 349 | version_tuple = version.rsplit('-', 1) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 350 | |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 351 | if re.match(devserver_constants.VERSION_RE, version): |
| 352 | # This is supposed to be a complete version number on GS. Return it. |
| 353 | return devserver_constants.IMAGE_DIR % {'board':board, |
| 354 | 'suffix':RELEASE, |
| 355 | 'version':version} |
| 356 | elif version == LATEST_OFFICIAL: |
| 357 | # latest-official --> LATEST build in board-release |
| 358 | return self._LookupOfficial(board) |
| 359 | elif version_tuple[0] == LATEST_OFFICIAL: |
| 360 | # latest-official-{suffix} --> LATEST build in board-{suffix} |
| 361 | return self._LookupOfficial(board, version_tuple[1]) |
| 362 | elif version == LATEST: |
| 363 | # latest --> latest build on stable channel |
| 364 | return self._LookupChannel(board) |
| 365 | elif version_tuple[0] == LATEST: |
| 366 | if re.match(devserver_constants.VERSION_RE, version_tuple[1]): |
| 367 | # latest-R* --> most recent qualifying build |
| 368 | return self._LookupVersion(board, version_tuple[1]) |
| 369 | else: |
| 370 | # latest-{channel} --> latest build within that channel |
| 371 | return self._LookupChannel(board, version_tuple[1]) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 372 | else: |
| 373 | # The given version doesn't match any known patterns. |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 374 | raise XBuddyException("Version %s unknown. Can't find on GS." % version) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 375 | |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 376 | @staticmethod |
| 377 | def _Symlink(link, target): |
| 378 | """Symlinks link to target, and removes whatever link was there before.""" |
| 379 | _Log("Linking to %s from %s", link, target) |
| 380 | if os.path.lexists(link): |
| 381 | os.unlink(link) |
| 382 | os.symlink(target, link) |
| 383 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 384 | def _GetLatestLocalVersion(self, board): |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 385 | """Get the version of the latest image built for board by build_image |
| 386 | |
| 387 | Updates the symlink reference within the xBuddy static dir to point to |
| 388 | the real image dir in the local /build/images directory. |
| 389 | |
| 390 | Args: |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 391 | board: board that image was built for. |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 392 | |
| 393 | Returns: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 394 | The discovered version of the image. |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 395 | |
| 396 | Raises: |
| 397 | XBuddyException if neither test nor dev image was found in latest built |
| 398 | directory. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 399 | """ |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 400 | latest_local_dir = self.GetLatestImageDir(board) |
joychen | b0dfe55 | 2013-07-30 10:02:06 -0700 | [diff] [blame] | 401 | if not latest_local_dir or not os.path.exists(latest_local_dir): |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 402 | raise XBuddyException('No builds found for %s. Did you run build_image?' % |
| 403 | board) |
| 404 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 405 | # Assume that the version number is the name of the directory. |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 406 | return os.path.basename(latest_local_dir.rstrip('/')) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 407 | |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 408 | @staticmethod |
| 409 | def _FindAny(local_dir): |
| 410 | """Returns the image_type for ANY given the local_dir.""" |
| 411 | dev_image = os.path.join(local_dir, devserver_constants.IMAGE_FILE) |
| 412 | test_image = os.path.join(local_dir, devserver_constants.TEST_IMAGE_FILE) |
| 413 | if os.path.exists(dev_image): |
| 414 | return 'dev' |
| 415 | |
| 416 | if os.path.exists(test_image): |
| 417 | return 'test' |
| 418 | |
| 419 | raise XBuddyException('No images found in %s' % local_dir) |
| 420 | |
| 421 | @staticmethod |
| 422 | def _InterpretPath(path): |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 423 | """Split and return the pieces of an xBuddy path name |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 424 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 425 | Args: |
| 426 | path: the path xBuddy Get was called with. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 427 | |
| 428 | Return: |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 429 | tuple of (image_type, board, version, whether the path is local) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 430 | |
| 431 | Raises: |
| 432 | XBuddyException: if the path can't be resolved into valid components |
| 433 | """ |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 434 | path_list = filter(None, path.split('/')) |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 435 | |
| 436 | # Required parts of path parsing. |
| 437 | try: |
| 438 | # Determine if image is explicitly local or remote. |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 439 | is_local = True |
| 440 | if path_list[0] in (REMOTE, LOCAL): |
joychen | 18737f3 | 2013-08-16 17:18:12 -0700 | [diff] [blame] | 441 | is_local = (path_list.pop(0) == LOCAL) |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 442 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 443 | # Set board. |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 444 | board = path_list.pop(0) |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 445 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 446 | # Set defaults. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 447 | version = LATEST |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 448 | image_type = GS_ALIASES[0] |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 449 | except IndexError: |
| 450 | msg = "Specify at least the board in your xBuddy call. Your path: %s" |
| 451 | raise XBuddyException(msg % os.path.join(path_list)) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 452 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 453 | # Read as much of the xBuddy path as possible. |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 454 | try: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 455 | # Override default if terminal is a valid artifact alias or a version. |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 456 | terminal = path_list[-1] |
| 457 | if terminal in GS_ALIASES + LOCAL_ALIASES: |
| 458 | image_type = terminal |
| 459 | version = path_list[-2] |
| 460 | else: |
| 461 | version = terminal |
| 462 | except IndexError: |
| 463 | # This path doesn't have an alias or a version. That's fine. |
| 464 | _Log("Some parts of the path not specified. Using defaults.") |
| 465 | |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 466 | _Log("Get artifact '%s' in '%s/%s'. Locally? %s", |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 467 | image_type, board, version, is_local) |
| 468 | |
| 469 | return image_type, board, version, is_local |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 470 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 471 | def _SyncRegistryWithBuildImages(self): |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 472 | """ Crawl images_dir for build_ids of images generated from build_image. |
| 473 | |
| 474 | This will find images and symlink them in xBuddy's static dir so that |
| 475 | xBuddy's cache can serve them. |
| 476 | If xBuddy's _manage_builds option is on, then a timestamp will also be |
| 477 | generated, and xBuddy will clear them from the directory they are in, as |
| 478 | necessary. |
| 479 | """ |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 480 | build_ids = [] |
| 481 | for b in os.listdir(self.images_dir): |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 482 | # Ensure we have directories to track all boards in build/images |
| 483 | common_util.MkDirP(os.path.join(self.static_dir, b)) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 484 | board_dir = os.path.join(self.images_dir, b) |
| 485 | build_ids.extend(['/'.join([b, v]) for v |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 486 | in os.listdir(board_dir) if not v == LATEST]) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 487 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 488 | # Check currently registered images. |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 489 | for f in os.listdir(self._timestamp_folder): |
| 490 | build_id = Timestamp.TimestampToBuild(f) |
| 491 | if build_id in build_ids: |
| 492 | build_ids.remove(build_id) |
| 493 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 494 | # Symlink undiscovered images, and update timestamps if manage_builds is on. |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 495 | for build_id in build_ids: |
| 496 | link = os.path.join(self.static_dir, build_id) |
| 497 | target = os.path.join(self.images_dir, build_id) |
| 498 | XBuddy._Symlink(link, target) |
| 499 | if self._manage_builds: |
| 500 | Timestamp.UpdateTimestamp(self._timestamp_folder, build_id) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 501 | |
| 502 | def _ListBuildTimes(self): |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 503 | """ Returns the currently cached builds and their last access timestamp. |
| 504 | |
| 505 | Returns: |
| 506 | list of tuples that matches xBuddy build/version to timestamps in long |
| 507 | """ |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 508 | # Update currently cached builds. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 509 | build_dict = {} |
| 510 | |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 511 | for f in os.listdir(self._timestamp_folder): |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 512 | last_accessed = os.path.getmtime(os.path.join(self._timestamp_folder, f)) |
| 513 | build_id = Timestamp.TimestampToBuild(f) |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 514 | stale_time = datetime.timedelta(seconds=(time.time() - last_accessed)) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 515 | build_dict[build_id] = stale_time |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 516 | return_tup = sorted(build_dict.iteritems(), key=operator.itemgetter(1)) |
| 517 | return return_tup |
| 518 | |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 519 | def _Download(self, gs_url, artifacts): |
| 520 | """Download the artifacts from the given gs_url. |
| 521 | |
| 522 | Raises: |
| 523 | build_artifact.ArtifactDownloadError: If we failed to download the |
| 524 | artifact. |
| 525 | """ |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 526 | with XBuddy._staging_thread_count_lock: |
| 527 | XBuddy._staging_thread_count += 1 |
| 528 | try: |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 529 | _Log("Downloading %s from %s", artifacts, gs_url) |
| 530 | downloader.Downloader(self.static_dir, gs_url).Download(artifacts, []) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 531 | finally: |
| 532 | with XBuddy._staging_thread_count_lock: |
| 533 | XBuddy._staging_thread_count -= 1 |
| 534 | |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 535 | def CleanCache(self): |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 536 | """Delete all builds besides the newest N builds""" |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 537 | if not self._manage_builds: |
| 538 | return |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 539 | cached_builds = [e[0] for e in self._ListBuildTimes()] |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 540 | _Log('In cache now: %s', cached_builds) |
| 541 | |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 542 | for b in range(self._Capacity(), len(cached_builds)): |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 543 | b_path = cached_builds[b] |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 544 | _Log("Clearing '%s' from cache", b_path) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 545 | |
| 546 | time_file = os.path.join(self._timestamp_folder, |
| 547 | Timestamp.BuildToTimestamp(b_path)) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 548 | os.unlink(time_file) |
| 549 | clear_dir = os.path.join(self.static_dir, b_path) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 550 | try: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 551 | # Handle symlinks, in the case of links to local builds if enabled. |
| 552 | if os.path.islink(clear_dir): |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 553 | target = os.readlink(clear_dir) |
| 554 | _Log('Deleting locally built image at %s', target) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 555 | |
| 556 | os.unlink(clear_dir) |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 557 | if os.path.exists(target): |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 558 | shutil.rmtree(target) |
| 559 | elif os.path.exists(clear_dir): |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 560 | _Log('Deleting downloaded image at %s', clear_dir) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 561 | shutil.rmtree(clear_dir) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 562 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 563 | except Exception as err: |
| 564 | raise XBuddyException('Failed to clear %s: %s' % (clear_dir, err)) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 565 | |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 566 | def _GetFromGS(self, build_id, image_type): |
| 567 | """Check if the artifact is available locally. Download from GS if not. |
| 568 | |
| 569 | Raises: |
| 570 | build_artifact.ArtifactDownloadError: If we failed to download the |
| 571 | artifact. |
| 572 | """ |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 573 | gs_url = os.path.join(devserver_constants.GS_IMAGE_DIR, |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 574 | build_id) |
| 575 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 576 | # Stage image if not found in cache. |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 577 | file_name = GS_ALIAS_TO_FILENAME[image_type] |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 578 | file_loc = os.path.join(self.static_dir, build_id, file_name) |
| 579 | cached = os.path.exists(file_loc) |
| 580 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 581 | if not cached: |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 582 | artifact = GS_ALIAS_TO_ARTIFACT[image_type] |
| 583 | self._Download(gs_url, [artifact]) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 584 | else: |
| 585 | _Log('Image already cached.') |
| 586 | |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 587 | def _GetArtifact(self, path_list, board=None, lookup_only=False): |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 588 | """Interpret an xBuddy path and return directory/file_name to resource. |
| 589 | |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 590 | Note board can be passed that in but by default if self._board is set, |
| 591 | that is used rather than board. |
| 592 | |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 593 | Returns: |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 594 | build_id to the directory |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 595 | file_name of the artifact |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 596 | |
| 597 | Raises: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 598 | XBuddyException: if the path could not be translated |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 599 | build_artifact.ArtifactDownloadError: if we failed to download the |
| 600 | artifact. |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 601 | """ |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 602 | path = '/'.join(path_list) |
joychen | b0dfe55 | 2013-07-30 10:02:06 -0700 | [diff] [blame] | 603 | # Rewrite the path if there is an appropriate default. |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 604 | path = self._LookupAlias(path, self._board if self._board else board) |
joychen | b0dfe55 | 2013-07-30 10:02:06 -0700 | [diff] [blame] | 605 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 606 | # Parse the path. |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 607 | image_type, board, version, is_local = self._InterpretPath(path) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 608 | |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 609 | if is_local: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 610 | # Get a local image. |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 611 | if version == LATEST: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 612 | # Get the latest local image for the given board. |
| 613 | version = self._GetLatestLocalVersion(board) |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 614 | |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 615 | build_id = os.path.join(board, version) |
| 616 | artifact_dir = os.path.join(self.static_dir, build_id) |
| 617 | if image_type == ANY: |
| 618 | image_type = self._FindAny(artifact_dir) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 619 | |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 620 | file_name = LOCAL_ALIAS_TO_FILENAME[image_type] |
| 621 | artifact_path = os.path.join(artifact_dir, file_name) |
| 622 | if not os.path.exists(artifact_path): |
| 623 | raise XBuddyException('Local %s artifact not in static_dir at %s' % |
| 624 | (image_type, artifact_path)) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 625 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 626 | else: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 627 | # Get a remote image. |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 628 | if image_type not in GS_ALIASES: |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 629 | raise XBuddyException('Bad remote image type: %s. Use one of: %s' % |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 630 | (image_type, GS_ALIASES)) |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 631 | build_id = self._ResolveVersionToUrl(board, version) |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 632 | _Log('Resolved version %s to %s.', version, build_id) |
| 633 | file_name = GS_ALIAS_TO_FILENAME[image_type] |
| 634 | if not lookup_only: |
| 635 | self._GetFromGS(build_id, image_type) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 636 | |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 637 | return build_id, file_name |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 638 | |
| 639 | ############################ BEGIN PUBLIC METHODS |
| 640 | |
| 641 | def List(self): |
| 642 | """Lists the currently available images & time since last access.""" |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 643 | self._SyncRegistryWithBuildImages() |
| 644 | builds = self._ListBuildTimes() |
| 645 | return_string = '' |
| 646 | for build, timestamp in builds: |
| 647 | return_string += '<b>' + build + '</b> ' |
| 648 | return_string += '(time since last access: ' + str(timestamp) + ')<br>' |
| 649 | return return_string |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 650 | |
| 651 | def Capacity(self): |
| 652 | """Returns the number of images cached by xBuddy.""" |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 653 | return str(self._Capacity()) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 654 | |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 655 | def Translate(self, path_list, board=None): |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 656 | """Translates an xBuddy path to a real path to artifact if it exists. |
| 657 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 658 | Equivalent to the Get call, minus downloading and updating timestamps, |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 659 | |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 660 | Returns: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 661 | build_id: Path to the image or update directory on the devserver. |
| 662 | e.g. 'x86-generic/R26-4000.0.0' |
| 663 | The returned path is always the path to the directory within |
| 664 | static_dir, so it is always the build_id of the image. |
| 665 | file_name: The file name of the artifact. Can take any of the file |
| 666 | values in devserver_constants. |
| 667 | e.g. 'chromiumos_test_image.bin' or 'update.gz' if the path list |
| 668 | specified 'test' or 'full_payload' artifacts, respectively. |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 669 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 670 | Raises: |
| 671 | XBuddyException: if the path couldn't be translated |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 672 | """ |
| 673 | self._SyncRegistryWithBuildImages() |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 674 | build_id, file_name = self._GetArtifact(path_list, board=board, |
| 675 | lookup_only=True) |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 676 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 677 | _Log('Returning path to payload: %s/%s', build_id, file_name) |
| 678 | return build_id, file_name |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 679 | |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 680 | def StageTestAritfactsForUpdate(self, path_list): |
| 681 | """Stages test artifacts for update and returns build_id. |
| 682 | |
| 683 | Raises: |
| 684 | XBuddyException: if the path could not be translated |
| 685 | build_artifact.ArtifactDownloadError: if we failed to download the test |
| 686 | artifacts. |
| 687 | """ |
| 688 | build_id, file_name = self.Translate(path_list) |
| 689 | if file_name == devserver_constants.TEST_IMAGE_FILE: |
| 690 | gs_url = os.path.join(devserver_constants.GS_IMAGE_DIR, |
| 691 | build_id) |
| 692 | artifacts = [FULL, STATEFUL] |
| 693 | self._Download(gs_url, artifacts) |
| 694 | return build_id |
| 695 | |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 696 | def Get(self, path_list): |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 697 | """The full xBuddy call, returns resource specified by path_list. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 698 | |
| 699 | Please see devserver.py:xbuddy for full documentation. |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 700 | |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 701 | Args: |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 702 | path_list: [board, version, alias] as split from the xbuddy call url |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 703 | |
| 704 | Returns: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 705 | build_id: Path to the image or update directory on the devserver. |
| 706 | e.g. 'x86-generic/R26-4000.0.0' |
| 707 | The returned path is always the path to the directory within |
| 708 | static_dir, so it is always the build_id of the image. |
| 709 | file_name: The file name of the artifact. Can take any of the file |
| 710 | values in devserver_constants. |
| 711 | e.g. 'chromiumos_test_image.bin' or 'update.gz' if the path list |
| 712 | specified 'test' or 'full_payload' artifacts, respectively. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 713 | |
| 714 | Raises: |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 715 | XBuddyException: if the path could not be translated |
| 716 | build_artifact.ArtifactDownloadError: if we failed to download the |
| 717 | artifact. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 718 | """ |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 719 | self._SyncRegistryWithBuildImages() |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 720 | build_id, file_name = self._GetArtifact(path_list) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 721 | Timestamp.UpdateTimestamp(self._timestamp_folder, build_id) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 722 | #TODO (joyc): run in sep thread |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 723 | self.CleanCache() |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 724 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 725 | _Log('Returning path to payload: %s/%s', build_id, file_name) |
| 726 | return build_id, file_name |