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 |
| 13 | import shutil |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 14 | import threading |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 15 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 16 | import log_util |
| 17 | |
| 18 | |
| 19 | # Module-local log function. |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 20 | def _Log(message, *args): |
| 21 | return log_util.LogWithTag('UTIL', message, *args) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 22 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 23 | |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 24 | _HASH_BLOCK_SIZE = 8192 |
| 25 | |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 26 | |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 27 | class CommonUtilError(Exception): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 28 | """Exception classes used by this module.""" |
| 29 | pass |
| 30 | |
| 31 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 32 | def MkDirP(directory): |
| 33 | """Thread-safely create a directory like mkdir -p.""" |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 34 | try: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 35 | os.makedirs(directory) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 36 | except OSError, e: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 37 | if not (e.errno == errno.EEXIST and os.path.isdir(directory)): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 38 | raise |
| 39 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 40 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 41 | def GetLatestBuildVersion(static_dir, target, milestone=None): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 42 | """Retrieves the latest build version for a given board. |
| 43 | |
| 44 | Args: |
| 45 | static_dir: Directory where builds are served from. |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 46 | target: The build target, typically a combination of the board and the |
| 47 | type of build e.g. x86-mario-release. |
| 48 | milestone: For latest build set to None, for builds only in a specific |
| 49 | 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] | 50 | |
| 51 | Returns: |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 52 | If latest found, a full build string is returned e.g. R17-1234.0.0-a1-b983. |
| 53 | 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] | 54 | |
| 55 | Raises: |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 56 | CommonUtilError: If for some reason the latest build cannot be |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 57 | deteremined, this could be due to the dir not existing or no builds |
| 58 | being present after filtering on milestone. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 59 | """ |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 60 | target_path = os.path.join(static_dir, target) |
| 61 | if not os.path.isdir(target_path): |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 62 | raise CommonUtilError('Cannot find path %s' % target_path) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 63 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 64 | builds = [distutils.version.LooseVersion(build) for build in |
| 65 | os.listdir(target_path)] |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 66 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 67 | if milestone and builds: |
| 68 | # Check if milestone Rxx is in the string representation of the build. |
| 69 | builds = filter(lambda x: milestone.upper() in str(x), builds) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 70 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 71 | if not builds: |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 72 | raise CommonUtilError('Could not determine build for %s' % target) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 73 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 74 | return str(max(builds)) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 75 | |
| 76 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 77 | def PathInDir(directory, path): |
| 78 | """Returns True if the path is in directory. |
| 79 | |
| 80 | Args: |
| 81 | directory: Directory where the path should be in. |
| 82 | path: Path to check. |
| 83 | |
| 84 | Returns: |
| 85 | True if path is in static_dir, False otherwise |
| 86 | """ |
| 87 | directory = os.path.realpath(directory) |
| 88 | path = os.path.realpath(path) |
| 89 | return (path.startswith(directory) and len(path) != len(directory)) |
| 90 | |
| 91 | |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 92 | def GetControlFile(static_dir, build, control_path): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 93 | """Attempts to pull the requested control file from the Dev Server. |
| 94 | |
| 95 | Args: |
| 96 | static_dir: Directory where builds are served from. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 97 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 98 | control_path: Path to control file on Dev Server relative to Autotest root. |
| 99 | |
| 100 | Raises: |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 101 | CommonUtilError: If lock can't be acquired. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 102 | |
| 103 | Returns: |
| 104 | Content of the requested control file. |
| 105 | """ |
Scott Zawalski | 1572d15 | 2012-01-16 14:36:02 -0500 | [diff] [blame] | 106 | # Be forgiving if the user passes in the control_path with a leading / |
| 107 | control_path = control_path.lstrip('/') |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 108 | control_path = os.path.join(static_dir, build, 'autotest', |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 109 | control_path) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 110 | if not PathInDir(static_dir, control_path): |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 111 | raise CommonUtilError('Invalid control file "%s".' % control_path) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 112 | |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 113 | if not os.path.exists(control_path): |
| 114 | # TODO(scottz): Come up with some sort of error mechanism. |
| 115 | # crosbug.com/25040 |
| 116 | return 'Unknown control path %s' % control_path |
| 117 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 118 | with open(control_path, 'r') as control_file: |
| 119 | return control_file.read() |
| 120 | |
| 121 | |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 122 | def GetControlFileList(static_dir, build): |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 123 | """List all control|control. files in the specified board/build path. |
| 124 | |
| 125 | Args: |
| 126 | static_dir: Directory where builds are served from. |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 127 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 128 | |
| 129 | Raises: |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 130 | CommonUtilError: If path is outside of sandbox. |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 131 | |
| 132 | Returns: |
| 133 | String of each file separated by a newline. |
| 134 | """ |
Scott Zawalski | 1572d15 | 2012-01-16 14:36:02 -0500 | [diff] [blame] | 135 | autotest_dir = os.path.join(static_dir, build, 'autotest/') |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 136 | if not PathInDir(static_dir, autotest_dir): |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 137 | raise CommonUtilError('Autotest dir not in sandbox "%s".' % autotest_dir) |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 138 | |
| 139 | control_files = set() |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 140 | if not os.path.exists(autotest_dir): |
| 141 | # TODO(scottz): Come up with some sort of error mechanism. |
| 142 | # crosbug.com/25040 |
| 143 | return 'Unknown build path %s' % autotest_dir |
| 144 | |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 145 | for entry in os.walk(autotest_dir): |
| 146 | dir_path, _, files = entry |
| 147 | for file_entry in files: |
| 148 | if file_entry.startswith('control.') or file_entry == 'control': |
| 149 | control_files.add(os.path.join(dir_path, |
Chris Sosa | ea148d9 | 2012-03-06 16:22:04 -0800 | [diff] [blame] | 150 | file_entry).replace(autotest_dir, '')) |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 151 | |
| 152 | return '\n'.join(control_files) |
| 153 | |
| 154 | |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 155 | def GetFileSize(file_path): |
| 156 | """Returns the size in bytes of the file given.""" |
| 157 | return os.path.getsize(file_path) |
| 158 | |
| 159 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 160 | # Hashlib is strange and doesn't actually define these in a sane way that |
| 161 | # pylint can find them. Disable checks for them. |
| 162 | # pylint: disable=E1101,W0106 |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 163 | def GetFileHashes(file_path, do_sha1=False, do_sha256=False, do_md5=False): |
| 164 | """Computes and returns a list of requested hashes. |
| 165 | |
| 166 | Args: |
| 167 | file_path: path to file to be hashed |
| 168 | do_sha1: whether or not to compute a SHA1 hash |
| 169 | do_sha256: whether or not to compute a SHA256 hash |
| 170 | do_md5: whether or not to compute a MD5 hash |
| 171 | Returns: |
| 172 | A dictionary containing binary hash values, keyed by 'sha1', 'sha256' and |
| 173 | 'md5', respectively. |
| 174 | """ |
| 175 | hashes = {} |
| 176 | if (do_sha1 or do_sha256 or do_md5): |
| 177 | # Initialize hashers. |
| 178 | hasher_sha1 = hashlib.sha1() if do_sha1 else None |
| 179 | hasher_sha256 = hashlib.sha256() if do_sha256 else None |
| 180 | hasher_md5 = hashlib.md5() if do_md5 else None |
| 181 | |
| 182 | # Read blocks from file, update hashes. |
| 183 | with open(file_path, 'rb') as fd: |
| 184 | while True: |
| 185 | block = fd.read(_HASH_BLOCK_SIZE) |
| 186 | if not block: |
| 187 | break |
| 188 | hasher_sha1 and hasher_sha1.update(block) |
| 189 | hasher_sha256 and hasher_sha256.update(block) |
| 190 | hasher_md5 and hasher_md5.update(block) |
| 191 | |
| 192 | # Update return values. |
| 193 | if hasher_sha1: |
| 194 | hashes['sha1'] = hasher_sha1.digest() |
| 195 | if hasher_sha256: |
| 196 | hashes['sha256'] = hasher_sha256.digest() |
| 197 | if hasher_md5: |
| 198 | hashes['md5'] = hasher_md5.digest() |
| 199 | |
| 200 | return hashes |
| 201 | |
| 202 | |
| 203 | def GetFileSha1(file_path): |
| 204 | """Returns the SHA1 checksum of the file given (base64 encoded).""" |
| 205 | return base64.b64encode(GetFileHashes(file_path, do_sha1=True)['sha1']) |
| 206 | |
| 207 | |
| 208 | def GetFileSha256(file_path): |
| 209 | """Returns the SHA256 checksum of the file given (base64 encoded).""" |
| 210 | return base64.b64encode(GetFileHashes(file_path, do_sha256=True)['sha256']) |
| 211 | |
| 212 | |
| 213 | def GetFileMd5(file_path): |
| 214 | """Returns the MD5 checksum of the file given (hex encoded).""" |
| 215 | return binascii.hexlify(GetFileHashes(file_path, do_md5=True)['md5']) |
| 216 | |
| 217 | |
| 218 | def CopyFile(source, dest): |
| 219 | """Copies a file from |source| to |dest|.""" |
| 220 | _Log('Copy File %s -> %s' % (source, dest)) |
| 221 | shutil.copy(source, dest) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 222 | |
| 223 | |
| 224 | class LockDict(object): |
| 225 | """A dictionary of locks. |
| 226 | |
| 227 | This class provides a thread-safe store of threading.Lock objects, which can |
| 228 | be used to regulate access to any set of hashable resources. Usage: |
| 229 | |
| 230 | foo_lock_dict = LockDict() |
| 231 | ... |
| 232 | with foo_lock_dict.lock('bar'): |
| 233 | # Critical section for 'bar' |
| 234 | """ |
| 235 | def __init__(self): |
| 236 | self._lock = self._new_lock() |
| 237 | self._dict = {} |
| 238 | |
| 239 | @staticmethod |
| 240 | def _new_lock(): |
| 241 | return threading.Lock() |
| 242 | |
| 243 | def lock(self, key): |
| 244 | with self._lock: |
| 245 | lock = self._dict.get(key) |
| 246 | if not lock: |
| 247 | lock = self._new_lock() |
| 248 | self._dict[key] = lock |
| 249 | return lock |