blob: 4bf10cd1a198a1ecbae2dac1bafbc5a5fae7e37c [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
Chris Sosa481fbbf2013-02-27 15:31:11 -08008import os
Gilad Arnold13180012013-01-24 17:58:21 -08009import re
Gilad Arnold84eb60c2012-11-05 23:46:10 -080010
Chris Sosabc69dea2013-02-22 16:09:40 -080011import common
12from autotest_lib.client.common_lib import global_config
Chris Sosa481fbbf2013-02-27 15:31:11 -080013
14try:
15 from devserver import gsutil_util
16except ImportError:
17 # Make this easy for users to automatically import the devserver if not found.
18 from autotest_lib.utils import build_externals, external_packages
19 tot = external_packages.find_top_of_autotest_tree()
20 install_dir = os.path.join(tot, build_externals.INSTALL_DIR)
21 build_externals.build_and_install_packages(
22 [external_packages.DevServerRepo()], install_dir)
23 from devserver import gsutil_util
Chris Sosabc69dea2013-02-22 16:09:40 -080024
Gilad Arnold84eb60c2012-11-05 23:46:10 -080025
26# A string indicating a zip-file boundary within a URI path. This string must
27# end with a '/', in order for standard basename code to work correctly for
28# zip-encapsulated paths.
29ZIPFILE_BOUNDARY = '//'
Chris Sosabc69dea2013-02-22 16:09:40 -080030ARCHIVE_URL_FORMAT = '%(archive_base)s/%(board)s-release/%(branch)s-%(release)s'
Gilad Arnold84eb60c2012-11-05 23:46:10 -080031
32
33class TestImageError(BaseException):
Chris Sosabc69dea2013-02-22 16:09:40 -080034 """Raised on any error in this module."""
Gilad Arnold84eb60c2012-11-05 23:46:10 -080035 pass
36
37
Chris Sosabc69dea2013-02-22 16:09:40 -080038def _get_archive_url(board, branch, release):
39 """Returns the gs archive_url for the respective arguments."""
Chris Sosabc69dea2013-02-22 16:09:40 -080040 archive_base = global_config.global_config.get_config_value(
41 'CROS', 'image_storage_server')
42 archive_base = archive_base.rstrip('/') # Remove any trailing /'s.
Chris Sosa1bf41c42013-02-27 11:34:23 -080043
44 # TODO(garnold) adjustment to -he variant board names; should be removed
45 # once we switch to using artifacts from gs://chromeos-images/
46 # (see chromium-os:38222)
Chris Sosabc69dea2013-02-22 16:09:40 -080047 board = re.sub('-he$', '_he', board)
Chris Sosa1bf41c42013-02-27 11:34:23 -080048
Chris Sosabc69dea2013-02-22 16:09:40 -080049 return ARCHIVE_URL_FORMAT % dict(
50 archive_base=archive_base, board=board, branch=branch,
51 release=release)
52
53
Chris Sosa1bf41c42013-02-27 11:34:23 -080054def gs_ls(pattern, archive_url, single):
Gilad Arnold84eb60c2012-11-05 23:46:10 -080055 """Returns a list of URIs that match a given pattern.
56
Chris Sosa1bf41c42013-02-27 11:34:23 -080057 @param pattern: a regexp pattern to match (feeds into re.match).
58 @param archive_url: the gs uri where to search (see ARCHIVE_URL_FORMAT).
59 @param single: if true, expect a single match and return it.
Gilad Arnold84eb60c2012-11-05 23:46:10 -080060
Chris Sosa1bf41c42013-02-27 11:34:23 -080061 @return A list of URIs (possibly an empty list).
Gilad Arnold84eb60c2012-11-05 23:46:10 -080062
63 """
Chris Sosa1bf41c42013-02-27 11:34:23 -080064 try:
65 logging.debug('Searching for pattern %s from url %s', pattern,
66 archive_url)
67 uri_list = gsutil_util.GetGSNamesWithWait(
68 pattern, archive_url, err_str=__name__, single_item=single,
69 timeout=1)
70 # Convert to the format our clients expect (full archive path).
71 return ['/'.join([archive_url, u]) for u in uri_list]
72 except gsutil_util.PatternNotSpecific as e:
73 raise TestImageError(str(e))
74 except gsutil_util.GSUtilError:
75 return []
Gilad Arnold84eb60c2012-11-05 23:46:10 -080076
77
78def find_payload_uri(board, release, branch, delta=False,
Chris Sosabc69dea2013-02-22 16:09:40 -080079 single=False, archive_url=None):
Gilad Arnold84eb60c2012-11-05 23:46:10 -080080 """Finds test payloads corresponding to a given board/release.
81
82 @param board: the platform name (string)
83 @param release: the release version (string), without milestone and
84 attempt/build counters
85 @param branch: the release's branch name
86 @param delta: if true, seek delta payloads to the given release
87 @param single: if true, expect a single match and return it, otherwise
88 None
Chris Sosabc69dea2013-02-22 16:09:40 -080089 @param archive_url: Optional archive_url directory to find the payload.
Gilad Arnold84eb60c2012-11-05 23:46:10 -080090
91 @return A (possibly empty) list of URIs, or a single (possibly None) URI if
92 |single| is True.
93
94 @raise TestImageError if an error has occurred.
95
96 """
Chris Sosabc69dea2013-02-22 16:09:40 -080097 if not archive_url:
98 archive_url = _get_archive_url(board, branch, release)
Gilad Arnold13180012013-01-24 17:58:21 -080099
Chris Sosabc69dea2013-02-22 16:09:40 -0800100 if delta:
Chris Sosa1bf41c42013-02-27 11:34:23 -0800101 pattern = '.*_delta_.*'
Chris Sosabc69dea2013-02-22 16:09:40 -0800102 else:
Chris Sosa1bf41c42013-02-27 11:34:23 -0800103 pattern = '.*_full_.*'
Chris Sosabc69dea2013-02-22 16:09:40 -0800104
Chris Sosa1bf41c42013-02-27 11:34:23 -0800105 payload_uri_list = gs_ls(pattern, archive_url, single)
Chris Sosa641c9292013-03-21 11:12:59 -0700106 if not payload_uri_list:
107 return None if single else []
108
Chris Sosa1bf41c42013-02-27 11:34:23 -0800109 return payload_uri_list[0] if single else payload_uri_list
Gilad Arnold84eb60c2012-11-05 23:46:10 -0800110
111
Chris Sosabc69dea2013-02-22 16:09:40 -0800112def find_image_uri(board, release, branch, archive_url=None):
Gilad Arnold84eb60c2012-11-05 23:46:10 -0800113 """Returns a URI to a test image.
114
115 @param board: the platform name (string)
116 @param release: the release version (string), without milestone and
117 attempt/build counters
118 @param branch: the release's branch name
Chris Sosabc69dea2013-02-22 16:09:40 -0800119 @param archive_url: Optional archive_url directory to find the payload.
Gilad Arnold84eb60c2012-11-05 23:46:10 -0800120
121 @return A URI to the desired image if found, None otherwise. It will most
122 likely be a file inside an image archive (image.zip), in which case
123 we'll be using ZIPFILE_BOUNDARY ('//') to denote a zip-encapsulated
124 file, for example:
125 gs://chromeos-image-archive/.../image.zip//chromiumos_test_image.bin
126
127 @raise TestImageError if an error has occurred.
128
129 """
Chris Sosabc69dea2013-02-22 16:09:40 -0800130 if not archive_url:
131 archive_url = _get_archive_url(board, branch, release)
Gilad Arnold13180012013-01-24 17:58:21 -0800132
Chris Sosa641c9292013-03-21 11:12:59 -0700133 image_archive = gs_ls('image.zip', archive_url, single=True)
134 if not image_archive:
135 return None
136
137 return (image_archive[0] + ZIPFILE_BOUNDARY + 'chromiumos_test_image.bin')