blob: 0bab714a4087125f45c3958167ae40d6b656c6f5 [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}
Simran Basi99e63c02014-05-20 10:39:52 -0700332 def _LookupChannel(self, board, channel='stable', image_dir=None):
joychenf8f07e22013-07-12 17:45:51 -0700333 """Check the channel folder for the version number of interest."""
joychen121fc9b2013-08-02 14:30:30 -0700334 # Get all names in channel dir. Get 10 highest directories by version.
joychen7df67f72013-07-18 14:21:12 -0700335 _Log("Checking channel '%s' for latest '%s' image", channel, board)
Yu-Ju Hongc54658c2014-01-22 09:18:07 -0800336 # Due to historical reasons, gs://chromeos-releases uses
337 # daisy-spring as opposed to the board name daisy_spring. Convert
338 # the board name for the lookup.
339 channel_dir = devserver_constants.GS_CHANNEL_DIR % {
340 'channel':channel,
341 'board':re.sub('_', '-', board)}
joychen562699a2013-08-13 15:22:14 -0700342 latest_version = gsutil_util.GetLatestVersionFromGSDir(
343 channel_dir, with_release=False)
joychenf8f07e22013-07-12 17:45:51 -0700344
joychen121fc9b2013-08-02 14:30:30 -0700345 # Figure out release number from the version number.
joychenc3944cb2013-08-19 10:42:07 -0700346 image_url = devserver_constants.IMAGE_DIR % {
347 'board':board,
348 'suffix':RELEASE,
349 'version':'R*' + latest_version}
Simran Basi99e63c02014-05-20 10:39:52 -0700350 image_dir = XBuddy._ResolveImageDir(image_dir)
351 gs_url = os.path.join(image_dir, image_url)
joychenf8f07e22013-07-12 17:45:51 -0700352
353 # There should only be one match on cros-image-archive.
Simran Basi99e63c02014-05-20 10:39:52 -0700354 full_version = gsutil_util.GetLatestVersionFromGSDir(gs_url)
joychenf8f07e22013-07-12 17:45:51 -0700355
356 return devserver_constants.IMAGE_DIR % {'board':board,
357 'suffix':RELEASE,
358 'version':full_version}
359
360 def _LookupVersion(self, board, version):
361 """Search GS image releases for the highest match to a version prefix."""
joychen121fc9b2013-08-02 14:30:30 -0700362 # Build the pattern for GS to match.
joychen7df67f72013-07-18 14:21:12 -0700363 _Log("Checking gs for latest '%s' image with prefix '%s'", board, version)
joychenf8f07e22013-07-12 17:45:51 -0700364 image_url = devserver_constants.IMAGE_DIR % {'board':board,
365 'suffix':RELEASE,
366 'version':version + '*'}
367 image_dir = os.path.join(devserver_constants.GS_IMAGE_DIR, image_url)
368
joychen121fc9b2013-08-02 14:30:30 -0700369 # Grab the newest version of the ones matched.
joychenf8f07e22013-07-12 17:45:51 -0700370 full_version = gsutil_util.GetLatestVersionFromGSDir(image_dir)
371 return devserver_constants.IMAGE_DIR % {'board':board,
372 'suffix':RELEASE,
373 'version':full_version}
374
Chris Sosaea734d92013-10-11 11:28:58 -0700375 def _RemoteBuildId(self, board, version):
376 """Returns the remote build_id for the given board and version.
377
378 Raises:
379 XBuddyException: If we failed to resolve the version to a valid build_id.
380 """
381 build_id_as_is = devserver_constants.IMAGE_DIR % {'board':board,
382 'suffix':'',
383 'version':version}
384 build_id_release = devserver_constants.IMAGE_DIR % {'board':board,
385 'suffix':RELEASE,
386 'version':version}
387 # Return the first path that exists. We assume that what the user typed
388 # is better than with a default suffix added i.e. x86-generic/blah is
389 # more valuable than x86-generic-release/blah.
390 for build_id in build_id_as_is, build_id_release:
391 cmd = 'gsutil ls %s/%s' % (devserver_constants.GS_IMAGE_DIR, build_id)
392 try:
393 version = gsutil_util.GSUtilRun(cmd, None)
394 return build_id
395 except gsutil_util.GSUtilError:
396 continue
Gilad Arnold5f46d8e2015-02-19 12:17:55 -0800397
398 raise XBuddyException('Could not find remote build_id for %s %s' % (
399 board, version))
Chris Sosaea734d92013-10-11 11:28:58 -0700400
Simran Basi99e63c02014-05-20 10:39:52 -0700401 def _ResolveVersionToBuildId(self, board, version, image_dir=None):
joychen121fc9b2013-08-02 14:30:30 -0700402 """Handle version aliases for remote payloads in GS.
joychen3cb228e2013-06-12 12:13:13 -0700403
404 Args:
405 board: as specified in the original call. (i.e. x86-generic, parrot)
406 version: as entered in the original call. can be
407 {TBD, 0. some custom alias as defined in a config file}
408 1. latest
409 2. latest-{channel}
410 3. latest-official-{board suffix}
411 4. version prefix (i.e. RX-Y.X, RX-Y, RX)
Simran Basi99e63c02014-05-20 10:39:52 -0700412 image_dir: image directory to check in Google Storage. If none,
413 the default bucket is used.
joychen3cb228e2013-06-12 12:13:13 -0700414
415 Returns:
Chris Sosaea734d92013-10-11 11:28:58 -0700416 Location where the image dir is actually found on GS (build_id)
joychen3cb228e2013-06-12 12:13:13 -0700417
Chris Sosaea734d92013-10-11 11:28:58 -0700418 Raises:
419 XBuddyException: If we failed to resolve the version to a valid url.
joychen3cb228e2013-06-12 12:13:13 -0700420 """
joychenf8f07e22013-07-12 17:45:51 -0700421 # Only the last segment of the alias is variable relative to the rest.
422 version_tuple = version.rsplit('-', 1)
joychen3cb228e2013-06-12 12:13:13 -0700423
joychenf8f07e22013-07-12 17:45:51 -0700424 if re.match(devserver_constants.VERSION_RE, version):
Chris Sosaea734d92013-10-11 11:28:58 -0700425 return self._RemoteBuildId(board, version)
joychenf8f07e22013-07-12 17:45:51 -0700426 elif version == LATEST_OFFICIAL:
427 # latest-official --> LATEST build in board-release
Simran Basi99e63c02014-05-20 10:39:52 -0700428 return self._LookupOfficial(board, image_dir=image_dir)
joychenf8f07e22013-07-12 17:45:51 -0700429 elif version_tuple[0] == LATEST_OFFICIAL:
430 # latest-official-{suffix} --> LATEST build in board-{suffix}
Simran Basi99e63c02014-05-20 10:39:52 -0700431 return self._LookupOfficial(board, version_tuple[1], image_dir=image_dir)
joychenf8f07e22013-07-12 17:45:51 -0700432 elif version == LATEST:
433 # latest --> latest build on stable channel
Simran Basi99e63c02014-05-20 10:39:52 -0700434 return self._LookupChannel(board, image_dir=image_dir)
joychenf8f07e22013-07-12 17:45:51 -0700435 elif version_tuple[0] == LATEST:
436 if re.match(devserver_constants.VERSION_RE, version_tuple[1]):
437 # latest-R* --> most recent qualifying build
438 return self._LookupVersion(board, version_tuple[1])
439 else:
440 # latest-{channel} --> latest build within that channel
Simran Basi99e63c02014-05-20 10:39:52 -0700441 return self._LookupChannel(board, version_tuple[1],
442 image_dir=image_dir)
joychen3cb228e2013-06-12 12:13:13 -0700443 else:
444 # The given version doesn't match any known patterns.
joychen921e1fb2013-06-28 11:12:20 -0700445 raise XBuddyException("Version %s unknown. Can't find on GS." % version)
joychen3cb228e2013-06-12 12:13:13 -0700446
joychen5260b9a2013-07-16 14:48:01 -0700447 @staticmethod
448 def _Symlink(link, target):
449 """Symlinks link to target, and removes whatever link was there before."""
450 _Log("Linking to %s from %s", link, target)
451 if os.path.lexists(link):
452 os.unlink(link)
453 os.symlink(target, link)
454
joychen121fc9b2013-08-02 14:30:30 -0700455 def _GetLatestLocalVersion(self, board):
joychen921e1fb2013-06-28 11:12:20 -0700456 """Get the version of the latest image built for board by build_image
457
458 Updates the symlink reference within the xBuddy static dir to point to
459 the real image dir in the local /build/images directory.
460
461 Args:
joychenc3944cb2013-08-19 10:42:07 -0700462 board: board that image was built for.
joychen921e1fb2013-06-28 11:12:20 -0700463
464 Returns:
joychen121fc9b2013-08-02 14:30:30 -0700465 The discovered version of the image.
joychenc3944cb2013-08-19 10:42:07 -0700466
467 Raises:
468 XBuddyException if neither test nor dev image was found in latest built
469 directory.
joychen3cb228e2013-06-12 12:13:13 -0700470 """
joychen921e1fb2013-06-28 11:12:20 -0700471 latest_local_dir = self.GetLatestImageDir(board)
joychenb0dfe552013-07-30 10:02:06 -0700472 if not latest_local_dir or not os.path.exists(latest_local_dir):
joychen921e1fb2013-06-28 11:12:20 -0700473 raise XBuddyException('No builds found for %s. Did you run build_image?' %
474 board)
475
joychen121fc9b2013-08-02 14:30:30 -0700476 # Assume that the version number is the name of the directory.
joychenc3944cb2013-08-19 10:42:07 -0700477 return os.path.basename(latest_local_dir.rstrip('/'))
joychen921e1fb2013-06-28 11:12:20 -0700478
joychenc3944cb2013-08-19 10:42:07 -0700479 @staticmethod
480 def _FindAny(local_dir):
481 """Returns the image_type for ANY given the local_dir."""
joychenc3944cb2013-08-19 10:42:07 -0700482 test_image = os.path.join(local_dir, devserver_constants.TEST_IMAGE_FILE)
Yu-Ju Hongc23c79b2014-03-17 12:40:33 -0700483 dev_image = os.path.join(local_dir, devserver_constants.IMAGE_FILE)
484 # Prioritize test images over dev images.
joychenc3944cb2013-08-19 10:42:07 -0700485 if os.path.exists(test_image):
486 return 'test'
487
Yu-Ju Hongc23c79b2014-03-17 12:40:33 -0700488 if os.path.exists(dev_image):
489 return 'dev'
490
joychenc3944cb2013-08-19 10:42:07 -0700491 raise XBuddyException('No images found in %s' % local_dir)
492
493 @staticmethod
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800494 def _InterpretPath(path, default_board=None, default_version=None):
joychen121fc9b2013-08-02 14:30:30 -0700495 """Split and return the pieces of an xBuddy path name
joychen921e1fb2013-06-28 11:12:20 -0700496
joychen121fc9b2013-08-02 14:30:30 -0700497 Args:
498 path: the path xBuddy Get was called with.
Chris Sosa0eecf962014-02-03 14:14:39 -0800499 default_board: board to use in case board isn't in path.
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800500 default_version: Version to use in case version isn't in path.
joychen3cb228e2013-06-12 12:13:13 -0700501
Yu-Ju Hongc54658c2014-01-22 09:18:07 -0800502 Returns:
Chris Sosa75490802013-09-30 17:21:45 -0700503 tuple of (image_type, board, version, whether the path is local)
joychen3cb228e2013-06-12 12:13:13 -0700504
505 Raises:
506 XBuddyException: if the path can't be resolved into valid components
507 """
joychen121fc9b2013-08-02 14:30:30 -0700508 path_list = filter(None, path.split('/'))
joychen7df67f72013-07-18 14:21:12 -0700509
Chris Sosa0eecf962014-02-03 14:14:39 -0800510 # Do the stuff that is well known first. We know that if paths have a
511 # image_type, it must be one of the GS/LOCAL aliases and it must be at the
512 # end. Similarly, local/remote are well-known and must start the path list.
513 is_local = True
514 if path_list and path_list[0] in (REMOTE, LOCAL):
515 is_local = (path_list.pop(0) == LOCAL)
joychen7df67f72013-07-18 14:21:12 -0700516
Chris Sosa0eecf962014-02-03 14:14:39 -0800517 # Default image type is determined by remote vs. local.
518 if is_local:
519 image_type = ANY
520 else:
521 image_type = TEST
joychen7df67f72013-07-18 14:21:12 -0700522
Chris Sosa0eecf962014-02-03 14:14:39 -0800523 if path_list and path_list[-1] in GS_ALIASES + LOCAL_ALIASES:
524 image_type = path_list.pop(-1)
joychen3cb228e2013-06-12 12:13:13 -0700525
Chris Sosa0eecf962014-02-03 14:14:39 -0800526 # Now for the tricky part. We don't actually know at this point if the rest
527 # of the path is just a board | version (like R33-2341.0.0) or just a board
528 # or just a version. So we do our best to do the right thing.
529 board = default_board
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800530 version = default_version or LATEST
Chris Sosa0eecf962014-02-03 14:14:39 -0800531 if len(path_list) == 1:
532 path = path_list.pop(0)
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800533 # Treat this as a version if it's one we know (contains default or
534 # latest), or we were given an actual default board.
535 if default_version in path or LATEST in path or default_board is not None:
Chris Sosa0eecf962014-02-03 14:14:39 -0800536 version = path
joychen7df67f72013-07-18 14:21:12 -0700537 else:
Chris Sosa0eecf962014-02-03 14:14:39 -0800538 board = path
joychen7df67f72013-07-18 14:21:12 -0700539
Chris Sosa0eecf962014-02-03 14:14:39 -0800540 elif len(path_list) == 2:
541 # Assumes board/version.
542 board = path_list.pop(0)
543 version = path_list.pop(0)
544
545 if path_list:
546 raise XBuddyException("Path isn't valid. Could not figure out how to "
547 "parse remaining components: %s." % path_list)
548
549 _Log("Get artifact '%s' with board %s and version %s'. Locally? %s",
joychen7df67f72013-07-18 14:21:12 -0700550 image_type, board, version, is_local)
551
552 return image_type, board, version, is_local
joychen3cb228e2013-06-12 12:13:13 -0700553
joychen921e1fb2013-06-28 11:12:20 -0700554 def _SyncRegistryWithBuildImages(self):
Gilad Arnold5f46d8e2015-02-19 12:17:55 -0800555 """Crawl images_dir for build_ids of images generated from build_image.
joychen5260b9a2013-07-16 14:48:01 -0700556
557 This will find images and symlink them in xBuddy's static dir so that
558 xBuddy's cache can serve them.
559 If xBuddy's _manage_builds option is on, then a timestamp will also be
560 generated, and xBuddy will clear them from the directory they are in, as
561 necessary.
562 """
Yu-Ju Hong235d1b52014-04-16 11:01:47 -0700563 if not os.path.isdir(self.images_dir):
564 # Skip syncing if images_dir does not exist.
565 _Log('Cannot find %s; skip syncing image registry.', self.images_dir)
566 return
567
joychen921e1fb2013-06-28 11:12:20 -0700568 build_ids = []
569 for b in os.listdir(self.images_dir):
joychen5260b9a2013-07-16 14:48:01 -0700570 # Ensure we have directories to track all boards in build/images
571 common_util.MkDirP(os.path.join(self.static_dir, b))
joychen921e1fb2013-06-28 11:12:20 -0700572 board_dir = os.path.join(self.images_dir, b)
573 build_ids.extend(['/'.join([b, v]) for v
joychenc3944cb2013-08-19 10:42:07 -0700574 in os.listdir(board_dir) if not v == LATEST])
joychen921e1fb2013-06-28 11:12:20 -0700575
joychen121fc9b2013-08-02 14:30:30 -0700576 # Symlink undiscovered images, and update timestamps if manage_builds is on.
joychen5260b9a2013-07-16 14:48:01 -0700577 for build_id in build_ids:
578 link = os.path.join(self.static_dir, build_id)
579 target = os.path.join(self.images_dir, build_id)
580 XBuddy._Symlink(link, target)
581 if self._manage_builds:
582 Timestamp.UpdateTimestamp(self._timestamp_folder, build_id)
joychen921e1fb2013-06-28 11:12:20 -0700583
584 def _ListBuildTimes(self):
Gilad Arnold5f46d8e2015-02-19 12:17:55 -0800585 """Returns the currently cached builds and their last access timestamp.
joychen3cb228e2013-06-12 12:13:13 -0700586
587 Returns:
588 list of tuples that matches xBuddy build/version to timestamps in long
589 """
joychen121fc9b2013-08-02 14:30:30 -0700590 # Update currently cached builds.
joychen3cb228e2013-06-12 12:13:13 -0700591 build_dict = {}
592
joychen7df67f72013-07-18 14:21:12 -0700593 for f in os.listdir(self._timestamp_folder):
joychen3cb228e2013-06-12 12:13:13 -0700594 last_accessed = os.path.getmtime(os.path.join(self._timestamp_folder, f))
595 build_id = Timestamp.TimestampToBuild(f)
joychenc3944cb2013-08-19 10:42:07 -0700596 stale_time = datetime.timedelta(seconds=(time.time() - last_accessed))
joychen921e1fb2013-06-28 11:12:20 -0700597 build_dict[build_id] = stale_time
joychen3cb228e2013-06-12 12:13:13 -0700598 return_tup = sorted(build_dict.iteritems(), key=operator.itemgetter(1))
599 return return_tup
600
Chris Sosa75490802013-09-30 17:21:45 -0700601 def _Download(self, gs_url, artifacts):
602 """Download the artifacts from the given gs_url.
603
604 Raises:
605 build_artifact.ArtifactDownloadError: If we failed to download the
606 artifact.
607 """
joychen3cb228e2013-06-12 12:13:13 -0700608 with XBuddy._staging_thread_count_lock:
609 XBuddy._staging_thread_count += 1
610 try:
Chris Sosa75490802013-09-30 17:21:45 -0700611 _Log("Downloading %s from %s", artifacts, gs_url)
612 downloader.Downloader(self.static_dir, gs_url).Download(artifacts, [])
joychen3cb228e2013-06-12 12:13:13 -0700613 finally:
614 with XBuddy._staging_thread_count_lock:
615 XBuddy._staging_thread_count -= 1
616
Chris Sosa75490802013-09-30 17:21:45 -0700617 def CleanCache(self):
joychen562699a2013-08-13 15:22:14 -0700618 """Delete all builds besides the newest N builds"""
joychen121fc9b2013-08-02 14:30:30 -0700619 if not self._manage_builds:
620 return
joychen921e1fb2013-06-28 11:12:20 -0700621 cached_builds = [e[0] for e in self._ListBuildTimes()]
joychen3cb228e2013-06-12 12:13:13 -0700622 _Log('In cache now: %s', cached_builds)
623
joychen562699a2013-08-13 15:22:14 -0700624 for b in range(self._Capacity(), len(cached_builds)):
joychen3cb228e2013-06-12 12:13:13 -0700625 b_path = cached_builds[b]
joychen7df67f72013-07-18 14:21:12 -0700626 _Log("Clearing '%s' from cache", b_path)
joychen3cb228e2013-06-12 12:13:13 -0700627
628 time_file = os.path.join(self._timestamp_folder,
629 Timestamp.BuildToTimestamp(b_path))
joychen921e1fb2013-06-28 11:12:20 -0700630 os.unlink(time_file)
631 clear_dir = os.path.join(self.static_dir, b_path)
joychen3cb228e2013-06-12 12:13:13 -0700632 try:
joychen121fc9b2013-08-02 14:30:30 -0700633 # Handle symlinks, in the case of links to local builds if enabled.
634 if os.path.islink(clear_dir):
joychen5260b9a2013-07-16 14:48:01 -0700635 target = os.readlink(clear_dir)
636 _Log('Deleting locally built image at %s', target)
joychen921e1fb2013-06-28 11:12:20 -0700637
638 os.unlink(clear_dir)
joychen5260b9a2013-07-16 14:48:01 -0700639 if os.path.exists(target):
joychen921e1fb2013-06-28 11:12:20 -0700640 shutil.rmtree(target)
641 elif os.path.exists(clear_dir):
joychen5260b9a2013-07-16 14:48:01 -0700642 _Log('Deleting downloaded image at %s', clear_dir)
joychen3cb228e2013-06-12 12:13:13 -0700643 shutil.rmtree(clear_dir)
joychen921e1fb2013-06-28 11:12:20 -0700644
joychen121fc9b2013-08-02 14:30:30 -0700645 except Exception as err:
646 raise XBuddyException('Failed to clear %s: %s' % (clear_dir, err))
joychen3cb228e2013-06-12 12:13:13 -0700647
Simran Basi99e63c02014-05-20 10:39:52 -0700648 def _GetFromGS(self, build_id, image_type, image_dir=None):
Chris Sosa75490802013-09-30 17:21:45 -0700649 """Check if the artifact is available locally. Download from GS if not.
650
Simran Basi99e63c02014-05-20 10:39:52 -0700651 Args:
652 build_id: Path to the image or update directory on the devserver or
653 in Google Storage. e.g. 'x86-generic/R26-4000.0.0'
654 image_type: Image type to download. Look at aliases at top of file for
655 options.
656 image_dir: Google Storage image archive to search in if requesting a
657 remote artifact. If none uses the default bucket.
658
Chris Sosa75490802013-09-30 17:21:45 -0700659 Raises:
660 build_artifact.ArtifactDownloadError: If we failed to download the
661 artifact.
662 """
Simran Basi99e63c02014-05-20 10:39:52 -0700663 image_dir = XBuddy._ResolveImageDir(image_dir)
664 gs_url = os.path.join(image_dir, build_id)
joychen921e1fb2013-06-28 11:12:20 -0700665
joychen121fc9b2013-08-02 14:30:30 -0700666 # Stage image if not found in cache.
joychen921e1fb2013-06-28 11:12:20 -0700667 file_name = GS_ALIAS_TO_FILENAME[image_type]
joychen346531c2013-07-24 16:55:56 -0700668 file_loc = os.path.join(self.static_dir, build_id, file_name)
669 cached = os.path.exists(file_loc)
670
joychen921e1fb2013-06-28 11:12:20 -0700671 if not cached:
Chris Sosa75490802013-09-30 17:21:45 -0700672 artifact = GS_ALIAS_TO_ARTIFACT[image_type]
673 self._Download(gs_url, [artifact])
joychen921e1fb2013-06-28 11:12:20 -0700674 else:
675 _Log('Image already cached.')
676
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800677 def _GetArtifact(self, path_list, board=None, version=None,
678 lookup_only=False, image_dir=None):
joychen346531c2013-07-24 16:55:56 -0700679 """Interpret an xBuddy path and return directory/file_name to resource.
680
Chris Sosa75490802013-09-30 17:21:45 -0700681 Note board can be passed that in but by default if self._board is set,
682 that is used rather than board.
683
Simran Basi99e63c02014-05-20 10:39:52 -0700684 Args:
685 path_list: [board, version, alias] as split from the xbuddy call url.
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800686 board: Board whos artifacts we are looking for. Only used if no board was
687 given during XBuddy initialization.
688 version: Version whose artifacts we are looking for. Used if no version
689 was given during XBuddy initialization. If None, defers to LATEST.
Simran Basi99e63c02014-05-20 10:39:52 -0700690 lookup_only: If true just look up the artifact, if False stage it on
691 the devserver as well.
692 image_dir: Google Storage image archive to search in if requesting a
693 remote artifact. If none uses the default bucket.
694
joychen346531c2013-07-24 16:55:56 -0700695 Returns:
Simran Basi99e63c02014-05-20 10:39:52 -0700696 build_id: Path to the image or update directory on the devserver or
697 in Google Storage. e.g. 'x86-generic/R26-4000.0.0'
698 file_name: of the artifact in the build_id directory.
joychen346531c2013-07-24 16:55:56 -0700699
700 Raises:
joychen121fc9b2013-08-02 14:30:30 -0700701 XBuddyException: if the path could not be translated
Chris Sosa75490802013-09-30 17:21:45 -0700702 build_artifact.ArtifactDownloadError: if we failed to download the
703 artifact.
joychen346531c2013-07-24 16:55:56 -0700704 """
joychen121fc9b2013-08-02 14:30:30 -0700705 path = '/'.join(path_list)
Chris Sosa0eecf962014-02-03 14:14:39 -0800706 default_board = self._board if self._board else board
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800707 default_version = self._version or version or LATEST
joychenb0dfe552013-07-30 10:02:06 -0700708 # Rewrite the path if there is an appropriate default.
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800709 path = self._LookupAlias(path, default_board, default_version)
joychen121fc9b2013-08-02 14:30:30 -0700710 # Parse the path.
Chris Sosa0eecf962014-02-03 14:14:39 -0800711 image_type, board, version, is_local = self._InterpretPath(
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800712 path, default_board, default_version)
joychen7df67f72013-07-18 14:21:12 -0700713 if is_local:
joychen121fc9b2013-08-02 14:30:30 -0700714 # Get a local image.
joychen7df67f72013-07-18 14:21:12 -0700715 if version == LATEST:
joychen121fc9b2013-08-02 14:30:30 -0700716 # Get the latest local image for the given board.
717 version = self._GetLatestLocalVersion(board)
joychen7df67f72013-07-18 14:21:12 -0700718
joychenc3944cb2013-08-19 10:42:07 -0700719 build_id = os.path.join(board, version)
720 artifact_dir = os.path.join(self.static_dir, build_id)
721 if image_type == ANY:
722 image_type = self._FindAny(artifact_dir)
joychen121fc9b2013-08-02 14:30:30 -0700723
joychenc3944cb2013-08-19 10:42:07 -0700724 file_name = LOCAL_ALIAS_TO_FILENAME[image_type]
725 artifact_path = os.path.join(artifact_dir, file_name)
726 if not os.path.exists(artifact_path):
727 raise XBuddyException('Local %s artifact not in static_dir at %s' %
728 (image_type, artifact_path))
joychen121fc9b2013-08-02 14:30:30 -0700729
joychen921e1fb2013-06-28 11:12:20 -0700730 else:
joychen121fc9b2013-08-02 14:30:30 -0700731 # Get a remote image.
joychen921e1fb2013-06-28 11:12:20 -0700732 if image_type not in GS_ALIASES:
joychen7df67f72013-07-18 14:21:12 -0700733 raise XBuddyException('Bad remote image type: %s. Use one of: %s' %
joychen921e1fb2013-06-28 11:12:20 -0700734 (image_type, GS_ALIASES))
Simran Basi99e63c02014-05-20 10:39:52 -0700735 build_id = self._ResolveVersionToBuildId(board, version,
736 image_dir=image_dir)
Chris Sosa75490802013-09-30 17:21:45 -0700737 _Log('Resolved version %s to %s.', version, build_id)
738 file_name = GS_ALIAS_TO_FILENAME[image_type]
739 if not lookup_only:
Simran Basi99e63c02014-05-20 10:39:52 -0700740 self._GetFromGS(build_id, image_type, image_dir=image_dir)
joychenf8f07e22013-07-12 17:45:51 -0700741
joychenc3944cb2013-08-19 10:42:07 -0700742 return build_id, file_name
joychen3cb228e2013-06-12 12:13:13 -0700743
744 ############################ BEGIN PUBLIC METHODS
745
746 def List(self):
747 """Lists the currently available images & time since last access."""
joychen921e1fb2013-06-28 11:12:20 -0700748 self._SyncRegistryWithBuildImages()
749 builds = self._ListBuildTimes()
750 return_string = ''
751 for build, timestamp in builds:
752 return_string += '<b>' + build + '</b> '
753 return_string += '(time since last access: ' + str(timestamp) + ')<br>'
754 return return_string
joychen3cb228e2013-06-12 12:13:13 -0700755
756 def Capacity(self):
757 """Returns the number of images cached by xBuddy."""
joychen562699a2013-08-13 15:22:14 -0700758 return str(self._Capacity())
joychen3cb228e2013-06-12 12:13:13 -0700759
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800760 def Translate(self, path_list, board=None, version=None, image_dir=None):
joychen346531c2013-07-24 16:55:56 -0700761 """Translates an xBuddy path to a real path to artifact if it exists.
762
joychen121fc9b2013-08-02 14:30:30 -0700763 Equivalent to the Get call, minus downloading and updating timestamps,
joychen346531c2013-07-24 16:55:56 -0700764
Simran Basi99e63c02014-05-20 10:39:52 -0700765 Args:
766 path_list: [board, version, alias] as split from the xbuddy call url.
767 board: Board whos artifacts we are looking for. If None, use the board
768 XBuddy was initialized to use.
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800769 version: Version whose artifacts we are looking for. If None, use the
770 version XBuddy was initialized with, or LATEST.
Simran Basi99e63c02014-05-20 10:39:52 -0700771 image_dir: image directory to check in Google Storage. If none,
772 the default bucket is used.
773
joychen7c2054a2013-07-25 11:14:07 -0700774 Returns:
joychen121fc9b2013-08-02 14:30:30 -0700775 build_id: Path to the image or update directory on the devserver.
776 e.g. 'x86-generic/R26-4000.0.0'
777 The returned path is always the path to the directory within
778 static_dir, so it is always the build_id of the image.
779 file_name: The file name of the artifact. Can take any of the file
780 values in devserver_constants.
781 e.g. 'chromiumos_test_image.bin' or 'update.gz' if the path list
782 specified 'test' or 'full_payload' artifacts, respectively.
joychen7c2054a2013-07-25 11:14:07 -0700783
joychen121fc9b2013-08-02 14:30:30 -0700784 Raises:
785 XBuddyException: if the path couldn't be translated
joychen346531c2013-07-24 16:55:56 -0700786 """
787 self._SyncRegistryWithBuildImages()
Chris Sosa75490802013-09-30 17:21:45 -0700788 build_id, file_name = self._GetArtifact(path_list, board=board,
Gilad Arnoldd04fcab2015-02-19 12:00:45 -0800789 version=version,
Simran Basi99e63c02014-05-20 10:39:52 -0700790 lookup_only=True,
791 image_dir=image_dir)
joychen346531c2013-07-24 16:55:56 -0700792
joychen121fc9b2013-08-02 14:30:30 -0700793 _Log('Returning path to payload: %s/%s', build_id, file_name)
794 return build_id, file_name
joychen346531c2013-07-24 16:55:56 -0700795
Yu-Ju Hong1bdb7a92014-04-10 16:02:11 -0700796 def StageTestArtifactsForUpdate(self, path_list):
Chris Sosa75490802013-09-30 17:21:45 -0700797 """Stages test artifacts for update and returns build_id.
798
799 Raises:
800 XBuddyException: if the path could not be translated
801 build_artifact.ArtifactDownloadError: if we failed to download the test
802 artifacts.
803 """
804 build_id, file_name = self.Translate(path_list)
805 if file_name == devserver_constants.TEST_IMAGE_FILE:
806 gs_url = os.path.join(devserver_constants.GS_IMAGE_DIR,
807 build_id)
808 artifacts = [FULL, STATEFUL]
809 self._Download(gs_url, artifacts)
810 return build_id
811
Simran Basi99e63c02014-05-20 10:39:52 -0700812 def Get(self, path_list, image_dir=None):
joychen921e1fb2013-06-28 11:12:20 -0700813 """The full xBuddy call, returns resource specified by path_list.
joychen3cb228e2013-06-12 12:13:13 -0700814
815 Please see devserver.py:xbuddy for full documentation.
joychen121fc9b2013-08-02 14:30:30 -0700816
joychen3cb228e2013-06-12 12:13:13 -0700817 Args:
Simran Basi99e63c02014-05-20 10:39:52 -0700818 path_list: [board, version, alias] as split from the xbuddy call url.
819 image_dir: image directory to check in Google Storage. If none,
820 the default bucket is used.
joychen3cb228e2013-06-12 12:13:13 -0700821
822 Returns:
joychen121fc9b2013-08-02 14:30:30 -0700823 build_id: Path to the image or update directory on the devserver.
Simran Basi99e63c02014-05-20 10:39:52 -0700824 e.g. 'x86-generic/R26-4000.0.0'
825 The returned path is always the path to the directory within
826 static_dir, so it is always the build_id of the image.
joychen121fc9b2013-08-02 14:30:30 -0700827 file_name: The file name of the artifact. Can take any of the file
Simran Basi99e63c02014-05-20 10:39:52 -0700828 values in devserver_constants.
829 e.g. 'chromiumos_test_image.bin' or 'update.gz' if the path list
830 specified 'test' or 'full_payload' artifacts, respectively.
joychen3cb228e2013-06-12 12:13:13 -0700831
832 Raises:
Chris Sosa75490802013-09-30 17:21:45 -0700833 XBuddyException: if the path could not be translated
834 build_artifact.ArtifactDownloadError: if we failed to download the
835 artifact.
joychen3cb228e2013-06-12 12:13:13 -0700836 """
joychen7df67f72013-07-18 14:21:12 -0700837 self._SyncRegistryWithBuildImages()
Simran Basi99e63c02014-05-20 10:39:52 -0700838 build_id, file_name = self._GetArtifact(path_list, image_dir=image_dir)
joychen921e1fb2013-06-28 11:12:20 -0700839 Timestamp.UpdateTimestamp(self._timestamp_folder, build_id)
joychen3cb228e2013-06-12 12:13:13 -0700840 #TODO (joyc): run in sep thread
Chris Sosa75490802013-09-30 17:21:45 -0700841 self.CleanCache()
joychen3cb228e2013-06-12 12:13:13 -0700842
joychen121fc9b2013-08-02 14:30:30 -0700843 _Log('Returning path to payload: %s/%s', build_id, file_name)
844 return build_id, file_name