blob: c9d78d81dbbb19793a56c6d9ecbae82bf740ea8c [file] [log] [blame]
Gilad Arnold84eb60c2012-11-05 23:46:10 -08001# Copyright (c) 2012 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
5"""Module for discovering Chrome OS test images and payloads."""
6
Chris Sosabc69dea2013-02-22 16:09:40 -08007import logging
Gilad Arnold13180012013-01-24 17:58:21 -08008import re
Gilad Arnold84eb60c2012-11-05 23:46:10 -08009
Chris Sosabc69dea2013-02-22 16:09:40 -080010import common
11from autotest_lib.client.common_lib import global_config
Chris Sosa1bf41c42013-02-27 11:34:23 -080012from devserver import gsutil_util
Chris Sosabc69dea2013-02-22 16:09:40 -080013
Gilad Arnold84eb60c2012-11-05 23:46:10 -080014
15# A string indicating a zip-file boundary within a URI path. This string must
16# end with a '/', in order for standard basename code to work correctly for
17# zip-encapsulated paths.
18ZIPFILE_BOUNDARY = '//'
Chris Sosabc69dea2013-02-22 16:09:40 -080019ARCHIVE_URL_FORMAT = '%(archive_base)s/%(board)s-release/%(branch)s-%(release)s'
Gilad Arnold84eb60c2012-11-05 23:46:10 -080020
21
22class TestImageError(BaseException):
Chris Sosabc69dea2013-02-22 16:09:40 -080023 """Raised on any error in this module."""
Gilad Arnold84eb60c2012-11-05 23:46:10 -080024 pass
25
26
Chris Sosabc69dea2013-02-22 16:09:40 -080027def _get_archive_url(board, branch, release):
28 """Returns the gs archive_url for the respective arguments."""
Chris Sosabc69dea2013-02-22 16:09:40 -080029 archive_base = global_config.global_config.get_config_value(
30 'CROS', 'image_storage_server')
31 archive_base = archive_base.rstrip('/') # Remove any trailing /'s.
Chris Sosa1bf41c42013-02-27 11:34:23 -080032
33 # TODO(garnold) adjustment to -he variant board names; should be removed
34 # once we switch to using artifacts from gs://chromeos-images/
35 # (see chromium-os:38222)
Chris Sosabc69dea2013-02-22 16:09:40 -080036 board = re.sub('-he$', '_he', board)
Chris Sosa1bf41c42013-02-27 11:34:23 -080037
Chris Sosabc69dea2013-02-22 16:09:40 -080038 return ARCHIVE_URL_FORMAT % dict(
39 archive_base=archive_base, board=board, branch=branch,
40 release=release)
41
42
Chris Sosa1bf41c42013-02-27 11:34:23 -080043def gs_ls(pattern, archive_url, single):
Gilad Arnold84eb60c2012-11-05 23:46:10 -080044 """Returns a list of URIs that match a given pattern.
45
Chris Sosa1bf41c42013-02-27 11:34:23 -080046 @param pattern: a regexp pattern to match (feeds into re.match).
47 @param archive_url: the gs uri where to search (see ARCHIVE_URL_FORMAT).
48 @param single: if true, expect a single match and return it.
Gilad Arnold84eb60c2012-11-05 23:46:10 -080049
Chris Sosa1bf41c42013-02-27 11:34:23 -080050 @return A list of URIs (possibly an empty list).
Gilad Arnold84eb60c2012-11-05 23:46:10 -080051
52 """
Chris Sosa1bf41c42013-02-27 11:34:23 -080053 try:
54 logging.debug('Searching for pattern %s from url %s', pattern,
55 archive_url)
56 uri_list = gsutil_util.GetGSNamesWithWait(
57 pattern, archive_url, err_str=__name__, single_item=single,
58 timeout=1)
59 # Convert to the format our clients expect (full archive path).
60 return ['/'.join([archive_url, u]) for u in uri_list]
61 except gsutil_util.PatternNotSpecific as e:
62 raise TestImageError(str(e))
63 except gsutil_util.GSUtilError:
64 return []
Gilad Arnold84eb60c2012-11-05 23:46:10 -080065
66
67def find_payload_uri(board, release, branch, delta=False,
Chris Sosabc69dea2013-02-22 16:09:40 -080068 single=False, archive_url=None):
Gilad Arnold84eb60c2012-11-05 23:46:10 -080069 """Finds test payloads corresponding to a given board/release.
70
71 @param board: the platform name (string)
72 @param release: the release version (string), without milestone and
73 attempt/build counters
74 @param branch: the release's branch name
75 @param delta: if true, seek delta payloads to the given release
76 @param single: if true, expect a single match and return it, otherwise
77 None
Chris Sosabc69dea2013-02-22 16:09:40 -080078 @param archive_url: Optional archive_url directory to find the payload.
Gilad Arnold84eb60c2012-11-05 23:46:10 -080079
80 @return A (possibly empty) list of URIs, or a single (possibly None) URI if
81 |single| is True.
82
83 @raise TestImageError if an error has occurred.
84
85 """
Chris Sosabc69dea2013-02-22 16:09:40 -080086 if not archive_url:
87 archive_url = _get_archive_url(board, branch, release)
Gilad Arnold13180012013-01-24 17:58:21 -080088
Chris Sosabc69dea2013-02-22 16:09:40 -080089 if delta:
Chris Sosa1bf41c42013-02-27 11:34:23 -080090 pattern = '.*_delta_.*'
Chris Sosabc69dea2013-02-22 16:09:40 -080091 else:
Chris Sosa1bf41c42013-02-27 11:34:23 -080092 pattern = '.*_full_.*'
Chris Sosabc69dea2013-02-22 16:09:40 -080093
Chris Sosa1bf41c42013-02-27 11:34:23 -080094 payload_uri_list = gs_ls(pattern, archive_url, single)
95 return payload_uri_list[0] if single else payload_uri_list
Gilad Arnold84eb60c2012-11-05 23:46:10 -080096
97
Chris Sosabc69dea2013-02-22 16:09:40 -080098def find_image_uri(board, release, branch, archive_url=None):
Gilad Arnold84eb60c2012-11-05 23:46:10 -080099 """Returns a URI to a test image.
100
101 @param board: the platform name (string)
102 @param release: the release version (string), without milestone and
103 attempt/build counters
104 @param branch: the release's branch name
Chris Sosabc69dea2013-02-22 16:09:40 -0800105 @param archive_url: Optional archive_url directory to find the payload.
Gilad Arnold84eb60c2012-11-05 23:46:10 -0800106
107 @return A URI to the desired image if found, None otherwise. It will most
108 likely be a file inside an image archive (image.zip), in which case
109 we'll be using ZIPFILE_BOUNDARY ('//') to denote a zip-encapsulated
110 file, for example:
111 gs://chromeos-image-archive/.../image.zip//chromiumos_test_image.bin
112
113 @raise TestImageError if an error has occurred.
114
115 """
Chris Sosabc69dea2013-02-22 16:09:40 -0800116 if not archive_url:
117 archive_url = _get_archive_url(board, branch, release)
Gilad Arnold13180012013-01-24 17:58:21 -0800118
Chris Sosa1bf41c42013-02-27 11:34:23 -0800119 image_archive = gs_ls('image.zip', archive_url, single=True)[0]
120 return (image_archive + ZIPFILE_BOUNDARY + 'chromiumos_test_image.bin')