maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 6 | """Reads a .isolated, creates a tree of hardlinks and runs the test. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 7 | |
| 8 | Keeps a local cache. |
| 9 | """ |
| 10 | |
maruel@chromium.org | b7e79a2 | 2013-09-13 01:24:56 +0000 | [diff] [blame] | 11 | __version__ = '0.2' |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 12 | |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 13 | import ctypes |
| 14 | import hashlib |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 15 | import logging |
| 16 | import optparse |
| 17 | import os |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 18 | import re |
| 19 | import shutil |
| 20 | import stat |
| 21 | import subprocess |
| 22 | import sys |
| 23 | import tempfile |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 24 | import time |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 25 | |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 26 | from third_party.depot_tools import fix_encoding |
| 27 | |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 28 | from utils import lru |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 29 | from utils import tools |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 30 | from utils import zip_package |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 31 | |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 32 | import isolateserver |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 33 | |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 34 | |
vadimsh@chromium.org | 8507106 | 2013-08-21 23:37:45 +0000 | [diff] [blame] | 35 | # Absolute path to this file (can be None if running from zip on Mac). |
| 36 | THIS_FILE_PATH = os.path.abspath(__file__) if __file__ else None |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 37 | |
| 38 | # Directory that contains this file (might be inside zip package). |
vadimsh@chromium.org | 8507106 | 2013-08-21 23:37:45 +0000 | [diff] [blame] | 39 | BASE_DIR = os.path.dirname(THIS_FILE_PATH) if __file__ else None |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 40 | |
| 41 | # Directory that contains currently running script file. |
| 42 | MAIN_DIR = os.path.dirname(os.path.abspath(zip_package.get_main_script_path())) |
| 43 | |
maruel@chromium.org | 6b365dc | 2012-10-18 19:17:56 +0000 | [diff] [blame] | 44 | # Types of action accepted by link_file(). |
maruel@chromium.org | ba6489b | 2013-07-11 20:23:33 +0000 | [diff] [blame] | 45 | HARDLINK, HARDLINK_WITH_FALLBACK, SYMLINK, COPY = range(1, 5) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 46 | |
csharp@chromium.org | ff2a466 | 2012-11-21 20:49:32 +0000 | [diff] [blame] | 47 | # The name of the log file to use. |
| 48 | RUN_ISOLATED_LOG_FILE = 'run_isolated.log' |
| 49 | |
csharp@chromium.org | e217f30 | 2012-11-22 16:51:53 +0000 | [diff] [blame] | 50 | # The name of the log to use for the run_test_cases.py command |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 51 | RUN_TEST_CASES_LOG = 'run_test_cases.log' |
csharp@chromium.org | e217f30 | 2012-11-22 16:51:53 +0000 | [diff] [blame] | 52 | |
vadimsh@chromium.org | 5db0f4f | 2013-07-04 13:57:02 +0000 | [diff] [blame] | 53 | # Maximum expected delay (in seconds) between successive file fetches |
| 54 | # in run_tha_test. If it takes longer than that, a deadlock might be happening |
| 55 | # and all stack frames for all threads are dumped to log. |
| 56 | DEADLOCK_TIMEOUT = 5 * 60 |
| 57 | |
vadimsh@chromium.org | 87d6326 | 2013-04-04 19:34:21 +0000 | [diff] [blame] | 58 | |
maruel@chromium.org | 9e9ceaa | 2013-04-05 15:42:42 +0000 | [diff] [blame] | 59 | # Used by get_flavor(). |
| 60 | FLAVOR_MAPPING = { |
| 61 | 'cygwin': 'win', |
| 62 | 'win32': 'win', |
| 63 | 'darwin': 'mac', |
| 64 | 'sunos5': 'solaris', |
| 65 | 'freebsd7': 'freebsd', |
| 66 | 'freebsd8': 'freebsd', |
| 67 | } |
| 68 | |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 69 | |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 70 | def get_as_zip_package(executable=True): |
| 71 | """Returns ZipPackage with this module and all its dependencies. |
| 72 | |
| 73 | If |executable| is True will store run_isolated.py as __main__.py so that |
| 74 | zip package is directly executable be python. |
| 75 | """ |
| 76 | # Building a zip package when running from another zip package is |
| 77 | # unsupported and probably unneeded. |
| 78 | assert not zip_package.is_zipped_module(sys.modules[__name__]) |
vadimsh@chromium.org | 8507106 | 2013-08-21 23:37:45 +0000 | [diff] [blame] | 79 | assert THIS_FILE_PATH |
| 80 | assert BASE_DIR |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 81 | package = zip_package.ZipPackage(root=BASE_DIR) |
| 82 | package.add_python_file(THIS_FILE_PATH, '__main__.py' if executable else None) |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 83 | package.add_python_file(os.path.join(BASE_DIR, 'isolateserver.py')) |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 84 | package.add_directory(os.path.join(BASE_DIR, 'third_party')) |
| 85 | package.add_directory(os.path.join(BASE_DIR, 'utils')) |
| 86 | return package |
| 87 | |
| 88 | |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 89 | def get_flavor(): |
| 90 | """Returns the system default flavor. Copied from gyp/pylib/gyp/common.py.""" |
maruel@chromium.org | 9e9ceaa | 2013-04-05 15:42:42 +0000 | [diff] [blame] | 91 | return FLAVOR_MAPPING.get(sys.platform, 'linux') |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 92 | |
| 93 | |
| 94 | def os_link(source, link_name): |
| 95 | """Add support for os.link() on Windows.""" |
| 96 | if sys.platform == 'win32': |
| 97 | if not ctypes.windll.kernel32.CreateHardLinkW( |
| 98 | unicode(link_name), unicode(source), 0): |
| 99 | raise OSError() |
| 100 | else: |
| 101 | os.link(source, link_name) |
| 102 | |
| 103 | |
| 104 | def readable_copy(outfile, infile): |
| 105 | """Makes a copy of the file that is readable by everyone.""" |
csharp@chromium.org | 59d116d | 2013-07-05 18:04:08 +0000 | [diff] [blame] | 106 | shutil.copy2(infile, outfile) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 107 | read_enabled_mode = (os.stat(outfile).st_mode | stat.S_IRUSR | |
| 108 | stat.S_IRGRP | stat.S_IROTH) |
| 109 | os.chmod(outfile, read_enabled_mode) |
| 110 | |
| 111 | |
| 112 | def link_file(outfile, infile, action): |
| 113 | """Links a file. The type of link depends on |action|.""" |
| 114 | logging.debug('Mapping %s to %s' % (infile, outfile)) |
maruel@chromium.org | ba6489b | 2013-07-11 20:23:33 +0000 | [diff] [blame] | 115 | if action not in (HARDLINK, HARDLINK_WITH_FALLBACK, SYMLINK, COPY): |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 116 | raise ValueError('Unknown mapping action %s' % action) |
| 117 | if not os.path.isfile(infile): |
maruel@chromium.org | 9958e4a | 2013-09-17 00:01:48 +0000 | [diff] [blame] | 118 | raise isolateserver.MappingError('%s is missing' % infile) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 119 | if os.path.isfile(outfile): |
maruel@chromium.org | 9958e4a | 2013-09-17 00:01:48 +0000 | [diff] [blame] | 120 | raise isolateserver.MappingError( |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 121 | '%s already exist; insize:%d; outsize:%d' % |
| 122 | (outfile, os.stat(infile).st_size, os.stat(outfile).st_size)) |
| 123 | |
| 124 | if action == COPY: |
| 125 | readable_copy(outfile, infile) |
| 126 | elif action == SYMLINK and sys.platform != 'win32': |
| 127 | # On windows, symlink are converted to hardlink and fails over to copy. |
maruel@chromium.org | f43e68b | 2012-10-15 20:23:10 +0000 | [diff] [blame] | 128 | os.symlink(infile, outfile) # pylint: disable=E1101 |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 129 | else: |
| 130 | try: |
| 131 | os_link(infile, outfile) |
maruel@chromium.org | ba6489b | 2013-07-11 20:23:33 +0000 | [diff] [blame] | 132 | except OSError as e: |
| 133 | if action == HARDLINK: |
maruel@chromium.org | 9958e4a | 2013-09-17 00:01:48 +0000 | [diff] [blame] | 134 | raise isolateserver.MappingError( |
maruel@chromium.org | ba6489b | 2013-07-11 20:23:33 +0000 | [diff] [blame] | 135 | 'Failed to hardlink %s to %s: %s' % (infile, outfile, e)) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 136 | # Probably a different file system. |
maruel@chromium.org | 9e98e43 | 2013-05-31 17:06:51 +0000 | [diff] [blame] | 137 | logging.warning( |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 138 | 'Failed to hardlink, failing back to copy %s to %s' % ( |
| 139 | infile, outfile)) |
| 140 | readable_copy(outfile, infile) |
| 141 | |
| 142 | |
| 143 | def _set_write_bit(path, read_only): |
| 144 | """Sets or resets the executable bit on a file or directory.""" |
| 145 | mode = os.lstat(path).st_mode |
| 146 | if read_only: |
| 147 | mode = mode & 0500 |
| 148 | else: |
| 149 | mode = mode | 0200 |
| 150 | if hasattr(os, 'lchmod'): |
| 151 | os.lchmod(path, mode) # pylint: disable=E1101 |
| 152 | else: |
| 153 | if stat.S_ISLNK(mode): |
| 154 | # Skip symlink without lchmod() support. |
| 155 | logging.debug('Can\'t change +w bit on symlink %s' % path) |
| 156 | return |
| 157 | |
| 158 | # TODO(maruel): Implement proper DACL modification on Windows. |
| 159 | os.chmod(path, mode) |
| 160 | |
| 161 | |
| 162 | def make_writable(root, read_only): |
| 163 | """Toggle the writable bit on a directory tree.""" |
csharp@chromium.org | 837352f | 2013-01-17 21:17:03 +0000 | [diff] [blame] | 164 | assert os.path.isabs(root), root |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 165 | for dirpath, dirnames, filenames in os.walk(root, topdown=True): |
| 166 | for filename in filenames: |
| 167 | _set_write_bit(os.path.join(dirpath, filename), read_only) |
| 168 | |
| 169 | for dirname in dirnames: |
| 170 | _set_write_bit(os.path.join(dirpath, dirname), read_only) |
| 171 | |
| 172 | |
| 173 | def rmtree(root): |
| 174 | """Wrapper around shutil.rmtree() to retry automatically on Windows.""" |
| 175 | make_writable(root, False) |
| 176 | if sys.platform == 'win32': |
| 177 | for i in range(3): |
| 178 | try: |
| 179 | shutil.rmtree(root) |
| 180 | break |
| 181 | except WindowsError: # pylint: disable=E0602 |
| 182 | delay = (i+1)*2 |
| 183 | print >> sys.stderr, ( |
| 184 | 'The test has subprocess outliving it. Sleep %d seconds.' % delay) |
| 185 | time.sleep(delay) |
| 186 | else: |
| 187 | shutil.rmtree(root) |
| 188 | |
| 189 | |
| 190 | def is_same_filesystem(path1, path2): |
| 191 | """Returns True if both paths are on the same filesystem. |
| 192 | |
| 193 | This is required to enable the use of hardlinks. |
| 194 | """ |
| 195 | assert os.path.isabs(path1), path1 |
| 196 | assert os.path.isabs(path2), path2 |
| 197 | if sys.platform == 'win32': |
| 198 | # If the drive letter mismatches, assume it's a separate partition. |
| 199 | # TODO(maruel): It should look at the underlying drive, a drive letter could |
| 200 | # be a mount point to a directory on another drive. |
| 201 | assert re.match(r'^[a-zA-Z]\:\\.*', path1), path1 |
| 202 | assert re.match(r'^[a-zA-Z]\:\\.*', path2), path2 |
| 203 | if path1[0].lower() != path2[0].lower(): |
| 204 | return False |
| 205 | return os.stat(path1).st_dev == os.stat(path2).st_dev |
| 206 | |
| 207 | |
| 208 | def get_free_space(path): |
| 209 | """Returns the number of free bytes.""" |
| 210 | if sys.platform == 'win32': |
| 211 | free_bytes = ctypes.c_ulonglong(0) |
| 212 | ctypes.windll.kernel32.GetDiskFreeSpaceExW( |
| 213 | ctypes.c_wchar_p(path), None, None, ctypes.pointer(free_bytes)) |
| 214 | return free_bytes.value |
maruel@chromium.org | f43e68b | 2012-10-15 20:23:10 +0000 | [diff] [blame] | 215 | # For OSes other than Windows. |
| 216 | f = os.statvfs(path) # pylint: disable=E1101 |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 217 | return f.f_bfree * f.f_frsize |
| 218 | |
| 219 | |
| 220 | def make_temp_dir(prefix, root_dir): |
| 221 | """Returns a temporary directory on the same file system as root_dir.""" |
| 222 | base_temp_dir = None |
| 223 | if not is_same_filesystem(root_dir, tempfile.gettempdir()): |
| 224 | base_temp_dir = os.path.dirname(root_dir) |
| 225 | return tempfile.mkdtemp(prefix=prefix, dir=base_temp_dir) |
| 226 | |
| 227 | |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 228 | class CachePolicies(object): |
| 229 | def __init__(self, max_cache_size, min_free_space, max_items): |
| 230 | """ |
| 231 | Arguments: |
| 232 | - max_cache_size: Trim if the cache gets larger than this value. If 0, the |
| 233 | cache is effectively a leak. |
| 234 | - min_free_space: Trim if disk free space becomes lower than this value. If |
| 235 | 0, it unconditionally fill the disk. |
| 236 | - max_items: Maximum number of items to keep in the cache. If 0, do not |
| 237 | enforce a limit. |
| 238 | """ |
| 239 | self.max_cache_size = max_cache_size |
| 240 | self.min_free_space = min_free_space |
| 241 | self.max_items = max_items |
| 242 | |
| 243 | |
maruel@chromium.org | e45728d | 2013-09-16 23:23:22 +0000 | [diff] [blame] | 244 | class DiskCache(object): |
| 245 | """Stateful LRU cache in a flat hash table in a directory. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 246 | |
| 247 | Saves its state as json file. |
| 248 | """ |
| 249 | STATE_FILE = 'state.json' |
| 250 | |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 251 | def __init__(self, cache_dir, retriever, policies, algo): |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 252 | """ |
| 253 | Arguments: |
| 254 | - cache_dir: Directory where to place the cache. |
maruel@chromium.org | 8750e4b | 2013-09-18 02:37:57 +0000 | [diff] [blame] | 255 | - retriever: API where to fetch items from. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 256 | - policies: cache retention policies. |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame] | 257 | - algo: hashing algorithm used. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 258 | """ |
| 259 | self.cache_dir = cache_dir |
maruel@chromium.org | 8750e4b | 2013-09-18 02:37:57 +0000 | [diff] [blame] | 260 | self.retriever = retriever |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 261 | self.policies = policies |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 262 | self._pool = None |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 263 | self.state_file = os.path.join(cache_dir, self.STATE_FILE) |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 264 | self.lru = lru.LRUDict() |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 265 | |
| 266 | # Items currently being fetched. Keep it local to reduce lock contention. |
| 267 | self._pending_queue = set() |
| 268 | |
| 269 | # Profiling values. |
| 270 | self._added = [] |
| 271 | self._removed = [] |
| 272 | self._free_disk = 0 |
| 273 | |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 274 | with tools.Profiler('Setup'): |
maruel@chromium.org | 770993b | 2012-12-11 17:16:48 +0000 | [diff] [blame] | 275 | if not os.path.isdir(self.cache_dir): |
| 276 | os.makedirs(self.cache_dir) |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 277 | |
| 278 | # Load state of the cache. |
vadimsh@chromium.org | a40428e | 2013-07-04 15:43:14 +0000 | [diff] [blame] | 279 | if os.path.isfile(self.state_file): |
| 280 | try: |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 281 | self.lru = lru.LRUDict.load(self.state_file) |
| 282 | except ValueError as err: |
| 283 | logging.error('Failed to load cache state: %s' % (err,)) |
| 284 | # Don't want to keep broken state file. |
| 285 | os.remove(self.state_file) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 286 | |
maruel@chromium.org | 770993b | 2012-12-11 17:16:48 +0000 | [diff] [blame] | 287 | # Ensure that all files listed in the state still exist and add new ones. |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 288 | previous = self.lru.keys_set() |
| 289 | unknown = [] |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 290 | for filename in os.listdir(self.cache_dir): |
| 291 | if filename == self.STATE_FILE: |
| 292 | continue |
| 293 | if filename in previous: |
| 294 | previous.remove(filename) |
| 295 | continue |
| 296 | # An untracked file. |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame] | 297 | if not isolateserver.is_valid_hash(filename, algo): |
maruel@chromium.org | 9e98e43 | 2013-05-31 17:06:51 +0000 | [diff] [blame] | 298 | logging.warning('Removing unknown file %s from cache', filename) |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 299 | os.remove(self._path(filename)) |
maruel@chromium.org | 770993b | 2012-12-11 17:16:48 +0000 | [diff] [blame] | 300 | continue |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 301 | # File that's not referenced in 'state.json'. |
| 302 | # TODO(vadimsh): Verify its SHA1 matches file name. |
| 303 | logging.warning('Adding unknown file %s to cache', filename) |
| 304 | unknown.append(filename) |
maruel@chromium.org | 770993b | 2012-12-11 17:16:48 +0000 | [diff] [blame] | 305 | |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 306 | if unknown: |
| 307 | # Add as oldest files. They will be deleted eventually if not accessed. |
| 308 | self._add_oldest_list(unknown) |
| 309 | logging.warning('Added back %d unknown files', len(unknown)) |
| 310 | |
maruel@chromium.org | 770993b | 2012-12-11 17:16:48 +0000 | [diff] [blame] | 311 | if previous: |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 312 | # Filter out entries that were not found. |
maruel@chromium.org | 9e98e43 | 2013-05-31 17:06:51 +0000 | [diff] [blame] | 313 | logging.warning('Removed %d lost files', len(previous)) |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 314 | for filename in previous: |
| 315 | self.lru.pop(filename) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 316 | self.trim() |
| 317 | |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 318 | def set_pool(self, pool): |
| 319 | """Sets an isolateserver.WorkerPool.""" |
| 320 | self._pool = pool |
| 321 | |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 322 | def __enter__(self): |
| 323 | return self |
| 324 | |
| 325 | def __exit__(self, _exc_type, _exec_value, _traceback): |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 326 | with tools.Profiler('CleanupTrimming'): |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 327 | self.trim() |
| 328 | |
| 329 | logging.info( |
maruel@chromium.org | 5fd6f47 | 2012-12-11 00:26:08 +0000 | [diff] [blame] | 330 | '%5d (%8dkb) added', len(self._added), sum(self._added) / 1024) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 331 | logging.info( |
maruel@chromium.org | 5fd6f47 | 2012-12-11 00:26:08 +0000 | [diff] [blame] | 332 | '%5d (%8dkb) current', |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 333 | len(self.lru), |
| 334 | sum(self.lru.itervalues()) / 1024) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 335 | logging.info( |
maruel@chromium.org | 5fd6f47 | 2012-12-11 00:26:08 +0000 | [diff] [blame] | 336 | '%5d (%8dkb) removed', len(self._removed), sum(self._removed) / 1024) |
| 337 | logging.info(' %8dkb free', self._free_disk / 1024) |
maruel@chromium.org | 4160164 | 2013-09-18 19:40:46 +0000 | [diff] [blame] | 338 | return False |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 339 | |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 340 | def trim(self): |
| 341 | """Trims anything we don't know, make sure enough free space exists.""" |
| 342 | # Ensure maximum cache size. |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 343 | if self.policies.max_cache_size: |
| 344 | total_size = sum(self.lru.itervalues()) |
| 345 | while total_size > self.policies.max_cache_size: |
maruel@chromium.org | e45728d | 2013-09-16 23:23:22 +0000 | [diff] [blame] | 346 | total_size -= self._remove_lru_file() |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 347 | |
| 348 | # Ensure maximum number of items in the cache. |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 349 | if self.policies.max_items and len(self.lru) > self.policies.max_items: |
| 350 | for _ in xrange(len(self.lru) - self.policies.max_items): |
maruel@chromium.org | e45728d | 2013-09-16 23:23:22 +0000 | [diff] [blame] | 351 | self._remove_lru_file() |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 352 | |
| 353 | # Ensure enough free space. |
| 354 | self._free_disk = get_free_space(self.cache_dir) |
maruel@chromium.org | 9e98e43 | 2013-05-31 17:06:51 +0000 | [diff] [blame] | 355 | trimmed_due_to_space = False |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 356 | while ( |
| 357 | self.policies.min_free_space and |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 358 | self.lru and |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 359 | self._free_disk < self.policies.min_free_space): |
maruel@chromium.org | 9e98e43 | 2013-05-31 17:06:51 +0000 | [diff] [blame] | 360 | trimmed_due_to_space = True |
maruel@chromium.org | e45728d | 2013-09-16 23:23:22 +0000 | [diff] [blame] | 361 | self._remove_lru_file() |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 362 | self._free_disk = get_free_space(self.cache_dir) |
maruel@chromium.org | 9e98e43 | 2013-05-31 17:06:51 +0000 | [diff] [blame] | 363 | if trimmed_due_to_space: |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 364 | total = sum(self.lru.itervalues()) |
maruel@chromium.org | 9e98e43 | 2013-05-31 17:06:51 +0000 | [diff] [blame] | 365 | logging.warning( |
| 366 | 'Trimmed due to not enough free disk space: %.1fkb free, %.1fkb ' |
| 367 | 'cache (%.1f%% of its maximum capacity)', |
| 368 | self._free_disk / 1024., |
| 369 | total / 1024., |
| 370 | 100. * self.policies.max_cache_size / float(total), |
| 371 | ) |
maruel@chromium.org | e45728d | 2013-09-16 23:23:22 +0000 | [diff] [blame] | 372 | self._save() |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 373 | |
csharp@chromium.org | 8dc5254 | 2012-11-08 20:29:55 +0000 | [diff] [blame] | 374 | def retrieve(self, priority, item, size): |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 375 | """Retrieves a file from the remote, if not already cached, and adds it to |
| 376 | the cache. |
csharp@chromium.org | 8dc5254 | 2012-11-08 20:29:55 +0000 | [diff] [blame] | 377 | |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 378 | If the file is in the cache, verify that the file is valid (i.e. it is |
csharp@chromium.org | 8dc5254 | 2012-11-08 20:29:55 +0000 | [diff] [blame] | 379 | the correct size), retrieving it again if it isn't. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 380 | """ |
| 381 | assert not '/' in item |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 382 | path = self._path(item) |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 383 | found = False |
csharp@chromium.org | 8dc5254 | 2012-11-08 20:29:55 +0000 | [diff] [blame] | 384 | |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 385 | if item in self.lru: |
maruel@chromium.org | e45728d | 2013-09-16 23:23:22 +0000 | [diff] [blame] | 386 | # Note that is doesn't compute the hash so it could still be corrupted. |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 387 | if not isolateserver.is_valid_file(self._path(item), size): |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 388 | self.lru.pop(item) |
| 389 | self._delete_file(item, size) |
csharp@chromium.org | 8dc5254 | 2012-11-08 20:29:55 +0000 | [diff] [blame] | 390 | else: |
csharp@chromium.org | 8dc5254 | 2012-11-08 20:29:55 +0000 | [diff] [blame] | 391 | # Was already in cache. Update it's LRU value by putting it at the end. |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 392 | self.lru.touch(item) |
| 393 | found = True |
csharp@chromium.org | 8dc5254 | 2012-11-08 20:29:55 +0000 | [diff] [blame] | 394 | |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 395 | if not found: |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 396 | if item in self._pending_queue: |
| 397 | # Already pending. The same object could be referenced multiple times. |
| 398 | return |
maruel@chromium.org | 9e98e43 | 2013-05-31 17:06:51 +0000 | [diff] [blame] | 399 | # TODO(maruel): It should look at the free disk space, the current cache |
| 400 | # size and the size of the new item on every new item: |
| 401 | # - Trim the cache as more entries are listed when free disk space is low, |
| 402 | # otherwise if the amount of data downloaded during the run > free disk |
| 403 | # space, it'll crash. |
| 404 | # - Make sure there's enough free disk space to fit all dependencies of |
| 405 | # this run! If not, abort early. |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 406 | self._pool.add_task(priority, self._store, item, path, size) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 407 | self._pending_queue.add(item) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 408 | |
| 409 | def add(self, filepath, obj): |
| 410 | """Forcibly adds a file to the cache.""" |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 411 | if obj not in self.lru: |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 412 | link_file(self._path(obj), filepath, HARDLINK) |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 413 | self._add(obj) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 414 | |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 415 | def store_to(self, obj, dest): |
| 416 | link_file(dest, self._path(obj), HARDLINK) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 417 | |
maruel@chromium.org | 4160164 | 2013-09-18 19:40:46 +0000 | [diff] [blame] | 418 | def read(self, item): |
| 419 | """Reads an item from the cache.""" |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 420 | with open(self._path(item), 'rb') as f: |
maruel@chromium.org | 4160164 | 2013-09-18 19:40:46 +0000 | [diff] [blame] | 421 | return f.read() |
| 422 | |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 423 | def wait_for(self, items): |
| 424 | """Starts a loop that waits for at least one of |items| to be retrieved. |
| 425 | |
| 426 | Returns the first item retrieved. |
| 427 | """ |
| 428 | # Flush items already present. |
| 429 | for item in items: |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 430 | if item in self.lru: |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 431 | return item |
| 432 | |
| 433 | assert all(i in self._pending_queue for i in items), ( |
| 434 | items, self._pending_queue) |
| 435 | # Note that: |
| 436 | # len(self._pending_queue) == |
maruel@chromium.org | 3e42ce8 | 2013-09-12 18:36:59 +0000 | [diff] [blame] | 437 | # ( len(self.remote_fetcher._workers) - self.remote_fetcher._ready + |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 438 | # len(self._remote._queue) + len(self._remote.done)) |
| 439 | # There is no lock-free way to verify that. |
| 440 | while self._pending_queue: |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 441 | item = self._pool.get_one_result() |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 442 | self._pending_queue.remove(item) |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 443 | self._add(item) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 444 | if item in items: |
| 445 | return item |
| 446 | |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 447 | def _path(self, item): |
| 448 | """Returns the path to one item.""" |
| 449 | return os.path.join(self.cache_dir, item) |
| 450 | |
maruel@chromium.org | e45728d | 2013-09-16 23:23:22 +0000 | [diff] [blame] | 451 | def _save(self): |
| 452 | """Saves the LRU ordering.""" |
| 453 | self.lru.save(self.state_file) |
| 454 | |
| 455 | def _remove_lru_file(self): |
| 456 | """Removes the last recently used file and returns its size.""" |
| 457 | item, size = self.lru.pop_oldest() |
| 458 | self._delete_file(item, size) |
| 459 | return size |
| 460 | |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 461 | def _add(self, item): |
| 462 | """Adds an item into LRU cache marking it as a newest one.""" |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 463 | size = os.stat(self._path(item)).st_size |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 464 | self._added.append(size) |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 465 | self.lru.add(item, size) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 466 | |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 467 | def _add_oldest_list(self, items): |
| 468 | """Adds a bunch of items into LRU cache marking them as oldest ones.""" |
| 469 | pairs = [] |
| 470 | for item in items: |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 471 | size = os.stat(self._path(item)).st_size |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 472 | self._added.append(size) |
| 473 | pairs.append((item, size)) |
| 474 | self.lru.batch_insert_oldest(pairs) |
| 475 | |
maruel@chromium.org | 8750e4b | 2013-09-18 02:37:57 +0000 | [diff] [blame] | 476 | def _store(self, item, path, expected_size): |
| 477 | """Stores the data generated by remote_fetcher.""" |
| 478 | isolateserver.file_write(path, self.retriever(item, expected_size)) |
| 479 | return item |
| 480 | |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 481 | def _delete_file(self, item, size): |
| 482 | """Deletes cache file from the file system.""" |
| 483 | self._removed.append(size) |
| 484 | try: |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 485 | os.remove(self._path(item)) |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 486 | except OSError as e: |
| 487 | logging.error('Error attempting to delete a file\n%s' % e) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 488 | |
| 489 | |
maruel@chromium.org | 3e42ce8 | 2013-09-12 18:36:59 +0000 | [diff] [blame] | 490 | def run_tha_test(isolated_hash, cache_dir, retriever, policies): |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 491 | """Downloads the dependencies in the cache, hardlinks them into a temporary |
| 492 | directory and runs the executable. |
| 493 | """ |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame] | 494 | algo = hashlib.sha1 |
maruel@chromium.org | 781ccf6 | 2013-09-17 19:39:47 +0000 | [diff] [blame] | 495 | outdir = None |
| 496 | try: |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 497 | cache = DiskCache(cache_dir, retriever, policies, algo) |
| 498 | # |cache_dir| may not exist until DiskCache() instance is created. |
| 499 | outdir = make_temp_dir('run_tha_test', cache_dir) |
| 500 | try: |
| 501 | settings = isolateserver.fetch_isolated( |
| 502 | isolated_hash, cache, outdir, get_flavor(), algo, True) |
| 503 | except isolateserver.ConfigError as e: |
| 504 | print >> sys.stderr, str(e) |
| 505 | return 1 |
maruel@chromium.org | 781ccf6 | 2013-09-17 19:39:47 +0000 | [diff] [blame] | 506 | |
| 507 | if settings.read_only: |
| 508 | logging.info('Making files read only') |
| 509 | make_writable(outdir, True) |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 510 | cwd = os.path.normpath(os.path.join(outdir, settings.relative_cwd)) |
| 511 | logging.info('Running %s, cwd=%s' % (settings.command, cwd)) |
maruel@chromium.org | 781ccf6 | 2013-09-17 19:39:47 +0000 | [diff] [blame] | 512 | |
| 513 | # TODO(csharp): This should be specified somewhere else. |
| 514 | # TODO(vadimsh): Pass it via 'env_vars' in manifest. |
| 515 | # Add a rotating log file if one doesn't already exist. |
| 516 | env = os.environ.copy() |
| 517 | env.setdefault('RUN_TEST_CASES_LOG_FILE', |
| 518 | os.path.join(MAIN_DIR, RUN_TEST_CASES_LOG)) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 519 | try: |
maruel@chromium.org | 781ccf6 | 2013-09-17 19:39:47 +0000 | [diff] [blame] | 520 | with tools.Profiler('RunTest'): |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 521 | return subprocess.call(settings.command, cwd=cwd, env=env) |
maruel@chromium.org | 781ccf6 | 2013-09-17 19:39:47 +0000 | [diff] [blame] | 522 | except OSError: |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 523 | print >> sys.stderr, 'Failed to run %s; cwd=%s' % (settings.command, cwd) |
| 524 | return 1 |
maruel@chromium.org | 781ccf6 | 2013-09-17 19:39:47 +0000 | [diff] [blame] | 525 | finally: |
| 526 | if outdir: |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 527 | rmtree(outdir) |
| 528 | |
| 529 | |
| 530 | def main(): |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 531 | tools.disable_buffering() |
| 532 | parser = tools.OptionParserWithLogging( |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 533 | usage='%prog <options>', |
| 534 | version=__version__, |
| 535 | log_file=RUN_ISOLATED_LOG_FILE) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 536 | |
| 537 | group = optparse.OptionGroup(parser, 'Data source') |
| 538 | group.add_option( |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 539 | '-s', '--isolated', |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 540 | metavar='FILE', |
| 541 | help='File/url describing what to map or run') |
| 542 | group.add_option( |
| 543 | '-H', '--hash', |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 544 | help='Hash of the .isolated to grab from the hash table') |
maruel@chromium.org | b7e79a2 | 2013-09-13 01:24:56 +0000 | [diff] [blame] | 545 | group.add_option( |
| 546 | '-I', '--isolate-server', metavar='URL', |
| 547 | default= |
| 548 | 'https://isolateserver.appspot.com', |
| 549 | help='Remote where to get the items. Defaults to %default') |
| 550 | group.add_option( |
| 551 | '-n', '--namespace', |
| 552 | default='default-gzip', |
| 553 | help='namespace to use when using isolateserver, default: %default') |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 554 | parser.add_option_group(group) |
| 555 | |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 556 | group = optparse.OptionGroup(parser, 'Cache management') |
| 557 | group.add_option( |
| 558 | '--cache', |
| 559 | default='cache', |
| 560 | metavar='DIR', |
| 561 | help='Cache directory, default=%default') |
| 562 | group.add_option( |
| 563 | '--max-cache-size', |
| 564 | type='int', |
| 565 | metavar='NNN', |
| 566 | default=20*1024*1024*1024, |
| 567 | help='Trim if the cache gets larger than this value, default=%default') |
| 568 | group.add_option( |
| 569 | '--min-free-space', |
| 570 | type='int', |
| 571 | metavar='NNN', |
maruel@chromium.org | 9e98e43 | 2013-05-31 17:06:51 +0000 | [diff] [blame] | 572 | default=2*1024*1024*1024, |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 573 | help='Trim if disk free space becomes lower than this value, ' |
| 574 | 'default=%default') |
| 575 | group.add_option( |
| 576 | '--max-items', |
| 577 | type='int', |
| 578 | metavar='NNN', |
| 579 | default=100000, |
| 580 | help='Trim if more than this number of items are in the cache ' |
| 581 | 'default=%default') |
| 582 | parser.add_option_group(group) |
| 583 | |
| 584 | options, args = parser.parse_args() |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 585 | |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 586 | if bool(options.isolated) == bool(options.hash): |
maruel@chromium.org | 5dd75dd | 2012-12-03 15:11:32 +0000 | [diff] [blame] | 587 | logging.debug('One and only one of --isolated or --hash is required.') |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 588 | parser.error('One and only one of --isolated or --hash is required.') |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 589 | if args: |
maruel@chromium.org | 5dd75dd | 2012-12-03 15:11:32 +0000 | [diff] [blame] | 590 | logging.debug('Unsupported args %s' % ' '.join(args)) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 591 | parser.error('Unsupported args %s' % ' '.join(args)) |
| 592 | |
csharp@chromium.org | ffd8cf0 | 2013-01-09 21:57:38 +0000 | [diff] [blame] | 593 | options.cache = os.path.abspath(options.cache) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 594 | policies = CachePolicies( |
| 595 | options.max_cache_size, options.min_free_space, options.max_items) |
csharp@chromium.org | ffd8cf0 | 2013-01-09 21:57:38 +0000 | [diff] [blame] | 596 | |
maruel@chromium.org | b7e79a2 | 2013-09-13 01:24:56 +0000 | [diff] [blame] | 597 | retriever = isolateserver.get_storage_api( |
| 598 | options.isolate_server, options.namespace) |
maruel@chromium.org | 3e42ce8 | 2013-09-12 18:36:59 +0000 | [diff] [blame] | 599 | try: |
| 600 | return run_tha_test( |
| 601 | options.isolated or options.hash, |
| 602 | options.cache, |
maruel@chromium.org | 8750e4b | 2013-09-18 02:37:57 +0000 | [diff] [blame] | 603 | retriever.fetch, |
maruel@chromium.org | 3e42ce8 | 2013-09-12 18:36:59 +0000 | [diff] [blame] | 604 | policies) |
| 605 | except Exception, e: |
| 606 | # Make sure any exception is logged. |
| 607 | logging.exception(e) |
| 608 | return 1 |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 609 | |
| 610 | |
| 611 | if __name__ == '__main__': |
csharp@chromium.org | bfb9874 | 2013-03-26 20:28:36 +0000 | [diff] [blame] | 612 | # Ensure that we are always running with the correct encoding. |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 613 | fix_encoding.fix_encoding() |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 614 | sys.exit(main()) |