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 |
| 15 | import json |
| 16 | import logging |
| 17 | import optparse |
| 18 | import os |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 19 | import re |
| 20 | import shutil |
| 21 | import stat |
| 22 | import subprocess |
| 23 | import sys |
| 24 | import tempfile |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 25 | import time |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 26 | |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 27 | from third_party.depot_tools import fix_encoding |
| 28 | |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 29 | from utils import lru |
vadimsh@chromium.org | b074b16 | 2013-08-22 17:55:46 +0000 | [diff] [blame] | 30 | from utils import threading_utils |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 31 | from utils import tools |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 32 | from utils import zip_package |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 33 | |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 34 | import isolateserver |
maruel@chromium.org | 9958e4a | 2013-09-17 00:01:48 +0000 | [diff] [blame] | 35 | from isolateserver import ConfigError |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 36 | |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 37 | |
vadimsh@chromium.org | 8507106 | 2013-08-21 23:37:45 +0000 | [diff] [blame] | 38 | # Absolute path to this file (can be None if running from zip on Mac). |
| 39 | THIS_FILE_PATH = os.path.abspath(__file__) if __file__ else None |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 40 | |
| 41 | # Directory that contains this file (might be inside zip package). |
vadimsh@chromium.org | 8507106 | 2013-08-21 23:37:45 +0000 | [diff] [blame] | 42 | 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] | 43 | |
| 44 | # Directory that contains currently running script file. |
| 45 | MAIN_DIR = os.path.dirname(os.path.abspath(zip_package.get_main_script_path())) |
| 46 | |
maruel@chromium.org | 6b365dc | 2012-10-18 19:17:56 +0000 | [diff] [blame] | 47 | # Types of action accepted by link_file(). |
maruel@chromium.org | ba6489b | 2013-07-11 20:23:33 +0000 | [diff] [blame] | 48 | HARDLINK, HARDLINK_WITH_FALLBACK, SYMLINK, COPY = range(1, 5) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 49 | |
csharp@chromium.org | ff2a466 | 2012-11-21 20:49:32 +0000 | [diff] [blame] | 50 | # The name of the log file to use. |
| 51 | RUN_ISOLATED_LOG_FILE = 'run_isolated.log' |
| 52 | |
csharp@chromium.org | e217f30 | 2012-11-22 16:51:53 +0000 | [diff] [blame] | 53 | # 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] | 54 | RUN_TEST_CASES_LOG = 'run_test_cases.log' |
csharp@chromium.org | e217f30 | 2012-11-22 16:51:53 +0000 | [diff] [blame] | 55 | |
csharp@chromium.org | 9c59ff1 | 2012-12-12 02:32:29 +0000 | [diff] [blame] | 56 | # The delay (in seconds) to wait between logging statements when retrieving |
| 57 | # the required files. This is intended to let the user (or buildbot) know that |
| 58 | # the program is still running. |
| 59 | DELAY_BETWEEN_UPDATES_IN_SECS = 30 |
| 60 | |
vadimsh@chromium.org | 5db0f4f | 2013-07-04 13:57:02 +0000 | [diff] [blame] | 61 | # Maximum expected delay (in seconds) between successive file fetches |
| 62 | # in run_tha_test. If it takes longer than that, a deadlock might be happening |
| 63 | # and all stack frames for all threads are dumped to log. |
| 64 | DEADLOCK_TIMEOUT = 5 * 60 |
| 65 | |
vadimsh@chromium.org | 87d6326 | 2013-04-04 19:34:21 +0000 | [diff] [blame] | 66 | |
maruel@chromium.org | 9e9ceaa | 2013-04-05 15:42:42 +0000 | [diff] [blame] | 67 | # Used by get_flavor(). |
| 68 | FLAVOR_MAPPING = { |
| 69 | 'cygwin': 'win', |
| 70 | 'win32': 'win', |
| 71 | 'darwin': 'mac', |
| 72 | 'sunos5': 'solaris', |
| 73 | 'freebsd7': 'freebsd', |
| 74 | 'freebsd8': 'freebsd', |
| 75 | } |
| 76 | |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 77 | |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 78 | def get_as_zip_package(executable=True): |
| 79 | """Returns ZipPackage with this module and all its dependencies. |
| 80 | |
| 81 | If |executable| is True will store run_isolated.py as __main__.py so that |
| 82 | zip package is directly executable be python. |
| 83 | """ |
| 84 | # Building a zip package when running from another zip package is |
| 85 | # unsupported and probably unneeded. |
| 86 | assert not zip_package.is_zipped_module(sys.modules[__name__]) |
vadimsh@chromium.org | 8507106 | 2013-08-21 23:37:45 +0000 | [diff] [blame] | 87 | assert THIS_FILE_PATH |
| 88 | assert BASE_DIR |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 89 | package = zip_package.ZipPackage(root=BASE_DIR) |
| 90 | 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] | 91 | package.add_python_file(os.path.join(BASE_DIR, 'isolateserver.py')) |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 92 | package.add_directory(os.path.join(BASE_DIR, 'third_party')) |
| 93 | package.add_directory(os.path.join(BASE_DIR, 'utils')) |
| 94 | return package |
| 95 | |
| 96 | |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 97 | def get_flavor(): |
| 98 | """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] | 99 | return FLAVOR_MAPPING.get(sys.platform, 'linux') |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 100 | |
| 101 | |
| 102 | def os_link(source, link_name): |
| 103 | """Add support for os.link() on Windows.""" |
| 104 | if sys.platform == 'win32': |
| 105 | if not ctypes.windll.kernel32.CreateHardLinkW( |
| 106 | unicode(link_name), unicode(source), 0): |
| 107 | raise OSError() |
| 108 | else: |
| 109 | os.link(source, link_name) |
| 110 | |
| 111 | |
| 112 | def readable_copy(outfile, infile): |
| 113 | """Makes a copy of the file that is readable by everyone.""" |
csharp@chromium.org | 59d116d | 2013-07-05 18:04:08 +0000 | [diff] [blame] | 114 | shutil.copy2(infile, outfile) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 115 | read_enabled_mode = (os.stat(outfile).st_mode | stat.S_IRUSR | |
| 116 | stat.S_IRGRP | stat.S_IROTH) |
| 117 | os.chmod(outfile, read_enabled_mode) |
| 118 | |
| 119 | |
| 120 | def link_file(outfile, infile, action): |
| 121 | """Links a file. The type of link depends on |action|.""" |
| 122 | logging.debug('Mapping %s to %s' % (infile, outfile)) |
maruel@chromium.org | ba6489b | 2013-07-11 20:23:33 +0000 | [diff] [blame] | 123 | if action not in (HARDLINK, HARDLINK_WITH_FALLBACK, SYMLINK, COPY): |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 124 | raise ValueError('Unknown mapping action %s' % action) |
| 125 | if not os.path.isfile(infile): |
maruel@chromium.org | 9958e4a | 2013-09-17 00:01:48 +0000 | [diff] [blame] | 126 | raise isolateserver.MappingError('%s is missing' % infile) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 127 | if os.path.isfile(outfile): |
maruel@chromium.org | 9958e4a | 2013-09-17 00:01:48 +0000 | [diff] [blame] | 128 | raise isolateserver.MappingError( |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 129 | '%s already exist; insize:%d; outsize:%d' % |
| 130 | (outfile, os.stat(infile).st_size, os.stat(outfile).st_size)) |
| 131 | |
| 132 | if action == COPY: |
| 133 | readable_copy(outfile, infile) |
| 134 | elif action == SYMLINK and sys.platform != 'win32': |
| 135 | # 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] | 136 | os.symlink(infile, outfile) # pylint: disable=E1101 |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 137 | else: |
| 138 | try: |
| 139 | os_link(infile, outfile) |
maruel@chromium.org | ba6489b | 2013-07-11 20:23:33 +0000 | [diff] [blame] | 140 | except OSError as e: |
| 141 | if action == HARDLINK: |
maruel@chromium.org | 9958e4a | 2013-09-17 00:01:48 +0000 | [diff] [blame] | 142 | raise isolateserver.MappingError( |
maruel@chromium.org | ba6489b | 2013-07-11 20:23:33 +0000 | [diff] [blame] | 143 | 'Failed to hardlink %s to %s: %s' % (infile, outfile, e)) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 144 | # Probably a different file system. |
maruel@chromium.org | 9e98e43 | 2013-05-31 17:06:51 +0000 | [diff] [blame] | 145 | logging.warning( |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 146 | 'Failed to hardlink, failing back to copy %s to %s' % ( |
| 147 | infile, outfile)) |
| 148 | readable_copy(outfile, infile) |
| 149 | |
| 150 | |
| 151 | def _set_write_bit(path, read_only): |
| 152 | """Sets or resets the executable bit on a file or directory.""" |
| 153 | mode = os.lstat(path).st_mode |
| 154 | if read_only: |
| 155 | mode = mode & 0500 |
| 156 | else: |
| 157 | mode = mode | 0200 |
| 158 | if hasattr(os, 'lchmod'): |
| 159 | os.lchmod(path, mode) # pylint: disable=E1101 |
| 160 | else: |
| 161 | if stat.S_ISLNK(mode): |
| 162 | # Skip symlink without lchmod() support. |
| 163 | logging.debug('Can\'t change +w bit on symlink %s' % path) |
| 164 | return |
| 165 | |
| 166 | # TODO(maruel): Implement proper DACL modification on Windows. |
| 167 | os.chmod(path, mode) |
| 168 | |
| 169 | |
| 170 | def make_writable(root, read_only): |
| 171 | """Toggle the writable bit on a directory tree.""" |
csharp@chromium.org | 837352f | 2013-01-17 21:17:03 +0000 | [diff] [blame] | 172 | assert os.path.isabs(root), root |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 173 | for dirpath, dirnames, filenames in os.walk(root, topdown=True): |
| 174 | for filename in filenames: |
| 175 | _set_write_bit(os.path.join(dirpath, filename), read_only) |
| 176 | |
| 177 | for dirname in dirnames: |
| 178 | _set_write_bit(os.path.join(dirpath, dirname), read_only) |
| 179 | |
| 180 | |
| 181 | def rmtree(root): |
| 182 | """Wrapper around shutil.rmtree() to retry automatically on Windows.""" |
| 183 | make_writable(root, False) |
| 184 | if sys.platform == 'win32': |
| 185 | for i in range(3): |
| 186 | try: |
| 187 | shutil.rmtree(root) |
| 188 | break |
| 189 | except WindowsError: # pylint: disable=E0602 |
| 190 | delay = (i+1)*2 |
| 191 | print >> sys.stderr, ( |
| 192 | 'The test has subprocess outliving it. Sleep %d seconds.' % delay) |
| 193 | time.sleep(delay) |
| 194 | else: |
| 195 | shutil.rmtree(root) |
| 196 | |
| 197 | |
| 198 | def is_same_filesystem(path1, path2): |
| 199 | """Returns True if both paths are on the same filesystem. |
| 200 | |
| 201 | This is required to enable the use of hardlinks. |
| 202 | """ |
| 203 | assert os.path.isabs(path1), path1 |
| 204 | assert os.path.isabs(path2), path2 |
| 205 | if sys.platform == 'win32': |
| 206 | # If the drive letter mismatches, assume it's a separate partition. |
| 207 | # TODO(maruel): It should look at the underlying drive, a drive letter could |
| 208 | # be a mount point to a directory on another drive. |
| 209 | assert re.match(r'^[a-zA-Z]\:\\.*', path1), path1 |
| 210 | assert re.match(r'^[a-zA-Z]\:\\.*', path2), path2 |
| 211 | if path1[0].lower() != path2[0].lower(): |
| 212 | return False |
| 213 | return os.stat(path1).st_dev == os.stat(path2).st_dev |
| 214 | |
| 215 | |
| 216 | def get_free_space(path): |
| 217 | """Returns the number of free bytes.""" |
| 218 | if sys.platform == 'win32': |
| 219 | free_bytes = ctypes.c_ulonglong(0) |
| 220 | ctypes.windll.kernel32.GetDiskFreeSpaceExW( |
| 221 | ctypes.c_wchar_p(path), None, None, ctypes.pointer(free_bytes)) |
| 222 | return free_bytes.value |
maruel@chromium.org | f43e68b | 2012-10-15 20:23:10 +0000 | [diff] [blame] | 223 | # For OSes other than Windows. |
| 224 | f = os.statvfs(path) # pylint: disable=E1101 |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 225 | return f.f_bfree * f.f_frsize |
| 226 | |
| 227 | |
| 228 | def make_temp_dir(prefix, root_dir): |
| 229 | """Returns a temporary directory on the same file system as root_dir.""" |
| 230 | base_temp_dir = None |
| 231 | if not is_same_filesystem(root_dir, tempfile.gettempdir()): |
| 232 | base_temp_dir = os.path.dirname(root_dir) |
| 233 | return tempfile.mkdtemp(prefix=prefix, dir=base_temp_dir) |
| 234 | |
| 235 | |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame^] | 236 | def load_isolated(content, os_flavor, algo): |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 237 | """Verifies the .isolated file is valid and loads this object with the json |
| 238 | data. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 239 | """ |
| 240 | try: |
| 241 | data = json.loads(content) |
| 242 | except ValueError: |
| 243 | raise ConfigError('Failed to parse: %s...' % content[:100]) |
| 244 | |
| 245 | if not isinstance(data, dict): |
| 246 | raise ConfigError('Expected dict, got %r' % data) |
| 247 | |
| 248 | for key, value in data.iteritems(): |
| 249 | if key == 'command': |
| 250 | if not isinstance(value, list): |
| 251 | raise ConfigError('Expected list, got %r' % value) |
maruel@chromium.org | 89ad2db | 2012-12-12 14:29:22 +0000 | [diff] [blame] | 252 | if not value: |
| 253 | raise ConfigError('Expected non-empty command') |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 254 | for subvalue in value: |
| 255 | if not isinstance(subvalue, basestring): |
| 256 | raise ConfigError('Expected string, got %r' % subvalue) |
| 257 | |
| 258 | elif key == 'files': |
| 259 | if not isinstance(value, dict): |
| 260 | raise ConfigError('Expected dict, got %r' % value) |
| 261 | for subkey, subvalue in value.iteritems(): |
| 262 | if not isinstance(subkey, basestring): |
| 263 | raise ConfigError('Expected string, got %r' % subkey) |
| 264 | if not isinstance(subvalue, dict): |
| 265 | raise ConfigError('Expected dict, got %r' % subvalue) |
| 266 | for subsubkey, subsubvalue in subvalue.iteritems(): |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 267 | if subsubkey == 'l': |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 268 | if not isinstance(subsubvalue, basestring): |
| 269 | raise ConfigError('Expected string, got %r' % subsubvalue) |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 270 | elif subsubkey == 'm': |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 271 | if not isinstance(subsubvalue, int): |
| 272 | raise ConfigError('Expected int, got %r' % subsubvalue) |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 273 | elif subsubkey == 'h': |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame^] | 274 | if not isolateserver.is_valid_hash(subsubvalue, algo): |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 275 | raise ConfigError('Expected sha-1, got %r' % subsubvalue) |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 276 | elif subsubkey == 's': |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 277 | if not isinstance(subsubvalue, int): |
| 278 | raise ConfigError('Expected int, got %r' % subsubvalue) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 279 | else: |
| 280 | raise ConfigError('Unknown subsubkey %s' % subsubkey) |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 281 | if bool('h' in subvalue) and bool('l' in subvalue): |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 282 | raise ConfigError( |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 283 | 'Did not expect both \'h\' (sha-1) and \'l\' (link), got: %r' % |
| 284 | subvalue) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 285 | |
| 286 | elif key == 'includes': |
| 287 | if not isinstance(value, list): |
| 288 | raise ConfigError('Expected list, got %r' % value) |
maruel@chromium.org | 89ad2db | 2012-12-12 14:29:22 +0000 | [diff] [blame] | 289 | if not value: |
| 290 | raise ConfigError('Expected non-empty includes list') |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 291 | for subvalue in value: |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame^] | 292 | if not isolateserver.is_valid_hash(subvalue, algo): |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 293 | raise ConfigError('Expected sha-1, got %r' % subvalue) |
| 294 | |
| 295 | elif key == 'read_only': |
| 296 | if not isinstance(value, bool): |
| 297 | raise ConfigError('Expected bool, got %r' % value) |
| 298 | |
| 299 | elif key == 'relative_cwd': |
| 300 | if not isinstance(value, basestring): |
| 301 | raise ConfigError('Expected string, got %r' % value) |
| 302 | |
| 303 | elif key == 'os': |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame^] | 304 | if os_flavor and value != os_flavor: |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 305 | raise ConfigError( |
| 306 | 'Expected \'os\' to be \'%s\' but got \'%s\'' % |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame^] | 307 | (os_flavor, value)) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 308 | |
| 309 | else: |
| 310 | raise ConfigError('Unknown key %s' % key) |
| 311 | |
| 312 | return data |
| 313 | |
| 314 | |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 315 | class CachePolicies(object): |
| 316 | def __init__(self, max_cache_size, min_free_space, max_items): |
| 317 | """ |
| 318 | Arguments: |
| 319 | - max_cache_size: Trim if the cache gets larger than this value. If 0, the |
| 320 | cache is effectively a leak. |
| 321 | - min_free_space: Trim if disk free space becomes lower than this value. If |
| 322 | 0, it unconditionally fill the disk. |
| 323 | - max_items: Maximum number of items to keep in the cache. If 0, do not |
| 324 | enforce a limit. |
| 325 | """ |
| 326 | self.max_cache_size = max_cache_size |
| 327 | self.min_free_space = min_free_space |
| 328 | self.max_items = max_items |
| 329 | |
| 330 | |
maruel@chromium.org | e45728d | 2013-09-16 23:23:22 +0000 | [diff] [blame] | 331 | class DiskCache(object): |
| 332 | """Stateful LRU cache in a flat hash table in a directory. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 333 | |
| 334 | Saves its state as json file. |
| 335 | """ |
| 336 | STATE_FILE = 'state.json' |
| 337 | |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame^] | 338 | def __init__(self, cache_dir, remote_fetcher, policies, algo): |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 339 | """ |
| 340 | Arguments: |
| 341 | - cache_dir: Directory where to place the cache. |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 342 | - remote_fetcher: isolateserver.RemoteOperation where to fetch items from. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 343 | - policies: cache retention policies. |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame^] | 344 | - algo: hashing algorithm used. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 345 | """ |
| 346 | self.cache_dir = cache_dir |
maruel@chromium.org | 3e42ce8 | 2013-09-12 18:36:59 +0000 | [diff] [blame] | 347 | self.remote_fetcher = remote_fetcher |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 348 | self.policies = policies |
| 349 | self.state_file = os.path.join(cache_dir, self.STATE_FILE) |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 350 | self.lru = lru.LRUDict() |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 351 | |
| 352 | # Items currently being fetched. Keep it local to reduce lock contention. |
| 353 | self._pending_queue = set() |
| 354 | |
| 355 | # Profiling values. |
| 356 | self._added = [] |
| 357 | self._removed = [] |
| 358 | self._free_disk = 0 |
| 359 | |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 360 | with tools.Profiler('Setup'): |
maruel@chromium.org | 770993b | 2012-12-11 17:16:48 +0000 | [diff] [blame] | 361 | if not os.path.isdir(self.cache_dir): |
| 362 | os.makedirs(self.cache_dir) |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 363 | |
| 364 | # Load state of the cache. |
vadimsh@chromium.org | a40428e | 2013-07-04 15:43:14 +0000 | [diff] [blame] | 365 | if os.path.isfile(self.state_file): |
| 366 | try: |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 367 | self.lru = lru.LRUDict.load(self.state_file) |
| 368 | except ValueError as err: |
| 369 | logging.error('Failed to load cache state: %s' % (err,)) |
| 370 | # Don't want to keep broken state file. |
| 371 | os.remove(self.state_file) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 372 | |
maruel@chromium.org | 770993b | 2012-12-11 17:16:48 +0000 | [diff] [blame] | 373 | # 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] | 374 | previous = self.lru.keys_set() |
| 375 | unknown = [] |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 376 | for filename in os.listdir(self.cache_dir): |
| 377 | if filename == self.STATE_FILE: |
| 378 | continue |
| 379 | if filename in previous: |
| 380 | previous.remove(filename) |
| 381 | continue |
| 382 | # An untracked file. |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame^] | 383 | if not isolateserver.is_valid_hash(filename, algo): |
maruel@chromium.org | 9e98e43 | 2013-05-31 17:06:51 +0000 | [diff] [blame] | 384 | logging.warning('Removing unknown file %s from cache', filename) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 385 | os.remove(self.path(filename)) |
maruel@chromium.org | 770993b | 2012-12-11 17:16:48 +0000 | [diff] [blame] | 386 | continue |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 387 | # File that's not referenced in 'state.json'. |
| 388 | # TODO(vadimsh): Verify its SHA1 matches file name. |
| 389 | logging.warning('Adding unknown file %s to cache', filename) |
| 390 | unknown.append(filename) |
maruel@chromium.org | 770993b | 2012-12-11 17:16:48 +0000 | [diff] [blame] | 391 | |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 392 | if unknown: |
| 393 | # Add as oldest files. They will be deleted eventually if not accessed. |
| 394 | self._add_oldest_list(unknown) |
| 395 | logging.warning('Added back %d unknown files', len(unknown)) |
| 396 | |
maruel@chromium.org | 770993b | 2012-12-11 17:16:48 +0000 | [diff] [blame] | 397 | if previous: |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 398 | # Filter out entries that were not found. |
maruel@chromium.org | 9e98e43 | 2013-05-31 17:06:51 +0000 | [diff] [blame] | 399 | logging.warning('Removed %d lost files', len(previous)) |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 400 | for filename in previous: |
| 401 | self.lru.pop(filename) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 402 | self.trim() |
| 403 | |
| 404 | def __enter__(self): |
| 405 | return self |
| 406 | |
| 407 | def __exit__(self, _exc_type, _exec_value, _traceback): |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 408 | with tools.Profiler('CleanupTrimming'): |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 409 | self.trim() |
| 410 | |
| 411 | logging.info( |
maruel@chromium.org | 5fd6f47 | 2012-12-11 00:26:08 +0000 | [diff] [blame] | 412 | '%5d (%8dkb) added', len(self._added), sum(self._added) / 1024) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 413 | logging.info( |
maruel@chromium.org | 5fd6f47 | 2012-12-11 00:26:08 +0000 | [diff] [blame] | 414 | '%5d (%8dkb) current', |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 415 | len(self.lru), |
| 416 | sum(self.lru.itervalues()) / 1024) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 417 | logging.info( |
maruel@chromium.org | 5fd6f47 | 2012-12-11 00:26:08 +0000 | [diff] [blame] | 418 | '%5d (%8dkb) removed', len(self._removed), sum(self._removed) / 1024) |
| 419 | logging.info(' %8dkb free', self._free_disk / 1024) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 420 | |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 421 | def trim(self): |
| 422 | """Trims anything we don't know, make sure enough free space exists.""" |
| 423 | # Ensure maximum cache size. |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 424 | if self.policies.max_cache_size: |
| 425 | total_size = sum(self.lru.itervalues()) |
| 426 | while total_size > self.policies.max_cache_size: |
maruel@chromium.org | e45728d | 2013-09-16 23:23:22 +0000 | [diff] [blame] | 427 | total_size -= self._remove_lru_file() |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 428 | |
| 429 | # Ensure maximum number of items in the cache. |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 430 | if self.policies.max_items and len(self.lru) > self.policies.max_items: |
| 431 | for _ in xrange(len(self.lru) - self.policies.max_items): |
maruel@chromium.org | e45728d | 2013-09-16 23:23:22 +0000 | [diff] [blame] | 432 | self._remove_lru_file() |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 433 | |
| 434 | # Ensure enough free space. |
| 435 | self._free_disk = get_free_space(self.cache_dir) |
maruel@chromium.org | 9e98e43 | 2013-05-31 17:06:51 +0000 | [diff] [blame] | 436 | trimmed_due_to_space = False |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 437 | while ( |
| 438 | self.policies.min_free_space and |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 439 | self.lru and |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 440 | self._free_disk < self.policies.min_free_space): |
maruel@chromium.org | 9e98e43 | 2013-05-31 17:06:51 +0000 | [diff] [blame] | 441 | trimmed_due_to_space = True |
maruel@chromium.org | e45728d | 2013-09-16 23:23:22 +0000 | [diff] [blame] | 442 | self._remove_lru_file() |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 443 | self._free_disk = get_free_space(self.cache_dir) |
maruel@chromium.org | 9e98e43 | 2013-05-31 17:06:51 +0000 | [diff] [blame] | 444 | if trimmed_due_to_space: |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 445 | total = sum(self.lru.itervalues()) |
maruel@chromium.org | 9e98e43 | 2013-05-31 17:06:51 +0000 | [diff] [blame] | 446 | logging.warning( |
| 447 | 'Trimmed due to not enough free disk space: %.1fkb free, %.1fkb ' |
| 448 | 'cache (%.1f%% of its maximum capacity)', |
| 449 | self._free_disk / 1024., |
| 450 | total / 1024., |
| 451 | 100. * self.policies.max_cache_size / float(total), |
| 452 | ) |
maruel@chromium.org | e45728d | 2013-09-16 23:23:22 +0000 | [diff] [blame] | 453 | self._save() |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 454 | |
csharp@chromium.org | 8dc5254 | 2012-11-08 20:29:55 +0000 | [diff] [blame] | 455 | def retrieve(self, priority, item, size): |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 456 | """Retrieves a file from the remote, if not already cached, and adds it to |
| 457 | the cache. |
csharp@chromium.org | 8dc5254 | 2012-11-08 20:29:55 +0000 | [diff] [blame] | 458 | |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 459 | 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] | 460 | the correct size), retrieving it again if it isn't. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 461 | """ |
| 462 | assert not '/' in item |
| 463 | path = self.path(item) |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 464 | found = False |
csharp@chromium.org | 8dc5254 | 2012-11-08 20:29:55 +0000 | [diff] [blame] | 465 | |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 466 | if item in self.lru: |
maruel@chromium.org | e45728d | 2013-09-16 23:23:22 +0000 | [diff] [blame] | 467 | # Note that is doesn't compute the hash so it could still be corrupted. |
| 468 | if not isolateserver.is_valid_file(self.path(item), size): |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 469 | self.lru.pop(item) |
| 470 | self._delete_file(item, size) |
csharp@chromium.org | 8dc5254 | 2012-11-08 20:29:55 +0000 | [diff] [blame] | 471 | else: |
csharp@chromium.org | 8dc5254 | 2012-11-08 20:29:55 +0000 | [diff] [blame] | 472 | # 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] | 473 | self.lru.touch(item) |
| 474 | found = True |
csharp@chromium.org | 8dc5254 | 2012-11-08 20:29:55 +0000 | [diff] [blame] | 475 | |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 476 | if not found: |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 477 | if item in self._pending_queue: |
| 478 | # Already pending. The same object could be referenced multiple times. |
| 479 | return |
maruel@chromium.org | 9e98e43 | 2013-05-31 17:06:51 +0000 | [diff] [blame] | 480 | # TODO(maruel): It should look at the free disk space, the current cache |
| 481 | # size and the size of the new item on every new item: |
| 482 | # - Trim the cache as more entries are listed when free disk space is low, |
| 483 | # otherwise if the amount of data downloaded during the run > free disk |
| 484 | # space, it'll crash. |
| 485 | # - Make sure there's enough free disk space to fit all dependencies of |
| 486 | # this run! If not, abort early. |
maruel@chromium.org | 3e42ce8 | 2013-09-12 18:36:59 +0000 | [diff] [blame] | 487 | self.remote_fetcher.add_item(priority, item, path, size) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 488 | self._pending_queue.add(item) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 489 | |
| 490 | def add(self, filepath, obj): |
| 491 | """Forcibly adds a file to the cache.""" |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 492 | if obj not in self.lru: |
| 493 | link_file(self.path(obj), filepath, HARDLINK) |
| 494 | self._add(obj) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 495 | |
| 496 | def path(self, item): |
| 497 | """Returns the path to one item.""" |
| 498 | return os.path.join(self.cache_dir, item) |
| 499 | |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 500 | def wait_for(self, items): |
| 501 | """Starts a loop that waits for at least one of |items| to be retrieved. |
| 502 | |
| 503 | Returns the first item retrieved. |
| 504 | """ |
| 505 | # Flush items already present. |
| 506 | for item in items: |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 507 | if item in self.lru: |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 508 | return item |
| 509 | |
| 510 | assert all(i in self._pending_queue for i in items), ( |
| 511 | items, self._pending_queue) |
| 512 | # Note that: |
| 513 | # len(self._pending_queue) == |
maruel@chromium.org | 3e42ce8 | 2013-09-12 18:36:59 +0000 | [diff] [blame] | 514 | # ( len(self.remote_fetcher._workers) - self.remote_fetcher._ready + |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 515 | # len(self._remote._queue) + len(self._remote.done)) |
| 516 | # There is no lock-free way to verify that. |
| 517 | while self._pending_queue: |
maruel@chromium.org | 3e42ce8 | 2013-09-12 18:36:59 +0000 | [diff] [blame] | 518 | item = self.remote_fetcher.get_one_result() |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 519 | self._pending_queue.remove(item) |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 520 | self._add(item) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 521 | if item in items: |
| 522 | return item |
| 523 | |
maruel@chromium.org | e45728d | 2013-09-16 23:23:22 +0000 | [diff] [blame] | 524 | def _save(self): |
| 525 | """Saves the LRU ordering.""" |
| 526 | self.lru.save(self.state_file) |
| 527 | |
| 528 | def _remove_lru_file(self): |
| 529 | """Removes the last recently used file and returns its size.""" |
| 530 | item, size = self.lru.pop_oldest() |
| 531 | self._delete_file(item, size) |
| 532 | return size |
| 533 | |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 534 | def _add(self, item): |
| 535 | """Adds an item into LRU cache marking it as a newest one.""" |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 536 | size = os.stat(self.path(item)).st_size |
| 537 | self._added.append(size) |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 538 | self.lru.add(item, size) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 539 | |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 540 | def _add_oldest_list(self, items): |
| 541 | """Adds a bunch of items into LRU cache marking them as oldest ones.""" |
| 542 | pairs = [] |
| 543 | for item in items: |
| 544 | size = os.stat(self.path(item)).st_size |
| 545 | self._added.append(size) |
| 546 | pairs.append((item, size)) |
| 547 | self.lru.batch_insert_oldest(pairs) |
| 548 | |
| 549 | def _delete_file(self, item, size): |
| 550 | """Deletes cache file from the file system.""" |
| 551 | self._removed.append(size) |
| 552 | try: |
| 553 | os.remove(self.path(item)) |
| 554 | except OSError as e: |
| 555 | logging.error('Error attempting to delete a file\n%s' % e) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 556 | |
| 557 | |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 558 | class IsolatedFile(object): |
| 559 | """Represents a single parsed .isolated file.""" |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame^] | 560 | def __init__(self, obj_hash, algo): |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 561 | """|obj_hash| is really the sha-1 of the file.""" |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 562 | logging.debug('IsolatedFile(%s)' % obj_hash) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 563 | self.obj_hash = obj_hash |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame^] | 564 | self.algo = algo |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 565 | # Set once all the left-side of the tree is parsed. 'Tree' here means the |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 566 | # .isolate and all the .isolated files recursively included by it with |
| 567 | # 'includes' key. The order of each sha-1 in 'includes', each representing a |
| 568 | # .isolated file in the hash table, is important, as the later ones are not |
| 569 | # processed until the firsts are retrieved and read. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 570 | self.can_fetch = False |
| 571 | |
| 572 | # Raw data. |
| 573 | self.data = {} |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 574 | # A IsolatedFile instance, one per object in self.includes. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 575 | self.children = [] |
| 576 | |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 577 | # Set once the .isolated file is loaded. |
| 578 | self._is_parsed = False |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 579 | # Set once the files are fetched. |
| 580 | self.files_fetched = False |
| 581 | |
| 582 | def load(self, content): |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 583 | """Verifies the .isolated file is valid and loads this object with the json |
| 584 | data. |
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 | logging.debug('IsolatedFile.load(%s)' % self.obj_hash) |
| 587 | assert not self._is_parsed |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame^] | 588 | self.data = load_isolated(content, get_flavor(), self.algo) |
| 589 | self.children = [ |
| 590 | IsolatedFile(i, self.algo) for i in self.data.get('includes', []) |
| 591 | ] |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 592 | self._is_parsed = True |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 593 | |
| 594 | def fetch_files(self, cache, files): |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 595 | """Adds files in this .isolated file not present in |files| dictionary. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 596 | |
| 597 | Preemptively request files. |
| 598 | |
| 599 | Note that |files| is modified by this function. |
| 600 | """ |
| 601 | assert self.can_fetch |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 602 | if not self._is_parsed or self.files_fetched: |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 603 | return |
| 604 | logging.debug('fetch_files(%s)' % self.obj_hash) |
| 605 | for filepath, properties in self.data.get('files', {}).iteritems(): |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 606 | # Root isolated has priority on the files being mapped. In particular, |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 607 | # overriden files must not be fetched. |
| 608 | if filepath not in files: |
| 609 | files[filepath] = properties |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 610 | if 'h' in properties: |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 611 | # Preemptively request files. |
| 612 | logging.debug('fetching %s' % filepath) |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 613 | cache.retrieve( |
| 614 | isolateserver.RemoteOperation.MED, |
| 615 | properties['h'], |
| 616 | properties['s']) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 617 | self.files_fetched = True |
| 618 | |
| 619 | |
| 620 | class Settings(object): |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 621 | """Results of a completely parsed .isolated file.""" |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 622 | def __init__(self): |
| 623 | self.command = [] |
| 624 | self.files = {} |
| 625 | self.read_only = None |
| 626 | self.relative_cwd = None |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 627 | # The main .isolated file, a IsolatedFile instance. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 628 | self.root = None |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 629 | |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame^] | 630 | def load(self, cache, root_isolated_hash, algo): |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 631 | """Loads the .isolated and all the included .isolated asynchronously. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 632 | |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 633 | It enables support for "included" .isolated files. They are processed in |
| 634 | strict order but fetched asynchronously from the cache. This is important so |
| 635 | that a file in an included .isolated file that is overridden by an embedding |
vadimsh@chromium.org | f4c063e | 2013-07-04 14:23:31 +0000 | [diff] [blame] | 636 | .isolated file is not fetched needlessly. The includes are fetched in one |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 637 | pass and the files are fetched as soon as all the ones on the left-side |
| 638 | of the tree were fetched. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 639 | |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 640 | The prioritization is very important here for nested .isolated files. |
| 641 | 'includes' have the highest priority and the algorithm is optimized for both |
| 642 | deep and wide trees. A deep one is a long link of .isolated files referenced |
| 643 | one at a time by one item in 'includes'. A wide one has a large number of |
| 644 | 'includes' in a single .isolated file. 'left' is defined as an included |
| 645 | .isolated file earlier in the 'includes' list. So the order of the elements |
| 646 | in 'includes' is important. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 647 | """ |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame^] | 648 | self.root = IsolatedFile(root_isolated_hash, algo) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 649 | |
vadimsh@chromium.org | f4c063e | 2013-07-04 14:23:31 +0000 | [diff] [blame] | 650 | # Isolated files being retrieved now: hash -> IsolatedFile instance. |
| 651 | pending = {} |
| 652 | # Set of hashes of already retrieved items to refuse recursive includes. |
| 653 | seen = set() |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 654 | |
vadimsh@chromium.org | f4c063e | 2013-07-04 14:23:31 +0000 | [diff] [blame] | 655 | def retrieve(isolated_file): |
| 656 | h = isolated_file.obj_hash |
| 657 | if h in seen: |
| 658 | raise ConfigError('IsolatedFile %s is retrieved recursively' % h) |
| 659 | assert h not in pending |
| 660 | seen.add(h) |
| 661 | pending[h] = isolated_file |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 662 | cache.retrieve( |
| 663 | isolateserver.RemoteOperation.HIGH, |
| 664 | h, |
| 665 | isolateserver.UNKNOWN_FILE_SIZE) |
vadimsh@chromium.org | f4c063e | 2013-07-04 14:23:31 +0000 | [diff] [blame] | 666 | |
| 667 | retrieve(self.root) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 668 | |
| 669 | while pending: |
| 670 | item_hash = cache.wait_for(pending) |
| 671 | item = pending.pop(item_hash) |
| 672 | item.load(open(cache.path(item_hash), 'r').read()) |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 673 | if item_hash == root_isolated_hash: |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 674 | # It's the root item. |
| 675 | item.can_fetch = True |
| 676 | |
| 677 | for new_child in item.children: |
vadimsh@chromium.org | f4c063e | 2013-07-04 14:23:31 +0000 | [diff] [blame] | 678 | retrieve(new_child) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 679 | |
| 680 | # Traverse the whole tree to see if files can now be fetched. |
vadimsh@chromium.org | f4c063e | 2013-07-04 14:23:31 +0000 | [diff] [blame] | 681 | self._traverse_tree(cache, self.root) |
| 682 | |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 683 | def check(n): |
| 684 | return all(check(x) for x in n.children) and n.files_fetched |
| 685 | assert check(self.root) |
vadimsh@chromium.org | f4c063e | 2013-07-04 14:23:31 +0000 | [diff] [blame] | 686 | |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 687 | self.relative_cwd = self.relative_cwd or '' |
| 688 | self.read_only = self.read_only or False |
| 689 | |
vadimsh@chromium.org | f4c063e | 2013-07-04 14:23:31 +0000 | [diff] [blame] | 690 | def _traverse_tree(self, cache, node): |
| 691 | if node.can_fetch: |
| 692 | if not node.files_fetched: |
| 693 | self._update_self(cache, node) |
| 694 | will_break = False |
| 695 | for i in node.children: |
| 696 | if not i.can_fetch: |
| 697 | if will_break: |
| 698 | break |
| 699 | # Automatically mark the first one as fetcheable. |
| 700 | i.can_fetch = True |
| 701 | will_break = True |
| 702 | self._traverse_tree(cache, i) |
| 703 | |
| 704 | def _update_self(self, cache, node): |
| 705 | node.fetch_files(cache, self.files) |
| 706 | # Grabs properties. |
| 707 | if not self.command and node.data.get('command'): |
| 708 | self.command = node.data['command'] |
| 709 | if self.read_only is None and node.data.get('read_only') is not None: |
| 710 | self.read_only = node.data['read_only'] |
| 711 | if (self.relative_cwd is None and |
| 712 | node.data.get('relative_cwd') is not None): |
| 713 | self.relative_cwd = node.data['relative_cwd'] |
| 714 | |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 715 | |
csharp@chromium.org | ffd8cf0 | 2013-01-09 21:57:38 +0000 | [diff] [blame] | 716 | def create_directories(base_directory, files): |
| 717 | """Creates the directory structure needed by the given list of files.""" |
| 718 | logging.debug('create_directories(%s, %d)', base_directory, len(files)) |
| 719 | # Creates the tree of directories to create. |
| 720 | directories = set(os.path.dirname(f) for f in files) |
| 721 | for item in list(directories): |
| 722 | while item: |
| 723 | directories.add(item) |
| 724 | item = os.path.dirname(item) |
| 725 | for d in sorted(directories): |
| 726 | if d: |
| 727 | os.mkdir(os.path.join(base_directory, d)) |
| 728 | |
| 729 | |
| 730 | def create_links(base_directory, files): |
| 731 | """Creates any links needed by the given set of files.""" |
| 732 | for filepath, properties in files: |
csharp@chromium.org | 89eaf08 | 2013-03-26 18:56:21 +0000 | [diff] [blame] | 733 | if 'l' not in properties: |
csharp@chromium.org | ffd8cf0 | 2013-01-09 21:57:38 +0000 | [diff] [blame] | 734 | continue |
maruel@chromium.org | 3320ee1 | 2013-03-28 13:23:31 +0000 | [diff] [blame] | 735 | if sys.platform == 'win32': |
| 736 | # TODO(maruel): Create junctions or empty text files similar to what |
| 737 | # cygwin do? |
| 738 | logging.warning('Ignoring symlink %s', filepath) |
| 739 | continue |
csharp@chromium.org | ffd8cf0 | 2013-01-09 21:57:38 +0000 | [diff] [blame] | 740 | outfile = os.path.join(base_directory, filepath) |
| 741 | # symlink doesn't exist on Windows. So the 'link' property should |
| 742 | # never be specified for windows .isolated file. |
| 743 | os.symlink(properties['l'], outfile) # pylint: disable=E1101 |
| 744 | if 'm' in properties: |
| 745 | lchmod = getattr(os, 'lchmod', None) |
| 746 | if lchmod: |
| 747 | lchmod(outfile, properties['m']) |
| 748 | |
| 749 | |
| 750 | def setup_commands(base_directory, cwd, cmd): |
| 751 | """Correctly adjusts and then returns the required working directory |
| 752 | and command needed to run the test. |
| 753 | """ |
| 754 | assert not os.path.isabs(cwd), 'The cwd must be a relative path, got %s' % cwd |
| 755 | cwd = os.path.join(base_directory, cwd) |
| 756 | if not os.path.isdir(cwd): |
| 757 | os.makedirs(cwd) |
| 758 | |
| 759 | # Ensure paths are correctly separated on windows. |
| 760 | cmd[0] = cmd[0].replace('/', os.path.sep) |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 761 | cmd = tools.fix_python_path(cmd) |
csharp@chromium.org | ffd8cf0 | 2013-01-09 21:57:38 +0000 | [diff] [blame] | 762 | |
| 763 | return cwd, cmd |
| 764 | |
| 765 | |
| 766 | def generate_remaining_files(files): |
| 767 | """Generates a dictionary of all the remaining files to be downloaded.""" |
| 768 | remaining = {} |
| 769 | for filepath, props in files: |
| 770 | if 'h' in props: |
| 771 | remaining.setdefault(props['h'], []).append((filepath, props)) |
| 772 | |
| 773 | return remaining |
| 774 | |
| 775 | |
maruel@chromium.org | 3e42ce8 | 2013-09-12 18:36:59 +0000 | [diff] [blame] | 776 | def run_tha_test(isolated_hash, cache_dir, retriever, policies): |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 777 | """Downloads the dependencies in the cache, hardlinks them into a temporary |
| 778 | directory and runs the executable. |
| 779 | """ |
| 780 | settings = Settings() |
maruel@chromium.org | e45728d | 2013-09-16 23:23:22 +0000 | [diff] [blame] | 781 | remote = isolateserver.RemoteOperation(retriever) |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame^] | 782 | algo = hashlib.sha1 |
| 783 | with DiskCache(cache_dir, remote, policies, algo) as cache: |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 784 | outdir = make_temp_dir('run_tha_test', cache_dir) |
| 785 | try: |
| 786 | # Initiate all the files download. |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 787 | with tools.Profiler('GetIsolateds'): |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 788 | # Optionally support local files. |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame^] | 789 | if not isolateserver.is_valid_hash(isolated_hash, algo): |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 790 | # Adds it in the cache. While not strictly necessary, this simplifies |
| 791 | # the rest. |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame^] | 792 | h = isolateserver.hash_file(isolated_hash, algo) |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 793 | cache.add(isolated_hash, h) |
| 794 | isolated_hash = h |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame^] | 795 | settings.load(cache, isolated_hash, algo) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 796 | |
| 797 | if not settings.command: |
| 798 | print >> sys.stderr, 'No command to run' |
| 799 | return 1 |
| 800 | |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 801 | with tools.Profiler('GetRest'): |
csharp@chromium.org | ffd8cf0 | 2013-01-09 21:57:38 +0000 | [diff] [blame] | 802 | create_directories(outdir, settings.files) |
| 803 | create_links(outdir, settings.files.iteritems()) |
| 804 | remaining = generate_remaining_files(settings.files.iteritems()) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 805 | |
| 806 | # Do bookkeeping while files are being downloaded in the background. |
csharp@chromium.org | ffd8cf0 | 2013-01-09 21:57:38 +0000 | [diff] [blame] | 807 | cwd, cmd = setup_commands(outdir, settings.relative_cwd, |
| 808 | settings.command[:]) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 809 | |
| 810 | # Now block on the remaining files to be downloaded and mapped. |
csharp@chromium.org | 9c59ff1 | 2012-12-12 02:32:29 +0000 | [diff] [blame] | 811 | logging.info('Retrieving remaining files') |
| 812 | last_update = time.time() |
vadimsh@chromium.org | b074b16 | 2013-08-22 17:55:46 +0000 | [diff] [blame] | 813 | with threading_utils.DeadlockDetector(DEADLOCK_TIMEOUT) as detector: |
vadimsh@chromium.org | 5db0f4f | 2013-07-04 13:57:02 +0000 | [diff] [blame] | 814 | while remaining: |
| 815 | detector.ping() |
| 816 | obj = cache.wait_for(remaining) |
| 817 | for filepath, properties in remaining.pop(obj): |
| 818 | outfile = os.path.join(outdir, filepath) |
maruel@chromium.org | b7c003d | 2013-07-24 13:04:30 +0000 | [diff] [blame] | 819 | link_file(outfile, cache.path(obj), HARDLINK) |
vadimsh@chromium.org | 5db0f4f | 2013-07-04 13:57:02 +0000 | [diff] [blame] | 820 | if 'm' in properties: |
| 821 | # It's not set on Windows. |
| 822 | os.chmod(outfile, properties['m']) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 823 | |
vadimsh@chromium.org | 5db0f4f | 2013-07-04 13:57:02 +0000 | [diff] [blame] | 824 | if time.time() - last_update > DELAY_BETWEEN_UPDATES_IN_SECS: |
| 825 | msg = '%d files remaining...' % len(remaining) |
| 826 | print msg |
| 827 | logging.info(msg) |
| 828 | last_update = time.time() |
csharp@chromium.org | 9c59ff1 | 2012-12-12 02:32:29 +0000 | [diff] [blame] | 829 | |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 830 | if settings.read_only: |
vadimsh@chromium.org | 5db0f4f | 2013-07-04 13:57:02 +0000 | [diff] [blame] | 831 | logging.info('Making files read only') |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 832 | make_writable(outdir, True) |
| 833 | logging.info('Running %s, cwd=%s' % (cmd, cwd)) |
csharp@chromium.org | e217f30 | 2012-11-22 16:51:53 +0000 | [diff] [blame] | 834 | |
| 835 | # TODO(csharp): This should be specified somewhere else. |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 836 | # TODO(vadimsh): Pass it via 'env_vars' in manifest. |
csharp@chromium.org | e217f30 | 2012-11-22 16:51:53 +0000 | [diff] [blame] | 837 | # Add a rotating log file if one doesn't already exist. |
| 838 | env = os.environ.copy() |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 839 | env.setdefault('RUN_TEST_CASES_LOG_FILE', |
| 840 | os.path.join(MAIN_DIR, RUN_TEST_CASES_LOG)) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 841 | try: |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 842 | with tools.Profiler('RunTest'): |
csharp@chromium.org | e217f30 | 2012-11-22 16:51:53 +0000 | [diff] [blame] | 843 | return subprocess.call(cmd, cwd=cwd, env=env) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 844 | except OSError: |
| 845 | print >> sys.stderr, 'Failed to run %s; cwd=%s' % (cmd, cwd) |
| 846 | raise |
| 847 | finally: |
| 848 | rmtree(outdir) |
| 849 | |
| 850 | |
| 851 | def main(): |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 852 | tools.disable_buffering() |
| 853 | parser = tools.OptionParserWithLogging( |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 854 | usage='%prog <options>', |
| 855 | version=__version__, |
| 856 | log_file=RUN_ISOLATED_LOG_FILE) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 857 | |
| 858 | group = optparse.OptionGroup(parser, 'Data source') |
| 859 | group.add_option( |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 860 | '-s', '--isolated', |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 861 | metavar='FILE', |
| 862 | help='File/url describing what to map or run') |
| 863 | group.add_option( |
| 864 | '-H', '--hash', |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 865 | help='Hash of the .isolated to grab from the hash table') |
maruel@chromium.org | b7e79a2 | 2013-09-13 01:24:56 +0000 | [diff] [blame] | 866 | group.add_option( |
| 867 | '-I', '--isolate-server', metavar='URL', |
| 868 | default= |
| 869 | 'https://isolateserver.appspot.com', |
| 870 | help='Remote where to get the items. Defaults to %default') |
| 871 | group.add_option( |
| 872 | '-n', '--namespace', |
| 873 | default='default-gzip', |
| 874 | help='namespace to use when using isolateserver, default: %default') |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 875 | parser.add_option_group(group) |
| 876 | |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 877 | group = optparse.OptionGroup(parser, 'Cache management') |
| 878 | group.add_option( |
| 879 | '--cache', |
| 880 | default='cache', |
| 881 | metavar='DIR', |
| 882 | help='Cache directory, default=%default') |
| 883 | group.add_option( |
| 884 | '--max-cache-size', |
| 885 | type='int', |
| 886 | metavar='NNN', |
| 887 | default=20*1024*1024*1024, |
| 888 | help='Trim if the cache gets larger than this value, default=%default') |
| 889 | group.add_option( |
| 890 | '--min-free-space', |
| 891 | type='int', |
| 892 | metavar='NNN', |
maruel@chromium.org | 9e98e43 | 2013-05-31 17:06:51 +0000 | [diff] [blame] | 893 | default=2*1024*1024*1024, |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 894 | help='Trim if disk free space becomes lower than this value, ' |
| 895 | 'default=%default') |
| 896 | group.add_option( |
| 897 | '--max-items', |
| 898 | type='int', |
| 899 | metavar='NNN', |
| 900 | default=100000, |
| 901 | help='Trim if more than this number of items are in the cache ' |
| 902 | 'default=%default') |
| 903 | parser.add_option_group(group) |
| 904 | |
| 905 | options, args = parser.parse_args() |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 906 | |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 907 | if bool(options.isolated) == bool(options.hash): |
maruel@chromium.org | 5dd75dd | 2012-12-03 15:11:32 +0000 | [diff] [blame] | 908 | 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] | 909 | 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] | 910 | if args: |
maruel@chromium.org | 5dd75dd | 2012-12-03 15:11:32 +0000 | [diff] [blame] | 911 | logging.debug('Unsupported args %s' % ' '.join(args)) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 912 | parser.error('Unsupported args %s' % ' '.join(args)) |
| 913 | |
csharp@chromium.org | ffd8cf0 | 2013-01-09 21:57:38 +0000 | [diff] [blame] | 914 | options.cache = os.path.abspath(options.cache) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 915 | policies = CachePolicies( |
| 916 | options.max_cache_size, options.min_free_space, options.max_items) |
csharp@chromium.org | ffd8cf0 | 2013-01-09 21:57:38 +0000 | [diff] [blame] | 917 | |
maruel@chromium.org | b7e79a2 | 2013-09-13 01:24:56 +0000 | [diff] [blame] | 918 | retriever = isolateserver.get_storage_api( |
| 919 | options.isolate_server, options.namespace) |
maruel@chromium.org | 3e42ce8 | 2013-09-12 18:36:59 +0000 | [diff] [blame] | 920 | try: |
| 921 | return run_tha_test( |
| 922 | options.isolated or options.hash, |
| 923 | options.cache, |
| 924 | retriever.retrieve, |
| 925 | policies) |
| 926 | except Exception, e: |
| 927 | # Make sure any exception is logged. |
| 928 | logging.exception(e) |
| 929 | return 1 |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 930 | |
| 931 | |
| 932 | if __name__ == '__main__': |
csharp@chromium.org | bfb9874 | 2013-03-26 20:28:36 +0000 | [diff] [blame] | 933 | # Ensure that we are always running with the correct encoding. |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 934 | fix_encoding.fix_encoding() |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 935 | sys.exit(main()) |