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