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 | |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 5 | """Main module for parsing and interpreting XBuddy paths for the devserver.""" |
| 6 | |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
Yiming Chen | aab488e | 2014-11-17 14:49:31 -0800 | [diff] [blame] | 9 | import cherrypy |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 10 | import ConfigParser |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 11 | import datetime |
xixuan | 44b5545 | 2016-09-06 15:35:56 -0700 | [diff] [blame] | 12 | import distutils.version |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 13 | import operator |
| 14 | import os |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 15 | import re |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 16 | import shutil |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 17 | import time |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 18 | import threading |
| 19 | |
| 20 | import artifact_info |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 21 | import build_artifact |
| 22 | import build_util |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 23 | import common_util |
| 24 | import devserver_constants |
| 25 | import downloader |
| 26 | import log_util |
| 27 | |
xixuan | 44b5545 | 2016-09-06 15:35:56 -0700 | [diff] [blame] | 28 | # Make sure that chromite is available to import. |
| 29 | import setup_chromite # pylint: disable=unused-import |
| 30 | |
| 31 | try: |
| 32 | from chromite.lib import gs |
Gwendal Grignou | ad4cb98 | 2017-03-31 11:36:19 -0700 | [diff] [blame^] | 33 | except ImportError: |
xixuan | 44b5545 | 2016-09-06 15:35:56 -0700 | [diff] [blame] | 34 | gs = None |
| 35 | |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 36 | # Module-local log function. |
| 37 | def _Log(message, *args): |
| 38 | return log_util.LogWithTag('XBUDDY', message, *args) |
| 39 | |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 40 | # xBuddy config constants |
| 41 | CONFIG_FILE = 'xbuddy_config.ini' |
| 42 | SHADOW_CONFIG_FILE = 'shadow_xbuddy_config.ini' |
| 43 | PATH_REWRITES = 'PATH_REWRITES' |
| 44 | GENERAL = 'GENERAL' |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 45 | LOCATION_SUFFIXES = 'LOCATION_SUFFIXES' |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 46 | |
Chris Sosa | c2abc72 | 2013-08-26 17:11:22 -0700 | [diff] [blame] | 47 | # Path for shadow config in chroot. |
| 48 | CHROOT_SHADOW_DIR = '/mnt/host/source/src/platform/dev' |
| 49 | |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 50 | # XBuddy aliases |
| 51 | TEST = 'test' |
| 52 | BASE = 'base' |
| 53 | DEV = 'dev' |
| 54 | FULL = 'full_payload' |
| 55 | RECOVERY = 'recovery' |
| 56 | STATEFUL = 'stateful' |
| 57 | AUTOTEST = 'autotest' |
Mike Frysinger | a0e6a28 | 2016-09-01 17:29:08 -0400 | [diff] [blame] | 58 | FACTORY_SHIM = 'factory_shim' |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 59 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 60 | # Local build constants |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 61 | ANY = "ANY" |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 62 | LATEST = "latest" |
| 63 | LOCAL = "local" |
| 64 | REMOTE = "remote" |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 65 | |
| 66 | # TODO(sosa): Fix a lot of assumptions about these aliases. There is too much |
| 67 | # implicit logic here that's unnecessary. What should be done: |
| 68 | # 1) Collapse Alias logic to one set of aliases for xbuddy (not local/remote). |
| 69 | # 2) Do not use zip when creating these dicts. Better to not rely on ordering. |
| 70 | # 3) Move alias/artifact mapping to a central module rather than having it here. |
| 71 | # 4) Be explicit when things are missing i.e. no dev images in image.zip. |
| 72 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 73 | LOCAL_ALIASES = [ |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 74 | TEST, |
| 75 | DEV, |
| 76 | BASE, |
| 77 | RECOVERY, |
Mike Frysinger | a0e6a28 | 2016-09-01 17:29:08 -0400 | [diff] [blame] | 78 | FACTORY_SHIM, |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 79 | FULL, |
| 80 | STATEFUL, |
| 81 | ANY, |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 82 | ] |
| 83 | |
| 84 | LOCAL_FILE_NAMES = [ |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 85 | devserver_constants.TEST_IMAGE_FILE, |
| 86 | devserver_constants.IMAGE_FILE, |
| 87 | devserver_constants.BASE_IMAGE_FILE, |
| 88 | devserver_constants.RECOVERY_IMAGE_FILE, |
Mike Frysinger | a0e6a28 | 2016-09-01 17:29:08 -0400 | [diff] [blame] | 89 | devserver_constants.FACTORY_SHIM_IMAGE_FILE, |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 90 | devserver_constants.UPDATE_FILE, |
| 91 | devserver_constants.STATEFUL_FILE, |
| 92 | None, # For ANY. |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 93 | ] |
| 94 | |
| 95 | LOCAL_ALIAS_TO_FILENAME = dict(zip(LOCAL_ALIASES, LOCAL_FILE_NAMES)) |
| 96 | |
| 97 | # Google Storage constants |
| 98 | GS_ALIASES = [ |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 99 | TEST, |
| 100 | BASE, |
| 101 | RECOVERY, |
Mike Frysinger | a0e6a28 | 2016-09-01 17:29:08 -0400 | [diff] [blame] | 102 | FACTORY_SHIM, |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 103 | FULL, |
| 104 | STATEFUL, |
| 105 | AUTOTEST, |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 106 | ] |
| 107 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 108 | GS_FILE_NAMES = [ |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 109 | devserver_constants.TEST_IMAGE_FILE, |
| 110 | devserver_constants.BASE_IMAGE_FILE, |
| 111 | devserver_constants.RECOVERY_IMAGE_FILE, |
Mike Frysinger | a0e6a28 | 2016-09-01 17:29:08 -0400 | [diff] [blame] | 112 | devserver_constants.FACTORY_SHIM_IMAGE_FILE, |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 113 | devserver_constants.UPDATE_FILE, |
| 114 | devserver_constants.STATEFUL_FILE, |
| 115 | devserver_constants.AUTOTEST_DIR, |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 116 | ] |
| 117 | |
| 118 | ARTIFACTS = [ |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 119 | artifact_info.TEST_IMAGE, |
| 120 | artifact_info.BASE_IMAGE, |
| 121 | artifact_info.RECOVERY_IMAGE, |
Mike Frysinger | a0e6a28 | 2016-09-01 17:29:08 -0400 | [diff] [blame] | 122 | artifact_info.FACTORY_SHIM_IMAGE, |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 123 | artifact_info.FULL_PAYLOAD, |
| 124 | artifact_info.STATEFUL_PAYLOAD, |
| 125 | artifact_info.AUTOTEST, |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 126 | ] |
| 127 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 128 | GS_ALIAS_TO_FILENAME = dict(zip(GS_ALIASES, GS_FILE_NAMES)) |
| 129 | GS_ALIAS_TO_ARTIFACT = dict(zip(GS_ALIASES, ARTIFACTS)) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 130 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 131 | LATEST_OFFICIAL = "latest-official" |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 132 | |
Chris Sosa | ea734d9 | 2013-10-11 11:28:58 -0700 | [diff] [blame] | 133 | RELEASE = "-release" |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 134 | |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 135 | |
| 136 | class XBuddyException(Exception): |
| 137 | """Exception classes used by this module.""" |
| 138 | pass |
| 139 | |
| 140 | |
| 141 | # no __init__ method |
| 142 | #pylint: disable=W0232 |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 143 | class Timestamp(object): |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 144 | """Class to translate build path strings and timestamp filenames.""" |
| 145 | |
| 146 | _TIMESTAMP_DELIMITER = 'SLASH' |
| 147 | XBUDDY_TIMESTAMP_DIR = 'xbuddy_UpdateTimestamps' |
| 148 | |
| 149 | @staticmethod |
| 150 | def TimestampToBuild(timestamp_filename): |
| 151 | return timestamp_filename.replace(Timestamp._TIMESTAMP_DELIMITER, '/') |
| 152 | |
| 153 | @staticmethod |
| 154 | def BuildToTimestamp(build_path): |
| 155 | return build_path.replace('/', Timestamp._TIMESTAMP_DELIMITER) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 156 | |
| 157 | @staticmethod |
| 158 | def UpdateTimestamp(timestamp_dir, build_id): |
| 159 | """Update timestamp file of build with build_id.""" |
| 160 | common_util.MkDirP(timestamp_dir) |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 161 | _Log("Updating timestamp for %s", build_id) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 162 | time_file = os.path.join(timestamp_dir, |
| 163 | Timestamp.BuildToTimestamp(build_id)) |
| 164 | with file(time_file, 'a'): |
| 165 | os.utime(time_file, None) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 166 | #pylint: enable=W0232 |
| 167 | |
| 168 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 169 | class XBuddy(build_util.BuildObject): |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 170 | """Class that manages image retrieval and caching by the devserver. |
| 171 | |
| 172 | Image retrieval by xBuddy path: |
| 173 | XBuddy accesses images and artifacts that it stores using an xBuddy |
| 174 | path of the form: board/version/alias |
| 175 | The primary xbuddy.Get call retrieves the correct artifact or url to where |
| 176 | the artifacts can be found. |
| 177 | |
| 178 | Image caching: |
| 179 | Images and other artifacts are stored identically to how they would have |
| 180 | been if devserver's stage rpc was called and the xBuddy cache replaces |
| 181 | build versions on a LRU basis. Timestamps are maintained by last accessed |
| 182 | times of representative files in the a directory in the static serve |
| 183 | directory (XBUDDY_TIMESTAMP_DIR). |
| 184 | |
| 185 | Private class members: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 186 | _true_values: used for interpreting boolean values |
| 187 | _staging_thread_count: track download requests |
| 188 | _timestamp_folder: directory with empty files standing in as timestamps |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 189 | for each image currently cached by xBuddy |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 190 | """ |
| 191 | _true_values = ['true', 't', 'yes', 'y'] |
| 192 | |
| 193 | # Number of threads that are staging images. |
| 194 | _staging_thread_count = 0 |
| 195 | # Lock used to lock increasing/decreasing count. |
| 196 | _staging_thread_count_lock = threading.Lock() |
| 197 | |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 198 | def __init__(self, manage_builds=False, board=None, version=None, |
| 199 | images_dir=None, log_screen=True, **kwargs): |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 200 | super(XBuddy, self).__init__(**kwargs) |
joychen | b0dfe55 | 2013-07-30 10:02:06 -0700 | [diff] [blame] | 201 | |
Yiming Chen | aab488e | 2014-11-17 14:49:31 -0800 | [diff] [blame] | 202 | if not log_screen: |
| 203 | cherrypy.config.update({'log.screen': False}) |
| 204 | |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 205 | self.config = self._ReadConfig() |
| 206 | self._manage_builds = manage_builds or self._ManageBuilds() |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 207 | self._board = board |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 208 | self._version = version |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 209 | self._timestamp_folder = os.path.join(self.static_dir, |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 210 | Timestamp.XBUDDY_TIMESTAMP_DIR) |
Chris Sosa | 7cd2320 | 2013-10-15 17:22:57 -0700 | [diff] [blame] | 211 | if images_dir: |
| 212 | self.images_dir = images_dir |
| 213 | else: |
| 214 | self.images_dir = os.path.join(self.GetSourceRoot(), 'src/build/images') |
| 215 | |
xixuan | 178263c | 2017-03-22 09:10:25 -0700 | [diff] [blame] | 216 | if common_util.IsRunningOnMoblab(): |
| 217 | self._ctx = gs.GSContext(cache_user='chronos') if gs else None |
| 218 | else: |
| 219 | self._ctx = gs.GSContext() if gs else None |
xixuan | 44b5545 | 2016-09-06 15:35:56 -0700 | [diff] [blame] | 220 | |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 221 | common_util.MkDirP(self._timestamp_folder) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 222 | |
| 223 | @classmethod |
| 224 | def ParseBoolean(cls, boolean_string): |
| 225 | """Evaluate a string to a boolean value""" |
| 226 | if boolean_string: |
| 227 | return boolean_string.lower() in cls._true_values |
| 228 | else: |
| 229 | return False |
| 230 | |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 231 | def _ReadConfig(self): |
| 232 | """Read xbuddy config from ini files. |
| 233 | |
| 234 | Reads the base config from xbuddy_config.ini, and then merges in the |
| 235 | shadow config from shadow_xbuddy_config.ini |
| 236 | |
| 237 | Returns: |
| 238 | The merged configuration. |
Yu-Ju Hong | c54658c | 2014-01-22 09:18:07 -0800 | [diff] [blame] | 239 | |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 240 | Raises: |
| 241 | XBuddyException if the config file is missing. |
| 242 | """ |
| 243 | xbuddy_config = ConfigParser.ConfigParser() |
| 244 | config_file = os.path.join(self.devserver_dir, CONFIG_FILE) |
| 245 | if os.path.exists(config_file): |
| 246 | xbuddy_config.read(config_file) |
| 247 | else: |
Yiming Chen | d920214 | 2014-11-07 14:56:52 -0800 | [diff] [blame] | 248 | # Get the directory of xbuddy.py file. |
| 249 | file_dir = os.path.dirname(os.path.realpath(__file__)) |
| 250 | # Read the default xbuddy_config.ini from the directory. |
| 251 | xbuddy_config.read(os.path.join(file_dir, CONFIG_FILE)) |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 252 | |
| 253 | # Read the shadow file if there is one. |
Chris Sosa | c2abc72 | 2013-08-26 17:11:22 -0700 | [diff] [blame] | 254 | if os.path.isdir(CHROOT_SHADOW_DIR): |
| 255 | shadow_config_file = os.path.join(CHROOT_SHADOW_DIR, SHADOW_CONFIG_FILE) |
| 256 | else: |
| 257 | shadow_config_file = os.path.join(self.devserver_dir, SHADOW_CONFIG_FILE) |
| 258 | |
| 259 | _Log('Using shadow config file stored at %s', shadow_config_file) |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 260 | if os.path.exists(shadow_config_file): |
| 261 | shadow_xbuddy_config = ConfigParser.ConfigParser() |
| 262 | shadow_xbuddy_config.read(shadow_config_file) |
| 263 | |
| 264 | # Merge shadow config in. |
| 265 | sections = shadow_xbuddy_config.sections() |
| 266 | for s in sections: |
| 267 | if not xbuddy_config.has_section(s): |
| 268 | xbuddy_config.add_section(s) |
| 269 | options = shadow_xbuddy_config.options(s) |
| 270 | for o in options: |
| 271 | val = shadow_xbuddy_config.get(s, o) |
| 272 | xbuddy_config.set(s, o, val) |
| 273 | |
| 274 | return xbuddy_config |
| 275 | |
| 276 | def _ManageBuilds(self): |
| 277 | """Checks if xBuddy is managing local builds using the current config.""" |
| 278 | try: |
| 279 | return self.ParseBoolean(self.config.get(GENERAL, 'manage_builds')) |
| 280 | except ConfigParser.Error: |
| 281 | return False |
| 282 | |
| 283 | def _Capacity(self): |
| 284 | """Gets the xbuddy capacity from the current config.""" |
| 285 | try: |
| 286 | return int(self.config.get(GENERAL, 'capacity')) |
| 287 | except ConfigParser.Error: |
| 288 | return 5 |
| 289 | |
Gilad Arnold | 38e828c | 2015-04-24 13:52:07 -0700 | [diff] [blame] | 290 | def LookupAlias(self, alias, board=None, version=None): |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 291 | """Given the full xbuddy config, look up an alias for path rewrite. |
| 292 | |
| 293 | Args: |
| 294 | alias: The xbuddy path that could be one of the aliases in the |
| 295 | rewrite table. |
| 296 | board: The board to fill in with when paths are rewritten. Can be from |
Gilad Arnold | 38e828c | 2015-04-24 13:52:07 -0700 | [diff] [blame] | 297 | the update request xml or the default board from devserver. If None, |
| 298 | defers to the value given during XBuddy initialization. |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 299 | version: The version to fill in when rewriting paths. Could be a specific |
Gilad Arnold | 38e828c | 2015-04-24 13:52:07 -0700 | [diff] [blame] | 300 | version number or a version alias like LATEST. If None, defers to the |
| 301 | value given during XBuddy initialization, or LATEST. |
Yu-Ju Hong | c54658c | 2014-01-22 09:18:07 -0800 | [diff] [blame] | 302 | |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 303 | Returns: |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 304 | A pair (val, suffix) where val is the rewritten path, or the original |
| 305 | string if no rewrite was found; and suffix is the assigned location |
| 306 | suffix, or the default suffix if none was found. |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 307 | """ |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 308 | try: |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 309 | suffix = self.config.get(LOCATION_SUFFIXES, alias) |
| 310 | except ConfigParser.Error: |
| 311 | suffix = RELEASE |
| 312 | |
| 313 | try: |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 314 | val = self.config.get(PATH_REWRITES, alias) |
| 315 | except ConfigParser.Error: |
| 316 | # No alias lookup found. Return original path. |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 317 | val = None |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 318 | |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 319 | if not (val and val.strip()): |
| 320 | val = alias |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 321 | else: |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 322 | # The found value is not an empty string. |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 323 | # Fill in the board and version. |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 324 | val = val.replace("BOARD", "%(board)s") |
| 325 | val = val.replace("VERSION", "%(version)s") |
Gilad Arnold | 38e828c | 2015-04-24 13:52:07 -0700 | [diff] [blame] | 326 | val = val % {'board': board or self._board, |
| 327 | 'version': version or self._version or LATEST} |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 328 | |
| 329 | _Log("Path is %s, location suffix is %s", val, suffix) |
| 330 | return val, suffix |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 331 | |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 332 | @staticmethod |
| 333 | def _ResolveImageDir(image_dir): |
| 334 | """Clean up and return the image dir to use. |
| 335 | |
| 336 | Args: |
| 337 | image_dir: directory in Google Storage to use. |
| 338 | |
| 339 | Returns: |
| 340 | |image_dir| if |image_dir| is not None. Otherwise, returns |
| 341 | devserver_constants.GS_IMAGE_DIR |
| 342 | """ |
| 343 | image_dir = image_dir or devserver_constants.GS_IMAGE_DIR |
| 344 | # Remove trailing slashes. |
| 345 | return image_dir.rstrip('/') |
| 346 | |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 347 | def _LookupOfficial(self, board, suffix, image_dir=None): |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 348 | """Check LATEST-master for the version number of interest.""" |
| 349 | _Log("Checking gs for latest %s-%s image", board, suffix) |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 350 | image_dir = XBuddy._ResolveImageDir(image_dir) |
| 351 | latest_addr = (devserver_constants.GS_LATEST_MASTER % |
| 352 | {'image_dir': image_dir, |
| 353 | 'board': board, |
| 354 | 'suffix': suffix}) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 355 | # Full release + version is in the LATEST file. |
xixuan | 44b5545 | 2016-09-06 15:35:56 -0700 | [diff] [blame] | 356 | version = self._ctx.Cat(latest_addr) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 357 | |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 358 | return devserver_constants.IMAGE_DIR % {'board':board, |
| 359 | 'suffix':suffix, |
| 360 | 'version':version} |
Gilad Arnold | 869e8ab | 2015-02-19 23:34:49 -0800 | [diff] [blame] | 361 | |
xixuan | 44b5545 | 2016-09-06 15:35:56 -0700 | [diff] [blame] | 362 | def _LS(self, path, list_subdirectory=False): |
| 363 | """Does a directory listing of the given gs path. |
| 364 | |
| 365 | Args: |
| 366 | path: directory location on google storage to check. |
| 367 | list_subdirectory: whether to only list subdirectory for |path|. |
| 368 | |
| 369 | Returns: |
| 370 | A list of paths that matched |path|. |
| 371 | """ |
| 372 | if list_subdirectory: |
| 373 | return self._ctx.DoCommand( |
| 374 | ['ls', '-d', '--', path]).output.splitlines() |
| 375 | else: |
| 376 | return self._ctx.LS(path) |
| 377 | |
| 378 | def _GetLatestVersionFromGsDir(self, path, list_subdirectory=False, |
| 379 | with_release=True): |
| 380 | """Returns most recent version number found in a google storage directory. |
| 381 | |
| 382 | This lists out the contents of the given GS bucket or regex to GS buckets, |
| 383 | and tries to grab the newest version found in the directory names. |
| 384 | |
| 385 | Args: |
| 386 | path: directory location on google storage to check. |
| 387 | list_subdirectory: whether to only list subdirectory for |path|. |
| 388 | with_release: whether versions include a release milestone (e.g. R12). |
| 389 | |
| 390 | Returns: |
| 391 | The most recent version number found. |
| 392 | """ |
| 393 | list_result = self._LS(path, list_subdirectory=list_subdirectory) |
| 394 | dir_names = [os.path.basename(p.rstrip('/')) for p in list_result] |
| 395 | try: |
| 396 | filter_re = re.compile(devserver_constants.VERSION_RE if with_release |
| 397 | else devserver_constants.VERSION) |
| 398 | versions = filter(filter_re.match, dir_names) |
| 399 | latest_version = max(versions, key=distutils.version.LooseVersion) |
| 400 | except ValueError: |
| 401 | raise gs.GSContextException( |
| 402 | 'Failed to find most recent builds at %s' % path) |
| 403 | |
| 404 | return latest_version |
| 405 | |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 406 | def _LookupChannel(self, board, suffix, channel='stable', |
| 407 | image_dir=None): |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 408 | """Check the channel folder for the version number of interest.""" |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 409 | # Get all names in channel dir. Get 10 highest directories by version. |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 410 | _Log("Checking channel '%s' for latest '%s' image", channel, board) |
Yu-Ju Hong | c54658c | 2014-01-22 09:18:07 -0800 | [diff] [blame] | 411 | # Due to historical reasons, gs://chromeos-releases uses |
| 412 | # daisy-spring as opposed to the board name daisy_spring. Convert |
xixuan | 44b5545 | 2016-09-06 15:35:56 -0700 | [diff] [blame] | 413 | # he board name for the lookup. |
Yu-Ju Hong | c54658c | 2014-01-22 09:18:07 -0800 | [diff] [blame] | 414 | channel_dir = devserver_constants.GS_CHANNEL_DIR % { |
| 415 | 'channel':channel, |
| 416 | 'board':re.sub('_', '-', board)} |
xixuan | 44b5545 | 2016-09-06 15:35:56 -0700 | [diff] [blame] | 417 | latest_version = self._GetLatestVersionFromGsDir(channel_dir, |
| 418 | with_release=False) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 419 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 420 | # Figure out release number from the version number. |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 421 | image_url = devserver_constants.IMAGE_DIR % { |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 422 | 'board': board, |
| 423 | 'suffix': suffix, |
| 424 | 'version': 'R*' + latest_version} |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 425 | image_dir = XBuddy._ResolveImageDir(image_dir) |
| 426 | gs_url = os.path.join(image_dir, image_url) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 427 | |
| 428 | # There should only be one match on cros-image-archive. |
xixuan | 44b5545 | 2016-09-06 15:35:56 -0700 | [diff] [blame] | 429 | full_version = self._GetLatestVersionFromGsDir(gs_url, |
| 430 | list_subdirectory=True) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 431 | |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 432 | return devserver_constants.IMAGE_DIR % {'board': board, |
| 433 | 'suffix': suffix, |
| 434 | 'version': full_version} |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 435 | |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 436 | def _LookupVersion(self, board, suffix, version): |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 437 | """Search GS image releases for the highest match to a version prefix.""" |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 438 | # Build the pattern for GS to match. |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 439 | _Log("Checking gs for latest '%s' image with prefix '%s'", board, version) |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 440 | image_url = devserver_constants.IMAGE_DIR % {'board': board, |
| 441 | 'suffix': suffix, |
| 442 | 'version': version + '*'} |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 443 | image_dir = os.path.join(devserver_constants.GS_IMAGE_DIR, image_url) |
| 444 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 445 | # Grab the newest version of the ones matched. |
xixuan | 44b5545 | 2016-09-06 15:35:56 -0700 | [diff] [blame] | 446 | full_version = self._GetLatestVersionFromGsDir(image_dir, |
| 447 | list_subdirectory=True) |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 448 | return devserver_constants.IMAGE_DIR % {'board': board, |
| 449 | 'suffix': suffix, |
| 450 | 'version': full_version} |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 451 | |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 452 | def _RemoteBuildId(self, board, suffix, version): |
Chris Sosa | ea734d9 | 2013-10-11 11:28:58 -0700 | [diff] [blame] | 453 | """Returns the remote build_id for the given board and version. |
| 454 | |
| 455 | Raises: |
| 456 | XBuddyException: If we failed to resolve the version to a valid build_id. |
| 457 | """ |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 458 | build_id_as_is = devserver_constants.IMAGE_DIR % {'board': board, |
| 459 | 'suffix': '', |
| 460 | 'version': version} |
| 461 | build_id_suffix = devserver_constants.IMAGE_DIR % {'board': board, |
| 462 | 'suffix': suffix, |
| 463 | 'version': version} |
Chris Sosa | ea734d9 | 2013-10-11 11:28:58 -0700 | [diff] [blame] | 464 | # Return the first path that exists. We assume that what the user typed |
| 465 | # is better than with a default suffix added i.e. x86-generic/blah is |
| 466 | # more valuable than x86-generic-release/blah. |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 467 | for build_id in build_id_as_is, build_id_suffix: |
Chris Sosa | ea734d9 | 2013-10-11 11:28:58 -0700 | [diff] [blame] | 468 | try: |
xixuan | 44b5545 | 2016-09-06 15:35:56 -0700 | [diff] [blame] | 469 | version = self._ctx.LS( |
| 470 | '%s/%s' % (devserver_constants.GS_IMAGE_DIR, build_id)) |
Chris Sosa | ea734d9 | 2013-10-11 11:28:58 -0700 | [diff] [blame] | 471 | return build_id |
xixuan | 44b5545 | 2016-09-06 15:35:56 -0700 | [diff] [blame] | 472 | except (gs.GSCommandError, gs.GSContextException, gs.GSNoSuchKey): |
Chris Sosa | ea734d9 | 2013-10-11 11:28:58 -0700 | [diff] [blame] | 473 | continue |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 474 | |
| 475 | raise XBuddyException('Could not find remote build_id for %s %s' % ( |
| 476 | board, version)) |
Chris Sosa | ea734d9 | 2013-10-11 11:28:58 -0700 | [diff] [blame] | 477 | |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 478 | def _ResolveBuildVersion(self, board, suffix, base_version): |
Gilad Arnold | 869e8ab | 2015-02-19 23:34:49 -0800 | [diff] [blame] | 479 | """Check LATEST-<base_version> and returns a full build version.""" |
| 480 | _Log('Checking gs for full version for %s of %s', base_version, board) |
| 481 | # TODO(garnold) We might want to accommodate version prefixes and pick the |
| 482 | # most recent found, as done in _LookupVersion(). |
| 483 | latest_addr = (devserver_constants.GS_LATEST_BASE_VERSION % |
| 484 | {'image_dir': devserver_constants.GS_IMAGE_DIR, |
| 485 | 'board': board, |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 486 | 'suffix': suffix, |
Gilad Arnold | 869e8ab | 2015-02-19 23:34:49 -0800 | [diff] [blame] | 487 | 'base_version': base_version}) |
Gilad Arnold | 869e8ab | 2015-02-19 23:34:49 -0800 | [diff] [blame] | 488 | # Full release + version is in the LATEST file. |
xixuan | 44b5545 | 2016-09-06 15:35:56 -0700 | [diff] [blame] | 489 | return self._ctx.Cat(latest_addr) |
Gilad Arnold | 869e8ab | 2015-02-19 23:34:49 -0800 | [diff] [blame] | 490 | |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 491 | def _ResolveVersionToBuildId(self, board, suffix, version, image_dir=None): |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 492 | """Handle version aliases for remote payloads in GS. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 493 | |
| 494 | Args: |
| 495 | board: as specified in the original call. (i.e. x86-generic, parrot) |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 496 | suffix: The location suffix, to be added to board name. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 497 | version: as entered in the original call. can be |
| 498 | {TBD, 0. some custom alias as defined in a config file} |
Ningning Xia | b2a1af5 | 2016-04-22 11:14:42 -0700 | [diff] [blame] | 499 | 1. fully qualified build version. |
Gilad Arnold | 869e8ab | 2015-02-19 23:34:49 -0800 | [diff] [blame] | 500 | 2. latest |
| 501 | 3. latest-{channel} |
| 502 | 4. latest-official-{board suffix} |
| 503 | 5. version prefix (i.e. RX-Y.X, RX-Y, RX) |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 504 | image_dir: image directory to check in Google Storage. If none, |
| 505 | the default bucket is used. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 506 | |
| 507 | Returns: |
Chris Sosa | ea734d9 | 2013-10-11 11:28:58 -0700 | [diff] [blame] | 508 | Location where the image dir is actually found on GS (build_id) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 509 | |
Chris Sosa | ea734d9 | 2013-10-11 11:28:58 -0700 | [diff] [blame] | 510 | Raises: |
| 511 | XBuddyException: If we failed to resolve the version to a valid url. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 512 | """ |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 513 | # Only the last segment of the alias is variable relative to the rest. |
| 514 | version_tuple = version.rsplit('-', 1) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 515 | |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 516 | if re.match(devserver_constants.VERSION_RE, version): |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 517 | return self._RemoteBuildId(board, suffix, version) |
Gilad Arnold | 869e8ab | 2015-02-19 23:34:49 -0800 | [diff] [blame] | 518 | elif re.match(devserver_constants.VERSION, version): |
Ningning Xia | b2a1af5 | 2016-04-22 11:14:42 -0700 | [diff] [blame] | 519 | raise XBuddyException('\'%s\' is not valid. Should provide the fully ' |
| 520 | 'qualified version with a version prefix \'RX-\' ' |
| 521 | 'due to crbug.com/585914' % version) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 522 | elif version == LATEST_OFFICIAL: |
| 523 | # latest-official --> LATEST build in board-release |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 524 | return self._LookupOfficial(board, suffix, image_dir=image_dir) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 525 | elif version_tuple[0] == LATEST_OFFICIAL: |
| 526 | # latest-official-{suffix} --> LATEST build in board-{suffix} |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 527 | return self._LookupOfficial(board, version_tuple[1], |
| 528 | image_dir=image_dir) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 529 | elif version == LATEST: |
| 530 | # latest --> latest build on stable channel |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 531 | return self._LookupChannel(board, suffix, image_dir=image_dir) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 532 | elif version_tuple[0] == LATEST: |
| 533 | if re.match(devserver_constants.VERSION_RE, version_tuple[1]): |
| 534 | # latest-R* --> most recent qualifying build |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 535 | return self._LookupVersion(board, suffix, version_tuple[1]) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 536 | else: |
| 537 | # latest-{channel} --> latest build within that channel |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 538 | return self._LookupChannel(board, suffix, channel=version_tuple[1], |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 539 | image_dir=image_dir) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 540 | else: |
| 541 | # The given version doesn't match any known patterns. |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 542 | raise XBuddyException("Version %s unknown. Can't find on GS." % version) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 543 | |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 544 | @staticmethod |
| 545 | def _Symlink(link, target): |
| 546 | """Symlinks link to target, and removes whatever link was there before.""" |
| 547 | _Log("Linking to %s from %s", link, target) |
| 548 | if os.path.lexists(link): |
| 549 | os.unlink(link) |
| 550 | os.symlink(target, link) |
| 551 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 552 | def _GetLatestLocalVersion(self, board): |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 553 | """Get the version of the latest image built for board by build_image |
| 554 | |
| 555 | Updates the symlink reference within the xBuddy static dir to point to |
| 556 | the real image dir in the local /build/images directory. |
| 557 | |
| 558 | Args: |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 559 | board: board that image was built for. |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 560 | |
| 561 | Returns: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 562 | The discovered version of the image. |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 563 | |
| 564 | Raises: |
| 565 | XBuddyException if neither test nor dev image was found in latest built |
| 566 | directory. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 567 | """ |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 568 | latest_local_dir = self.GetLatestImageDir(board) |
joychen | b0dfe55 | 2013-07-30 10:02:06 -0700 | [diff] [blame] | 569 | if not latest_local_dir or not os.path.exists(latest_local_dir): |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 570 | raise XBuddyException('No builds found for %s. Did you run build_image?' % |
| 571 | board) |
| 572 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 573 | # Assume that the version number is the name of the directory. |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 574 | return os.path.basename(latest_local_dir.rstrip('/')) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 575 | |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 576 | @staticmethod |
| 577 | def _FindAny(local_dir): |
| 578 | """Returns the image_type for ANY given the local_dir.""" |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 579 | test_image = os.path.join(local_dir, devserver_constants.TEST_IMAGE_FILE) |
Yu-Ju Hong | c23c79b | 2014-03-17 12:40:33 -0700 | [diff] [blame] | 580 | dev_image = os.path.join(local_dir, devserver_constants.IMAGE_FILE) |
| 581 | # Prioritize test images over dev images. |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 582 | if os.path.exists(test_image): |
| 583 | return 'test' |
| 584 | |
Yu-Ju Hong | c23c79b | 2014-03-17 12:40:33 -0700 | [diff] [blame] | 585 | if os.path.exists(dev_image): |
| 586 | return 'dev' |
| 587 | |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 588 | raise XBuddyException('No images found in %s' % local_dir) |
| 589 | |
| 590 | @staticmethod |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 591 | def _InterpretPath(path, default_board=None, default_version=None): |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 592 | """Split and return the pieces of an xBuddy path name |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 593 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 594 | Args: |
| 595 | path: the path xBuddy Get was called with. |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 596 | default_board: board to use in case board isn't in path. |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 597 | default_version: Version to use in case version isn't in path. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 598 | |
Yu-Ju Hong | c54658c | 2014-01-22 09:18:07 -0800 | [diff] [blame] | 599 | Returns: |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 600 | tuple of (image_type, board, version, whether the path is local) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 601 | |
| 602 | Raises: |
| 603 | XBuddyException: if the path can't be resolved into valid components |
| 604 | """ |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 605 | path_list = filter(None, path.split('/')) |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 606 | |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 607 | # Do the stuff that is well known first. We know that if paths have a |
| 608 | # image_type, it must be one of the GS/LOCAL aliases and it must be at the |
| 609 | # end. Similarly, local/remote are well-known and must start the path list. |
| 610 | is_local = True |
| 611 | if path_list and path_list[0] in (REMOTE, LOCAL): |
| 612 | is_local = (path_list.pop(0) == LOCAL) |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 613 | |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 614 | # Default image type is determined by remote vs. local. |
| 615 | if is_local: |
| 616 | image_type = ANY |
| 617 | else: |
| 618 | image_type = TEST |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 619 | |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 620 | if path_list and path_list[-1] in GS_ALIASES + LOCAL_ALIASES: |
| 621 | image_type = path_list.pop(-1) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 622 | |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 623 | # Now for the tricky part. We don't actually know at this point if the rest |
| 624 | # of the path is just a board | version (like R33-2341.0.0) or just a board |
| 625 | # or just a version. So we do our best to do the right thing. |
| 626 | board = default_board |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 627 | version = default_version or LATEST |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 628 | if len(path_list) == 1: |
| 629 | path = path_list.pop(0) |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 630 | # Treat this as a version if it's one we know (contains default or |
| 631 | # latest), or we were given an actual default board. |
| 632 | if default_version in path or LATEST in path or default_board is not None: |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 633 | version = path |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 634 | else: |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 635 | board = path |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 636 | |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 637 | elif len(path_list) == 2: |
| 638 | # Assumes board/version. |
| 639 | board = path_list.pop(0) |
| 640 | version = path_list.pop(0) |
| 641 | |
| 642 | if path_list: |
| 643 | raise XBuddyException("Path isn't valid. Could not figure out how to " |
| 644 | "parse remaining components: %s." % path_list) |
| 645 | |
| 646 | _Log("Get artifact '%s' with board %s and version %s'. Locally? %s", |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 647 | image_type, board, version, is_local) |
| 648 | |
| 649 | return image_type, board, version, is_local |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 650 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 651 | def _SyncRegistryWithBuildImages(self): |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 652 | """Crawl images_dir for build_ids of images generated from build_image. |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 653 | |
| 654 | This will find images and symlink them in xBuddy's static dir so that |
| 655 | xBuddy's cache can serve them. |
| 656 | If xBuddy's _manage_builds option is on, then a timestamp will also be |
| 657 | generated, and xBuddy will clear them from the directory they are in, as |
| 658 | necessary. |
| 659 | """ |
Yu-Ju Hong | 235d1b5 | 2014-04-16 11:01:47 -0700 | [diff] [blame] | 660 | if not os.path.isdir(self.images_dir): |
| 661 | # Skip syncing if images_dir does not exist. |
| 662 | _Log('Cannot find %s; skip syncing image registry.', self.images_dir) |
| 663 | return |
| 664 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 665 | build_ids = [] |
| 666 | for b in os.listdir(self.images_dir): |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 667 | # Ensure we have directories to track all boards in build/images |
| 668 | common_util.MkDirP(os.path.join(self.static_dir, b)) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 669 | board_dir = os.path.join(self.images_dir, b) |
| 670 | build_ids.extend(['/'.join([b, v]) for v |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 671 | in os.listdir(board_dir) if not v == LATEST]) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 672 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 673 | # Symlink undiscovered images, and update timestamps if manage_builds is on. |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 674 | for build_id in build_ids: |
| 675 | link = os.path.join(self.static_dir, build_id) |
| 676 | target = os.path.join(self.images_dir, build_id) |
| 677 | XBuddy._Symlink(link, target) |
| 678 | if self._manage_builds: |
| 679 | Timestamp.UpdateTimestamp(self._timestamp_folder, build_id) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 680 | |
| 681 | def _ListBuildTimes(self): |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 682 | """Returns the currently cached builds and their last access timestamp. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 683 | |
| 684 | Returns: |
| 685 | list of tuples that matches xBuddy build/version to timestamps in long |
| 686 | """ |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 687 | # Update currently cached builds. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 688 | build_dict = {} |
| 689 | |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 690 | for f in os.listdir(self._timestamp_folder): |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 691 | last_accessed = os.path.getmtime(os.path.join(self._timestamp_folder, f)) |
| 692 | build_id = Timestamp.TimestampToBuild(f) |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 693 | stale_time = datetime.timedelta(seconds=(time.time() - last_accessed)) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 694 | build_dict[build_id] = stale_time |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 695 | return_tup = sorted(build_dict.iteritems(), key=operator.itemgetter(1)) |
| 696 | return return_tup |
| 697 | |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 698 | def _Download(self, gs_url, artifacts): |
| 699 | """Download the artifacts from the given gs_url. |
| 700 | |
| 701 | Raises: |
| 702 | build_artifact.ArtifactDownloadError: If we failed to download the |
| 703 | artifact. |
| 704 | """ |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 705 | with XBuddy._staging_thread_count_lock: |
| 706 | XBuddy._staging_thread_count += 1 |
| 707 | try: |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 708 | _Log("Downloading %s from %s", artifacts, gs_url) |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 709 | dl = downloader.GoogleStorageDownloader(self.static_dir, gs_url) |
| 710 | factory = build_artifact.ChromeOSArtifactFactory( |
| 711 | dl.GetBuildDir(), artifacts, [], dl.GetBuild()) |
| 712 | dl.Download(factory) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 713 | finally: |
| 714 | with XBuddy._staging_thread_count_lock: |
| 715 | XBuddy._staging_thread_count -= 1 |
| 716 | |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 717 | def CleanCache(self): |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 718 | """Delete all builds besides the newest N builds""" |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 719 | if not self._manage_builds: |
| 720 | return |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 721 | cached_builds = [e[0] for e in self._ListBuildTimes()] |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 722 | _Log('In cache now: %s', cached_builds) |
| 723 | |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 724 | for b in range(self._Capacity(), len(cached_builds)): |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 725 | b_path = cached_builds[b] |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 726 | _Log("Clearing '%s' from cache", b_path) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 727 | |
| 728 | time_file = os.path.join(self._timestamp_folder, |
| 729 | Timestamp.BuildToTimestamp(b_path)) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 730 | os.unlink(time_file) |
| 731 | clear_dir = os.path.join(self.static_dir, b_path) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 732 | try: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 733 | # Handle symlinks, in the case of links to local builds if enabled. |
| 734 | if os.path.islink(clear_dir): |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 735 | target = os.readlink(clear_dir) |
| 736 | _Log('Deleting locally built image at %s', target) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 737 | |
| 738 | os.unlink(clear_dir) |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 739 | if os.path.exists(target): |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 740 | shutil.rmtree(target) |
| 741 | elif os.path.exists(clear_dir): |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 742 | _Log('Deleting downloaded image at %s', clear_dir) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 743 | shutil.rmtree(clear_dir) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 744 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 745 | except Exception as err: |
| 746 | raise XBuddyException('Failed to clear %s: %s' % (clear_dir, err)) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 747 | |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 748 | def _GetFromGS(self, build_id, image_type, image_dir=None): |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 749 | """Check if the artifact is available locally. Download from GS if not. |
| 750 | |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 751 | Args: |
| 752 | build_id: Path to the image or update directory on the devserver or |
| 753 | in Google Storage. e.g. 'x86-generic/R26-4000.0.0' |
| 754 | image_type: Image type to download. Look at aliases at top of file for |
| 755 | options. |
| 756 | image_dir: Google Storage image archive to search in if requesting a |
| 757 | remote artifact. If none uses the default bucket. |
| 758 | |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 759 | Raises: |
| 760 | build_artifact.ArtifactDownloadError: If we failed to download the |
| 761 | artifact. |
| 762 | """ |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 763 | image_dir = XBuddy._ResolveImageDir(image_dir) |
| 764 | gs_url = os.path.join(image_dir, build_id) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 765 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 766 | # Stage image if not found in cache. |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 767 | file_name = GS_ALIAS_TO_FILENAME[image_type] |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 768 | file_loc = os.path.join(self.static_dir, build_id, file_name) |
| 769 | cached = os.path.exists(file_loc) |
| 770 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 771 | if not cached: |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 772 | artifact = GS_ALIAS_TO_ARTIFACT[image_type] |
| 773 | self._Download(gs_url, [artifact]) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 774 | else: |
| 775 | _Log('Image already cached.') |
| 776 | |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 777 | def _GetArtifact(self, path_list, board=None, version=None, |
| 778 | lookup_only=False, image_dir=None): |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 779 | """Interpret an xBuddy path and return directory/file_name to resource. |
| 780 | |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 781 | Note board can be passed that in but by default if self._board is set, |
| 782 | that is used rather than board. |
| 783 | |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 784 | Args: |
| 785 | path_list: [board, version, alias] as split from the xbuddy call url. |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 786 | board: Board whos artifacts we are looking for. Only used if no board was |
| 787 | given during XBuddy initialization. |
| 788 | version: Version whose artifacts we are looking for. Used if no version |
| 789 | was given during XBuddy initialization. If None, defers to LATEST. |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 790 | lookup_only: If true just look up the artifact, if False stage it on |
| 791 | the devserver as well. |
| 792 | image_dir: Google Storage image archive to search in if requesting a |
| 793 | remote artifact. If none uses the default bucket. |
| 794 | |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 795 | Returns: |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 796 | build_id: Path to the image or update directory on the devserver or |
| 797 | in Google Storage. e.g. 'x86-generic/R26-4000.0.0' |
| 798 | file_name: of the artifact in the build_id directory. |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 799 | |
| 800 | Raises: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 801 | XBuddyException: if the path could not be translated |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 802 | build_artifact.ArtifactDownloadError: if we failed to download the |
| 803 | artifact. |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 804 | """ |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 805 | path = '/'.join(path_list) |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 806 | default_board = self._board if self._board else board |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 807 | default_version = self._version or version or LATEST |
joychen | b0dfe55 | 2013-07-30 10:02:06 -0700 | [diff] [blame] | 808 | # Rewrite the path if there is an appropriate default. |
Gilad Arnold | 38e828c | 2015-04-24 13:52:07 -0700 | [diff] [blame] | 809 | path, suffix = self.LookupAlias(path, board=default_board, |
| 810 | version=default_version) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 811 | # Parse the path. |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 812 | image_type, board, version, is_local = self._InterpretPath( |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 813 | path, default_board, default_version) |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 814 | if is_local: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 815 | # Get a local image. |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 816 | if version == LATEST: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 817 | # Get the latest local image for the given board. |
| 818 | version = self._GetLatestLocalVersion(board) |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 819 | |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 820 | build_id = os.path.join(board, version) |
| 821 | artifact_dir = os.path.join(self.static_dir, build_id) |
| 822 | if image_type == ANY: |
| 823 | image_type = self._FindAny(artifact_dir) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 824 | |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 825 | file_name = LOCAL_ALIAS_TO_FILENAME[image_type] |
| 826 | artifact_path = os.path.join(artifact_dir, file_name) |
| 827 | if not os.path.exists(artifact_path): |
| 828 | raise XBuddyException('Local %s artifact not in static_dir at %s' % |
| 829 | (image_type, artifact_path)) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 830 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 831 | else: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 832 | # Get a remote image. |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 833 | if image_type not in GS_ALIASES: |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 834 | raise XBuddyException('Bad remote image type: %s. Use one of: %s' % |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 835 | (image_type, GS_ALIASES)) |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame] | 836 | build_id = self._ResolveVersionToBuildId(board, suffix, version, |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 837 | image_dir=image_dir) |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 838 | _Log('Resolved version %s to %s.', version, build_id) |
| 839 | file_name = GS_ALIAS_TO_FILENAME[image_type] |
| 840 | if not lookup_only: |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 841 | self._GetFromGS(build_id, image_type, image_dir=image_dir) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 842 | |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 843 | return build_id, file_name |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 844 | |
| 845 | ############################ BEGIN PUBLIC METHODS |
| 846 | |
| 847 | def List(self): |
| 848 | """Lists the currently available images & time since last access.""" |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 849 | self._SyncRegistryWithBuildImages() |
| 850 | builds = self._ListBuildTimes() |
| 851 | return_string = '' |
| 852 | for build, timestamp in builds: |
| 853 | return_string += '<b>' + build + '</b> ' |
| 854 | return_string += '(time since last access: ' + str(timestamp) + ')<br>' |
| 855 | return return_string |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 856 | |
| 857 | def Capacity(self): |
| 858 | """Returns the number of images cached by xBuddy.""" |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 859 | return str(self._Capacity()) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 860 | |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 861 | def Translate(self, path_list, board=None, version=None, image_dir=None): |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 862 | """Translates an xBuddy path to a real path to artifact if it exists. |
| 863 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 864 | Equivalent to the Get call, minus downloading and updating timestamps, |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 865 | |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 866 | Args: |
| 867 | path_list: [board, version, alias] as split from the xbuddy call url. |
| 868 | board: Board whos artifacts we are looking for. If None, use the board |
| 869 | XBuddy was initialized to use. |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 870 | version: Version whose artifacts we are looking for. If None, use the |
| 871 | version XBuddy was initialized with, or LATEST. |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 872 | image_dir: image directory to check in Google Storage. If none, |
| 873 | the default bucket is used. |
| 874 | |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 875 | Returns: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 876 | build_id: Path to the image or update directory on the devserver. |
| 877 | e.g. 'x86-generic/R26-4000.0.0' |
| 878 | The returned path is always the path to the directory within |
| 879 | static_dir, so it is always the build_id of the image. |
| 880 | file_name: The file name of the artifact. Can take any of the file |
| 881 | values in devserver_constants. |
| 882 | e.g. 'chromiumos_test_image.bin' or 'update.gz' if the path list |
| 883 | specified 'test' or 'full_payload' artifacts, respectively. |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 884 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 885 | Raises: |
| 886 | XBuddyException: if the path couldn't be translated |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 887 | """ |
| 888 | self._SyncRegistryWithBuildImages() |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 889 | build_id, file_name = self._GetArtifact(path_list, board=board, |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 890 | version=version, |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 891 | lookup_only=True, |
| 892 | image_dir=image_dir) |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 893 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 894 | _Log('Returning path to payload: %s/%s', build_id, file_name) |
| 895 | return build_id, file_name |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 896 | |
Yu-Ju Hong | 1bdb7a9 | 2014-04-10 16:02:11 -0700 | [diff] [blame] | 897 | def StageTestArtifactsForUpdate(self, path_list): |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 898 | """Stages test artifacts for update and returns build_id. |
| 899 | |
| 900 | Raises: |
| 901 | XBuddyException: if the path could not be translated |
| 902 | build_artifact.ArtifactDownloadError: if we failed to download the test |
| 903 | artifacts. |
| 904 | """ |
| 905 | build_id, file_name = self.Translate(path_list) |
| 906 | if file_name == devserver_constants.TEST_IMAGE_FILE: |
| 907 | gs_url = os.path.join(devserver_constants.GS_IMAGE_DIR, |
| 908 | build_id) |
| 909 | artifacts = [FULL, STATEFUL] |
| 910 | self._Download(gs_url, artifacts) |
| 911 | return build_id |
| 912 | |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 913 | def Get(self, path_list, image_dir=None): |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 914 | """The full xBuddy call, returns resource specified by path_list. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 915 | |
| 916 | Please see devserver.py:xbuddy for full documentation. |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 917 | |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 918 | Args: |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 919 | path_list: [board, version, alias] as split from the xbuddy call url. |
| 920 | image_dir: image directory to check in Google Storage. If none, |
| 921 | the default bucket is used. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 922 | |
| 923 | Returns: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 924 | build_id: Path to the image or update directory on the devserver. |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 925 | e.g. 'x86-generic/R26-4000.0.0' |
| 926 | The returned path is always the path to the directory within |
| 927 | static_dir, so it is always the build_id of the image. |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 928 | file_name: The file name of the artifact. Can take any of the file |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 929 | values in devserver_constants. |
| 930 | e.g. 'chromiumos_test_image.bin' or 'update.gz' if the path list |
| 931 | specified 'test' or 'full_payload' artifacts, respectively. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 932 | |
| 933 | Raises: |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 934 | XBuddyException: if the path could not be translated |
| 935 | build_artifact.ArtifactDownloadError: if we failed to download the |
| 936 | artifact. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 937 | """ |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 938 | self._SyncRegistryWithBuildImages() |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 939 | build_id, file_name = self._GetArtifact(path_list, image_dir=image_dir) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 940 | Timestamp.UpdateTimestamp(self._timestamp_folder, build_id) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 941 | #TODO (joyc): run in sep thread |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 942 | self.CleanCache() |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 943 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 944 | _Log('Returning path to payload: %s/%s', build_id, file_name) |
| 945 | return build_id, file_name |