Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 1 | # 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 | |
| 7 | |
| 8 | import subprocess |
| 9 | |
| 10 | |
| 11 | # A string indicating a zip-file boundary within a URI path. This string must |
| 12 | # end with a '/', in order for standard basename code to work correctly for |
| 13 | # zip-encapsulated paths. |
| 14 | ZIPFILE_BOUNDARY = '//' |
| 15 | |
| 16 | |
| 17 | class TestImageError(BaseException): |
| 18 | pass |
| 19 | |
| 20 | |
| 21 | def gs_ls(uri_pattern): |
| 22 | """Returns a list of URIs that match a given pattern. |
| 23 | |
| 24 | @param uri_pattern: a GS URI pattern, may contain wildcards |
| 25 | |
| 26 | @return A list of URIs matching the given pattern. |
| 27 | |
| 28 | """ |
| 29 | gs_cmd = ['gsutil', 'ls', uri_pattern] |
| 30 | output = subprocess.Popen(gs_cmd, stdout=subprocess.PIPE, |
| 31 | stderr=subprocess.PIPE).stdout |
| 32 | return [path.rstrip() for path in output if path] |
| 33 | |
| 34 | |
| 35 | def find_payload_uri(board, release, branch, delta=False, |
| 36 | single=False): |
| 37 | """Finds test payloads corresponding to a given board/release. |
| 38 | |
| 39 | @param board: the platform name (string) |
| 40 | @param release: the release version (string), without milestone and |
| 41 | attempt/build counters |
| 42 | @param branch: the release's branch name |
| 43 | @param delta: if true, seek delta payloads to the given release |
| 44 | @param single: if true, expect a single match and return it, otherwise |
| 45 | None |
| 46 | |
| 47 | @return A (possibly empty) list of URIs, or a single (possibly None) URI if |
| 48 | |single| is True. |
| 49 | |
| 50 | @raise TestImageError if an error has occurred. |
| 51 | |
| 52 | """ |
| 53 | payload_uri_list = gs_ls( |
| 54 | 'gs://chromeos-image-archive/%s-release/%s-%s/%s' % |
| 55 | (board, branch, release, |
| 56 | ('chromeos_*_%s-%s*_%s_delta_dev.bin' % |
| 57 | (branch, release, board)) |
| 58 | if delta |
| 59 | else ('chromeos_%s-%s*_%s_full_dev.bin' % |
| 60 | (branch, release, board)))) |
| 61 | |
| 62 | if single: |
| 63 | payload_uri_list_len = len(payload_uri_list) |
| 64 | if payload_uri_list_len == 0: |
| 65 | return None |
| 66 | elif payload_uri_list_len != 1: |
| 67 | raise TestImageError('unexpected number of results (%d instead ' |
| 68 | 'of 1)' % payload_uri_list_len) |
| 69 | return payload_uri_list[0] |
| 70 | |
| 71 | return payload_uri_list |
| 72 | |
| 73 | |
| 74 | def find_image_uri(board, release, branch): |
| 75 | """Returns a URI to a test image. |
| 76 | |
| 77 | @param board: the platform name (string) |
| 78 | @param release: the release version (string), without milestone and |
| 79 | attempt/build counters |
| 80 | @param branch: the release's branch name |
| 81 | |
| 82 | @return A URI to the desired image if found, None otherwise. It will most |
| 83 | likely be a file inside an image archive (image.zip), in which case |
| 84 | we'll be using ZIPFILE_BOUNDARY ('//') to denote a zip-encapsulated |
| 85 | file, for example: |
| 86 | gs://chromeos-image-archive/.../image.zip//chromiumos_test_image.bin |
| 87 | |
| 88 | @raise TestImageError if an error has occurred. |
| 89 | |
| 90 | """ |
| 91 | image_archive_uri_list = gs_ls('gs://chromeos-image-archive/%s-release/' |
| 92 | '%s-%s/image.zip' % (board, branch, release)) |
| 93 | |
| 94 | image_archive_uri_list_len = len(image_archive_uri_list) |
| 95 | if image_archive_uri_list_len == 0: |
| 96 | return None |
| 97 | elif image_archive_uri_list_len != 1: |
| 98 | raise TestImageError('unexpected number of results (%d > 1)' % |
| 99 | image_archive_uri_list_len) |
| 100 | |
| 101 | return (image_archive_uri_list[0] + ZIPFILE_BOUNDARY + |
| 102 | 'chromiumos_test_image.bin') |