maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Marc-Antoine Ruel | 8add124 | 2013-11-05 17:28:27 -0500 | [diff] [blame] | 2 | # Copyright 2012 The Swarming Authors. All rights reserved. |
Marc-Antoine Ruel | e98b112 | 2013-11-05 20:27:57 -0500 | [diff] [blame] | 3 | # Use of this source code is governed under the Apache License, Version 2.0 that |
| 4 | # can be found in the LICENSE file. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 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 | |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 8 | To improve performance, it keeps a local cache. The local cache can safely be |
| 9 | deleted. |
| 10 | |
| 11 | Any ${ISOLATED_OUTDIR} on the command line will be replaced by the location of a |
| 12 | temporary directory upon execution of the command specified in the .isolated |
| 13 | file. All content written to this directory will be uploaded upon termination |
| 14 | and the .isolated file describing this directory will be printed to stdout. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 15 | """ |
| 16 | |
maruel | 49f0468 | 2015-09-03 13:50:26 -0700 | [diff] [blame^] | 17 | __version__ = '0.4.3' |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 18 | |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 19 | import logging |
| 20 | import optparse |
| 21 | import os |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 22 | import sys |
| 23 | import tempfile |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 24 | |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 25 | from third_party.depot_tools import fix_encoding |
| 26 | |
Vadim Shtayura | 6b555c1 | 2014-07-23 16:22:18 -0700 | [diff] [blame] | 27 | from utils import file_path |
Marc-Antoine Ruel | f74cffe | 2015-07-15 15:21:34 -0400 | [diff] [blame] | 28 | from utils import logging_utils |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 29 | from utils import on_error |
Marc-Antoine Ruel | c44f572 | 2015-01-08 16:10:01 -0500 | [diff] [blame] | 30 | from utils import subprocess42 |
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 | |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 34 | import auth |
Marc-Antoine Ruel | 8bee66d | 2014-08-28 19:02:07 -0400 | [diff] [blame] | 35 | import isolated_format |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 36 | import isolateserver |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 37 | |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 38 | |
vadimsh@chromium.org | 8507106 | 2013-08-21 23:37:45 +0000 | [diff] [blame] | 39 | # Absolute path to this file (can be None if running from zip on Mac). |
| 40 | THIS_FILE_PATH = os.path.abspath(__file__) if __file__ else None |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 41 | |
| 42 | # Directory that contains this file (might be inside zip package). |
vadimsh@chromium.org | 8507106 | 2013-08-21 23:37:45 +0000 | [diff] [blame] | 43 | 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] | 44 | |
| 45 | # Directory that contains currently running script file. |
maruel@chromium.org | 814d23f | 2013-10-01 19:08:00 +0000 | [diff] [blame] | 46 | if zip_package.get_main_script_path(): |
| 47 | MAIN_DIR = os.path.dirname( |
| 48 | os.path.abspath(zip_package.get_main_script_path())) |
| 49 | else: |
| 50 | # This happens when 'import run_isolated' is executed at the python |
| 51 | # interactive prompt, in that case __file__ is undefined. |
| 52 | MAIN_DIR = None |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 53 | |
csharp@chromium.org | ff2a466 | 2012-11-21 20:49:32 +0000 | [diff] [blame] | 54 | # The name of the log file to use. |
| 55 | RUN_ISOLATED_LOG_FILE = 'run_isolated.log' |
| 56 | |
csharp@chromium.org | e217f30 | 2012-11-22 16:51:53 +0000 | [diff] [blame] | 57 | # 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] | 58 | RUN_TEST_CASES_LOG = 'run_test_cases.log' |
csharp@chromium.org | e217f30 | 2012-11-22 16:51:53 +0000 | [diff] [blame] | 59 | |
vadimsh@chromium.org | 87d6326 | 2013-04-04 19:34:21 +0000 | [diff] [blame] | 60 | |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 61 | def get_as_zip_package(executable=True): |
| 62 | """Returns ZipPackage with this module and all its dependencies. |
| 63 | |
| 64 | If |executable| is True will store run_isolated.py as __main__.py so that |
| 65 | zip package is directly executable be python. |
| 66 | """ |
| 67 | # Building a zip package when running from another zip package is |
| 68 | # unsupported and probably unneeded. |
| 69 | assert not zip_package.is_zipped_module(sys.modules[__name__]) |
vadimsh@chromium.org | 8507106 | 2013-08-21 23:37:45 +0000 | [diff] [blame] | 70 | assert THIS_FILE_PATH |
| 71 | assert BASE_DIR |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 72 | package = zip_package.ZipPackage(root=BASE_DIR) |
| 73 | package.add_python_file(THIS_FILE_PATH, '__main__.py' if executable else None) |
Marc-Antoine Ruel | 8bee66d | 2014-08-28 19:02:07 -0400 | [diff] [blame] | 74 | package.add_python_file(os.path.join(BASE_DIR, 'isolated_format.py')) |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 75 | package.add_python_file(os.path.join(BASE_DIR, 'isolateserver.py')) |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 76 | package.add_python_file(os.path.join(BASE_DIR, 'auth.py')) |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 77 | package.add_directory(os.path.join(BASE_DIR, 'third_party')) |
| 78 | package.add_directory(os.path.join(BASE_DIR, 'utils')) |
| 79 | return package |
| 80 | |
| 81 | |
Vadim Shtayura | cb0b743 | 2015-07-31 13:26:50 -0700 | [diff] [blame] | 82 | def make_temp_dir(prefix, root_dir=None): |
| 83 | """Returns a temporary directory. |
| 84 | |
| 85 | If root_dir is given and /tmp is on same file system as root_dir, uses /tmp. |
| 86 | Otherwise makes a new temp directory under root_dir. |
| 87 | """ |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 88 | base_temp_dir = None |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 89 | if (root_dir and |
| 90 | not file_path.is_same_filesystem(root_dir, tempfile.gettempdir())): |
Paweł Hajdan, Jr | f7d5872 | 2015-04-27 14:54:42 +0200 | [diff] [blame] | 91 | base_temp_dir = root_dir |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 92 | return tempfile.mkdtemp(prefix=prefix, dir=base_temp_dir) |
| 93 | |
| 94 | |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 95 | def change_tree_read_only(rootdir, read_only): |
| 96 | """Changes the tree read-only bits according to the read_only specification. |
| 97 | |
| 98 | The flag can be 0, 1 or 2, which will affect the possibility to modify files |
| 99 | and create or delete files. |
| 100 | """ |
| 101 | if read_only == 2: |
| 102 | # Files and directories (except on Windows) are marked read only. This |
| 103 | # inhibits modifying, creating or deleting files in the test directory, |
| 104 | # except on Windows where creating and deleting files is still possible. |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 105 | file_path.make_tree_read_only(rootdir) |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 106 | elif read_only == 1: |
| 107 | # Files are marked read only but not the directories. This inhibits |
| 108 | # modifying files but creating or deleting files is still possible. |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 109 | file_path.make_tree_files_read_only(rootdir) |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 110 | elif read_only in (0, None): |
Marc-Antoine Ruel | f1d827c | 2014-11-24 15:22:25 -0500 | [diff] [blame] | 111 | # Anything can be modified. |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 112 | # TODO(maruel): This is currently dangerous as long as DiskCache.touch() |
| 113 | # is not yet changed to verify the hash of the content of the files it is |
| 114 | # looking at, so that if a test modifies an input file, the file must be |
| 115 | # deleted. |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 116 | file_path.make_tree_writeable(rootdir) |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 117 | else: |
| 118 | raise ValueError( |
| 119 | 'change_tree_read_only(%s, %s): Unknown flag %s' % |
| 120 | (rootdir, read_only, read_only)) |
| 121 | |
| 122 | |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 123 | def process_command(command, out_dir): |
| 124 | """Replaces isolated specific variables in a command line.""" |
Vadim Shtayura | 51aba36 | 2014-05-14 15:39:23 -0700 | [diff] [blame] | 125 | filtered = [] |
| 126 | for arg in command: |
| 127 | if '${ISOLATED_OUTDIR}' in arg: |
| 128 | arg = arg.replace('${ISOLATED_OUTDIR}', out_dir).replace('/', os.sep) |
| 129 | filtered.append(arg) |
| 130 | return filtered |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 131 | |
| 132 | |
Marc-Antoine Ruel | 0ec868b | 2015-08-12 14:12:46 -0400 | [diff] [blame] | 133 | def run_tha_test( |
| 134 | isolated_hash, storage, cache, leak_temp_dir, result_json, extra_args): |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 135 | """Downloads the dependencies in the cache, hardlinks them into a temporary |
| 136 | directory and runs the executable from there. |
| 137 | |
| 138 | A temporary directory is created to hold the output files. The content inside |
| 139 | this directory will be uploaded back to |storage| packaged as a .isolated |
| 140 | file. |
| 141 | |
| 142 | Arguments: |
Marc-Antoine Ruel | 35b5843 | 2014-12-08 17:40:40 -0500 | [diff] [blame] | 143 | isolated_hash: the SHA-1 of the .isolated file that must be retrieved to |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 144 | recreate the tree of files to run the target executable. |
| 145 | storage: an isolateserver.Storage object to retrieve remote objects. This |
| 146 | object has a reference to an isolateserver.StorageApi, which does |
| 147 | the actual I/O. |
| 148 | cache: an isolateserver.LocalCache to keep from retrieving the same objects |
| 149 | constantly by caching the objects retrieved. Can be on-disk or |
| 150 | in-memory. |
Kenneth Russell | 61d4235 | 2014-09-15 11:41:16 -0700 | [diff] [blame] | 151 | leak_temp_dir: if true, the temporary directory will be deliberately leaked |
| 152 | for later examination. |
Marc-Antoine Ruel | 0ec868b | 2015-08-12 14:12:46 -0400 | [diff] [blame] | 153 | result_json: file path to dump result metadata into. |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 154 | extra_args: optional arguments to add to the command stated in the .isolate |
| 155 | file. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 156 | """ |
Vadim Shtayura | cb0b743 | 2015-07-31 13:26:50 -0700 | [diff] [blame] | 157 | tmp_root = os.path.dirname(cache.cache_dir) if cache.cache_dir else None |
| 158 | run_dir = make_temp_dir(u'run_tha_test', tmp_root) |
| 159 | out_dir = unicode(make_temp_dir(u'isolated_out', tmp_root)) |
Marc-Antoine Ruel | 3a96379 | 2013-12-11 11:33:49 -0500 | [diff] [blame] | 160 | result = 0 |
maruel@chromium.org | 781ccf6 | 2013-09-17 19:39:47 +0000 | [diff] [blame] | 161 | try: |
maruel@chromium.org | 4f2ebe4 | 2013-09-19 13:09:08 +0000 | [diff] [blame] | 162 | try: |
Vadim Shtayura | 7f7459c | 2014-09-04 13:25:10 -0700 | [diff] [blame] | 163 | bundle = isolateserver.fetch_isolated( |
vadimsh@chromium.org | 7b5dae3 | 2013-10-03 16:59:59 +0000 | [diff] [blame] | 164 | isolated_hash=isolated_hash, |
| 165 | storage=storage, |
| 166 | cache=cache, |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 167 | outdir=run_dir, |
vadimsh@chromium.org | 7b5dae3 | 2013-10-03 16:59:59 +0000 | [diff] [blame] | 168 | require_command=True) |
Marc-Antoine Ruel | 1e7658c | 2014-08-28 19:46:39 -0400 | [diff] [blame] | 169 | except isolated_format.IsolatedError: |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 170 | on_error.report(None) |
Vadim Shtayura | 51aba36 | 2014-05-14 15:39:23 -0700 | [diff] [blame] | 171 | return 1 |
maruel@chromium.org | 781ccf6 | 2013-09-17 19:39:47 +0000 | [diff] [blame] | 172 | |
Vadim Shtayura | 7f7459c | 2014-09-04 13:25:10 -0700 | [diff] [blame] | 173 | change_tree_read_only(run_dir, bundle.read_only) |
| 174 | cwd = os.path.normpath(os.path.join(run_dir, bundle.relative_cwd)) |
| 175 | command = bundle.command + extra_args |
Vadim Shtayura | e4a780b | 2014-01-17 13:18:53 -0800 | [diff] [blame] | 176 | |
John Abd-El-Malek | 3f99868 | 2014-09-17 17:48:09 -0700 | [diff] [blame] | 177 | file_path.ensure_command_has_abs_path(command, cwd) |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 178 | command = process_command(command, out_dir) |
Marc-Antoine Ruel | def5b80 | 2014-01-08 20:57:12 -0500 | [diff] [blame] | 179 | logging.info('Running %s, cwd=%s' % (command, cwd)) |
maruel@chromium.org | 781ccf6 | 2013-09-17 19:39:47 +0000 | [diff] [blame] | 180 | |
| 181 | # TODO(csharp): This should be specified somewhere else. |
| 182 | # TODO(vadimsh): Pass it via 'env_vars' in manifest. |
| 183 | # Add a rotating log file if one doesn't already exist. |
| 184 | env = os.environ.copy() |
maruel@chromium.org | 814d23f | 2013-10-01 19:08:00 +0000 | [diff] [blame] | 185 | if MAIN_DIR: |
| 186 | env.setdefault('RUN_TEST_CASES_LOG_FILE', |
vadimsh@chromium.org | 7b5dae3 | 2013-10-03 16:59:59 +0000 | [diff] [blame] | 187 | os.path.join(MAIN_DIR, RUN_TEST_CASES_LOG)) |
Marc-Antoine Ruel | c44f572 | 2015-01-08 16:10:01 -0500 | [diff] [blame] | 188 | sys.stdout.flush() |
| 189 | with tools.Profiler('RunTest'): |
| 190 | try: |
| 191 | with subprocess42.Popen_with_handler(command, cwd=cwd, env=env) as p: |
| 192 | p.communicate() |
| 193 | result = p.returncode |
| 194 | except OSError: |
| 195 | on_error.report('Failed to run %s; cwd=%s' % (command, cwd)) |
| 196 | result = 1 |
| 197 | logging.info( |
| 198 | 'Command finished with exit code %d (%s)', |
| 199 | result, hex(0xffffffff & result)) |
maruel@chromium.org | 781ccf6 | 2013-09-17 19:39:47 +0000 | [diff] [blame] | 200 | finally: |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 201 | try: |
Kenneth Russell | 61d4235 | 2014-09-15 11:41:16 -0700 | [diff] [blame] | 202 | if leak_temp_dir: |
| 203 | logging.warning('Deliberately leaking %s for later examination', |
| 204 | run_dir) |
| 205 | else: |
| 206 | try: |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 207 | if not file_path.rmtree(run_dir): |
Kenneth Russell | 61d4235 | 2014-09-15 11:41:16 -0700 | [diff] [blame] | 208 | print >> sys.stderr, ( |
| 209 | 'Failed to delete the temporary directory, forcibly failing\n' |
| 210 | 'the task because of it. No zombie process can outlive a\n' |
| 211 | 'successful task run and still be marked as successful.\n' |
| 212 | 'Fix your stuff.') |
| 213 | result = result or 1 |
Vadim Shtayura | cb0b743 | 2015-07-31 13:26:50 -0700 | [diff] [blame] | 214 | except OSError as exc: |
| 215 | logging.error('Leaking run_dir %s: %s', run_dir, exc) |
Kenneth Russell | 61d4235 | 2014-09-15 11:41:16 -0700 | [diff] [blame] | 216 | result = 1 |
Vadim Shtayura | 51aba36 | 2014-05-14 15:39:23 -0700 | [diff] [blame] | 217 | |
| 218 | # HACK(vadimsh): On Windows rmtree(run_dir) call above has |
| 219 | # a synchronization effect: it finishes only when all task child processes |
| 220 | # terminate (since a running process locks *.exe file). Examine out_dir |
| 221 | # only after that call completes (since child processes may |
| 222 | # write to out_dir too and we need to wait for them to finish). |
| 223 | |
| 224 | # Upload out_dir and generate a .isolated file out of this directory. |
| 225 | # It is only done if files were written in the directory. |
Marc-Antoine Ruel | 7eb35c8 | 2015-01-16 20:30:45 -0500 | [diff] [blame] | 226 | if os.path.isdir(out_dir) and os.listdir(out_dir): |
Vadim Shtayura | 51aba36 | 2014-05-14 15:39:23 -0700 | [diff] [blame] | 227 | with tools.Profiler('ArchiveOutput'): |
Marc-Antoine Ruel | 933027e | 2015-05-04 14:20:18 -0400 | [diff] [blame] | 228 | try: |
| 229 | results = isolateserver.archive_files_to_storage( |
| 230 | storage, [out_dir], None) |
| 231 | except isolateserver.Aborted: |
| 232 | # This happens when a signal SIGTERM was received while uploading |
| 233 | # data. There is 2 causes: |
| 234 | # - The task was too slow and was about to be killed anyway due to |
| 235 | # exceeding the hard timeout. |
| 236 | # - The amount of data uploaded back is very large and took too much |
| 237 | # time to archive. |
| 238 | # |
| 239 | # There's 3 options to handle this: |
| 240 | # - Ignore the upload failure as a silent failure. This can be |
| 241 | # detected client side by the fact no result file exists. |
| 242 | # - Return as if the task failed. This is not factually correct. |
| 243 | # - Return an internal failure. Sadly, it's impossible at this level |
| 244 | # at the moment. |
| 245 | # |
| 246 | # For now, silently drop the upload. |
| 247 | # |
| 248 | # In any case, the process only has a very short grace period so it |
| 249 | # needs to exit right away. |
| 250 | sys.stderr.write('Received SIGTERM while uploading') |
| 251 | results = None |
| 252 | |
| 253 | if results: |
Marc-Antoine Ruel | 933027e | 2015-05-04 14:20:18 -0400 | [diff] [blame] | 254 | output_data = { |
| 255 | 'hash': results[0][0], |
| 256 | 'namespace': storage.namespace, |
| 257 | 'storage': storage.location, |
| 258 | } |
| 259 | sys.stdout.flush() |
Marc-Antoine Ruel | 0ec868b | 2015-08-12 14:12:46 -0400 | [diff] [blame] | 260 | # TODO(maruel): Skip this when result_json is set. swarming.py needs |
| 261 | # to be updated first. |
| 262 | data = tools.format_json(output_data, dense=True) |
| 263 | print('[run_isolated_out_hack]%s[/run_isolated_out_hack]' % data) |
| 264 | if result_json: |
| 265 | output_data = { |
| 266 | 'isolated': results[0][0], |
maruel | 49f0468 | 2015-09-03 13:50:26 -0700 | [diff] [blame^] | 267 | 'isolatedserver': storage.location, |
Marc-Antoine Ruel | 0ec868b | 2015-08-12 14:12:46 -0400 | [diff] [blame] | 268 | 'namespace': storage.namespace, |
| 269 | } |
| 270 | tools.write_json(result_json, output_data, dense=True) |
Vadim Shtayura | 51aba36 | 2014-05-14 15:39:23 -0700 | [diff] [blame] | 271 | |
| 272 | finally: |
Marc-Antoine Ruel | af9d837 | 2014-07-21 19:50:57 -0400 | [diff] [blame] | 273 | try: |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 274 | if os.path.isdir(out_dir) and not file_path.rmtree(out_dir): |
Vadim Shtayura | 2866a22 | 2015-07-31 14:44:54 -0700 | [diff] [blame] | 275 | logging.error('Had difficulties removing out_dir %s', out_dir) |
Marc-Antoine Ruel | af9d837 | 2014-07-21 19:50:57 -0400 | [diff] [blame] | 276 | result = result or 1 |
Vadim Shtayura | cb0b743 | 2015-07-31 13:26:50 -0700 | [diff] [blame] | 277 | except OSError as exc: |
| 278 | # Only report on non-Windows or on Windows when the process had |
| 279 | # succeeded. Due to the way file sharing works on Windows, it's sadly |
| 280 | # expected that file deletion may fail when a test failed. |
| 281 | logging.error('Failed to remove out_dir %s: %s', out_dir, exc) |
Marc-Antoine Ruel | fc059e2 | 2015-02-26 14:02:38 -0500 | [diff] [blame] | 282 | if sys.platform != 'win32' or not result: |
| 283 | on_error.report(None) |
Marc-Antoine Ruel | af9d837 | 2014-07-21 19:50:57 -0400 | [diff] [blame] | 284 | result = 1 |
Vadim Shtayura | 51aba36 | 2014-05-14 15:39:23 -0700 | [diff] [blame] | 285 | |
Marc-Antoine Ruel | 3a96379 | 2013-12-11 11:33:49 -0500 | [diff] [blame] | 286 | return result |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 287 | |
| 288 | |
Marc-Antoine Ruel | 90c9816 | 2013-12-18 15:11:57 -0500 | [diff] [blame] | 289 | def main(args): |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 290 | tools.disable_buffering() |
Marc-Antoine Ruel | f74cffe | 2015-07-15 15:21:34 -0400 | [diff] [blame] | 291 | parser = logging_utils.OptionParserWithLogging( |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 292 | usage='%prog <options>', |
| 293 | version=__version__, |
| 294 | log_file=RUN_ISOLATED_LOG_FILE) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 295 | |
Marc-Antoine Ruel | 0ec868b | 2015-08-12 14:12:46 -0400 | [diff] [blame] | 296 | parser.add_option('--json', help='dump output metadata to json file') |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 297 | data_group = optparse.OptionGroup(parser, 'Data source') |
| 298 | data_group.add_option( |
Marc-Antoine Ruel | 185ded4 | 2015-01-28 20:49:18 -0500 | [diff] [blame] | 299 | '-s', '--isolated', |
| 300 | help='Hash of the .isolated to grab from the isolate server') |
Marc-Antoine Ruel | c698ea2 | 2015-01-30 14:03:26 -0800 | [diff] [blame] | 301 | data_group.add_option( |
| 302 | '-H', dest='isolated', help=optparse.SUPPRESS_HELP) |
Marc-Antoine Ruel | f7d737d | 2014-12-10 15:36:29 -0500 | [diff] [blame] | 303 | isolateserver.add_isolate_server_options(data_group) |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 304 | parser.add_option_group(data_group) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 305 | |
Marc-Antoine Ruel | a57d7db | 2014-10-15 20:31:19 -0400 | [diff] [blame] | 306 | isolateserver.add_cache_options(parser) |
| 307 | parser.set_defaults(cache='cache') |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 308 | |
Kenneth Russell | 61d4235 | 2014-09-15 11:41:16 -0700 | [diff] [blame] | 309 | debug_group = optparse.OptionGroup(parser, 'Debugging') |
| 310 | debug_group.add_option( |
| 311 | '--leak-temp-dir', |
| 312 | action='store_true', |
| 313 | help='Deliberately leak isolate\'s temp dir for later examination ' |
| 314 | '[default: %default]') |
| 315 | parser.add_option_group(debug_group) |
| 316 | |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 317 | auth.add_auth_options(parser) |
Marc-Antoine Ruel | 90c9816 | 2013-12-18 15:11:57 -0500 | [diff] [blame] | 318 | options, args = parser.parse_args(args) |
Marc-Antoine Ruel | 185ded4 | 2015-01-28 20:49:18 -0500 | [diff] [blame] | 319 | if not options.isolated: |
| 320 | parser.error('--isolated is required.') |
Vadim Shtayura | 5d1efce | 2014-02-04 10:55:43 -0800 | [diff] [blame] | 321 | auth.process_auth_options(parser, options) |
Marc-Antoine Ruel | e290ada | 2014-12-10 19:48:49 -0500 | [diff] [blame] | 322 | isolateserver.process_isolate_server_options(parser, options, True) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 323 | |
Marc-Antoine Ruel | a57d7db | 2014-10-15 20:31:19 -0400 | [diff] [blame] | 324 | cache = isolateserver.process_cache_options(options) |
Marc-Antoine Ruel | f7d737d | 2014-12-10 15:36:29 -0500 | [diff] [blame] | 325 | with isolateserver.get_storage( |
| 326 | options.isolate_server, options.namespace) as storage: |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 327 | # Hashing schemes used by |storage| and |cache| MUST match. |
| 328 | assert storage.hash_algo == cache.hash_algo |
| 329 | return run_tha_test( |
Marc-Antoine Ruel | 0ec868b | 2015-08-12 14:12:46 -0400 | [diff] [blame] | 330 | options.isolated, storage, cache, options.leak_temp_dir, options.json, |
| 331 | args) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 332 | |
| 333 | |
| 334 | if __name__ == '__main__': |
csharp@chromium.org | bfb9874 | 2013-03-26 20:28:36 +0000 | [diff] [blame] | 335 | # Ensure that we are always running with the correct encoding. |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 336 | fix_encoding.fix_encoding() |
Marc-Antoine Ruel | 90c9816 | 2013-12-18 15:11:57 -0500 | [diff] [blame] | 337 | sys.exit(main(sys.argv[1:])) |