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