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