blob: dc5e6d4a3f571d70494fb20a88080d71b49c1661 [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
5import datetime
6import operator
7import os
joychenf8f07e22013-07-12 17:45:51 -07008import re
joychen3cb228e2013-06-12 12:13:13 -07009import shutil
joychenf8f07e22013-07-12 17:45:51 -070010import time
joychen3cb228e2013-06-12 12:13:13 -070011import threading
12
joychen921e1fb2013-06-28 11:12:20 -070013import build_util
joychen3cb228e2013-06-12 12:13:13 -070014import artifact_info
15import build_artifact
16import common_util
17import devserver_constants
18import downloader
joychenf8f07e22013-07-12 17:45:51 -070019import gsutil_util
joychen3cb228e2013-06-12 12:13:13 -070020import log_util
21
22# Module-local log function.
23def _Log(message, *args):
24 return log_util.LogWithTag('XBUDDY', message, *args)
25
joychen3cb228e2013-06-12 12:13:13 -070026_XBUDDY_CAPACITY = 5
joychen921e1fb2013-06-28 11:12:20 -070027
28# Local build constants
29LATEST_LOCAL = "latest-local"
30LOCAL_ALIASES = [
31 'test',
32 'base',
33 'dev',
34]
35
36LOCAL_FILE_NAMES = [
37 devserver_constants.TEST_IMAGE_FILE,
joychenf8f07e22013-07-12 17:45:51 -070038
joychen921e1fb2013-06-28 11:12:20 -070039 devserver_constants.BASE_IMAGE_FILE,
40 devserver_constants.IMAGE_FILE,
41]
42
43LOCAL_ALIAS_TO_FILENAME = dict(zip(LOCAL_ALIASES, LOCAL_FILE_NAMES))
44
45# Google Storage constants
46GS_ALIASES = [
joychen3cb228e2013-06-12 12:13:13 -070047 'test',
48 'base',
49 'recovery',
50 'full_payload',
51 'stateful',
52 'autotest',
53]
54
55# TODO(joyc) these should become devserver constants.
56# currently, storage locations are embedded in the artifact classes defined in
57# build_artifact
58
joychen921e1fb2013-06-28 11:12:20 -070059GS_FILE_NAMES = [
60 devserver_constants.TEST_IMAGE_FILE,
61 devserver_constants.BASE_IMAGE_FILE,
62 devserver_constants.RECOVERY_IMAGE_FILE,
joychen3cb228e2013-06-12 12:13:13 -070063 devserver_constants.ROOT_UPDATE_FILE,
64 build_artifact.STATEFUL_UPDATE_FILE,
65 devserver_constants.AUTOTEST_DIR,
66]
67
68ARTIFACTS = [
69 artifact_info.TEST_IMAGE,
70 artifact_info.BASE_IMAGE,
71 artifact_info.RECOVERY_IMAGE,
72 artifact_info.FULL_PAYLOAD,
73 artifact_info.STATEFUL_PAYLOAD,
74 artifact_info.AUTOTEST,
75]
76
joychen921e1fb2013-06-28 11:12:20 -070077GS_ALIAS_TO_FILENAME = dict(zip(GS_ALIASES, GS_FILE_NAMES))
78GS_ALIAS_TO_ARTIFACT = dict(zip(GS_ALIASES, ARTIFACTS))
joychen3cb228e2013-06-12 12:13:13 -070079
joychen921e1fb2013-06-28 11:12:20 -070080LATEST_OFFICIAL = "latest-official"
joychen3cb228e2013-06-12 12:13:13 -070081
82LATEST = "latest"
joychenf8f07e22013-07-12 17:45:51 -070083RELEASE = "release"
joychen3cb228e2013-06-12 12:13:13 -070084
joychen3cb228e2013-06-12 12:13:13 -070085
86class XBuddyException(Exception):
87 """Exception classes used by this module."""
88 pass
89
90
91# no __init__ method
92#pylint: disable=W0232
93class Timestamp():
94 """Class to translate build path strings and timestamp filenames."""
95
96 _TIMESTAMP_DELIMITER = 'SLASH'
97 XBUDDY_TIMESTAMP_DIR = 'xbuddy_UpdateTimestamps'
98
99 @staticmethod
100 def TimestampToBuild(timestamp_filename):
101 return timestamp_filename.replace(Timestamp._TIMESTAMP_DELIMITER, '/')
102
103 @staticmethod
104 def BuildToTimestamp(build_path):
105 return build_path.replace('/', Timestamp._TIMESTAMP_DELIMITER)
joychen921e1fb2013-06-28 11:12:20 -0700106
107 @staticmethod
108 def UpdateTimestamp(timestamp_dir, build_id):
109 """Update timestamp file of build with build_id."""
110 common_util.MkDirP(timestamp_dir)
111 time_file = os.path.join(timestamp_dir,
112 Timestamp.BuildToTimestamp(build_id))
113 with file(time_file, 'a'):
114 os.utime(time_file, None)
joychen3cb228e2013-06-12 12:13:13 -0700115#pylint: enable=W0232
116
117
joychen921e1fb2013-06-28 11:12:20 -0700118class XBuddy(build_util.BuildObject):
joychen3cb228e2013-06-12 12:13:13 -0700119 """Class that manages image retrieval and caching by the devserver.
120
121 Image retrieval by xBuddy path:
122 XBuddy accesses images and artifacts that it stores using an xBuddy
123 path of the form: board/version/alias
124 The primary xbuddy.Get call retrieves the correct artifact or url to where
125 the artifacts can be found.
126
127 Image caching:
128 Images and other artifacts are stored identically to how they would have
129 been if devserver's stage rpc was called and the xBuddy cache replaces
130 build versions on a LRU basis. Timestamps are maintained by last accessed
131 times of representative files in the a directory in the static serve
132 directory (XBUDDY_TIMESTAMP_DIR).
133
134 Private class members:
135 _true_values - used for interpreting boolean values
136 _staging_thread_count - track download requests
joychen921e1fb2013-06-28 11:12:20 -0700137 _timestamp_folder - directory with empty files standing in as timestamps
138 for each image currently cached by xBuddy
joychen3cb228e2013-06-12 12:13:13 -0700139 """
140 _true_values = ['true', 't', 'yes', 'y']
141
142 # Number of threads that are staging images.
143 _staging_thread_count = 0
144 # Lock used to lock increasing/decreasing count.
145 _staging_thread_count_lock = threading.Lock()
146
joychen5260b9a2013-07-16 14:48:01 -0700147 def __init__(self, manage_builds=False, **kwargs):
joychen921e1fb2013-06-28 11:12:20 -0700148 super(XBuddy, self).__init__(**kwargs)
joychen5260b9a2013-07-16 14:48:01 -0700149 self._manage_builds = manage_builds
joychen921e1fb2013-06-28 11:12:20 -0700150 self._timestamp_folder = os.path.join(self.static_dir,
joychen3cb228e2013-06-12 12:13:13 -0700151 Timestamp.XBUDDY_TIMESTAMP_DIR)
152
153 @classmethod
154 def ParseBoolean(cls, boolean_string):
155 """Evaluate a string to a boolean value"""
156 if boolean_string:
157 return boolean_string.lower() in cls._true_values
158 else:
159 return False
160
joychenf8f07e22013-07-12 17:45:51 -0700161 def _LookupOfficial(self, board, suffix=RELEASE):
162 """Check LATEST-master for the version number of interest."""
163 _Log("Checking gs for latest %s-%s image", board, suffix)
164 latest_addr = devserver_constants.GS_LATEST_MASTER % {'board':board,
165 'suffix':suffix}
166 cmd = 'gsutil cat %s' % latest_addr
167 msg = 'Failed to find build at %s' % latest_addr
168 # Full release + version is in the LATEST file
169 version = gsutil_util.GSUtilRun(cmd, msg)
joychen3cb228e2013-06-12 12:13:13 -0700170
joychenf8f07e22013-07-12 17:45:51 -0700171 return devserver_constants.IMAGE_DIR % {'board':board,
172 'suffix':suffix,
173 'version':version}
174
175 def _LookupChannel(self, board, channel='stable'):
176 """Check the channel folder for the version number of interest."""
177 # Get all names in channel dir. Get 10 highest directories by version
178 _Log("Checking channel %s for latest %s image", channel, board)
179 channel_dir = devserver_constants.GS_CHANNEL_DIR % {'channel':channel,
180 'board':board}
181 latest_version = gsutil_util.GetLatestVersionFromGSDir(channel_dir)
182
183 # Figure out release number from the version number
184 image_url = devserver_constants.IMAGE_DIR % {'board':board,
185 'suffix':RELEASE,
186 'version':'R*'+latest_version}
187 image_dir = os.path.join(devserver_constants.GS_IMAGE_DIR, image_url)
188
189 # There should only be one match on cros-image-archive.
190 full_version = gsutil_util.GetLatestVersionFromGSDir(image_dir)
191
192 return devserver_constants.IMAGE_DIR % {'board':board,
193 'suffix':RELEASE,
194 'version':full_version}
195
196 def _LookupVersion(self, board, version):
197 """Search GS image releases for the highest match to a version prefix."""
198 # Build the pattern for GS to match
199 _Log("Checking gs for latest %s image with prefix %s", board, version)
200 image_url = devserver_constants.IMAGE_DIR % {'board':board,
201 'suffix':RELEASE,
202 'version':version + '*'}
203 image_dir = os.path.join(devserver_constants.GS_IMAGE_DIR, image_url)
204
205 # grab the newest version of the ones matched
206 full_version = gsutil_util.GetLatestVersionFromGSDir(image_dir)
207 return devserver_constants.IMAGE_DIR % {'board':board,
208 'suffix':RELEASE,
209 'version':full_version}
210
211 def _ResolveVersionToUrl(self, board, version):
joychen3cb228e2013-06-12 12:13:13 -0700212 """
joychen921e1fb2013-06-28 11:12:20 -0700213 Handle version aliases for remote payloads in GS.
joychen3cb228e2013-06-12 12:13:13 -0700214
215 Args:
216 board: as specified in the original call. (i.e. x86-generic, parrot)
217 version: as entered in the original call. can be
218 {TBD, 0. some custom alias as defined in a config file}
219 1. latest
220 2. latest-{channel}
221 3. latest-official-{board suffix}
222 4. version prefix (i.e. RX-Y.X, RX-Y, RX)
joychen3cb228e2013-06-12 12:13:13 -0700223
224 Returns:
joychenf8f07e22013-07-12 17:45:51 -0700225 image_url is where the image dir is actually found on GS
joychen3cb228e2013-06-12 12:13:13 -0700226
227 """
joychenf8f07e22013-07-12 17:45:51 -0700228 # TODO (joychen) Convert separate calls to a dict + error out bad paths
joychen3cb228e2013-06-12 12:13:13 -0700229
joychenf8f07e22013-07-12 17:45:51 -0700230 # Only the last segment of the alias is variable relative to the rest.
231 version_tuple = version.rsplit('-', 1)
joychen3cb228e2013-06-12 12:13:13 -0700232
joychenf8f07e22013-07-12 17:45:51 -0700233 if re.match(devserver_constants.VERSION_RE, version):
234 # This is supposed to be a complete version number on GS. Return it.
235 return devserver_constants.IMAGE_DIR % {'board':board,
236 'suffix':RELEASE,
237 'version':version}
238 elif version == LATEST_OFFICIAL:
239 # latest-official --> LATEST build in board-release
240 return self._LookupOfficial(board)
241 elif version_tuple[0] == LATEST_OFFICIAL:
242 # latest-official-{suffix} --> LATEST build in board-{suffix}
243 return self._LookupOfficial(board, version_tuple[1])
244 elif version == LATEST:
245 # latest --> latest build on stable channel
246 return self._LookupChannel(board)
247 elif version_tuple[0] == LATEST:
248 if re.match(devserver_constants.VERSION_RE, version_tuple[1]):
249 # latest-R* --> most recent qualifying build
250 return self._LookupVersion(board, version_tuple[1])
251 else:
252 # latest-{channel} --> latest build within that channel
253 return self._LookupChannel(board, version_tuple[1])
joychen3cb228e2013-06-12 12:13:13 -0700254 else:
255 # The given version doesn't match any known patterns.
joychen921e1fb2013-06-28 11:12:20 -0700256 raise XBuddyException("Version %s unknown. Can't find on GS." % version)
joychen3cb228e2013-06-12 12:13:13 -0700257
joychen5260b9a2013-07-16 14:48:01 -0700258 @staticmethod
259 def _Symlink(link, target):
260 """Symlinks link to target, and removes whatever link was there before."""
261 _Log("Linking to %s from %s", link, target)
262 if os.path.lexists(link):
263 os.unlink(link)
264 os.symlink(target, link)
265
joychen921e1fb2013-06-28 11:12:20 -0700266 def _GetLatestLocalVersion(self, board, file_name):
267 """Get the version of the latest image built for board by build_image
268
269 Updates the symlink reference within the xBuddy static dir to point to
270 the real image dir in the local /build/images directory.
271
272 Args:
273 board - board-suffix
274 file_name - the filename of the image we have cached
275
276 Returns:
277 version - the discovered version of the image.
joychen3cb228e2013-06-12 12:13:13 -0700278 """
joychen921e1fb2013-06-28 11:12:20 -0700279 latest_local_dir = self.GetLatestImageDir(board)
280 if not (latest_local_dir and os.path.exists(latest_local_dir)):
281 raise XBuddyException('No builds found for %s. Did you run build_image?' %
282 board)
283
284 # assume that the version number is the name of the directory
285 version = os.path.basename(latest_local_dir)
286
287 path_to_image = os.path.join(latest_local_dir, file_name)
288 if not os.path.exists(path_to_image):
289 raise XBuddyException('%s not found in %s. Did you run build_image?' %
290 (file_name, latest_local_dir))
291
292 # symlink the directories
293 common_util.MkDirP(os.path.join(self.static_dir, board))
294 link = os.path.join(self.static_dir, board, version)
joychen5260b9a2013-07-16 14:48:01 -0700295 XBuddy._Symlink(link, latest_local_dir)
joychen921e1fb2013-06-28 11:12:20 -0700296
297 return version
298
299 def _InterpretPath(self, path_list):
300 """
301 Split and return the pieces of an xBuddy path name
joychen3cb228e2013-06-12 12:13:13 -0700302
303 input:
joychen921e1fb2013-06-28 11:12:20 -0700304 path_list: the segments of the path xBuddy Get was called with.
305 Documentation of path_list can be found in devserver.py:xbuddy
joychen3cb228e2013-06-12 12:13:13 -0700306
307 Return:
joychenf8f07e22013-07-12 17:45:51 -0700308 tuple of (image_type, board, version)
joychen3cb228e2013-06-12 12:13:13 -0700309
310 Raises:
311 XBuddyException: if the path can't be resolved into valid components
312 """
joychen921e1fb2013-06-28 11:12:20 -0700313 if len(path_list) == 3:
joychen3cb228e2013-06-12 12:13:13 -0700314 # We have a full path, with b/v/a
joychen921e1fb2013-06-28 11:12:20 -0700315 board, version, image_type = path_list
316 elif len(path_list) == 2:
joychen3cb228e2013-06-12 12:13:13 -0700317 # We have only the board and the version, default to test image
joychen921e1fb2013-06-28 11:12:20 -0700318 board, version = path_list
319 image_type = GS_ALIASES[0]
320 elif len(path_list) == 1:
joychen3cb228e2013-06-12 12:13:13 -0700321 # We have only the board. default to latest test image.
joychen921e1fb2013-06-28 11:12:20 -0700322 board = path_list[0]
joychen3cb228e2013-06-12 12:13:13 -0700323 version = LATEST
joychen921e1fb2013-06-28 11:12:20 -0700324 image_type = GS_ALIASES[0]
joychen3cb228e2013-06-12 12:13:13 -0700325 else:
326 # Misshapen beyond recognition
joychen921e1fb2013-06-28 11:12:20 -0700327 raise XBuddyException('Invalid path, %s.' % '/'.join(path_list))
joychen3cb228e2013-06-12 12:13:13 -0700328
joychenf8f07e22013-07-12 17:45:51 -0700329 return image_type, board, version
joychen3cb228e2013-06-12 12:13:13 -0700330
joychen921e1fb2013-06-28 11:12:20 -0700331 def _SyncRegistryWithBuildImages(self):
joychen5260b9a2013-07-16 14:48:01 -0700332 """ Crawl images_dir for build_ids of images generated from build_image.
333
334 This will find images and symlink them in xBuddy's static dir so that
335 xBuddy's cache can serve them.
336 If xBuddy's _manage_builds option is on, then a timestamp will also be
337 generated, and xBuddy will clear them from the directory they are in, as
338 necessary.
339 """
joychen921e1fb2013-06-28 11:12:20 -0700340 build_ids = []
341 for b in os.listdir(self.images_dir):
joychen5260b9a2013-07-16 14:48:01 -0700342 # Ensure we have directories to track all boards in build/images
343 common_util.MkDirP(os.path.join(self.static_dir, b))
joychen921e1fb2013-06-28 11:12:20 -0700344 board_dir = os.path.join(self.images_dir, b)
345 build_ids.extend(['/'.join([b, v]) for v
346 in os.listdir(board_dir) if not v==LATEST])
347
348 # Check currently registered images
349 for f in os.listdir(self._timestamp_folder):
350 build_id = Timestamp.TimestampToBuild(f)
351 if build_id in build_ids:
352 build_ids.remove(build_id)
353
joychen5260b9a2013-07-16 14:48:01 -0700354 # Symlink undiscovered images, and update timestamps if manage_builds is on
355 for build_id in build_ids:
356 link = os.path.join(self.static_dir, build_id)
357 target = os.path.join(self.images_dir, build_id)
358 XBuddy._Symlink(link, target)
359 if self._manage_builds:
360 Timestamp.UpdateTimestamp(self._timestamp_folder, build_id)
joychen921e1fb2013-06-28 11:12:20 -0700361
362 def _ListBuildTimes(self):
joychen3cb228e2013-06-12 12:13:13 -0700363 """ Returns the currently cached builds and their last access timestamp.
364
365 Returns:
366 list of tuples that matches xBuddy build/version to timestamps in long
367 """
368 # update currently cached builds
369 build_dict = {}
370
joychen921e1fb2013-06-28 11:12:20 -0700371 common_util.MkDirP(self._timestamp_folder)
joychen3cb228e2013-06-12 12:13:13 -0700372 filenames = os.listdir(self._timestamp_folder)
373 for f in filenames:
374 last_accessed = os.path.getmtime(os.path.join(self._timestamp_folder, f))
375 build_id = Timestamp.TimestampToBuild(f)
376 stale_time = datetime.timedelta(seconds = (time.time()-last_accessed))
joychen921e1fb2013-06-28 11:12:20 -0700377 build_dict[build_id] = stale_time
joychen3cb228e2013-06-12 12:13:13 -0700378 return_tup = sorted(build_dict.iteritems(), key=operator.itemgetter(1))
379 return return_tup
380
joychen3cb228e2013-06-12 12:13:13 -0700381 def _Download(self, gs_url, artifact):
382 """Download the single artifact from the given gs_url."""
383 with XBuddy._staging_thread_count_lock:
384 XBuddy._staging_thread_count += 1
385 try:
joychenf8f07e22013-07-12 17:45:51 -0700386 _Log('Downloading %s from %s', artifact, gs_url)
joychen921e1fb2013-06-28 11:12:20 -0700387 downloader.Downloader(self.static_dir, gs_url).Download(
joychen3cb228e2013-06-12 12:13:13 -0700388 [artifact])
389 finally:
390 with XBuddy._staging_thread_count_lock:
391 XBuddy._staging_thread_count -= 1
392
393 def _CleanCache(self):
394 """Delete all builds besides the first _XBUDDY_CAPACITY builds"""
joychen921e1fb2013-06-28 11:12:20 -0700395 self._SyncRegistryWithBuildImages()
396 cached_builds = [e[0] for e in self._ListBuildTimes()]
joychen3cb228e2013-06-12 12:13:13 -0700397 _Log('In cache now: %s', cached_builds)
398
399 for b in range(_XBUDDY_CAPACITY, len(cached_builds)):
400 b_path = cached_builds[b]
401 _Log('Clearing %s from cache', b_path)
402
403 time_file = os.path.join(self._timestamp_folder,
404 Timestamp.BuildToTimestamp(b_path))
joychen921e1fb2013-06-28 11:12:20 -0700405 os.unlink(time_file)
406 clear_dir = os.path.join(self.static_dir, b_path)
joychen3cb228e2013-06-12 12:13:13 -0700407 try:
joychen5260b9a2013-07-16 14:48:01 -0700408 # handle symlinks, in the case of links to local builds if enabled
409 if self._manage_builds and os.path.islink(clear_dir):
410 target = os.readlink(clear_dir)
411 _Log('Deleting locally built image at %s', target)
joychen921e1fb2013-06-28 11:12:20 -0700412
413 os.unlink(clear_dir)
joychen5260b9a2013-07-16 14:48:01 -0700414 if os.path.exists(target):
joychen921e1fb2013-06-28 11:12:20 -0700415 shutil.rmtree(target)
416 elif os.path.exists(clear_dir):
joychen5260b9a2013-07-16 14:48:01 -0700417 _Log('Deleting downloaded image at %s', clear_dir)
joychen3cb228e2013-06-12 12:13:13 -0700418 shutil.rmtree(clear_dir)
joychen921e1fb2013-06-28 11:12:20 -0700419
joychen3cb228e2013-06-12 12:13:13 -0700420 except Exception:
421 raise XBuddyException('Failed to clear build in %s.' % clear_dir)
422
joychen921e1fb2013-06-28 11:12:20 -0700423 def _GetFromGS(self, build_id, image_type):
424 """Check if the artifact is available locally. Download from GS if not."""
joychenf8f07e22013-07-12 17:45:51 -0700425 gs_url = os.path.join(devserver_constants.GS_IMAGE_DIR,
joychen921e1fb2013-06-28 11:12:20 -0700426 build_id)
427
428 # stage image if not found in cache
429 file_name = GS_ALIAS_TO_FILENAME[image_type]
430 cached = os.path.exists(os.path.join(self.static_dir,
431 build_id,
432 file_name))
433 if not cached:
434 artifact = GS_ALIAS_TO_ARTIFACT[image_type]
joychen921e1fb2013-06-28 11:12:20 -0700435 self._Download(gs_url, artifact)
436 else:
437 _Log('Image already cached.')
438
439 def _GetArtifact(self, path):
440 """Interpret an xBuddy path and return directory/file_name to resource."""
joychenf8f07e22013-07-12 17:45:51 -0700441 image_type, board, version = self._InterpretPath(path)
joychen921e1fb2013-06-28 11:12:20 -0700442
443 if version in [LATEST_LOCAL, '']:
444 # Get a local image
445 if image_type not in LOCAL_ALIASES:
446 raise XBuddyException('Bad image type: %s. Use one of: %s' %
447 (image_type, LOCAL_ALIASES))
448
449 file_name = LOCAL_ALIAS_TO_FILENAME[image_type]
450 version = self._GetLatestLocalVersion(board, file_name)
joychenf8f07e22013-07-12 17:45:51 -0700451 image_url = os.path.join(board, version)
joychen921e1fb2013-06-28 11:12:20 -0700452 else:
453 # Get a remote image
454 if image_type not in GS_ALIASES:
455 raise XBuddyException('Bad image type: %s. Use one of: %s' %
456 (image_type, GS_ALIASES))
joychen921e1fb2013-06-28 11:12:20 -0700457 file_name = GS_ALIAS_TO_FILENAME[image_type]
joychen921e1fb2013-06-28 11:12:20 -0700458
joychenf8f07e22013-07-12 17:45:51 -0700459 # Interpret the version (alias), and get gs address
460 image_url = self._ResolveVersionToUrl(board, version)
joychen921e1fb2013-06-28 11:12:20 -0700461
joychenf8f07e22013-07-12 17:45:51 -0700462 self._GetFromGS(image_url, image_type)
463
464 _Log("Get artifact %s from image %s", image_type, image_url)
465
466 return image_url, file_name
joychen3cb228e2013-06-12 12:13:13 -0700467
468 ############################ BEGIN PUBLIC METHODS
469
470 def List(self):
471 """Lists the currently available images & time since last access."""
joychen921e1fb2013-06-28 11:12:20 -0700472 self._SyncRegistryWithBuildImages()
473 builds = self._ListBuildTimes()
474 return_string = ''
475 for build, timestamp in builds:
476 return_string += '<b>' + build + '</b> '
477 return_string += '(time since last access: ' + str(timestamp) + ')<br>'
478 return return_string
joychen3cb228e2013-06-12 12:13:13 -0700479
480 def Capacity(self):
481 """Returns the number of images cached by xBuddy."""
482 return str(_XBUDDY_CAPACITY)
483
joychen921e1fb2013-06-28 11:12:20 -0700484 def Get(self, path_list, return_dir=False):
485 """The full xBuddy call, returns resource specified by path_list.
joychen3cb228e2013-06-12 12:13:13 -0700486
487 Please see devserver.py:xbuddy for full documentation.
488 Args:
joychen921e1fb2013-06-28 11:12:20 -0700489 path_list: [board, version, alias] as split from the xbuddy call url
joychen3cb228e2013-06-12 12:13:13 -0700490 return_dir: boolean, if set to true, returns the dir name instead.
491
492 Returns:
493 Path to the image or update directory on the devserver.
joychenf8f07e22013-07-12 17:45:51 -0700494 e.g. http://host/static/x86-generic/
joychen3cb228e2013-06-12 12:13:13 -0700495 R26-4000.0.0/chromium-test-image.bin
496 or
joychenf8f07e22013-07-12 17:45:51 -0700497 http://host/static/x86-generic/R26-4000.0.0/
joychen3cb228e2013-06-12 12:13:13 -0700498
499 Raises:
500 XBuddyException if path is invalid or XBuddy's cache fails
501 """
joychen921e1fb2013-06-28 11:12:20 -0700502 build_id, file_name = self._GetArtifact(path_list)
joychen3cb228e2013-06-12 12:13:13 -0700503
joychen921e1fb2013-06-28 11:12:20 -0700504 Timestamp.UpdateTimestamp(self._timestamp_folder, build_id)
joychen3cb228e2013-06-12 12:13:13 -0700505
506 #TODO (joyc): run in sep thread
507 self._CleanCache()
508
509 #TODO (joyc) static dir dependent on bug id: 214373
joychen921e1fb2013-06-28 11:12:20 -0700510 return_url = os.path.join('static', build_id)
joychen3cb228e2013-06-12 12:13:13 -0700511 if not return_dir:
512 return_url = os.path.join(return_url, file_name)
513
514 _Log('Returning path to payload: %s', return_url)
515 return return_url