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