blob: 5e7dfcb0d2d3d3229d9faf9236bb97c06f6e09a0 [file] [log] [blame]
joychen3cb228e2013-06-12 12:13:13 -07001# 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 Sosa0eecf962014-02-03 14:14:39 -08005"""Main module for parsing and interpreting XBuddy paths for the devserver."""
6
Gilad Arnold5f46d8e2015-02-19 12:17:55 -08007from __future__ import print_function
8
Yiming Chenaab488e2014-11-17 14:49:31 -08009import cherrypy
joychen562699a2013-08-13 15:22:14 -070010import ConfigParser
joychen3cb228e2013-06-12 12:13:13 -070011import datetime
12import operator
13import os
joychenf8f07e22013-07-12 17:45:51 -070014import re
joychen3cb228e2013-06-12 12:13:13 -070015import shutil
joychenf8f07e22013-07-12 17:45:51 -070016import time
joychen3cb228e2013-06-12 12:13:13 -070017import threading
18
joychen921e1fb2013-06-28 11:12:20 -070019import build_util
joychen3cb228e2013-06-12 12:13:13 -070020import artifact_info
joychen3cb228e2013-06-12 12:13:13 -070021import common_util
22import devserver_constants
23import downloader
joychenf8f07e22013-07-12 17:45:51 -070024import gsutil_util
joychen3cb228e2013-06-12 12:13:13 -070025import log_util
26
27# Module-local log function.
28def _Log(message, *args):
29 return log_util.LogWithTag('XBUDDY', message, *args)
30
joychen562699a2013-08-13 15:22:14 -070031# xBuddy config constants
32CONFIG_FILE = 'xbuddy_config.ini'
33SHADOW_CONFIG_FILE = 'shadow_xbuddy_config.ini'
34PATH_REWRITES = 'PATH_REWRITES'
35GENERAL = 'GENERAL'
joychen921e1fb2013-06-28 11:12:20 -070036
Chris Sosac2abc722013-08-26 17:11:22 -070037# Path for shadow config in chroot.
38CHROOT_SHADOW_DIR = '/mnt/host/source/src/platform/dev'
39
joychen25d25972013-07-30 14:54:16 -070040# XBuddy aliases
41TEST = 'test'
42BASE = 'base'
43DEV = 'dev'
44FULL = 'full_payload'
45RECOVERY = 'recovery'
46STATEFUL = 'stateful'
47AUTOTEST = 'autotest'
48
joychen921e1fb2013-06-28 11:12:20 -070049# Local build constants
joychenc3944cb2013-08-19 10:42:07 -070050ANY = "ANY"
joychen7df67f72013-07-18 14:21:12 -070051LATEST = "latest"
52LOCAL = "local"
53REMOTE = "remote"
Chris Sosa75490802013-09-30 17:21:45 -070054
55# TODO(sosa): Fix a lot of assumptions about these aliases. There is too much
56# implicit logic here that's unnecessary. What should be done:
57# 1) Collapse Alias logic to one set of aliases for xbuddy (not local/remote).
58# 2) Do not use zip when creating these dicts. Better to not rely on ordering.
59# 3) Move alias/artifact mapping to a central module rather than having it here.
60# 4) Be explicit when things are missing i.e. no dev images in image.zip.
61
joychen921e1fb2013-06-28 11:12:20 -070062LOCAL_ALIASES = [
Gilad Arnold5f46d8e2015-02-19 12:17:55 -080063 TEST,
64 DEV,
65 BASE,
66 RECOVERY,
67 FULL,
68 STATEFUL,
69 ANY,
joychen921e1fb2013-06-28 11:12:20 -070070]
71
72LOCAL_FILE_NAMES = [
Gilad Arnold5f46d8e2015-02-19 12:17:55 -080073 devserver_constants.TEST_IMAGE_FILE,
74 devserver_constants.IMAGE_FILE,
75 devserver_constants.BASE_IMAGE_FILE,
76 devserver_constants.RECOVERY_IMAGE_FILE,
77 devserver_constants.UPDATE_FILE,
78 devserver_constants.STATEFUL_FILE,
79 None, # For ANY.
joychen921e1fb2013-06-28 11:12:20 -070080]
81
82LOCAL_ALIAS_TO_FILENAME = dict(zip(LOCAL_ALIASES, LOCAL_FILE_NAMES))
83
84# Google Storage constants
85GS_ALIASES = [
Gilad Arnold5f46d8e2015-02-19 12:17:55 -080086 TEST,
87 BASE,
88 RECOVERY,
89 FULL,
90 STATEFUL,
91 AUTOTEST,
joychen3cb228e2013-06-12 12:13:13 -070092]
93
joychen921e1fb2013-06-28 11:12:20 -070094GS_FILE_NAMES = [
Gilad Arnold5f46d8e2015-02-19 12:17:55 -080095 devserver_constants.TEST_IMAGE_FILE,
96 devserver_constants.BASE_IMAGE_FILE,
97 devserver_constants.RECOVERY_IMAGE_FILE,
98 devserver_constants.UPDATE_FILE,
99 devserver_constants.STATEFUL_FILE,
100 devserver_constants.AUTOTEST_DIR,
joychen3cb228e2013-06-12 12:13:13 -0700101]
102
103ARTIFACTS = [
Gilad Arnold5f46d8e2015-02-19 12:17:55 -0800104 artifact_info.TEST_IMAGE,
105 artifact_info.BASE_IMAGE,
106 artifact_info.RECOVERY_IMAGE,
107 artifact_info.FULL_PAYLOAD,
108 artifact_info.STATEFUL_PAYLOAD,
109 artifact_info.AUTOTEST,
joychen3cb228e2013-06-12 12:13:13 -0700110]
111
joychen921e1fb2013-06-28 11:12:20 -0700112GS_ALIAS_TO_FILENAME = dict(zip(GS_ALIASES, GS_FILE_NAMES))
113GS_ALIAS_TO_ARTIFACT = dict(zip(GS_ALIASES, ARTIFACTS))
joychen3cb228e2013-06-12 12:13:13 -0700114
joychen921e1fb2013-06-28 11:12:20 -0700115LATEST_OFFICIAL = "latest-official"
joychen3cb228e2013-06-12 12:13:13 -0700116
Chris Sosaea734d92013-10-11 11:28:58 -0700117RELEASE = "-release"
joychen3cb228e2013-06-12 12:13:13 -0700118
joychen3cb228e2013-06-12 12:13:13 -0700119
120class XBuddyException(Exception):
121 """Exception classes used by this module."""
122 pass
123
124
125# no __init__ method
126#pylint: disable=W0232
Gilad Arnold5f46d8e2015-02-19 12:17:55 -0800127class Timestamp(object):
joychen3cb228e2013-06-12 12:13:13 -0700128 """Class to translate build path strings and timestamp filenames."""
129
130 _TIMESTAMP_DELIMITER = 'SLASH'
131 XBUDDY_TIMESTAMP_DIR = 'xbuddy_UpdateTimestamps'
132
133 @staticmethod
134 def TimestampToBuild(timestamp_filename):
135 return timestamp_filename.replace(Timestamp._TIMESTAMP_DELIMITER, '/')
136
137 @staticmethod
138 def BuildToTimestamp(build_path):
139 return build_path.replace('/', Timestamp._TIMESTAMP_DELIMITER)
joychen921e1fb2013-06-28 11:12:20 -0700140
141 @staticmethod
142 def UpdateTimestamp(timestamp_dir, build_id):
143 """Update timestamp file of build with build_id."""
144 common_util.MkDirP(timestamp_dir)
joychen562699a2013-08-13 15:22:14 -0700145 _Log("Updating timestamp for %s", build_id)
joychen921e1fb2013-06-28 11:12:20 -0700146 time_file = os.path.join(timestamp_dir,
147 Timestamp.BuildToTimestamp(build_id))
148 with file(time_file, 'a'):
149 os.utime(time_file, None)
joychen3cb228e2013-06-12 12:13:13 -0700150#pylint: enable=W0232
151
152
joychen921e1fb2013-06-28 11:12:20 -0700153class XBuddy(build_util.BuildObject):
joychen3cb228e2013-06-12 12:13:13 -0700154 """Class that manages image retrieval and caching by the devserver.
155
156 Image retrieval by xBuddy path:
157 XBuddy accesses images and artifacts that it stores using an xBuddy
158 path of the form: board/version/alias
159 The primary xbuddy.Get call retrieves the correct artifact or url to where
160 the artifacts can be found.
161
162 Image caching:
163 Images and other artifacts are stored identically to how they would have
164 been if devserver's stage rpc was called and the xBuddy cache replaces
165 build versions on a LRU basis. Timestamps are maintained by last accessed
166 times of representative files in the a directory in the static serve
167 directory (XBUDDY_TIMESTAMP_DIR).
168
169 Private class members:
joychen121fc9b2013-08-02 14:30:30 -0700170 _true_values: used for interpreting boolean values
171 _staging_thread_count: track download requests
172 _timestamp_folder: directory with empty files standing in as timestamps
joychen921e1fb2013-06-28 11:12:20 -0700173 for each image currently cached by xBuddy
joychen3cb228e2013-06-12 12:13:13 -0700174 """
175 _true_values = ['true', 't', 'yes', 'y']
176
177 # Number of threads that are staging images.
178 _staging_thread_count = 0
179 # Lock used to lock increasing/decreasing count.
180 _staging_thread_count_lock = threading.Lock()
181
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800182 def __init__(self, manage_builds=False, board=None, version=None,
183 images_dir=None, log_screen=True, **kwargs):
joychen921e1fb2013-06-28 11:12:20 -0700184 super(XBuddy, self).__init__(**kwargs)
joychenb0dfe552013-07-30 10:02:06 -0700185
Yiming Chenaab488e2014-11-17 14:49:31 -0800186 if not log_screen:
187 cherrypy.config.update({'log.screen': False})
188
joychen562699a2013-08-13 15:22:14 -0700189 self.config = self._ReadConfig()
190 self._manage_builds = manage_builds or self._ManageBuilds()
Chris Sosa75490802013-09-30 17:21:45 -0700191 self._board = board
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800192 self._version = version
joychen921e1fb2013-06-28 11:12:20 -0700193 self._timestamp_folder = os.path.join(self.static_dir,
joychen3cb228e2013-06-12 12:13:13 -0700194 Timestamp.XBUDDY_TIMESTAMP_DIR)
Chris Sosa7cd23202013-10-15 17:22:57 -0700195 if images_dir:
196 self.images_dir = images_dir
197 else:
198 self.images_dir = os.path.join(self.GetSourceRoot(), 'src/build/images')
199
joychen7df67f72013-07-18 14:21:12 -0700200 common_util.MkDirP(self._timestamp_folder)
joychen3cb228e2013-06-12 12:13:13 -0700201
202 @classmethod
203 def ParseBoolean(cls, boolean_string):
204 """Evaluate a string to a boolean value"""
205 if boolean_string:
206 return boolean_string.lower() in cls._true_values
207 else:
208 return False
209
joychen562699a2013-08-13 15:22:14 -0700210 def _ReadConfig(self):
211 """Read xbuddy config from ini files.
212
213 Reads the base config from xbuddy_config.ini, and then merges in the
214 shadow config from shadow_xbuddy_config.ini
215
216 Returns:
217 The merged configuration.
Yu-Ju Hongc54658c2014-01-22 09:18:07 -0800218
joychen562699a2013-08-13 15:22:14 -0700219 Raises:
220 XBuddyException if the config file is missing.
221 """
222 xbuddy_config = ConfigParser.ConfigParser()
223 config_file = os.path.join(self.devserver_dir, CONFIG_FILE)
224 if os.path.exists(config_file):
225 xbuddy_config.read(config_file)
226 else:
Yiming Chend9202142014-11-07 14:56:52 -0800227 # Get the directory of xbuddy.py file.
228 file_dir = os.path.dirname(os.path.realpath(__file__))
229 # Read the default xbuddy_config.ini from the directory.
230 xbuddy_config.read(os.path.join(file_dir, CONFIG_FILE))
joychen562699a2013-08-13 15:22:14 -0700231
232 # Read the shadow file if there is one.
Chris Sosac2abc722013-08-26 17:11:22 -0700233 if os.path.isdir(CHROOT_SHADOW_DIR):
234 shadow_config_file = os.path.join(CHROOT_SHADOW_DIR, SHADOW_CONFIG_FILE)
235 else:
236 shadow_config_file = os.path.join(self.devserver_dir, SHADOW_CONFIG_FILE)
237
238 _Log('Using shadow config file stored at %s', shadow_config_file)
joychen562699a2013-08-13 15:22:14 -0700239 if os.path.exists(shadow_config_file):
240 shadow_xbuddy_config = ConfigParser.ConfigParser()
241 shadow_xbuddy_config.read(shadow_config_file)
242
243 # Merge shadow config in.
244 sections = shadow_xbuddy_config.sections()
245 for s in sections:
246 if not xbuddy_config.has_section(s):
247 xbuddy_config.add_section(s)
248 options = shadow_xbuddy_config.options(s)
249 for o in options:
250 val = shadow_xbuddy_config.get(s, o)
251 xbuddy_config.set(s, o, val)
252
253 return xbuddy_config
254
255 def _ManageBuilds(self):
256 """Checks if xBuddy is managing local builds using the current config."""
257 try:
258 return self.ParseBoolean(self.config.get(GENERAL, 'manage_builds'))
259 except ConfigParser.Error:
260 return False
261
262 def _Capacity(self):
263 """Gets the xbuddy capacity from the current config."""
264 try:
265 return int(self.config.get(GENERAL, 'capacity'))
266 except ConfigParser.Error:
267 return 5
268
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800269 def _LookupAlias(self, alias, board, version):
joychen562699a2013-08-13 15:22:14 -0700270 """Given the full xbuddy config, look up an alias for path rewrite.
271
272 Args:
273 alias: The xbuddy path that could be one of the aliases in the
274 rewrite table.
275 board: The board to fill in with when paths are rewritten. Can be from
276 the update request xml or the default board from devserver.
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800277 version: The version to fill in when rewriting paths. Could be a specific
278 version number or a version alias like LATEST.
Yu-Ju Hongc54658c2014-01-22 09:18:07 -0800279
joychen562699a2013-08-13 15:22:14 -0700280 Returns:
281 If a rewrite is found, a string with the current board substituted in.
282 If no rewrite is found, just return the original string.
283 """
joychen562699a2013-08-13 15:22:14 -0700284 try:
285 val = self.config.get(PATH_REWRITES, alias)
286 except ConfigParser.Error:
287 # No alias lookup found. Return original path.
288 return alias
289
290 if not val.strip():
291 # The found value was an empty string.
292 return alias
293 else:
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800294 # Fill in the board and version.
295 rewrite = val.replace("BOARD", "%(board)s")
296 rewrite = rewrite.replace("VERSION", "%(version)s")
297 rewrite = rewrite % {'board': board, 'version': version}
joychen562699a2013-08-13 15:22:14 -0700298 _Log("Path was rewritten to %s", rewrite)
299 return rewrite
300
Simran Basi99e63c02014-05-20 10:39:52 -0700301 @staticmethod
302 def _ResolveImageDir(image_dir):
303 """Clean up and return the image dir to use.
304
305 Args:
306 image_dir: directory in Google Storage to use.
307
308 Returns:
309 |image_dir| if |image_dir| is not None. Otherwise, returns
310 devserver_constants.GS_IMAGE_DIR
311 """
312 image_dir = image_dir or devserver_constants.GS_IMAGE_DIR
313 # Remove trailing slashes.
314 return image_dir.rstrip('/')
315
316 def _LookupOfficial(self, board, suffix=RELEASE, image_dir=None):
joychenf8f07e22013-07-12 17:45:51 -0700317 """Check LATEST-master for the version number of interest."""
318 _Log("Checking gs for latest %s-%s image", board, suffix)
Simran Basi99e63c02014-05-20 10:39:52 -0700319 image_dir = XBuddy._ResolveImageDir(image_dir)
320 latest_addr = (devserver_constants.GS_LATEST_MASTER %
321 {'image_dir': image_dir,
322 'board': board,
323 'suffix': suffix})
joychenf8f07e22013-07-12 17:45:51 -0700324 cmd = 'gsutil cat %s' % latest_addr
325 msg = 'Failed to find build at %s' % latest_addr
joychen121fc9b2013-08-02 14:30:30 -0700326 # Full release + version is in the LATEST file.
joychenf8f07e22013-07-12 17:45:51 -0700327 version = gsutil_util.GSUtilRun(cmd, msg)
joychen3cb228e2013-06-12 12:13:13 -0700328
joychenf8f07e22013-07-12 17:45:51 -0700329 return devserver_constants.IMAGE_DIR % {'board':board,
330 'suffix':suffix,
331 'version':version}
Gilad Arnold869e8ab2015-02-19 23:34:49 -0800332
Simran Basi99e63c02014-05-20 10:39:52 -0700333 def _LookupChannel(self, board, channel='stable', image_dir=None):
joychenf8f07e22013-07-12 17:45:51 -0700334 """Check the channel folder for the version number of interest."""
joychen121fc9b2013-08-02 14:30:30 -0700335 # Get all names in channel dir. Get 10 highest directories by version.
joychen7df67f72013-07-18 14:21:12 -0700336 _Log("Checking channel '%s' for latest '%s' image", channel, board)
Yu-Ju Hongc54658c2014-01-22 09:18:07 -0800337 # Due to historical reasons, gs://chromeos-releases uses
338 # daisy-spring as opposed to the board name daisy_spring. Convert
339 # the board name for the lookup.
340 channel_dir = devserver_constants.GS_CHANNEL_DIR % {
341 'channel':channel,
342 'board':re.sub('_', '-', board)}
joychen562699a2013-08-13 15:22:14 -0700343 latest_version = gsutil_util.GetLatestVersionFromGSDir(
344 channel_dir, with_release=False)
joychenf8f07e22013-07-12 17:45:51 -0700345
joychen121fc9b2013-08-02 14:30:30 -0700346 # Figure out release number from the version number.
joychenc3944cb2013-08-19 10:42:07 -0700347 image_url = devserver_constants.IMAGE_DIR % {
348 'board':board,
349 'suffix':RELEASE,
350 'version':'R*' + latest_version}
Simran Basi99e63c02014-05-20 10:39:52 -0700351 image_dir = XBuddy._ResolveImageDir(image_dir)
352 gs_url = os.path.join(image_dir, image_url)
joychenf8f07e22013-07-12 17:45:51 -0700353
354 # There should only be one match on cros-image-archive.
Simran Basi99e63c02014-05-20 10:39:52 -0700355 full_version = gsutil_util.GetLatestVersionFromGSDir(gs_url)
joychenf8f07e22013-07-12 17:45:51 -0700356
357 return devserver_constants.IMAGE_DIR % {'board':board,
358 'suffix':RELEASE,
359 'version':full_version}
360
361 def _LookupVersion(self, board, version):
362 """Search GS image releases for the highest match to a version prefix."""
joychen121fc9b2013-08-02 14:30:30 -0700363 # Build the pattern for GS to match.
joychen7df67f72013-07-18 14:21:12 -0700364 _Log("Checking gs for latest '%s' image with prefix '%s'", board, version)
joychenf8f07e22013-07-12 17:45:51 -0700365 image_url = devserver_constants.IMAGE_DIR % {'board':board,
366 'suffix':RELEASE,
367 'version':version + '*'}
368 image_dir = os.path.join(devserver_constants.GS_IMAGE_DIR, image_url)
369
joychen121fc9b2013-08-02 14:30:30 -0700370 # Grab the newest version of the ones matched.
joychenf8f07e22013-07-12 17:45:51 -0700371 full_version = gsutil_util.GetLatestVersionFromGSDir(image_dir)
372 return devserver_constants.IMAGE_DIR % {'board':board,
373 'suffix':RELEASE,
374 'version':full_version}
375
Chris Sosaea734d92013-10-11 11:28:58 -0700376 def _RemoteBuildId(self, board, version):
377 """Returns the remote build_id for the given board and version.
378
379 Raises:
380 XBuddyException: If we failed to resolve the version to a valid build_id.
381 """
382 build_id_as_is = devserver_constants.IMAGE_DIR % {'board':board,
383 'suffix':'',
384 'version':version}
385 build_id_release = devserver_constants.IMAGE_DIR % {'board':board,
386 'suffix':RELEASE,
387 'version':version}
388 # Return the first path that exists. We assume that what the user typed
389 # is better than with a default suffix added i.e. x86-generic/blah is
390 # more valuable than x86-generic-release/blah.
391 for build_id in build_id_as_is, build_id_release:
392 cmd = 'gsutil ls %s/%s' % (devserver_constants.GS_IMAGE_DIR, build_id)
393 try:
394 version = gsutil_util.GSUtilRun(cmd, None)
395 return build_id
396 except gsutil_util.GSUtilError:
397 continue
Gilad Arnold5f46d8e2015-02-19 12:17:55 -0800398
399 raise XBuddyException('Could not find remote build_id for %s %s' % (
400 board, version))
Chris Sosaea734d92013-10-11 11:28:58 -0700401
Gilad Arnold869e8ab2015-02-19 23:34:49 -0800402 def _ResolveBuildVersion(self, board, base_version):
403 """Check LATEST-<base_version> and returns a full build version."""
404 _Log('Checking gs for full version for %s of %s', base_version, board)
405 # TODO(garnold) We might want to accommodate version prefixes and pick the
406 # most recent found, as done in _LookupVersion().
407 latest_addr = (devserver_constants.GS_LATEST_BASE_VERSION %
408 {'image_dir': devserver_constants.GS_IMAGE_DIR,
409 'board': board,
410 'suffix': RELEASE,
411 'base_version': base_version})
412 cmd = 'gsutil cat %s' % latest_addr
413 msg = 'Failed to find build at %s' % latest_addr
414 # Full release + version is in the LATEST file.
415 return gsutil_util.GSUtilRun(cmd, msg)
416
Simran Basi99e63c02014-05-20 10:39:52 -0700417 def _ResolveVersionToBuildId(self, board, version, image_dir=None):
joychen121fc9b2013-08-02 14:30:30 -0700418 """Handle version aliases for remote payloads in GS.
joychen3cb228e2013-06-12 12:13:13 -0700419
420 Args:
421 board: as specified in the original call. (i.e. x86-generic, parrot)
422 version: as entered in the original call. can be
423 {TBD, 0. some custom alias as defined in a config file}
Gilad Arnold869e8ab2015-02-19 23:34:49 -0800424 1. fully qualified build version or base version.
425 2. latest
426 3. latest-{channel}
427 4. latest-official-{board suffix}
428 5. version prefix (i.e. RX-Y.X, RX-Y, RX)
Simran Basi99e63c02014-05-20 10:39:52 -0700429 image_dir: image directory to check in Google Storage. If none,
430 the default bucket is used.
joychen3cb228e2013-06-12 12:13:13 -0700431
432 Returns:
Chris Sosaea734d92013-10-11 11:28:58 -0700433 Location where the image dir is actually found on GS (build_id)
joychen3cb228e2013-06-12 12:13:13 -0700434
Chris Sosaea734d92013-10-11 11:28:58 -0700435 Raises:
436 XBuddyException: If we failed to resolve the version to a valid url.
joychen3cb228e2013-06-12 12:13:13 -0700437 """
joychenf8f07e22013-07-12 17:45:51 -0700438 # Only the last segment of the alias is variable relative to the rest.
439 version_tuple = version.rsplit('-', 1)
joychen3cb228e2013-06-12 12:13:13 -0700440
joychenf8f07e22013-07-12 17:45:51 -0700441 if re.match(devserver_constants.VERSION_RE, version):
Chris Sosaea734d92013-10-11 11:28:58 -0700442 return self._RemoteBuildId(board, version)
Gilad Arnold869e8ab2015-02-19 23:34:49 -0800443 elif re.match(devserver_constants.VERSION, version):
444 return self._RemoteBuildId(board,
445 self._ResolveBuildVersion(board, version))
joychenf8f07e22013-07-12 17:45:51 -0700446 elif version == LATEST_OFFICIAL:
447 # latest-official --> LATEST build in board-release
Simran Basi99e63c02014-05-20 10:39:52 -0700448 return self._LookupOfficial(board, image_dir=image_dir)
joychenf8f07e22013-07-12 17:45:51 -0700449 elif version_tuple[0] == LATEST_OFFICIAL:
450 # latest-official-{suffix} --> LATEST build in board-{suffix}
Simran Basi99e63c02014-05-20 10:39:52 -0700451 return self._LookupOfficial(board, version_tuple[1], image_dir=image_dir)
joychenf8f07e22013-07-12 17:45:51 -0700452 elif version == LATEST:
453 # latest --> latest build on stable channel
Simran Basi99e63c02014-05-20 10:39:52 -0700454 return self._LookupChannel(board, image_dir=image_dir)
joychenf8f07e22013-07-12 17:45:51 -0700455 elif version_tuple[0] == LATEST:
456 if re.match(devserver_constants.VERSION_RE, version_tuple[1]):
457 # latest-R* --> most recent qualifying build
458 return self._LookupVersion(board, version_tuple[1])
459 else:
460 # latest-{channel} --> latest build within that channel
Simran Basi99e63c02014-05-20 10:39:52 -0700461 return self._LookupChannel(board, version_tuple[1],
462 image_dir=image_dir)
joychen3cb228e2013-06-12 12:13:13 -0700463 else:
464 # The given version doesn't match any known patterns.
joychen921e1fb2013-06-28 11:12:20 -0700465 raise XBuddyException("Version %s unknown. Can't find on GS." % version)
joychen3cb228e2013-06-12 12:13:13 -0700466
joychen5260b9a2013-07-16 14:48:01 -0700467 @staticmethod
468 def _Symlink(link, target):
469 """Symlinks link to target, and removes whatever link was there before."""
470 _Log("Linking to %s from %s", link, target)
471 if os.path.lexists(link):
472 os.unlink(link)
473 os.symlink(target, link)
474
joychen121fc9b2013-08-02 14:30:30 -0700475 def _GetLatestLocalVersion(self, board):
joychen921e1fb2013-06-28 11:12:20 -0700476 """Get the version of the latest image built for board by build_image
477
478 Updates the symlink reference within the xBuddy static dir to point to
479 the real image dir in the local /build/images directory.
480
481 Args:
joychenc3944cb2013-08-19 10:42:07 -0700482 board: board that image was built for.
joychen921e1fb2013-06-28 11:12:20 -0700483
484 Returns:
joychen121fc9b2013-08-02 14:30:30 -0700485 The discovered version of the image.
joychenc3944cb2013-08-19 10:42:07 -0700486
487 Raises:
488 XBuddyException if neither test nor dev image was found in latest built
489 directory.
joychen3cb228e2013-06-12 12:13:13 -0700490 """
joychen921e1fb2013-06-28 11:12:20 -0700491 latest_local_dir = self.GetLatestImageDir(board)
joychenb0dfe552013-07-30 10:02:06 -0700492 if not latest_local_dir or not os.path.exists(latest_local_dir):
joychen921e1fb2013-06-28 11:12:20 -0700493 raise XBuddyException('No builds found for %s. Did you run build_image?' %
494 board)
495
joychen121fc9b2013-08-02 14:30:30 -0700496 # Assume that the version number is the name of the directory.
joychenc3944cb2013-08-19 10:42:07 -0700497 return os.path.basename(latest_local_dir.rstrip('/'))
joychen921e1fb2013-06-28 11:12:20 -0700498
joychenc3944cb2013-08-19 10:42:07 -0700499 @staticmethod
500 def _FindAny(local_dir):
501 """Returns the image_type for ANY given the local_dir."""
joychenc3944cb2013-08-19 10:42:07 -0700502 test_image = os.path.join(local_dir, devserver_constants.TEST_IMAGE_FILE)
Yu-Ju Hongc23c79b2014-03-17 12:40:33 -0700503 dev_image = os.path.join(local_dir, devserver_constants.IMAGE_FILE)
504 # Prioritize test images over dev images.
joychenc3944cb2013-08-19 10:42:07 -0700505 if os.path.exists(test_image):
506 return 'test'
507
Yu-Ju Hongc23c79b2014-03-17 12:40:33 -0700508 if os.path.exists(dev_image):
509 return 'dev'
510
joychenc3944cb2013-08-19 10:42:07 -0700511 raise XBuddyException('No images found in %s' % local_dir)
512
513 @staticmethod
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800514 def _InterpretPath(path, default_board=None, default_version=None):
joychen121fc9b2013-08-02 14:30:30 -0700515 """Split and return the pieces of an xBuddy path name
joychen921e1fb2013-06-28 11:12:20 -0700516
joychen121fc9b2013-08-02 14:30:30 -0700517 Args:
518 path: the path xBuddy Get was called with.
Chris Sosa0eecf962014-02-03 14:14:39 -0800519 default_board: board to use in case board isn't in path.
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800520 default_version: Version to use in case version isn't in path.
joychen3cb228e2013-06-12 12:13:13 -0700521
Yu-Ju Hongc54658c2014-01-22 09:18:07 -0800522 Returns:
Chris Sosa75490802013-09-30 17:21:45 -0700523 tuple of (image_type, board, version, whether the path is local)
joychen3cb228e2013-06-12 12:13:13 -0700524
525 Raises:
526 XBuddyException: if the path can't be resolved into valid components
527 """
joychen121fc9b2013-08-02 14:30:30 -0700528 path_list = filter(None, path.split('/'))
joychen7df67f72013-07-18 14:21:12 -0700529
Chris Sosa0eecf962014-02-03 14:14:39 -0800530 # Do the stuff that is well known first. We know that if paths have a
531 # image_type, it must be one of the GS/LOCAL aliases and it must be at the
532 # end. Similarly, local/remote are well-known and must start the path list.
533 is_local = True
534 if path_list and path_list[0] in (REMOTE, LOCAL):
535 is_local = (path_list.pop(0) == LOCAL)
joychen7df67f72013-07-18 14:21:12 -0700536
Chris Sosa0eecf962014-02-03 14:14:39 -0800537 # Default image type is determined by remote vs. local.
538 if is_local:
539 image_type = ANY
540 else:
541 image_type = TEST
joychen7df67f72013-07-18 14:21:12 -0700542
Chris Sosa0eecf962014-02-03 14:14:39 -0800543 if path_list and path_list[-1] in GS_ALIASES + LOCAL_ALIASES:
544 image_type = path_list.pop(-1)
joychen3cb228e2013-06-12 12:13:13 -0700545
Chris Sosa0eecf962014-02-03 14:14:39 -0800546 # Now for the tricky part. We don't actually know at this point if the rest
547 # of the path is just a board | version (like R33-2341.0.0) or just a board
548 # or just a version. So we do our best to do the right thing.
549 board = default_board
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800550 version = default_version or LATEST
Chris Sosa0eecf962014-02-03 14:14:39 -0800551 if len(path_list) == 1:
552 path = path_list.pop(0)
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800553 # Treat this as a version if it's one we know (contains default or
554 # latest), or we were given an actual default board.
555 if default_version in path or LATEST in path or default_board is not None:
Chris Sosa0eecf962014-02-03 14:14:39 -0800556 version = path
joychen7df67f72013-07-18 14:21:12 -0700557 else:
Chris Sosa0eecf962014-02-03 14:14:39 -0800558 board = path
joychen7df67f72013-07-18 14:21:12 -0700559
Chris Sosa0eecf962014-02-03 14:14:39 -0800560 elif len(path_list) == 2:
561 # Assumes board/version.
562 board = path_list.pop(0)
563 version = path_list.pop(0)
564
565 if path_list:
566 raise XBuddyException("Path isn't valid. Could not figure out how to "
567 "parse remaining components: %s." % path_list)
568
569 _Log("Get artifact '%s' with board %s and version %s'. Locally? %s",
joychen7df67f72013-07-18 14:21:12 -0700570 image_type, board, version, is_local)
571
572 return image_type, board, version, is_local
joychen3cb228e2013-06-12 12:13:13 -0700573
joychen921e1fb2013-06-28 11:12:20 -0700574 def _SyncRegistryWithBuildImages(self):
Gilad Arnold5f46d8e2015-02-19 12:17:55 -0800575 """Crawl images_dir for build_ids of images generated from build_image.
joychen5260b9a2013-07-16 14:48:01 -0700576
577 This will find images and symlink them in xBuddy's static dir so that
578 xBuddy's cache can serve them.
579 If xBuddy's _manage_builds option is on, then a timestamp will also be
580 generated, and xBuddy will clear them from the directory they are in, as
581 necessary.
582 """
Yu-Ju Hong235d1b52014-04-16 11:01:47 -0700583 if not os.path.isdir(self.images_dir):
584 # Skip syncing if images_dir does not exist.
585 _Log('Cannot find %s; skip syncing image registry.', self.images_dir)
586 return
587
joychen921e1fb2013-06-28 11:12:20 -0700588 build_ids = []
589 for b in os.listdir(self.images_dir):
joychen5260b9a2013-07-16 14:48:01 -0700590 # Ensure we have directories to track all boards in build/images
591 common_util.MkDirP(os.path.join(self.static_dir, b))
joychen921e1fb2013-06-28 11:12:20 -0700592 board_dir = os.path.join(self.images_dir, b)
593 build_ids.extend(['/'.join([b, v]) for v
joychenc3944cb2013-08-19 10:42:07 -0700594 in os.listdir(board_dir) if not v == LATEST])
joychen921e1fb2013-06-28 11:12:20 -0700595
joychen121fc9b2013-08-02 14:30:30 -0700596 # Symlink undiscovered images, and update timestamps if manage_builds is on.
joychen5260b9a2013-07-16 14:48:01 -0700597 for build_id in build_ids:
598 link = os.path.join(self.static_dir, build_id)
599 target = os.path.join(self.images_dir, build_id)
600 XBuddy._Symlink(link, target)
601 if self._manage_builds:
602 Timestamp.UpdateTimestamp(self._timestamp_folder, build_id)
joychen921e1fb2013-06-28 11:12:20 -0700603
604 def _ListBuildTimes(self):
Gilad Arnold5f46d8e2015-02-19 12:17:55 -0800605 """Returns the currently cached builds and their last access timestamp.
joychen3cb228e2013-06-12 12:13:13 -0700606
607 Returns:
608 list of tuples that matches xBuddy build/version to timestamps in long
609 """
joychen121fc9b2013-08-02 14:30:30 -0700610 # Update currently cached builds.
joychen3cb228e2013-06-12 12:13:13 -0700611 build_dict = {}
612
joychen7df67f72013-07-18 14:21:12 -0700613 for f in os.listdir(self._timestamp_folder):
joychen3cb228e2013-06-12 12:13:13 -0700614 last_accessed = os.path.getmtime(os.path.join(self._timestamp_folder, f))
615 build_id = Timestamp.TimestampToBuild(f)
joychenc3944cb2013-08-19 10:42:07 -0700616 stale_time = datetime.timedelta(seconds=(time.time() - last_accessed))
joychen921e1fb2013-06-28 11:12:20 -0700617 build_dict[build_id] = stale_time
joychen3cb228e2013-06-12 12:13:13 -0700618 return_tup = sorted(build_dict.iteritems(), key=operator.itemgetter(1))
619 return return_tup
620
Chris Sosa75490802013-09-30 17:21:45 -0700621 def _Download(self, gs_url, artifacts):
622 """Download the artifacts from the given gs_url.
623
624 Raises:
625 build_artifact.ArtifactDownloadError: If we failed to download the
626 artifact.
627 """
joychen3cb228e2013-06-12 12:13:13 -0700628 with XBuddy._staging_thread_count_lock:
629 XBuddy._staging_thread_count += 1
630 try:
Chris Sosa75490802013-09-30 17:21:45 -0700631 _Log("Downloading %s from %s", artifacts, gs_url)
632 downloader.Downloader(self.static_dir, gs_url).Download(artifacts, [])
joychen3cb228e2013-06-12 12:13:13 -0700633 finally:
634 with XBuddy._staging_thread_count_lock:
635 XBuddy._staging_thread_count -= 1
636
Chris Sosa75490802013-09-30 17:21:45 -0700637 def CleanCache(self):
joychen562699a2013-08-13 15:22:14 -0700638 """Delete all builds besides the newest N builds"""
joychen121fc9b2013-08-02 14:30:30 -0700639 if not self._manage_builds:
640 return
joychen921e1fb2013-06-28 11:12:20 -0700641 cached_builds = [e[0] for e in self._ListBuildTimes()]
joychen3cb228e2013-06-12 12:13:13 -0700642 _Log('In cache now: %s', cached_builds)
643
joychen562699a2013-08-13 15:22:14 -0700644 for b in range(self._Capacity(), len(cached_builds)):
joychen3cb228e2013-06-12 12:13:13 -0700645 b_path = cached_builds[b]
joychen7df67f72013-07-18 14:21:12 -0700646 _Log("Clearing '%s' from cache", b_path)
joychen3cb228e2013-06-12 12:13:13 -0700647
648 time_file = os.path.join(self._timestamp_folder,
649 Timestamp.BuildToTimestamp(b_path))
joychen921e1fb2013-06-28 11:12:20 -0700650 os.unlink(time_file)
651 clear_dir = os.path.join(self.static_dir, b_path)
joychen3cb228e2013-06-12 12:13:13 -0700652 try:
joychen121fc9b2013-08-02 14:30:30 -0700653 # Handle symlinks, in the case of links to local builds if enabled.
654 if os.path.islink(clear_dir):
joychen5260b9a2013-07-16 14:48:01 -0700655 target = os.readlink(clear_dir)
656 _Log('Deleting locally built image at %s', target)
joychen921e1fb2013-06-28 11:12:20 -0700657
658 os.unlink(clear_dir)
joychen5260b9a2013-07-16 14:48:01 -0700659 if os.path.exists(target):
joychen921e1fb2013-06-28 11:12:20 -0700660 shutil.rmtree(target)
661 elif os.path.exists(clear_dir):
joychen5260b9a2013-07-16 14:48:01 -0700662 _Log('Deleting downloaded image at %s', clear_dir)
joychen3cb228e2013-06-12 12:13:13 -0700663 shutil.rmtree(clear_dir)
joychen921e1fb2013-06-28 11:12:20 -0700664
joychen121fc9b2013-08-02 14:30:30 -0700665 except Exception as err:
666 raise XBuddyException('Failed to clear %s: %s' % (clear_dir, err))
joychen3cb228e2013-06-12 12:13:13 -0700667
Simran Basi99e63c02014-05-20 10:39:52 -0700668 def _GetFromGS(self, build_id, image_type, image_dir=None):
Chris Sosa75490802013-09-30 17:21:45 -0700669 """Check if the artifact is available locally. Download from GS if not.
670
Simran Basi99e63c02014-05-20 10:39:52 -0700671 Args:
672 build_id: Path to the image or update directory on the devserver or
673 in Google Storage. e.g. 'x86-generic/R26-4000.0.0'
674 image_type: Image type to download. Look at aliases at top of file for
675 options.
676 image_dir: Google Storage image archive to search in if requesting a
677 remote artifact. If none uses the default bucket.
678
Chris Sosa75490802013-09-30 17:21:45 -0700679 Raises:
680 build_artifact.ArtifactDownloadError: If we failed to download the
681 artifact.
682 """
Simran Basi99e63c02014-05-20 10:39:52 -0700683 image_dir = XBuddy._ResolveImageDir(image_dir)
684 gs_url = os.path.join(image_dir, build_id)
joychen921e1fb2013-06-28 11:12:20 -0700685
joychen121fc9b2013-08-02 14:30:30 -0700686 # Stage image if not found in cache.
joychen921e1fb2013-06-28 11:12:20 -0700687 file_name = GS_ALIAS_TO_FILENAME[image_type]
joychen346531c2013-07-24 16:55:56 -0700688 file_loc = os.path.join(self.static_dir, build_id, file_name)
689 cached = os.path.exists(file_loc)
690
joychen921e1fb2013-06-28 11:12:20 -0700691 if not cached:
Chris Sosa75490802013-09-30 17:21:45 -0700692 artifact = GS_ALIAS_TO_ARTIFACT[image_type]
693 self._Download(gs_url, [artifact])
joychen921e1fb2013-06-28 11:12:20 -0700694 else:
695 _Log('Image already cached.')
696
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800697 def _GetArtifact(self, path_list, board=None, version=None,
698 lookup_only=False, image_dir=None):
joychen346531c2013-07-24 16:55:56 -0700699 """Interpret an xBuddy path and return directory/file_name to resource.
700
Chris Sosa75490802013-09-30 17:21:45 -0700701 Note board can be passed that in but by default if self._board is set,
702 that is used rather than board.
703
Simran Basi99e63c02014-05-20 10:39:52 -0700704 Args:
705 path_list: [board, version, alias] as split from the xbuddy call url.
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800706 board: Board whos artifacts we are looking for. Only used if no board was
707 given during XBuddy initialization.
708 version: Version whose artifacts we are looking for. Used if no version
709 was given during XBuddy initialization. If None, defers to LATEST.
Simran Basi99e63c02014-05-20 10:39:52 -0700710 lookup_only: If true just look up the artifact, if False stage it on
711 the devserver as well.
712 image_dir: Google Storage image archive to search in if requesting a
713 remote artifact. If none uses the default bucket.
714
joychen346531c2013-07-24 16:55:56 -0700715 Returns:
Simran Basi99e63c02014-05-20 10:39:52 -0700716 build_id: Path to the image or update directory on the devserver or
717 in Google Storage. e.g. 'x86-generic/R26-4000.0.0'
718 file_name: of the artifact in the build_id directory.
joychen346531c2013-07-24 16:55:56 -0700719
720 Raises:
joychen121fc9b2013-08-02 14:30:30 -0700721 XBuddyException: if the path could not be translated
Chris Sosa75490802013-09-30 17:21:45 -0700722 build_artifact.ArtifactDownloadError: if we failed to download the
723 artifact.
joychen346531c2013-07-24 16:55:56 -0700724 """
joychen121fc9b2013-08-02 14:30:30 -0700725 path = '/'.join(path_list)
Chris Sosa0eecf962014-02-03 14:14:39 -0800726 default_board = self._board if self._board else board
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800727 default_version = self._version or version or LATEST
joychenb0dfe552013-07-30 10:02:06 -0700728 # Rewrite the path if there is an appropriate default.
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800729 path = self._LookupAlias(path, default_board, default_version)
joychen121fc9b2013-08-02 14:30:30 -0700730 # Parse the path.
Chris Sosa0eecf962014-02-03 14:14:39 -0800731 image_type, board, version, is_local = self._InterpretPath(
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800732 path, default_board, default_version)
joychen7df67f72013-07-18 14:21:12 -0700733 if is_local:
joychen121fc9b2013-08-02 14:30:30 -0700734 # Get a local image.
joychen7df67f72013-07-18 14:21:12 -0700735 if version == LATEST:
joychen121fc9b2013-08-02 14:30:30 -0700736 # Get the latest local image for the given board.
737 version = self._GetLatestLocalVersion(board)
joychen7df67f72013-07-18 14:21:12 -0700738
joychenc3944cb2013-08-19 10:42:07 -0700739 build_id = os.path.join(board, version)
740 artifact_dir = os.path.join(self.static_dir, build_id)
741 if image_type == ANY:
742 image_type = self._FindAny(artifact_dir)
joychen121fc9b2013-08-02 14:30:30 -0700743
joychenc3944cb2013-08-19 10:42:07 -0700744 file_name = LOCAL_ALIAS_TO_FILENAME[image_type]
745 artifact_path = os.path.join(artifact_dir, file_name)
746 if not os.path.exists(artifact_path):
747 raise XBuddyException('Local %s artifact not in static_dir at %s' %
748 (image_type, artifact_path))
joychen121fc9b2013-08-02 14:30:30 -0700749
joychen921e1fb2013-06-28 11:12:20 -0700750 else:
joychen121fc9b2013-08-02 14:30:30 -0700751 # Get a remote image.
joychen921e1fb2013-06-28 11:12:20 -0700752 if image_type not in GS_ALIASES:
joychen7df67f72013-07-18 14:21:12 -0700753 raise XBuddyException('Bad remote image type: %s. Use one of: %s' %
joychen921e1fb2013-06-28 11:12:20 -0700754 (image_type, GS_ALIASES))
Simran Basi99e63c02014-05-20 10:39:52 -0700755 build_id = self._ResolveVersionToBuildId(board, version,
756 image_dir=image_dir)
Chris Sosa75490802013-09-30 17:21:45 -0700757 _Log('Resolved version %s to %s.', version, build_id)
758 file_name = GS_ALIAS_TO_FILENAME[image_type]
759 if not lookup_only:
Simran Basi99e63c02014-05-20 10:39:52 -0700760 self._GetFromGS(build_id, image_type, image_dir=image_dir)
joychenf8f07e22013-07-12 17:45:51 -0700761
joychenc3944cb2013-08-19 10:42:07 -0700762 return build_id, file_name
joychen3cb228e2013-06-12 12:13:13 -0700763
764 ############################ BEGIN PUBLIC METHODS
765
766 def List(self):
767 """Lists the currently available images & time since last access."""
joychen921e1fb2013-06-28 11:12:20 -0700768 self._SyncRegistryWithBuildImages()
769 builds = self._ListBuildTimes()
770 return_string = ''
771 for build, timestamp in builds:
772 return_string += '<b>' + build + '</b> '
773 return_string += '(time since last access: ' + str(timestamp) + ')<br>'
774 return return_string
joychen3cb228e2013-06-12 12:13:13 -0700775
776 def Capacity(self):
777 """Returns the number of images cached by xBuddy."""
joychen562699a2013-08-13 15:22:14 -0700778 return str(self._Capacity())
joychen3cb228e2013-06-12 12:13:13 -0700779
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800780 def Translate(self, path_list, board=None, version=None, image_dir=None):
joychen346531c2013-07-24 16:55:56 -0700781 """Translates an xBuddy path to a real path to artifact if it exists.
782
joychen121fc9b2013-08-02 14:30:30 -0700783 Equivalent to the Get call, minus downloading and updating timestamps,
joychen346531c2013-07-24 16:55:56 -0700784
Simran Basi99e63c02014-05-20 10:39:52 -0700785 Args:
786 path_list: [board, version, alias] as split from the xbuddy call url.
787 board: Board whos artifacts we are looking for. If None, use the board
788 XBuddy was initialized to use.
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800789 version: Version whose artifacts we are looking for. If None, use the
790 version XBuddy was initialized with, or LATEST.
Simran Basi99e63c02014-05-20 10:39:52 -0700791 image_dir: image directory to check in Google Storage. If none,
792 the default bucket is used.
793
joychen7c2054a2013-07-25 11:14:07 -0700794 Returns:
joychen121fc9b2013-08-02 14:30:30 -0700795 build_id: Path to the image or update directory on the devserver.
796 e.g. 'x86-generic/R26-4000.0.0'
797 The returned path is always the path to the directory within
798 static_dir, so it is always the build_id of the image.
799 file_name: The file name of the artifact. Can take any of the file
800 values in devserver_constants.
801 e.g. 'chromiumos_test_image.bin' or 'update.gz' if the path list
802 specified 'test' or 'full_payload' artifacts, respectively.
joychen7c2054a2013-07-25 11:14:07 -0700803
joychen121fc9b2013-08-02 14:30:30 -0700804 Raises:
805 XBuddyException: if the path couldn't be translated
joychen346531c2013-07-24 16:55:56 -0700806 """
807 self._SyncRegistryWithBuildImages()
Chris Sosa75490802013-09-30 17:21:45 -0700808 build_id, file_name = self._GetArtifact(path_list, board=board,
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800809 version=version,
Simran Basi99e63c02014-05-20 10:39:52 -0700810 lookup_only=True,
811 image_dir=image_dir)
joychen346531c2013-07-24 16:55:56 -0700812
joychen121fc9b2013-08-02 14:30:30 -0700813 _Log('Returning path to payload: %s/%s', build_id, file_name)
814 return build_id, file_name
joychen346531c2013-07-24 16:55:56 -0700815
Yu-Ju Hong1bdb7a92014-04-10 16:02:11 -0700816 def StageTestArtifactsForUpdate(self, path_list):
Chris Sosa75490802013-09-30 17:21:45 -0700817 """Stages test artifacts for update and returns build_id.
818
819 Raises:
820 XBuddyException: if the path could not be translated
821 build_artifact.ArtifactDownloadError: if we failed to download the test
822 artifacts.
823 """
824 build_id, file_name = self.Translate(path_list)
825 if file_name == devserver_constants.TEST_IMAGE_FILE:
826 gs_url = os.path.join(devserver_constants.GS_IMAGE_DIR,
827 build_id)
828 artifacts = [FULL, STATEFUL]
829 self._Download(gs_url, artifacts)
830 return build_id
831
Simran Basi99e63c02014-05-20 10:39:52 -0700832 def Get(self, path_list, image_dir=None):
joychen921e1fb2013-06-28 11:12:20 -0700833 """The full xBuddy call, returns resource specified by path_list.
joychen3cb228e2013-06-12 12:13:13 -0700834
835 Please see devserver.py:xbuddy for full documentation.
joychen121fc9b2013-08-02 14:30:30 -0700836
joychen3cb228e2013-06-12 12:13:13 -0700837 Args:
Simran Basi99e63c02014-05-20 10:39:52 -0700838 path_list: [board, version, alias] as split from the xbuddy call url.
839 image_dir: image directory to check in Google Storage. If none,
840 the default bucket is used.
joychen3cb228e2013-06-12 12:13:13 -0700841
842 Returns:
joychen121fc9b2013-08-02 14:30:30 -0700843 build_id: Path to the image or update directory on the devserver.
Simran Basi99e63c02014-05-20 10:39:52 -0700844 e.g. 'x86-generic/R26-4000.0.0'
845 The returned path is always the path to the directory within
846 static_dir, so it is always the build_id of the image.
joychen121fc9b2013-08-02 14:30:30 -0700847 file_name: The file name of the artifact. Can take any of the file
Simran Basi99e63c02014-05-20 10:39:52 -0700848 values in devserver_constants.
849 e.g. 'chromiumos_test_image.bin' or 'update.gz' if the path list
850 specified 'test' or 'full_payload' artifacts, respectively.
joychen3cb228e2013-06-12 12:13:13 -0700851
852 Raises:
Chris Sosa75490802013-09-30 17:21:45 -0700853 XBuddyException: if the path could not be translated
854 build_artifact.ArtifactDownloadError: if we failed to download the
855 artifact.
joychen3cb228e2013-06-12 12:13:13 -0700856 """
joychen7df67f72013-07-18 14:21:12 -0700857 self._SyncRegistryWithBuildImages()
Simran Basi99e63c02014-05-20 10:39:52 -0700858 build_id, file_name = self._GetArtifact(path_list, image_dir=image_dir)
joychen921e1fb2013-06-28 11:12:20 -0700859 Timestamp.UpdateTimestamp(self._timestamp_folder, build_id)
joychen3cb228e2013-06-12 12:13:13 -0700860 #TODO (joyc): run in sep thread
Chris Sosa75490802013-09-30 17:21:45 -0700861 self.CleanCache()
joychen3cb228e2013-06-12 12:13:13 -0700862
joychen121fc9b2013-08-02 14:30:30 -0700863 _Log('Returning path to payload: %s/%s', build_id, file_name)
864 return build_id, file_name