Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Helper class for interacting with the Dev Server.""" |
| 6 | |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 7 | import base64 |
| 8 | import binascii |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 9 | import distutils.version |
| 10 | import errno |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 11 | import hashlib |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 12 | import os |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 13 | import random |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 14 | import re |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 15 | import shutil |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 16 | import time |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 17 | |
Gilad Arnold | abb352e | 2012-09-23 01:24:27 -0700 | [diff] [blame] | 18 | import lockfile |
| 19 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 20 | import build_artifact |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 21 | import gsutil_util |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 22 | import log_util |
| 23 | |
| 24 | |
| 25 | # Module-local log function. |
Chris Sosa | 65d339b | 2013-01-21 18:59:21 -0800 | [diff] [blame] | 26 | def _Log(message, *args, **kwargs): |
| 27 | return log_util.LogWithTag('UTIL', message, *args, **kwargs) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 28 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 29 | |
| 30 | AU_BASE = 'au' |
| 31 | NTON_DIR_SUFFIX = '_nton' |
| 32 | MTON_DIR_SUFFIX = '_mton' |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 33 | UPLOADED_LIST = 'UPLOADED' |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 34 | DEVSERVER_LOCK_FILE = 'devserver' |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 35 | |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 36 | _HASH_BLOCK_SIZE = 8192 |
| 37 | |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 38 | |
| 39 | def CommaSeparatedList(value_list, is_quoted=False): |
| 40 | """Concatenates a list of strings. |
| 41 | |
| 42 | This turns ['a', 'b', 'c'] into a single string 'a, b and c'. It optionally |
| 43 | adds quotes (`a') around each element. Used for logging. |
| 44 | |
| 45 | """ |
| 46 | if is_quoted: |
| 47 | value_list = ["`" + value + "'" for value in value_list] |
| 48 | |
| 49 | if len(value_list) > 1: |
| 50 | return (', '.join(value_list[:-1]) + ' and ' + value_list[-1]) |
| 51 | elif value_list: |
| 52 | return value_list[0] |
| 53 | else: |
| 54 | return '' |
| 55 | |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 56 | class CommonUtilError(Exception): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 57 | """Exception classes used by this module.""" |
| 58 | pass |
| 59 | |
| 60 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 61 | def ParsePayloadList(archive_url, payload_list): |
Vadim Bendebury | d369839 | 2012-12-27 18:21:32 -0800 | [diff] [blame] | 62 | """Parse and return the full, delta, and firmware payload URLs. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 63 | |
| 64 | Args: |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 65 | archive_url: The URL of the Google Storage bucket. |
| 66 | payload_list: A list filenames. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 67 | |
| 68 | Returns: |
Vadim Bendebury | d369839 | 2012-12-27 18:21:32 -0800 | [diff] [blame] | 69 | Tuple of 4 payload URLs: (full, nton, mton, firmware). |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 70 | |
| 71 | Raises: |
Vadim Bendebury | d369839 | 2012-12-27 18:21:32 -0800 | [diff] [blame] | 72 | CommonUtilError: If full payload is missing or invalid. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 73 | """ |
| 74 | full_payload_url = None |
| 75 | mton_payload_url = None |
| 76 | nton_payload_url = None |
Vadim Bendebury | d369839 | 2012-12-27 18:21:32 -0800 | [diff] [blame] | 77 | firmware_payload_url = None |
| 78 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 79 | for payload in payload_list: |
| 80 | if '_full_' in payload: |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 81 | full_payload_url = '/'.join([archive_url, payload]) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 82 | elif '_delta_' in payload: |
| 83 | # e.g. chromeos_{from_version}_{to_version}_x86-generic_delta_dev.bin |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 84 | from_version, to_version = payload.split('_')[1:3] |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 85 | if from_version == to_version: |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 86 | nton_payload_url = '/'.join([archive_url, payload]) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 87 | else: |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 88 | mton_payload_url = '/'.join([archive_url, payload]) |
Vadim Bendebury | d369839 | 2012-12-27 18:21:32 -0800 | [diff] [blame] | 89 | elif build_artifact.FIRMWARE_ARCHIVE in payload: |
| 90 | firmware_payload_url = '/'.join([archive_url, payload]) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 91 | |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 92 | if not full_payload_url: |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 93 | raise CommonUtilError( |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 94 | 'Full payload is missing or has unexpected name format.', payload_list) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 95 | |
Vadim Bendebury | d369839 | 2012-12-27 18:21:32 -0800 | [diff] [blame] | 96 | return (full_payload_url, nton_payload_url, |
| 97 | mton_payload_url, firmware_payload_url) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 98 | |
| 99 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 100 | def IsAvailable(pattern_list, uploaded_list): |
| 101 | """Checks whether the target artifacts we wait for are available. |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 102 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 103 | This method searches the uploaded_list for a match for every pattern |
| 104 | in the pattern_list. It aborts and returns false if no filename |
| 105 | matches a given pattern. |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 106 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 107 | Args: |
| 108 | pattern_list: List of regular expression patterns to identify |
| 109 | the target artifacts. |
| 110 | uploaded_list: List of all uploaded files. |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 111 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 112 | Returns: |
| 113 | True if there is a match for every pattern; false otherwise. |
| 114 | """ |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 115 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 116 | # Pre-compile the regular expression patterns |
| 117 | compiled_patterns = [] |
| 118 | for p in pattern_list: |
| 119 | compiled_patterns.append(re.compile(p)) |
| 120 | |
| 121 | for pattern in compiled_patterns: |
| 122 | found = False |
| 123 | for filename in uploaded_list: |
| 124 | if re.search(pattern, filename): |
| 125 | found = True |
| 126 | break |
| 127 | if not found: |
| 128 | return False |
| 129 | |
| 130 | return True |
| 131 | |
| 132 | |
| 133 | def WaitUntilAvailable(to_wait_list, archive_url, err_str, timeout=600, |
| 134 | delay=10): |
| 135 | """Waits until all target artifacts are available in Google Storage or |
| 136 | until the request times out. |
| 137 | |
| 138 | This method polls Google Storage until all target artifacts are |
| 139 | available or until the timeout occurs. Because we may not know the |
| 140 | exact name of the target artifacts, the method accepts to_wait_list, a |
| 141 | list of filename patterns, to identify whether an artifact whose name |
| 142 | matches the pattern exists (e.g. use pattern '_full_' to search for |
| 143 | the full payload 'chromeos_R17-1413.0.0-a1_x86-mario_full_dev.bin'). |
| 144 | |
| 145 | Args: |
| 146 | to_wait_list: List of regular expression patterns to identify |
| 147 | the target artifacts. |
| 148 | archive_url: URL of the Google Storage bucket. |
| 149 | err_str: String to display in the error message. |
| 150 | |
| 151 | Returns: |
| 152 | The list of artifacts in the Google Storage bucket. |
| 153 | |
| 154 | Raises: |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 155 | CommonUtilError: If timeout occurs. |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 156 | """ |
| 157 | |
| 158 | cmd = 'gsutil cat %s/%s' % (archive_url, UPLOADED_LIST) |
| 159 | msg = 'Failed to get a list of uploaded files.' |
| 160 | |
| 161 | deadline = time.time() + timeout |
| 162 | while time.time() < deadline: |
Yu-Ju Hong | 9278405 | 2012-08-22 13:08:54 -0700 | [diff] [blame] | 163 | uploaded_list = [] |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 164 | to_delay = delay + random.uniform(.5 * delay, 1.5 * delay) |
Yu-Ju Hong | 9278405 | 2012-08-22 13:08:54 -0700 | [diff] [blame] | 165 | try: |
| 166 | # Run "gsutil cat" to retrieve the list. |
| 167 | uploaded_list = gsutil_util.GSUtilRun(cmd, msg).splitlines() |
| 168 | except gsutil_util.GSUtilError: |
| 169 | # For backward compatibility, fallling back to use "gsutil ls" |
| 170 | # when the manifest file is not present. |
| 171 | cmd = 'gsutil ls %s/*' % archive_url |
| 172 | msg = 'Failed to list payloads.' |
| 173 | payload_list = gsutil_util.GSUtilRun(cmd, msg).splitlines() |
| 174 | for payload in payload_list: |
| 175 | uploaded_list.append(payload.rsplit('/', 1)[1]) |
| 176 | |
| 177 | # Check if all target artifacts are available. |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 178 | if IsAvailable(to_wait_list, uploaded_list): |
| 179 | return uploaded_list |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 180 | _Log('Retrying in %f seconds...%s' % (to_delay, err_str)) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 181 | time.sleep(to_delay) |
| 182 | |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 183 | raise CommonUtilError('Missing %s for %s.' % (err_str, archive_url)) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 184 | |
| 185 | |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 186 | def GatherArtifactDownloads(main_staging_dir, archive_url, build_dir, build, |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 187 | timeout=600, delay=10): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 188 | """Generates artifacts that we mean to download and install for autotest. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 189 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 190 | This method generates the list of artifacts we will need for autotest. These |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 191 | artifacts are instances of build_artifact.BuildArtifact. |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 192 | |
| 193 | Note, these artifacts can be downloaded asynchronously iff |
| 194 | !artifact.Synchronous(). |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 195 | """ |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 196 | |
| 197 | # Wait up to 10 minutes for the full payload to be uploaded because we |
| 198 | # do not know the exact name of the full payload. |
| 199 | |
| 200 | # We also wait for 'autotest.tar' because we do not know what type of |
| 201 | # autotest tarballs (tar or tar.bz2) is available |
| 202 | # (crosbug.com/32312). This dependency can be removed once all |
| 203 | # branches move to the new 'tar' format. |
Vadim Bendebury | d369839 | 2012-12-27 18:21:32 -0800 | [diff] [blame] | 204 | to_wait_list = ['_full_', build_artifact.AUTOTEST_PACKAGE] |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 205 | err_str = 'full payload or autotest tarball' |
| 206 | uploaded_list = WaitUntilAvailable(to_wait_list, archive_url, err_str, |
| 207 | timeout=600) |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 208 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 209 | # First we gather the urls/paths for the update payloads. |
Vadim Bendebury | d369839 | 2012-12-27 18:21:32 -0800 | [diff] [blame] | 210 | full_url, nton_url, mton_url, fw_url = ParsePayloadList( |
| 211 | archive_url, uploaded_list) |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 212 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 213 | full_payload = os.path.join(build_dir, build_artifact.ROOT_UPDATE) |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 214 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 215 | artifacts = [] |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 216 | artifacts.append(build_artifact.BuildArtifact( |
| 217 | full_url, main_staging_dir, full_payload, synchronous=True)) |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 218 | |
| 219 | if nton_url: |
| 220 | nton_payload = os.path.join(build_dir, AU_BASE, build + NTON_DIR_SUFFIX, |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 221 | build_artifact.ROOT_UPDATE) |
| 222 | artifacts.append(build_artifact.AUTestPayloadBuildArtifact( |
| 223 | nton_url, main_staging_dir, nton_payload)) |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 224 | |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 225 | if mton_url: |
| 226 | mton_payload = os.path.join(build_dir, AU_BASE, build + MTON_DIR_SUFFIX, |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 227 | build_artifact.ROOT_UPDATE) |
| 228 | artifacts.append(build_artifact.AUTestPayloadBuildArtifact( |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 229 | mton_url, main_staging_dir, mton_payload)) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 230 | |
Vadim Bendebury | d369839 | 2012-12-27 18:21:32 -0800 | [diff] [blame] | 231 | if fw_url: |
| 232 | artifacts.append(build_artifact.BuildArtifact( |
| 233 | fw_url, main_staging_dir, build_dir)) |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 234 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 235 | # Gather information about autotest tarballs. Use autotest.tar if available. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 236 | if build_artifact.AUTOTEST_PACKAGE in uploaded_list: |
| 237 | autotest_url = '%s/%s' % (archive_url, build_artifact.AUTOTEST_PACKAGE) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 238 | else: |
| 239 | # Use autotest.tar.bz for backward compatibility. This can be |
| 240 | # removed once all branches start using "autotest.tar" |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 241 | autotest_url = '%s/%s' % ( |
| 242 | archive_url, build_artifact.AUTOTEST_ZIPPED_PACKAGE) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 243 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 244 | # Next we gather the miscellaneous payloads. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 245 | stateful_url = archive_url + '/' + build_artifact.STATEFUL_UPDATE |
| 246 | test_suites_url = (archive_url + '/' + build_artifact.TEST_SUITES_PACKAGE) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 247 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 248 | stateful_payload = os.path.join(build_dir, build_artifact.STATEFUL_UPDATE) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 249 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 250 | artifacts.append(build_artifact.BuildArtifact( |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 251 | stateful_url, main_staging_dir, stateful_payload, synchronous=True)) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 252 | artifacts.append(build_artifact.AutotestTarballBuildArtifact( |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 253 | autotest_url, main_staging_dir, build_dir)) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 254 | artifacts.append(build_artifact.TarballBuildArtifact( |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 255 | test_suites_url, main_staging_dir, build_dir, synchronous=True)) |
| 256 | return artifacts |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 257 | |
| 258 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 259 | def GatherSymbolArtifactDownloads(temp_download_dir, archive_url, staging_dir, |
| 260 | timeout=600, delay=10): |
| 261 | """Generates debug symbol artifacts that we mean to download and stage. |
| 262 | |
| 263 | This method generates the list of artifacts we will need to |
| 264 | symbolicate crash dumps that occur during autotest runs. These |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 265 | artifacts are instances of build_artifact.BuildArtifact. |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 266 | |
| 267 | This will poll google storage until the debug symbol artifact becomes |
| 268 | available, or until the 10 minute timeout is up. |
| 269 | |
| 270 | @param temp_download_dir: the tempdir into which we're downloading artifacts |
| 271 | prior to staging them. |
| 272 | @param archive_url: the google storage url of the bucket where the debug |
| 273 | symbols for the desired build are stored. |
| 274 | @param staging_dir: the dir into which to stage the symbols |
| 275 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 276 | @return an iterable of one DebugTarballBuildArtifact pointing to the right |
| 277 | debug symbols. This is an iterable so that it's similar to |
| 278 | GatherArtifactDownloads. Also, it's possible that someday we might |
| 279 | have more than one. |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 280 | """ |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 281 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 282 | artifact_name = build_artifact.DEBUG_SYMBOLS |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 283 | WaitUntilAvailable([artifact_name], archive_url, 'debug symbols', |
| 284 | timeout=timeout, delay=delay) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 285 | artifact = build_artifact.DebugTarballBuildArtifact( |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 286 | archive_url + '/' + artifact_name, |
| 287 | temp_download_dir, |
| 288 | staging_dir) |
| 289 | return [artifact] |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 290 | |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 291 | |
| 292 | def GatherImageArchiveArtifactDownloads(temp_download_dir, archive_url, |
| 293 | staging_dir, image_file_list, |
| 294 | timeout=600, delay=10): |
| 295 | """Generates image archive artifact(s) for downloading / staging. |
| 296 | |
| 297 | Generates the list of artifacts that are used for extracting Chrome OS images |
| 298 | from. Currently, it returns a single artifact, which is a zipfile configured |
| 299 | to extract a given list of images. It first polls Google Storage unti lthe |
| 300 | desired artifacts become available (or a timeout expires). |
| 301 | |
| 302 | Args: |
| 303 | temp_download_dir: temporary directory, used for downloading artifacts |
| 304 | archive_url: URI to the bucket where the artifacts are stored |
| 305 | staging_dir: directory into which to stage the extracted files |
| 306 | image_file_list: list of image files to be extracted |
| 307 | Returns: |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 308 | list of downloadable artifacts (of type ZipfileBuildArtifact), currently |
| 309 | containing a single obejct |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 310 | """ |
| 311 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 312 | artifact_name = build_artifact.IMAGE_ARCHIVE |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 313 | WaitUntilAvailable([artifact_name], archive_url, 'image archive', |
| 314 | timeout=timeout, delay=delay) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 315 | artifact = build_artifact.ZipfileBuildArtifact( |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 316 | archive_url + '/' + artifact_name, |
| 317 | temp_download_dir, staging_dir, |
| 318 | unzip_file_list=image_file_list) |
| 319 | return [artifact] |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 320 | |
| 321 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 322 | def PrepareBuildDirectory(build_dir): |
| 323 | """Preliminary staging of installation directory for build. |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 324 | |
| 325 | Args: |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 326 | build_dir: Directory to install build components into. |
| 327 | """ |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 328 | if not os.path.isdir(build_dir): |
| 329 | os.path.makedirs(build_dir) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 330 | |
| 331 | # Create blank chromiumos_test_image.bin. Otherwise the Dev Server will |
| 332 | # try to rebuild it unnecessarily. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 333 | test_image = os.path.join(build_dir, build_artifact.TEST_IMAGE) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 334 | open(test_image, 'a').close() |
| 335 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 336 | |
| 337 | def SafeSandboxAccess(static_dir, path): |
| 338 | """Verify that the path is in static_dir. |
| 339 | |
| 340 | Args: |
| 341 | static_dir: Directory where builds are served from. |
| 342 | path: Path to verify. |
| 343 | |
| 344 | Returns: |
| 345 | True if path is in static_dir, False otherwise |
| 346 | """ |
| 347 | static_dir = os.path.realpath(static_dir) |
| 348 | path = os.path.realpath(path) |
| 349 | return (path.startswith(static_dir) and path != static_dir) |
| 350 | |
| 351 | |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 352 | def AcquireLock(static_dir, tag, create_once=True): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 353 | """Acquires a lock for a given tag. |
| 354 | |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 355 | Creates a directory for the specified tag, and atomically creates a lock file |
| 356 | in it. This tells other components the resource/task represented by the tag |
| 357 | is unavailable. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 358 | |
| 359 | Args: |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 360 | static_dir: Directory where builds are served from. |
| 361 | tag: Unique resource/task identifier. Use '/' for nested tags. |
| 362 | create_once: Determines whether the directory must be freshly created; this |
| 363 | preserves previous semantics of the lock acquisition. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 364 | |
| 365 | Returns: |
| 366 | Path to the created directory or None if creation failed. |
| 367 | |
| 368 | Raises: |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 369 | CommonUtilError: If lock can't be acquired. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 370 | """ |
| 371 | build_dir = os.path.join(static_dir, tag) |
| 372 | if not SafeSandboxAccess(static_dir, build_dir): |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 373 | raise CommonUtilError('Invalid tag "%s".' % tag) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 374 | |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 375 | # Create the directory. |
| 376 | is_created = False |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 377 | try: |
| 378 | os.makedirs(build_dir) |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 379 | is_created = True |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 380 | except OSError, e: |
| 381 | if e.errno == errno.EEXIST: |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 382 | if create_once: |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 383 | raise CommonUtilError(str(e)) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 384 | else: |
| 385 | raise |
| 386 | |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 387 | # Lock the directory. |
| 388 | try: |
| 389 | lock = lockfile.FileLock(os.path.join(build_dir, DEVSERVER_LOCK_FILE)) |
| 390 | lock.acquire(timeout=0) |
| 391 | except lockfile.AlreadyLocked, e: |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 392 | raise CommonUtilError(str(e)) |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 393 | except: |
| 394 | # In any other case, remove the directory if we actually created it, so |
| 395 | # that subsequent attempts won't fail to re-create it. |
| 396 | if is_created: |
| 397 | shutil.rmtree(build_dir) |
| 398 | raise |
| 399 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 400 | return build_dir |
| 401 | |
| 402 | |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 403 | def ReleaseLock(static_dir, tag, destroy=False): |
| 404 | """Releases the lock for a given tag. |
| 405 | |
| 406 | Optionally, removes the locked directory entirely. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 407 | |
| 408 | Args: |
| 409 | static_dir: Directory where builds are served from. |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 410 | tag: Unique resource/task identifier. Use '/' for nested tags. |
| 411 | destroy: Determines whether the locked directory should be removed |
| 412 | entirely. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 413 | |
| 414 | Raises: |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 415 | CommonUtilError: If lock can't be released. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 416 | """ |
| 417 | build_dir = os.path.join(static_dir, tag) |
| 418 | if not SafeSandboxAccess(static_dir, build_dir): |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 419 | raise CommonUtilError('Invalid tag "%s".' % tag) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 420 | |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 421 | lock = lockfile.FileLock(os.path.join(build_dir, DEVSERVER_LOCK_FILE)) |
Gilad Arnold | ea162aa | 2012-09-24 16:57:59 -0700 | [diff] [blame] | 422 | try: |
| 423 | lock.break_lock() |
| 424 | if destroy: |
| 425 | shutil.rmtree(build_dir) |
| 426 | except Exception, e: |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 427 | raise CommonUtilError(str(e)) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 428 | |
| 429 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 430 | def GetLatestBuildVersion(static_dir, target, milestone=None): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 431 | """Retrieves the latest build version for a given board. |
| 432 | |
| 433 | Args: |
| 434 | static_dir: Directory where builds are served from. |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 435 | target: The build target, typically a combination of the board and the |
| 436 | type of build e.g. x86-mario-release. |
| 437 | milestone: For latest build set to None, for builds only in a specific |
| 438 | milestone set to a str of format Rxx (e.g. R16). Default: None. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 439 | |
| 440 | Returns: |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 441 | If latest found, a full build string is returned e.g. R17-1234.0.0-a1-b983. |
| 442 | If no latest is found for some reason or another a '' string is returned. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 443 | |
| 444 | Raises: |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 445 | CommonUtilError: If for some reason the latest build cannot be |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 446 | deteremined, this could be due to the dir not existing or no builds |
| 447 | being present after filtering on milestone. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 448 | """ |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 449 | target_path = os.path.join(static_dir, target) |
| 450 | if not os.path.isdir(target_path): |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 451 | raise CommonUtilError('Cannot find path %s' % target_path) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 452 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 453 | builds = [distutils.version.LooseVersion(build) for build in |
| 454 | os.listdir(target_path)] |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 455 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 456 | if milestone and builds: |
| 457 | # Check if milestone Rxx is in the string representation of the build. |
| 458 | builds = filter(lambda x: milestone.upper() in str(x), builds) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 459 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 460 | if not builds: |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 461 | raise CommonUtilError('Could not determine build for %s' % target) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 462 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 463 | return str(max(builds)) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 464 | |
| 465 | |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 466 | def GetControlFile(static_dir, build, control_path): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 467 | """Attempts to pull the requested control file from the Dev Server. |
| 468 | |
| 469 | Args: |
| 470 | static_dir: Directory where builds are served from. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 471 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 472 | control_path: Path to control file on Dev Server relative to Autotest root. |
| 473 | |
| 474 | Raises: |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 475 | CommonUtilError: If lock can't be acquired. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 476 | |
| 477 | Returns: |
| 478 | Content of the requested control file. |
| 479 | """ |
Scott Zawalski | 1572d15 | 2012-01-16 14:36:02 -0500 | [diff] [blame] | 480 | # Be forgiving if the user passes in the control_path with a leading / |
| 481 | control_path = control_path.lstrip('/') |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 482 | control_path = os.path.join(static_dir, build, 'autotest', |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 483 | control_path) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 484 | if not SafeSandboxAccess(static_dir, control_path): |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 485 | raise CommonUtilError('Invalid control file "%s".' % control_path) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 486 | |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 487 | if not os.path.exists(control_path): |
| 488 | # TODO(scottz): Come up with some sort of error mechanism. |
| 489 | # crosbug.com/25040 |
| 490 | return 'Unknown control path %s' % control_path |
| 491 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 492 | with open(control_path, 'r') as control_file: |
| 493 | return control_file.read() |
| 494 | |
| 495 | |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 496 | def GetControlFileList(static_dir, build): |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 497 | """List all control|control. files in the specified board/build path. |
| 498 | |
| 499 | Args: |
| 500 | static_dir: Directory where builds are served from. |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 501 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 502 | |
| 503 | Raises: |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 504 | CommonUtilError: If path is outside of sandbox. |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 505 | |
| 506 | Returns: |
| 507 | String of each file separated by a newline. |
| 508 | """ |
Scott Zawalski | 1572d15 | 2012-01-16 14:36:02 -0500 | [diff] [blame] | 509 | autotest_dir = os.path.join(static_dir, build, 'autotest/') |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 510 | if not SafeSandboxAccess(static_dir, autotest_dir): |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 511 | raise CommonUtilError('Autotest dir not in sandbox "%s".' % autotest_dir) |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 512 | |
| 513 | control_files = set() |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 514 | if not os.path.exists(autotest_dir): |
| 515 | # TODO(scottz): Come up with some sort of error mechanism. |
| 516 | # crosbug.com/25040 |
| 517 | return 'Unknown build path %s' % autotest_dir |
| 518 | |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 519 | for entry in os.walk(autotest_dir): |
| 520 | dir_path, _, files = entry |
| 521 | for file_entry in files: |
| 522 | if file_entry.startswith('control.') or file_entry == 'control': |
| 523 | control_files.add(os.path.join(dir_path, |
Chris Sosa | ea148d9 | 2012-03-06 16:22:04 -0800 | [diff] [blame] | 524 | file_entry).replace(autotest_dir, '')) |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 525 | |
| 526 | return '\n'.join(control_files) |
| 527 | |
| 528 | |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 529 | def GetFileSize(file_path): |
| 530 | """Returns the size in bytes of the file given.""" |
| 531 | return os.path.getsize(file_path) |
| 532 | |
| 533 | |
| 534 | def GetFileHashes(file_path, do_sha1=False, do_sha256=False, do_md5=False): |
| 535 | """Computes and returns a list of requested hashes. |
| 536 | |
| 537 | Args: |
| 538 | file_path: path to file to be hashed |
| 539 | do_sha1: whether or not to compute a SHA1 hash |
| 540 | do_sha256: whether or not to compute a SHA256 hash |
| 541 | do_md5: whether or not to compute a MD5 hash |
| 542 | Returns: |
| 543 | A dictionary containing binary hash values, keyed by 'sha1', 'sha256' and |
| 544 | 'md5', respectively. |
| 545 | """ |
| 546 | hashes = {} |
| 547 | if (do_sha1 or do_sha256 or do_md5): |
| 548 | # Initialize hashers. |
| 549 | hasher_sha1 = hashlib.sha1() if do_sha1 else None |
| 550 | hasher_sha256 = hashlib.sha256() if do_sha256 else None |
| 551 | hasher_md5 = hashlib.md5() if do_md5 else None |
| 552 | |
| 553 | # Read blocks from file, update hashes. |
| 554 | with open(file_path, 'rb') as fd: |
| 555 | while True: |
| 556 | block = fd.read(_HASH_BLOCK_SIZE) |
| 557 | if not block: |
| 558 | break |
| 559 | hasher_sha1 and hasher_sha1.update(block) |
| 560 | hasher_sha256 and hasher_sha256.update(block) |
| 561 | hasher_md5 and hasher_md5.update(block) |
| 562 | |
| 563 | # Update return values. |
| 564 | if hasher_sha1: |
| 565 | hashes['sha1'] = hasher_sha1.digest() |
| 566 | if hasher_sha256: |
| 567 | hashes['sha256'] = hasher_sha256.digest() |
| 568 | if hasher_md5: |
| 569 | hashes['md5'] = hasher_md5.digest() |
| 570 | |
| 571 | return hashes |
| 572 | |
| 573 | |
| 574 | def GetFileSha1(file_path): |
| 575 | """Returns the SHA1 checksum of the file given (base64 encoded).""" |
| 576 | return base64.b64encode(GetFileHashes(file_path, do_sha1=True)['sha1']) |
| 577 | |
| 578 | |
| 579 | def GetFileSha256(file_path): |
| 580 | """Returns the SHA256 checksum of the file given (base64 encoded).""" |
| 581 | return base64.b64encode(GetFileHashes(file_path, do_sha256=True)['sha256']) |
| 582 | |
| 583 | |
| 584 | def GetFileMd5(file_path): |
| 585 | """Returns the MD5 checksum of the file given (hex encoded).""" |
| 586 | return binascii.hexlify(GetFileHashes(file_path, do_md5=True)['md5']) |
| 587 | |
| 588 | |
| 589 | def CopyFile(source, dest): |
| 590 | """Copies a file from |source| to |dest|.""" |
| 591 | _Log('Copy File %s -> %s' % (source, dest)) |
| 592 | shutil.copy(source, dest) |