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