maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +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 | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 6 | """Front end tool to operate on .isolate files. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 7 | |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 8 | This includes creating, merging or compiling them to generate a .isolated file. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 9 | |
| 10 | See more information at |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 11 | https://code.google.com/p/swarming/wiki/IsolateDesign |
| 12 | https://code.google.com/p/swarming/wiki/IsolateUserGuide |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 13 | """ |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 14 | # Run ./isolate.py --help for more detailed information. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 15 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 16 | import ast |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 17 | import copy |
| 18 | import hashlib |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 19 | import itertools |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 20 | import logging |
| 21 | import optparse |
| 22 | import os |
| 23 | import posixpath |
| 24 | import re |
| 25 | import stat |
| 26 | import subprocess |
| 27 | import sys |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 28 | |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 29 | import isolateserver_archive |
maruel@chromium.org | b8375c2 | 2012-10-05 18:10:01 +0000 | [diff] [blame] | 30 | import run_isolated |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 31 | import short_expression_finder |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 32 | import trace_inputs |
| 33 | |
| 34 | # Import here directly so isolate is easier to use as a library. |
maruel@chromium.org | b8375c2 | 2012-10-05 18:10:01 +0000 | [diff] [blame] | 35 | from run_isolated import get_flavor |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 36 | |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 37 | from third_party import colorama |
| 38 | from third_party.depot_tools import fix_encoding |
| 39 | from third_party.depot_tools import subcommand |
| 40 | |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 41 | from utils import tools |
| 42 | |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 43 | |
maruel@chromium.org | 3d67199 | 2013-08-20 00:38:27 +0000 | [diff] [blame] | 44 | __version__ = '0.1' |
| 45 | |
| 46 | |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 47 | PATH_VARIABLES = ('DEPTH', 'PRODUCT_DIR') |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 48 | |
| 49 | # Files that should be 0-length when mapped. |
| 50 | KEY_TOUCHED = 'isolate_dependency_touched' |
| 51 | # Files that should be tracked by the build tool. |
| 52 | KEY_TRACKED = 'isolate_dependency_tracked' |
| 53 | # Files that should not be tracked by the build tool. |
| 54 | KEY_UNTRACKED = 'isolate_dependency_untracked' |
| 55 | |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 56 | |
| 57 | class ExecutionError(Exception): |
| 58 | """A generic error occurred.""" |
| 59 | def __str__(self): |
| 60 | return self.args[0] |
| 61 | |
| 62 | |
| 63 | ### Path handling code. |
| 64 | |
| 65 | |
maruel@chromium.org | 3683afe | 2013-07-27 00:09:27 +0000 | [diff] [blame] | 66 | DEFAULT_BLACKLIST = ( |
| 67 | # Temporary vim or python files. |
| 68 | r'^.+\.(?:pyc|swp)$', |
| 69 | # .git or .svn directory. |
| 70 | r'^(?:.+' + re.escape(os.path.sep) + r'|)\.(?:git|svn)$', |
| 71 | ) |
| 72 | |
| 73 | |
| 74 | # Chromium-specific. |
| 75 | DEFAULT_BLACKLIST += ( |
| 76 | r'^.+\.(?:run_test_cases)$', |
| 77 | r'^(?:.+' + re.escape(os.path.sep) + r'|)testserver\.log$', |
| 78 | ) |
| 79 | |
| 80 | |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 81 | def relpath(path, root): |
| 82 | """os.path.relpath() that keeps trailing os.path.sep.""" |
| 83 | out = os.path.relpath(path, root) |
| 84 | if path.endswith(os.path.sep): |
| 85 | out += os.path.sep |
| 86 | return out |
| 87 | |
| 88 | |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 89 | def safe_relpath(filepath, basepath): |
| 90 | """Do not throw on Windows when filepath and basepath are on different drives. |
| 91 | |
| 92 | Different than relpath() above since this one doesn't keep the trailing |
| 93 | os.path.sep and it swallows exceptions on Windows and return the original |
| 94 | absolute path in the case of different drives. |
| 95 | """ |
| 96 | try: |
| 97 | return os.path.relpath(filepath, basepath) |
| 98 | except ValueError: |
| 99 | assert sys.platform == 'win32' |
| 100 | return filepath |
| 101 | |
| 102 | |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 103 | def normpath(path): |
| 104 | """os.path.normpath() that keeps trailing os.path.sep.""" |
| 105 | out = os.path.normpath(path) |
| 106 | if path.endswith(os.path.sep): |
| 107 | out += os.path.sep |
| 108 | return out |
| 109 | |
| 110 | |
| 111 | def posix_relpath(path, root): |
| 112 | """posix.relpath() that keeps trailing slash.""" |
| 113 | out = posixpath.relpath(path, root) |
| 114 | if path.endswith('/'): |
| 115 | out += '/' |
| 116 | return out |
| 117 | |
| 118 | |
| 119 | def cleanup_path(x): |
| 120 | """Cleans up a relative path. Converts any os.path.sep to '/' on Windows.""" |
| 121 | if x: |
| 122 | x = x.rstrip(os.path.sep).replace(os.path.sep, '/') |
| 123 | if x == '.': |
| 124 | x = '' |
| 125 | if x: |
| 126 | x += '/' |
| 127 | return x |
| 128 | |
| 129 | |
maruel@chromium.org | b9520b0 | 2013-03-13 18:00:03 +0000 | [diff] [blame] | 130 | def is_url(path): |
| 131 | return bool(re.match(r'^https?://.+$', path)) |
| 132 | |
| 133 | |
benrg@chromium.org | 9ae7286 | 2013-02-11 05:05:51 +0000 | [diff] [blame] | 134 | def path_starts_with(prefix, path): |
| 135 | """Returns true if the components of the path |prefix| are the same as the |
| 136 | initial components of |path| (or all of the components of |path|). The paths |
| 137 | must be absolute. |
| 138 | """ |
| 139 | assert os.path.isabs(prefix) and os.path.isabs(path) |
| 140 | prefix = os.path.normpath(prefix) |
| 141 | path = os.path.normpath(path) |
| 142 | assert prefix == trace_inputs.get_native_path_case(prefix), prefix |
| 143 | assert path == trace_inputs.get_native_path_case(path), path |
| 144 | prefix = prefix.rstrip(os.path.sep) + os.path.sep |
| 145 | path = path.rstrip(os.path.sep) + os.path.sep |
| 146 | return path.startswith(prefix) |
| 147 | |
| 148 | |
csharp@chromium.org | f2eacff | 2013-04-04 14:20:20 +0000 | [diff] [blame] | 149 | def fix_native_path_case(root, path): |
| 150 | """Ensures that each component of |path| has the proper native case by |
| 151 | iterating slowly over the directory elements of |path|.""" |
| 152 | native_case_path = root |
| 153 | for raw_part in path.split(os.sep): |
| 154 | if not raw_part or raw_part == '.': |
| 155 | break |
| 156 | |
| 157 | part = trace_inputs.find_item_native_case(native_case_path, raw_part) |
| 158 | if not part: |
| 159 | raise run_isolated.MappingError( |
| 160 | 'Input file %s doesn\'t exist' % |
| 161 | os.path.join(native_case_path, raw_part)) |
| 162 | native_case_path = os.path.join(native_case_path, part) |
| 163 | |
| 164 | return os.path.normpath(native_case_path) |
| 165 | |
| 166 | |
benrg@chromium.org | 9ae7286 | 2013-02-11 05:05:51 +0000 | [diff] [blame] | 167 | def expand_symlinks(indir, relfile): |
| 168 | """Follows symlinks in |relfile|, but treating symlinks that point outside the |
| 169 | build tree as if they were ordinary directories/files. Returns the final |
| 170 | symlink-free target and a list of paths to symlinks encountered in the |
| 171 | process. |
| 172 | |
| 173 | The rule about symlinks outside the build tree is for the benefit of the |
| 174 | Chromium OS ebuild, which symlinks the output directory to an unrelated path |
| 175 | in the chroot. |
| 176 | |
| 177 | Fails when a directory loop is detected, although in theory we could support |
| 178 | that case. |
| 179 | """ |
benrg@chromium.org | 9ae7286 | 2013-02-11 05:05:51 +0000 | [diff] [blame] | 180 | is_directory = relfile.endswith(os.path.sep) |
| 181 | done = indir |
| 182 | todo = relfile.strip(os.path.sep) |
| 183 | symlinks = [] |
| 184 | |
| 185 | while todo: |
| 186 | pre_symlink, symlink, post_symlink = trace_inputs.split_at_symlink( |
| 187 | done, todo) |
| 188 | if not symlink: |
csharp@chromium.org | f2eacff | 2013-04-04 14:20:20 +0000 | [diff] [blame] | 189 | todo = fix_native_path_case(done, todo) |
benrg@chromium.org | 9ae7286 | 2013-02-11 05:05:51 +0000 | [diff] [blame] | 190 | done = os.path.join(done, todo) |
| 191 | break |
| 192 | symlink_path = os.path.join(done, pre_symlink, symlink) |
| 193 | post_symlink = post_symlink.lstrip(os.path.sep) |
| 194 | # readlink doesn't exist on Windows. |
| 195 | # pylint: disable=E1101 |
csharp@chromium.org | f2eacff | 2013-04-04 14:20:20 +0000 | [diff] [blame] | 196 | target = os.path.normpath(os.path.join(done, pre_symlink)) |
| 197 | symlink_target = os.readlink(symlink_path) |
maruel@chromium.org | 28c1967 | 2013-04-29 18:51:09 +0000 | [diff] [blame] | 198 | if os.path.isabs(symlink_target): |
| 199 | # Absolute path are considered a normal directories. The use case is |
| 200 | # generally someone who puts the output directory on a separate drive. |
| 201 | target = symlink_target |
| 202 | else: |
| 203 | # The symlink itself could be using the wrong path case. |
| 204 | target = fix_native_path_case(target, symlink_target) |
csharp@chromium.org | f2eacff | 2013-04-04 14:20:20 +0000 | [diff] [blame] | 205 | |
benrg@chromium.org | 9ae7286 | 2013-02-11 05:05:51 +0000 | [diff] [blame] | 206 | if not os.path.exists(target): |
| 207 | raise run_isolated.MappingError( |
| 208 | 'Symlink target doesn\'t exist: %s -> %s' % (symlink_path, target)) |
| 209 | target = trace_inputs.get_native_path_case(target) |
| 210 | if not path_starts_with(indir, target): |
| 211 | done = symlink_path |
| 212 | todo = post_symlink |
| 213 | continue |
| 214 | if path_starts_with(target, symlink_path): |
| 215 | raise run_isolated.MappingError( |
| 216 | 'Can\'t map recursive symlink reference %s -> %s' % |
| 217 | (symlink_path, target)) |
| 218 | logging.info('Found symlink: %s -> %s', symlink_path, target) |
| 219 | symlinks.append(os.path.relpath(symlink_path, indir)) |
| 220 | # Treat the common prefix of the old and new paths as done, and start |
| 221 | # scanning again. |
| 222 | target = target.split(os.path.sep) |
| 223 | symlink_path = symlink_path.split(os.path.sep) |
| 224 | prefix_length = 0 |
| 225 | for target_piece, symlink_path_piece in zip(target, symlink_path): |
| 226 | if target_piece == symlink_path_piece: |
| 227 | prefix_length += 1 |
| 228 | else: |
| 229 | break |
| 230 | done = os.path.sep.join(target[:prefix_length]) |
| 231 | todo = os.path.join( |
| 232 | os.path.sep.join(target[prefix_length:]), post_symlink) |
| 233 | |
| 234 | relfile = os.path.relpath(done, indir) |
| 235 | relfile = relfile.rstrip(os.path.sep) + is_directory * os.path.sep |
| 236 | return relfile, symlinks |
| 237 | |
| 238 | |
csharp@chromium.org | 84d2e2e | 2013-03-27 13:38:42 +0000 | [diff] [blame] | 239 | def expand_directory_and_symlink(indir, relfile, blacklist, follow_symlinks): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 240 | """Expands a single input. It can result in multiple outputs. |
| 241 | |
benrg@chromium.org | 9ae7286 | 2013-02-11 05:05:51 +0000 | [diff] [blame] | 242 | This function is recursive when relfile is a directory. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 243 | |
| 244 | Note: this code doesn't properly handle recursive symlink like one created |
| 245 | with: |
| 246 | ln -s .. foo |
| 247 | """ |
| 248 | if os.path.isabs(relfile): |
maruel@chromium.org | b8375c2 | 2012-10-05 18:10:01 +0000 | [diff] [blame] | 249 | raise run_isolated.MappingError( |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 250 | 'Can\'t map absolute path %s' % relfile) |
| 251 | |
| 252 | infile = normpath(os.path.join(indir, relfile)) |
| 253 | if not infile.startswith(indir): |
maruel@chromium.org | b8375c2 | 2012-10-05 18:10:01 +0000 | [diff] [blame] | 254 | raise run_isolated.MappingError( |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 255 | 'Can\'t map file %s outside %s' % (infile, indir)) |
| 256 | |
csharp@chromium.org | f972d93 | 2013-03-05 19:29:31 +0000 | [diff] [blame] | 257 | filepath = os.path.join(indir, relfile) |
| 258 | native_filepath = trace_inputs.get_native_path_case(filepath) |
| 259 | if filepath != native_filepath: |
maruel@chromium.org | 59bb2a3 | 2013-03-21 17:08:39 +0000 | [diff] [blame] | 260 | # Special case './'. |
| 261 | if filepath != native_filepath + '.' + os.path.sep: |
maruel@chromium.org | 7f66a98 | 2013-06-06 15:58:59 +0000 | [diff] [blame] | 262 | # Give up enforcing strict path case on OSX. Really, it's that sad. The |
| 263 | # case where it happens is very specific and hard to reproduce: |
| 264 | # get_native_path_case( |
| 265 | # u'Foo.framework/Versions/A/Resources/Something.nib') will return |
| 266 | # u'Foo.framework/Versions/A/resources/Something.nib', e.g. lowercase 'r'. |
| 267 | # |
| 268 | # Note that this is really something deep in OSX because running |
| 269 | # ls Foo.framework/Versions/A |
| 270 | # will print out 'Resources', while trace_inputs.get_native_path_case() |
| 271 | # returns a lower case 'r'. |
| 272 | # |
| 273 | # So *something* is happening under the hood resulting in the command 'ls' |
| 274 | # and Carbon.File.FSPathMakeRef('path').FSRefMakePath() to disagree. We |
| 275 | # have no idea why. |
| 276 | if sys.platform != 'darwin': |
| 277 | raise run_isolated.MappingError( |
| 278 | 'File path doesn\'t equal native file path\n%s != %s' % |
| 279 | (filepath, native_filepath)) |
csharp@chromium.org | f972d93 | 2013-03-05 19:29:31 +0000 | [diff] [blame] | 280 | |
csharp@chromium.org | 84d2e2e | 2013-03-27 13:38:42 +0000 | [diff] [blame] | 281 | symlinks = [] |
| 282 | if follow_symlinks: |
| 283 | relfile, symlinks = expand_symlinks(indir, relfile) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 284 | |
| 285 | if relfile.endswith(os.path.sep): |
| 286 | if not os.path.isdir(infile): |
maruel@chromium.org | b8375c2 | 2012-10-05 18:10:01 +0000 | [diff] [blame] | 287 | raise run_isolated.MappingError( |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 288 | '%s is not a directory but ends with "%s"' % (infile, os.path.sep)) |
| 289 | |
maruel@chromium.org | 59bb2a3 | 2013-03-21 17:08:39 +0000 | [diff] [blame] | 290 | # Special case './'. |
| 291 | if relfile.startswith('.' + os.path.sep): |
| 292 | relfile = relfile[2:] |
benrg@chromium.org | 9ae7286 | 2013-02-11 05:05:51 +0000 | [diff] [blame] | 293 | outfiles = symlinks |
csharp@chromium.org | 63a96d9 | 2013-01-16 19:50:14 +0000 | [diff] [blame] | 294 | try: |
| 295 | for filename in os.listdir(infile): |
| 296 | inner_relfile = os.path.join(relfile, filename) |
| 297 | if blacklist(inner_relfile): |
| 298 | continue |
| 299 | if os.path.isdir(os.path.join(indir, inner_relfile)): |
| 300 | inner_relfile += os.path.sep |
| 301 | outfiles.extend( |
csharp@chromium.org | 84d2e2e | 2013-03-27 13:38:42 +0000 | [diff] [blame] | 302 | expand_directory_and_symlink(indir, inner_relfile, blacklist, |
| 303 | follow_symlinks)) |
csharp@chromium.org | 63a96d9 | 2013-01-16 19:50:14 +0000 | [diff] [blame] | 304 | return outfiles |
| 305 | except OSError as e: |
maruel@chromium.org | 1cd786e | 2013-04-26 18:48:40 +0000 | [diff] [blame] | 306 | raise run_isolated.MappingError( |
| 307 | 'Unable to iterate over directory %s.\n%s' % (infile, e)) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 308 | else: |
| 309 | # Always add individual files even if they were blacklisted. |
| 310 | if os.path.isdir(infile): |
maruel@chromium.org | b8375c2 | 2012-10-05 18:10:01 +0000 | [diff] [blame] | 311 | raise run_isolated.MappingError( |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 312 | 'Input directory %s must have a trailing slash' % infile) |
| 313 | |
| 314 | if not os.path.isfile(infile): |
maruel@chromium.org | b8375c2 | 2012-10-05 18:10:01 +0000 | [diff] [blame] | 315 | raise run_isolated.MappingError( |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 316 | 'Input file %s doesn\'t exist' % infile) |
| 317 | |
benrg@chromium.org | 9ae7286 | 2013-02-11 05:05:51 +0000 | [diff] [blame] | 318 | return symlinks + [relfile] |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 319 | |
| 320 | |
csharp@chromium.org | 0185680 | 2012-11-12 17:48:13 +0000 | [diff] [blame] | 321 | def expand_directories_and_symlinks(indir, infiles, blacklist, |
csharp@chromium.org | 84d2e2e | 2013-03-27 13:38:42 +0000 | [diff] [blame] | 322 | follow_symlinks, ignore_broken_items): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 323 | """Expands the directories and the symlinks, applies the blacklist and |
| 324 | verifies files exist. |
| 325 | |
| 326 | Files are specified in os native path separator. |
| 327 | """ |
| 328 | outfiles = [] |
| 329 | for relfile in infiles: |
csharp@chromium.org | 0185680 | 2012-11-12 17:48:13 +0000 | [diff] [blame] | 330 | try: |
csharp@chromium.org | 84d2e2e | 2013-03-27 13:38:42 +0000 | [diff] [blame] | 331 | outfiles.extend(expand_directory_and_symlink(indir, relfile, blacklist, |
| 332 | follow_symlinks)) |
csharp@chromium.org | 0185680 | 2012-11-12 17:48:13 +0000 | [diff] [blame] | 333 | except run_isolated.MappingError as e: |
| 334 | if ignore_broken_items: |
| 335 | logging.info('warning: %s', e) |
| 336 | else: |
| 337 | raise |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 338 | return outfiles |
| 339 | |
| 340 | |
| 341 | def recreate_tree(outdir, indir, infiles, action, as_sha1): |
| 342 | """Creates a new tree with only the input files in it. |
| 343 | |
| 344 | Arguments: |
| 345 | outdir: Output directory to create the files in. |
| 346 | indir: Root directory the infiles are based in. |
| 347 | infiles: dict of files to map from |indir| to |outdir|. |
maruel@chromium.org | ba6489b | 2013-07-11 20:23:33 +0000 | [diff] [blame] | 348 | action: One of accepted action of run_isolated.link_file(). |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 349 | as_sha1: Output filename is the sha1 instead of relfile. |
| 350 | """ |
| 351 | logging.info( |
| 352 | 'recreate_tree(outdir=%s, indir=%s, files=%d, action=%s, as_sha1=%s)' % |
| 353 | (outdir, indir, len(infiles), action, as_sha1)) |
| 354 | |
maruel@chromium.org | 61a9b3b | 2012-12-12 17:18:52 +0000 | [diff] [blame] | 355 | assert os.path.isabs(outdir) and outdir == os.path.normpath(outdir), outdir |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 356 | if not os.path.isdir(outdir): |
maruel@chromium.org | 61a9b3b | 2012-12-12 17:18:52 +0000 | [diff] [blame] | 357 | logging.info('Creating %s' % outdir) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 358 | os.makedirs(outdir) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 359 | |
| 360 | for relfile, metadata in infiles.iteritems(): |
| 361 | infile = os.path.join(indir, relfile) |
| 362 | if as_sha1: |
| 363 | # Do the hashtable specific checks. |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 364 | if 'l' in metadata: |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 365 | # Skip links when storing a hashtable. |
| 366 | continue |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 367 | outfile = os.path.join(outdir, metadata['h']) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 368 | if os.path.isfile(outfile): |
| 369 | # Just do a quick check that the file size matches. No need to stat() |
| 370 | # again the input file, grab the value from the dict. |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 371 | if not 's' in metadata: |
maruel@chromium.org | 861a5e7 | 2012-10-09 14:49:42 +0000 | [diff] [blame] | 372 | raise run_isolated.MappingError( |
| 373 | 'Misconfigured item %s: %s' % (relfile, metadata)) |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 374 | if metadata['s'] == os.stat(outfile).st_size: |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 375 | continue |
| 376 | else: |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 377 | logging.warn('Overwritting %s' % metadata['h']) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 378 | os.remove(outfile) |
| 379 | else: |
| 380 | outfile = os.path.join(outdir, relfile) |
| 381 | outsubdir = os.path.dirname(outfile) |
| 382 | if not os.path.isdir(outsubdir): |
| 383 | os.makedirs(outsubdir) |
| 384 | |
| 385 | # TODO(csharp): Fix crbug.com/150823 and enable the touched logic again. |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 386 | # if metadata.get('T') == True: |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 387 | # open(outfile, 'ab').close() |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 388 | if 'l' in metadata: |
| 389 | pointed = metadata['l'] |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 390 | logging.debug('Symlink: %s -> %s' % (outfile, pointed)) |
maruel@chromium.org | f43e68b | 2012-10-15 20:23:10 +0000 | [diff] [blame] | 391 | # symlink doesn't exist on Windows. |
| 392 | os.symlink(pointed, outfile) # pylint: disable=E1101 |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 393 | else: |
maruel@chromium.org | b8375c2 | 2012-10-05 18:10:01 +0000 | [diff] [blame] | 394 | run_isolated.link_file(outfile, infile, action) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 395 | |
| 396 | |
maruel@chromium.org | baa108d | 2013-03-28 13:24:51 +0000 | [diff] [blame] | 397 | def process_input(filepath, prevdict, read_only, flavor): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 398 | """Processes an input file, a dependency, and return meta data about it. |
| 399 | |
| 400 | Arguments: |
| 401 | - filepath: File to act on. |
| 402 | - prevdict: the previous dictionary. It is used to retrieve the cached sha-1 |
| 403 | to skip recalculating the hash. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 404 | - read_only: If True, the file mode is manipulated. In practice, only save |
| 405 | one of 4 modes: 0755 (rwx), 0644 (rw), 0555 (rx), 0444 (r). On |
| 406 | windows, mode is not set since all files are 'executable' by |
| 407 | default. |
| 408 | |
| 409 | Behaviors: |
maruel@chromium.org | f2826a3 | 2012-10-16 18:26:17 +0000 | [diff] [blame] | 410 | - Retrieves the file mode, file size, file timestamp, file link |
| 411 | destination if it is a file link and calcultate the SHA-1 of the file's |
| 412 | content if the path points to a file and not a symlink. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 413 | """ |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 414 | out = {} |
| 415 | # TODO(csharp): Fix crbug.com/150823 and enable the touched logic again. |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 416 | # if prevdict.get('T') == True: |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 417 | # # The file's content is ignored. Skip the time and hard code mode. |
| 418 | # if get_flavor() != 'win': |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 419 | # out['m'] = stat.S_IRUSR | stat.S_IRGRP |
| 420 | # out['s'] = 0 |
| 421 | # out['h'] = SHA_1_NULL |
| 422 | # out['T'] = True |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 423 | # return out |
| 424 | |
maruel@chromium.org | f2826a3 | 2012-10-16 18:26:17 +0000 | [diff] [blame] | 425 | # Always check the file stat and check if it is a link. The timestamp is used |
| 426 | # to know if the file's content/symlink destination should be looked into. |
| 427 | # E.g. only reuse from prevdict if the timestamp hasn't changed. |
| 428 | # There is the risk of the file's timestamp being reset to its last value |
| 429 | # manually while its content changed. We don't protect against that use case. |
| 430 | try: |
| 431 | filestats = os.lstat(filepath) |
| 432 | except OSError: |
| 433 | # The file is not present. |
| 434 | raise run_isolated.MappingError('%s is missing' % filepath) |
| 435 | is_link = stat.S_ISLNK(filestats.st_mode) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 436 | |
maruel@chromium.org | baa108d | 2013-03-28 13:24:51 +0000 | [diff] [blame] | 437 | if flavor != 'win': |
maruel@chromium.org | f2826a3 | 2012-10-16 18:26:17 +0000 | [diff] [blame] | 438 | # Ignore file mode on Windows since it's not really useful there. |
| 439 | filemode = stat.S_IMODE(filestats.st_mode) |
| 440 | # Remove write access for group and all access to 'others'. |
| 441 | filemode &= ~(stat.S_IWGRP | stat.S_IRWXO) |
| 442 | if read_only: |
| 443 | filemode &= ~stat.S_IWUSR |
| 444 | if filemode & stat.S_IXUSR: |
| 445 | filemode |= stat.S_IXGRP |
| 446 | else: |
| 447 | filemode &= ~stat.S_IXGRP |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 448 | out['m'] = filemode |
maruel@chromium.org | f2826a3 | 2012-10-16 18:26:17 +0000 | [diff] [blame] | 449 | |
| 450 | # Used to skip recalculating the hash or link destination. Use the most recent |
| 451 | # update time. |
| 452 | # TODO(maruel): Save it in the .state file instead of .isolated so the |
| 453 | # .isolated file is deterministic. |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 454 | out['t'] = int(round(filestats.st_mtime)) |
maruel@chromium.org | f2826a3 | 2012-10-16 18:26:17 +0000 | [diff] [blame] | 455 | |
| 456 | if not is_link: |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 457 | out['s'] = filestats.st_size |
maruel@chromium.org | f2826a3 | 2012-10-16 18:26:17 +0000 | [diff] [blame] | 458 | # If the timestamp wasn't updated and the file size is still the same, carry |
| 459 | # on the sha-1. |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 460 | if (prevdict.get('t') == out['t'] and |
| 461 | prevdict.get('s') == out['s']): |
maruel@chromium.org | f2826a3 | 2012-10-16 18:26:17 +0000 | [diff] [blame] | 462 | # Reuse the previous hash if available. |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 463 | out['h'] = prevdict.get('h') |
| 464 | if not out.get('h'): |
maruel@chromium.org | 6da3877 | 2012-12-11 21:36:37 +0000 | [diff] [blame] | 465 | out['h'] = isolateserver_archive.sha1_file(filepath) |
maruel@chromium.org | f2826a3 | 2012-10-16 18:26:17 +0000 | [diff] [blame] | 466 | else: |
| 467 | # If the timestamp wasn't updated, carry on the link destination. |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 468 | if prevdict.get('t') == out['t']: |
maruel@chromium.org | f2826a3 | 2012-10-16 18:26:17 +0000 | [diff] [blame] | 469 | # Reuse the previous link destination if available. |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 470 | out['l'] = prevdict.get('l') |
| 471 | if out.get('l') is None: |
maruel@chromium.org | 8d159e3 | 2013-04-18 15:29:50 +0000 | [diff] [blame] | 472 | # The link could be in an incorrect path case. In practice, this only |
| 473 | # happen on OSX on case insensitive HFS. |
| 474 | # TODO(maruel): It'd be better if it was only done once, in |
| 475 | # expand_directory_and_symlink(), so it would not be necessary to do again |
| 476 | # here. |
| 477 | symlink_value = os.readlink(filepath) # pylint: disable=E1101 |
| 478 | filedir = trace_inputs.get_native_path_case(os.path.dirname(filepath)) |
| 479 | native_dest = fix_native_path_case(filedir, symlink_value) |
| 480 | out['l'] = os.path.relpath(native_dest, filedir) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 481 | return out |
| 482 | |
| 483 | |
| 484 | ### Variable stuff. |
| 485 | |
| 486 | |
maruel@chromium.org | 4b57f69 | 2012-10-05 20:33:09 +0000 | [diff] [blame] | 487 | def isolatedfile_to_state(filename): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 488 | """Replaces the file's extension.""" |
maruel@chromium.org | 4d52ce4 | 2012-10-05 12:22:35 +0000 | [diff] [blame] | 489 | return filename + '.state' |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 490 | |
| 491 | |
| 492 | def determine_root_dir(relative_root, infiles): |
| 493 | """For a list of infiles, determines the deepest root directory that is |
| 494 | referenced indirectly. |
| 495 | |
| 496 | All arguments must be using os.path.sep. |
| 497 | """ |
| 498 | # The trick used to determine the root directory is to look at "how far" back |
| 499 | # up it is looking up. |
| 500 | deepest_root = relative_root |
| 501 | for i in infiles: |
| 502 | x = relative_root |
| 503 | while i.startswith('..' + os.path.sep): |
| 504 | i = i[3:] |
| 505 | assert not i.startswith(os.path.sep) |
| 506 | x = os.path.dirname(x) |
| 507 | if deepest_root.startswith(x): |
| 508 | deepest_root = x |
| 509 | logging.debug( |
| 510 | 'determine_root_dir(%s, %d files) -> %s' % ( |
| 511 | relative_root, len(infiles), deepest_root)) |
| 512 | return deepest_root |
| 513 | |
| 514 | |
| 515 | def replace_variable(part, variables): |
| 516 | m = re.match(r'<\(([A-Z_]+)\)', part) |
| 517 | if m: |
| 518 | if m.group(1) not in variables: |
| 519 | raise ExecutionError( |
| 520 | 'Variable "%s" was not found in %s.\nDid you forget to specify ' |
| 521 | '--variable?' % (m.group(1), variables)) |
| 522 | return variables[m.group(1)] |
| 523 | return part |
| 524 | |
| 525 | |
maruel@chromium.org | 61a9b3b | 2012-12-12 17:18:52 +0000 | [diff] [blame] | 526 | def process_variables(cwd, variables, relative_base_dir): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 527 | """Processes path variables as a special case and returns a copy of the dict. |
| 528 | |
maruel@chromium.org | 61a9b3b | 2012-12-12 17:18:52 +0000 | [diff] [blame] | 529 | For each 'path' variable: first normalizes it based on |cwd|, verifies it |
| 530 | exists then sets it as relative to relative_base_dir. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 531 | """ |
benrg@chromium.org | 9ae7286 | 2013-02-11 05:05:51 +0000 | [diff] [blame] | 532 | relative_base_dir = trace_inputs.get_native_path_case(relative_base_dir) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 533 | variables = variables.copy() |
| 534 | for i in PATH_VARIABLES: |
| 535 | if i not in variables: |
| 536 | continue |
csharp@chromium.org | dd23b17 | 2013-03-15 16:00:27 +0000 | [diff] [blame] | 537 | variable = variables[i].strip() |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 538 | # Variables could contain / or \ on windows. Always normalize to |
| 539 | # os.path.sep. |
csharp@chromium.org | dd23b17 | 2013-03-15 16:00:27 +0000 | [diff] [blame] | 540 | variable = variable.replace('/', os.path.sep) |
maruel@chromium.org | 61a9b3b | 2012-12-12 17:18:52 +0000 | [diff] [blame] | 541 | variable = os.path.join(cwd, variable) |
| 542 | variable = os.path.normpath(variable) |
benrg@chromium.org | 9ae7286 | 2013-02-11 05:05:51 +0000 | [diff] [blame] | 543 | variable = trace_inputs.get_native_path_case(variable) |
maruel@chromium.org | 61a9b3b | 2012-12-12 17:18:52 +0000 | [diff] [blame] | 544 | if not os.path.isdir(variable): |
| 545 | raise ExecutionError('%s=%s is not a directory' % (i, variable)) |
| 546 | |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 547 | # All variables are relative to the .isolate file. |
maruel@chromium.org | 61a9b3b | 2012-12-12 17:18:52 +0000 | [diff] [blame] | 548 | variable = os.path.relpath(variable, relative_base_dir) |
| 549 | logging.debug( |
| 550 | 'Translated variable %s from %s to %s', i, variables[i], variable) |
| 551 | variables[i] = variable |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 552 | return variables |
| 553 | |
| 554 | |
| 555 | def eval_variables(item, variables): |
| 556 | """Replaces the .isolate variables in a string item. |
| 557 | |
| 558 | Note that the .isolate format is a subset of the .gyp dialect. |
| 559 | """ |
| 560 | return ''.join( |
| 561 | replace_variable(p, variables) for p in re.split(r'(<\([A-Z_]+\))', item)) |
| 562 | |
| 563 | |
| 564 | def classify_files(root_dir, tracked, untracked): |
| 565 | """Converts the list of files into a .isolate 'variables' dictionary. |
| 566 | |
| 567 | Arguments: |
| 568 | - tracked: list of files names to generate a dictionary out of that should |
| 569 | probably be tracked. |
| 570 | - untracked: list of files names that must not be tracked. |
| 571 | """ |
| 572 | # These directories are not guaranteed to be always present on every builder. |
| 573 | OPTIONAL_DIRECTORIES = ( |
| 574 | 'test/data/plugin', |
| 575 | 'third_party/WebKit/LayoutTests', |
| 576 | ) |
| 577 | |
| 578 | new_tracked = [] |
| 579 | new_untracked = list(untracked) |
| 580 | |
| 581 | def should_be_tracked(filepath): |
| 582 | """Returns True if it is a file without whitespace in a non-optional |
| 583 | directory that has no symlink in its path. |
| 584 | """ |
| 585 | if filepath.endswith('/'): |
| 586 | return False |
| 587 | if ' ' in filepath: |
| 588 | return False |
| 589 | if any(i in filepath for i in OPTIONAL_DIRECTORIES): |
| 590 | return False |
| 591 | # Look if any element in the path is a symlink. |
| 592 | split = filepath.split('/') |
| 593 | for i in range(len(split)): |
| 594 | if os.path.islink(os.path.join(root_dir, '/'.join(split[:i+1]))): |
| 595 | return False |
| 596 | return True |
| 597 | |
| 598 | for filepath in sorted(tracked): |
| 599 | if should_be_tracked(filepath): |
| 600 | new_tracked.append(filepath) |
| 601 | else: |
| 602 | # Anything else. |
| 603 | new_untracked.append(filepath) |
| 604 | |
| 605 | variables = {} |
| 606 | if new_tracked: |
| 607 | variables[KEY_TRACKED] = sorted(new_tracked) |
| 608 | if new_untracked: |
| 609 | variables[KEY_UNTRACKED] = sorted(new_untracked) |
| 610 | return variables |
| 611 | |
| 612 | |
maruel@chromium.org | 2bbc9cd | 2012-11-15 19:35:32 +0000 | [diff] [blame] | 613 | def chromium_fix(f, variables): |
| 614 | """Fixes an isolate dependnecy with Chromium-specific fixes.""" |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 615 | # Skip log in PRODUCT_DIR. Note that these are applied on '/' style path |
| 616 | # separator. |
| 617 | LOG_FILE = re.compile(r'^\<\(PRODUCT_DIR\)\/[^\/]+\.log$') |
maruel@chromium.org | a5c1c90 | 2012-11-15 18:47:53 +0000 | [diff] [blame] | 618 | # Ignored items. |
| 619 | IGNORED_ITEMS = ( |
maruel@chromium.org | d37462e | 2012-11-16 14:58:58 +0000 | [diff] [blame] | 620 | # http://crbug.com/160539, on Windows, it's in chrome/. |
maruel@chromium.org | a5c1c90 | 2012-11-15 18:47:53 +0000 | [diff] [blame] | 621 | 'Media Cache/', |
maruel@chromium.org | d37462e | 2012-11-16 14:58:58 +0000 | [diff] [blame] | 622 | 'chrome/Media Cache/', |
maruel@chromium.org | a5c1c90 | 2012-11-15 18:47:53 +0000 | [diff] [blame] | 623 | # 'First Run' is not created by the compile, but by the test itself. |
| 624 | '<(PRODUCT_DIR)/First Run') |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 625 | |
maruel@chromium.org | 2bbc9cd | 2012-11-15 19:35:32 +0000 | [diff] [blame] | 626 | # Blacklist logs and other unimportant files. |
| 627 | if LOG_FILE.match(f) or f in IGNORED_ITEMS: |
| 628 | logging.debug('Ignoring %s', f) |
| 629 | return None |
| 630 | |
maruel@chromium.org | 7650e42 | 2012-11-16 21:56:42 +0000 | [diff] [blame] | 631 | EXECUTABLE = re.compile( |
| 632 | r'^(\<\(PRODUCT_DIR\)\/[^\/\.]+)' + |
| 633 | re.escape(variables.get('EXECUTABLE_SUFFIX', '')) + |
| 634 | r'$') |
| 635 | match = EXECUTABLE.match(f) |
| 636 | if match: |
| 637 | return match.group(1) + '<(EXECUTABLE_SUFFIX)' |
| 638 | |
maruel@chromium.org | 2bbc9cd | 2012-11-15 19:35:32 +0000 | [diff] [blame] | 639 | if sys.platform == 'darwin': |
| 640 | # On OSX, the name of the output is dependent on gyp define, it can be |
| 641 | # 'Google Chrome.app' or 'Chromium.app', same for 'XXX |
| 642 | # Framework.framework'. Furthermore, they are versioned with a gyp |
| 643 | # variable. To lower the complexity of the .isolate file, remove all the |
| 644 | # individual entries that show up under any of the 4 entries and replace |
| 645 | # them with the directory itself. Overall, this results in a bit more |
| 646 | # files than strictly necessary. |
| 647 | OSX_BUNDLES = ( |
| 648 | '<(PRODUCT_DIR)/Chromium Framework.framework/', |
| 649 | '<(PRODUCT_DIR)/Chromium.app/', |
| 650 | '<(PRODUCT_DIR)/Google Chrome Framework.framework/', |
| 651 | '<(PRODUCT_DIR)/Google Chrome.app/', |
| 652 | ) |
| 653 | for prefix in OSX_BUNDLES: |
| 654 | if f.startswith(prefix): |
| 655 | # Note this result in duplicate values, so the a set() must be used to |
| 656 | # remove duplicates. |
| 657 | return prefix |
| 658 | return f |
| 659 | |
| 660 | |
| 661 | def generate_simplified( |
maruel@chromium.org | 3683afe | 2013-07-27 00:09:27 +0000 | [diff] [blame] | 662 | tracked, untracked, touched, root_dir, variables, relative_cwd, |
| 663 | trace_blacklist): |
maruel@chromium.org | 2bbc9cd | 2012-11-15 19:35:32 +0000 | [diff] [blame] | 664 | """Generates a clean and complete .isolate 'variables' dictionary. |
| 665 | |
| 666 | Cleans up and extracts only files from within root_dir then processes |
| 667 | variables and relative_cwd. |
| 668 | """ |
| 669 | root_dir = os.path.realpath(root_dir) |
| 670 | logging.info( |
| 671 | 'generate_simplified(%d files, %s, %s, %s)' % |
| 672 | (len(tracked) + len(untracked) + len(touched), |
| 673 | root_dir, variables, relative_cwd)) |
| 674 | |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 675 | # Preparation work. |
| 676 | relative_cwd = cleanup_path(relative_cwd) |
maruel@chromium.org | ec91af1 | 2012-10-18 20:45:57 +0000 | [diff] [blame] | 677 | assert not os.path.isabs(relative_cwd), relative_cwd |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 678 | # Creates the right set of variables here. We only care about PATH_VARIABLES. |
maruel@chromium.org | 136b05a | 2012-11-20 18:49:44 +0000 | [diff] [blame] | 679 | path_variables = dict( |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 680 | ('<(%s)' % k, variables[k].replace(os.path.sep, '/')) |
| 681 | for k in PATH_VARIABLES if k in variables) |
maruel@chromium.org | 136b05a | 2012-11-20 18:49:44 +0000 | [diff] [blame] | 682 | variables = variables.copy() |
| 683 | variables.update(path_variables) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 684 | |
| 685 | # Actual work: Process the files. |
| 686 | # TODO(maruel): if all the files in a directory are in part tracked and in |
| 687 | # part untracked, the directory will not be extracted. Tracked files should be |
| 688 | # 'promoted' to be untracked as needed. |
| 689 | tracked = trace_inputs.extract_directories( |
maruel@chromium.org | 3683afe | 2013-07-27 00:09:27 +0000 | [diff] [blame] | 690 | root_dir, tracked, trace_blacklist) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 691 | untracked = trace_inputs.extract_directories( |
maruel@chromium.org | 3683afe | 2013-07-27 00:09:27 +0000 | [diff] [blame] | 692 | root_dir, untracked, trace_blacklist) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 693 | # touched is not compressed, otherwise it would result in files to be archived |
| 694 | # that we don't need. |
| 695 | |
maruel@chromium.org | ec91af1 | 2012-10-18 20:45:57 +0000 | [diff] [blame] | 696 | root_dir_posix = root_dir.replace(os.path.sep, '/') |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 697 | def fix(f): |
| 698 | """Bases the file on the most restrictive variable.""" |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 699 | # Important, GYP stores the files with / and not \. |
| 700 | f = f.replace(os.path.sep, '/') |
maruel@chromium.org | ec91af1 | 2012-10-18 20:45:57 +0000 | [diff] [blame] | 701 | logging.debug('fix(%s)' % f) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 702 | # If it's not already a variable. |
| 703 | if not f.startswith('<'): |
| 704 | # relative_cwd is usually the directory containing the gyp file. It may be |
| 705 | # empty if the whole directory containing the gyp file is needed. |
maruel@chromium.org | 8b056ba | 2012-10-16 14:04:49 +0000 | [diff] [blame] | 706 | # Use absolute paths in case cwd_dir is outside of root_dir. |
maruel@chromium.org | ec91af1 | 2012-10-18 20:45:57 +0000 | [diff] [blame] | 707 | # Convert the whole thing to / since it's isolate's speak. |
maruel@chromium.org | 8b056ba | 2012-10-16 14:04:49 +0000 | [diff] [blame] | 708 | f = posix_relpath( |
maruel@chromium.org | ec91af1 | 2012-10-18 20:45:57 +0000 | [diff] [blame] | 709 | posixpath.join(root_dir_posix, f), |
| 710 | posixpath.join(root_dir_posix, relative_cwd)) or './' |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 711 | |
maruel@chromium.org | 136b05a | 2012-11-20 18:49:44 +0000 | [diff] [blame] | 712 | for variable, root_path in path_variables.iteritems(): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 713 | if f.startswith(root_path): |
| 714 | f = variable + f[len(root_path):] |
maruel@chromium.org | 6b365dc | 2012-10-18 19:17:56 +0000 | [diff] [blame] | 715 | logging.debug('Converted to %s' % f) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 716 | break |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 717 | return f |
| 718 | |
maruel@chromium.org | 2bbc9cd | 2012-11-15 19:35:32 +0000 | [diff] [blame] | 719 | def fix_all(items): |
| 720 | """Reduces the items to convert variables, removes unneeded items, apply |
| 721 | chromium-specific fixes and only return unique items. |
| 722 | """ |
| 723 | variables_converted = (fix(f.path) for f in items) |
| 724 | chromium_fixed = (chromium_fix(f, variables) for f in variables_converted) |
| 725 | return set(f for f in chromium_fixed if f) |
| 726 | |
| 727 | tracked = fix_all(tracked) |
| 728 | untracked = fix_all(untracked) |
| 729 | touched = fix_all(touched) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 730 | out = classify_files(root_dir, tracked, untracked) |
| 731 | if touched: |
| 732 | out[KEY_TOUCHED] = sorted(touched) |
| 733 | return out |
| 734 | |
| 735 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 736 | def chromium_filter_flags(variables): |
| 737 | """Filters out build flags used in Chromium that we don't want to treat as |
| 738 | configuration variables. |
| 739 | """ |
| 740 | # TODO(benrg): Need a better way to determine this. |
| 741 | blacklist = set(PATH_VARIABLES + ('EXECUTABLE_SUFFIX', 'FLAG')) |
| 742 | return dict((k, v) for k, v in variables.iteritems() if k not in blacklist) |
| 743 | |
| 744 | |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 745 | def generate_isolate( |
maruel@chromium.org | 3683afe | 2013-07-27 00:09:27 +0000 | [diff] [blame] | 746 | tracked, untracked, touched, root_dir, variables, relative_cwd, |
| 747 | trace_blacklist): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 748 | """Generates a clean and complete .isolate file.""" |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 749 | dependencies = generate_simplified( |
maruel@chromium.org | 3683afe | 2013-07-27 00:09:27 +0000 | [diff] [blame] | 750 | tracked, untracked, touched, root_dir, variables, relative_cwd, |
| 751 | trace_blacklist) |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 752 | config_variables = chromium_filter_flags(variables) |
| 753 | config_variable_names, config_values = zip( |
| 754 | *sorted(config_variables.iteritems())) |
| 755 | out = Configs(None) |
| 756 | # The new dependencies apply to just one configuration, namely config_values. |
| 757 | out.merge_dependencies(dependencies, config_variable_names, [config_values]) |
| 758 | return out.make_isolate_file() |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 759 | |
| 760 | |
| 761 | def split_touched(files): |
| 762 | """Splits files that are touched vs files that are read.""" |
| 763 | tracked = [] |
| 764 | touched = [] |
| 765 | for f in files: |
| 766 | if f.size: |
| 767 | tracked.append(f) |
| 768 | else: |
| 769 | touched.append(f) |
| 770 | return tracked, touched |
| 771 | |
| 772 | |
| 773 | def pretty_print(variables, stdout): |
| 774 | """Outputs a gyp compatible list from the decoded variables. |
| 775 | |
| 776 | Similar to pprint.print() but with NIH syndrome. |
| 777 | """ |
| 778 | # Order the dictionary keys by these keys in priority. |
| 779 | ORDER = ( |
| 780 | 'variables', 'condition', 'command', 'relative_cwd', 'read_only', |
| 781 | KEY_TRACKED, KEY_UNTRACKED) |
| 782 | |
| 783 | def sorting_key(x): |
| 784 | """Gives priority to 'most important' keys before the others.""" |
| 785 | if x in ORDER: |
| 786 | return str(ORDER.index(x)) |
| 787 | return x |
| 788 | |
| 789 | def loop_list(indent, items): |
| 790 | for item in items: |
| 791 | if isinstance(item, basestring): |
| 792 | stdout.write('%s\'%s\',\n' % (indent, item)) |
| 793 | elif isinstance(item, dict): |
| 794 | stdout.write('%s{\n' % indent) |
| 795 | loop_dict(indent + ' ', item) |
| 796 | stdout.write('%s},\n' % indent) |
| 797 | elif isinstance(item, list): |
| 798 | # A list inside a list will write the first item embedded. |
| 799 | stdout.write('%s[' % indent) |
| 800 | for index, i in enumerate(item): |
| 801 | if isinstance(i, basestring): |
| 802 | stdout.write( |
| 803 | '\'%s\', ' % i.replace('\\', '\\\\').replace('\'', '\\\'')) |
| 804 | elif isinstance(i, dict): |
| 805 | stdout.write('{\n') |
| 806 | loop_dict(indent + ' ', i) |
| 807 | if index != len(item) - 1: |
| 808 | x = ', ' |
| 809 | else: |
| 810 | x = '' |
| 811 | stdout.write('%s}%s' % (indent, x)) |
| 812 | else: |
| 813 | assert False |
| 814 | stdout.write('],\n') |
| 815 | else: |
| 816 | assert False |
| 817 | |
| 818 | def loop_dict(indent, items): |
| 819 | for key in sorted(items, key=sorting_key): |
| 820 | item = items[key] |
| 821 | stdout.write("%s'%s': " % (indent, key)) |
| 822 | if isinstance(item, dict): |
| 823 | stdout.write('{\n') |
| 824 | loop_dict(indent + ' ', item) |
| 825 | stdout.write(indent + '},\n') |
| 826 | elif isinstance(item, list): |
| 827 | stdout.write('[\n') |
| 828 | loop_list(indent + ' ', item) |
| 829 | stdout.write(indent + '],\n') |
| 830 | elif isinstance(item, basestring): |
| 831 | stdout.write( |
| 832 | '\'%s\',\n' % item.replace('\\', '\\\\').replace('\'', '\\\'')) |
| 833 | elif item in (True, False, None): |
| 834 | stdout.write('%s\n' % item) |
| 835 | else: |
| 836 | assert False, item |
| 837 | |
| 838 | stdout.write('{\n') |
| 839 | loop_dict(' ', variables) |
| 840 | stdout.write('}\n') |
| 841 | |
| 842 | |
| 843 | def union(lhs, rhs): |
| 844 | """Merges two compatible datastructures composed of dict/list/set.""" |
| 845 | assert lhs is not None or rhs is not None |
| 846 | if lhs is None: |
| 847 | return copy.deepcopy(rhs) |
| 848 | if rhs is None: |
| 849 | return copy.deepcopy(lhs) |
| 850 | assert type(lhs) == type(rhs), (lhs, rhs) |
| 851 | if hasattr(lhs, 'union'): |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 852 | # Includes set, ConfigSettings and Configs. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 853 | return lhs.union(rhs) |
| 854 | if isinstance(lhs, dict): |
| 855 | return dict((k, union(lhs.get(k), rhs.get(k))) for k in set(lhs).union(rhs)) |
| 856 | elif isinstance(lhs, list): |
| 857 | # Do not go inside the list. |
| 858 | return lhs + rhs |
| 859 | assert False, type(lhs) |
| 860 | |
| 861 | |
| 862 | def extract_comment(content): |
| 863 | """Extracts file level comment.""" |
| 864 | out = [] |
| 865 | for line in content.splitlines(True): |
| 866 | if line.startswith('#'): |
| 867 | out.append(line) |
| 868 | else: |
| 869 | break |
| 870 | return ''.join(out) |
| 871 | |
| 872 | |
| 873 | def eval_content(content): |
| 874 | """Evaluates a python file and return the value defined in it. |
| 875 | |
| 876 | Used in practice for .isolate files. |
| 877 | """ |
| 878 | globs = {'__builtins__': None} |
| 879 | locs = {} |
maruel@chromium.org | 8007b8f | 2012-12-14 15:45:18 +0000 | [diff] [blame] | 880 | try: |
| 881 | value = eval(content, globs, locs) |
| 882 | except TypeError as e: |
| 883 | e.args = list(e.args) + [content] |
| 884 | raise |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 885 | assert locs == {}, locs |
| 886 | assert globs == {'__builtins__': None}, globs |
| 887 | return value |
| 888 | |
| 889 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 890 | def match_configs(expr, config_variables, all_configs): |
| 891 | """Returns the configs from |all_configs| that match the |expr|, where |
| 892 | the elements of |all_configs| are tuples of values for the |config_variables|. |
| 893 | Example: |
| 894 | >>> match_configs(expr = "(foo==1 or foo==2) and bar=='b'", |
| 895 | config_variables = ["foo", "bar"], |
| 896 | all_configs = [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]) |
| 897 | [(1, 'b'), (2, 'b')] |
| 898 | """ |
| 899 | return [ |
| 900 | config for config in all_configs |
| 901 | if eval(expr, dict(zip(config_variables, config))) |
| 902 | ] |
| 903 | |
| 904 | |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 905 | def verify_variables(variables): |
| 906 | """Verifies the |variables| dictionary is in the expected format.""" |
| 907 | VALID_VARIABLES = [ |
| 908 | KEY_TOUCHED, |
| 909 | KEY_TRACKED, |
| 910 | KEY_UNTRACKED, |
| 911 | 'command', |
| 912 | 'read_only', |
| 913 | ] |
| 914 | assert isinstance(variables, dict), variables |
| 915 | assert set(VALID_VARIABLES).issuperset(set(variables)), variables.keys() |
| 916 | for name, value in variables.iteritems(): |
| 917 | if name == 'read_only': |
| 918 | assert value in (True, False, None), value |
| 919 | else: |
| 920 | assert isinstance(value, list), value |
| 921 | assert all(isinstance(i, basestring) for i in value), value |
| 922 | |
| 923 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 924 | def verify_ast(expr, variables_and_values): |
| 925 | """Verifies that |expr| is of the form |
| 926 | expr ::= expr ( "or" | "and" ) expr |
| 927 | | identifier "==" ( string | int ) |
| 928 | Also collects the variable identifiers and string/int values in the dict |
| 929 | |variables_and_values|, in the form {'var': set([val1, val2, ...]), ...}. |
| 930 | """ |
| 931 | assert isinstance(expr, (ast.BoolOp, ast.Compare)) |
| 932 | if isinstance(expr, ast.BoolOp): |
| 933 | assert isinstance(expr.op, (ast.And, ast.Or)) |
| 934 | for subexpr in expr.values: |
| 935 | verify_ast(subexpr, variables_and_values) |
| 936 | else: |
| 937 | assert isinstance(expr.left.ctx, ast.Load) |
| 938 | assert len(expr.ops) == 1 |
| 939 | assert isinstance(expr.ops[0], ast.Eq) |
| 940 | var_values = variables_and_values.setdefault(expr.left.id, set()) |
| 941 | rhs = expr.comparators[0] |
| 942 | assert isinstance(rhs, (ast.Str, ast.Num)) |
| 943 | var_values.add(rhs.n if isinstance(rhs, ast.Num) else rhs.s) |
| 944 | |
| 945 | |
| 946 | def verify_condition(condition, variables_and_values): |
| 947 | """Verifies the |condition| dictionary is in the expected format. |
| 948 | See verify_ast() for the meaning of |variables_and_values|. |
| 949 | """ |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 950 | VALID_INSIDE_CONDITION = ['variables'] |
| 951 | assert isinstance(condition, list), condition |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 952 | assert len(condition) == 2, condition |
| 953 | expr, then = condition |
| 954 | |
| 955 | test_ast = compile(expr, '<condition>', 'eval', ast.PyCF_ONLY_AST) |
| 956 | verify_ast(test_ast.body, variables_and_values) |
| 957 | |
| 958 | assert isinstance(then, dict), then |
| 959 | assert set(VALID_INSIDE_CONDITION).issuperset(set(then)), then.keys() |
| 960 | verify_variables(then['variables']) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 961 | |
| 962 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 963 | def verify_root(value, variables_and_values): |
| 964 | """Verifies that |value| is the parsed form of a valid .isolate file. |
| 965 | See verify_ast() for the meaning of |variables_and_values|. |
| 966 | """ |
| 967 | VALID_ROOTS = ['includes', 'conditions'] |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 968 | assert isinstance(value, dict), value |
| 969 | assert set(VALID_ROOTS).issuperset(set(value)), value.keys() |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 970 | |
maruel@chromium.org | 8007b8f | 2012-12-14 15:45:18 +0000 | [diff] [blame] | 971 | includes = value.get('includes', []) |
| 972 | assert isinstance(includes, list), includes |
| 973 | for include in includes: |
| 974 | assert isinstance(include, basestring), include |
| 975 | |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 976 | conditions = value.get('conditions', []) |
| 977 | assert isinstance(conditions, list), conditions |
| 978 | for condition in conditions: |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 979 | verify_condition(condition, variables_and_values) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 980 | |
| 981 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 982 | def remove_weak_dependencies(values, key, item, item_configs): |
| 983 | """Removes any configs from this key if the item is already under a |
| 984 | strong key. |
| 985 | """ |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 986 | if key == KEY_TOUCHED: |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 987 | item_configs = set(item_configs) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 988 | for stronger_key in (KEY_TRACKED, KEY_UNTRACKED): |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 989 | try: |
| 990 | item_configs -= values[stronger_key][item] |
| 991 | except KeyError: |
| 992 | pass |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 993 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 994 | return item_configs |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 995 | |
| 996 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 997 | def remove_repeated_dependencies(folders, key, item, item_configs): |
| 998 | """Removes any configs from this key if the item is in a folder that is |
| 999 | already included.""" |
csharp@chromium.org | 3117625 | 2012-11-02 13:04:40 +0000 | [diff] [blame] | 1000 | |
| 1001 | if key in (KEY_UNTRACKED, KEY_TRACKED, KEY_TOUCHED): |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1002 | item_configs = set(item_configs) |
| 1003 | for (folder, configs) in folders.iteritems(): |
csharp@chromium.org | 3117625 | 2012-11-02 13:04:40 +0000 | [diff] [blame] | 1004 | if folder != item and item.startswith(folder): |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1005 | item_configs -= configs |
csharp@chromium.org | 3117625 | 2012-11-02 13:04:40 +0000 | [diff] [blame] | 1006 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1007 | return item_configs |
csharp@chromium.org | 3117625 | 2012-11-02 13:04:40 +0000 | [diff] [blame] | 1008 | |
| 1009 | |
| 1010 | def get_folders(values_dict): |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1011 | """Returns a dict of all the folders in the given value_dict.""" |
| 1012 | return dict( |
| 1013 | (item, configs) for (item, configs) in values_dict.iteritems() |
| 1014 | if item.endswith('/') |
| 1015 | ) |
csharp@chromium.org | 3117625 | 2012-11-02 13:04:40 +0000 | [diff] [blame] | 1016 | |
| 1017 | |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1018 | def invert_map(variables): |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1019 | """Converts {config: {deptype: list(depvals)}} to |
| 1020 | {deptype: {depval: set(configs)}}. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1021 | """ |
| 1022 | KEYS = ( |
| 1023 | KEY_TOUCHED, |
| 1024 | KEY_TRACKED, |
| 1025 | KEY_UNTRACKED, |
| 1026 | 'command', |
| 1027 | 'read_only', |
| 1028 | ) |
| 1029 | out = dict((key, {}) for key in KEYS) |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1030 | for config, values in variables.iteritems(): |
| 1031 | for key in KEYS: |
| 1032 | if key == 'command': |
| 1033 | items = [tuple(values[key])] if key in values else [] |
| 1034 | elif key == 'read_only': |
| 1035 | items = [values[key]] if key in values else [] |
| 1036 | else: |
| 1037 | assert key in (KEY_TOUCHED, KEY_TRACKED, KEY_UNTRACKED) |
| 1038 | items = values.get(key, []) |
| 1039 | for item in items: |
| 1040 | out[key].setdefault(item, set()).add(config) |
| 1041 | return out |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1042 | |
| 1043 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1044 | def reduce_inputs(values): |
| 1045 | """Reduces the output of invert_map() to the strictest minimum list. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1046 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1047 | Looks at each individual file and directory, maps where they are used and |
| 1048 | reconstructs the inverse dictionary. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1049 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1050 | Returns the minimized dictionary. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1051 | """ |
| 1052 | KEYS = ( |
| 1053 | KEY_TOUCHED, |
| 1054 | KEY_TRACKED, |
| 1055 | KEY_UNTRACKED, |
| 1056 | 'command', |
| 1057 | 'read_only', |
| 1058 | ) |
csharp@chromium.org | 3117625 | 2012-11-02 13:04:40 +0000 | [diff] [blame] | 1059 | |
| 1060 | # Folders can only live in KEY_UNTRACKED. |
| 1061 | folders = get_folders(values.get(KEY_UNTRACKED, {})) |
| 1062 | |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1063 | out = dict((key, {}) for key in KEYS) |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1064 | for key in KEYS: |
| 1065 | for item, item_configs in values.get(key, {}).iteritems(): |
| 1066 | item_configs = remove_weak_dependencies(values, key, item, item_configs) |
| 1067 | item_configs = remove_repeated_dependencies( |
| 1068 | folders, key, item, item_configs) |
| 1069 | if item_configs: |
| 1070 | out[key][item] = item_configs |
| 1071 | return out |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1072 | |
| 1073 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1074 | def convert_map_to_isolate_dict(values, config_variables): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1075 | """Regenerates back a .isolate configuration dict from files and dirs |
| 1076 | mappings generated from reduce_inputs(). |
| 1077 | """ |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1078 | # Gather a list of configurations for set inversion later. |
| 1079 | all_mentioned_configs = set() |
| 1080 | for configs_by_item in values.itervalues(): |
| 1081 | for configs in configs_by_item.itervalues(): |
| 1082 | all_mentioned_configs.update(configs) |
| 1083 | |
| 1084 | # Invert the mapping to make it dict first. |
| 1085 | conditions = {} |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1086 | for key in values: |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1087 | for item, configs in values[key].iteritems(): |
| 1088 | then = conditions.setdefault(frozenset(configs), {}) |
| 1089 | variables = then.setdefault('variables', {}) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1090 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1091 | if item in (True, False): |
| 1092 | # One-off for read_only. |
| 1093 | variables[key] = item |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1094 | else: |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1095 | assert item |
| 1096 | if isinstance(item, tuple): |
| 1097 | # One-off for command. |
| 1098 | # Do not merge lists and do not sort! |
| 1099 | # Note that item is a tuple. |
| 1100 | assert key not in variables |
| 1101 | variables[key] = list(item) |
| 1102 | else: |
| 1103 | # The list of items (files or dirs). Append the new item and keep |
| 1104 | # the list sorted. |
| 1105 | l = variables.setdefault(key, []) |
| 1106 | l.append(item) |
| 1107 | l.sort() |
| 1108 | |
| 1109 | if all_mentioned_configs: |
| 1110 | config_values = map(set, zip(*all_mentioned_configs)) |
| 1111 | sef = short_expression_finder.ShortExpressionFinder( |
| 1112 | zip(config_variables, config_values)) |
| 1113 | |
| 1114 | conditions = sorted( |
| 1115 | [sef.get_expr(configs), then] for configs, then in conditions.iteritems()) |
| 1116 | return {'conditions': conditions} |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1117 | |
| 1118 | |
| 1119 | ### Internal state files. |
| 1120 | |
| 1121 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1122 | class ConfigSettings(object): |
| 1123 | """Represents the dependency variables for a single build configuration. |
| 1124 | The structure is immutable. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1125 | """ |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1126 | def __init__(self, config, values): |
| 1127 | self.config = config |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1128 | verify_variables(values) |
| 1129 | self.touched = sorted(values.get(KEY_TOUCHED, [])) |
| 1130 | self.tracked = sorted(values.get(KEY_TRACKED, [])) |
| 1131 | self.untracked = sorted(values.get(KEY_UNTRACKED, [])) |
| 1132 | self.command = values.get('command', [])[:] |
| 1133 | self.read_only = values.get('read_only') |
| 1134 | |
| 1135 | def union(self, rhs): |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1136 | assert not (self.config and rhs.config) or (self.config == rhs.config) |
maruel@chromium.org | 669edcb | 2012-11-02 19:16:14 +0000 | [diff] [blame] | 1137 | assert not (self.command and rhs.command) or (self.command == rhs.command) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1138 | var = { |
| 1139 | KEY_TOUCHED: sorted(self.touched + rhs.touched), |
| 1140 | KEY_TRACKED: sorted(self.tracked + rhs.tracked), |
| 1141 | KEY_UNTRACKED: sorted(self.untracked + rhs.untracked), |
| 1142 | 'command': self.command or rhs.command, |
| 1143 | 'read_only': rhs.read_only if self.read_only is None else self.read_only, |
| 1144 | } |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1145 | return ConfigSettings(self.config or rhs.config, var) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1146 | |
| 1147 | def flatten(self): |
| 1148 | out = {} |
| 1149 | if self.command: |
| 1150 | out['command'] = self.command |
| 1151 | if self.touched: |
| 1152 | out[KEY_TOUCHED] = self.touched |
| 1153 | if self.tracked: |
| 1154 | out[KEY_TRACKED] = self.tracked |
| 1155 | if self.untracked: |
| 1156 | out[KEY_UNTRACKED] = self.untracked |
| 1157 | if self.read_only is not None: |
| 1158 | out['read_only'] = self.read_only |
| 1159 | return out |
| 1160 | |
| 1161 | |
| 1162 | class Configs(object): |
| 1163 | """Represents a processed .isolate file. |
| 1164 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1165 | Stores the file in a processed way, split by configuration. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1166 | """ |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1167 | def __init__(self, file_comment): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1168 | self.file_comment = file_comment |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1169 | # The keys of by_config are tuples of values for the configuration |
| 1170 | # variables. The names of the variables (which must be the same for |
| 1171 | # every by_config key) are kept in config_variables. Initially by_config |
| 1172 | # is empty and we don't know what configuration variables will be used, |
| 1173 | # so config_variables also starts out empty. It will be set by the first |
| 1174 | # call to union() or merge_dependencies(). |
| 1175 | self.by_config = {} |
| 1176 | self.config_variables = () |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1177 | |
| 1178 | def union(self, rhs): |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1179 | """Adds variables from rhs (a Configs) to the existing variables. |
| 1180 | """ |
| 1181 | config_variables = self.config_variables |
| 1182 | if not config_variables: |
| 1183 | config_variables = rhs.config_variables |
| 1184 | else: |
| 1185 | # We can't proceed if this isn't true since we don't know the correct |
| 1186 | # default values for extra variables. The variables are sorted so we |
| 1187 | # don't need to worry about permutations. |
| 1188 | if rhs.config_variables and rhs.config_variables != config_variables: |
| 1189 | raise ExecutionError( |
| 1190 | 'Variables in merged .isolate files do not match: %r and %r' % ( |
| 1191 | config_variables, rhs.config_variables)) |
| 1192 | |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1193 | # Takes the first file comment, prefering lhs. |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1194 | out = Configs(self.file_comment or rhs.file_comment) |
| 1195 | out.config_variables = config_variables |
| 1196 | for config in set(self.by_config) | set(rhs.by_config): |
| 1197 | out.by_config[config] = union( |
| 1198 | self.by_config.get(config), rhs.by_config.get(config)) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1199 | return out |
| 1200 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1201 | def merge_dependencies(self, values, config_variables, configs): |
| 1202 | """Adds new dependencies to this object for the given configurations. |
| 1203 | Arguments: |
| 1204 | values: A variables dict as found in a .isolate file, e.g., |
| 1205 | {KEY_TOUCHED: [...], 'command': ...}. |
| 1206 | config_variables: An ordered list of configuration variables, e.g., |
| 1207 | ["OS", "chromeos"]. If this object already contains any dependencies, |
| 1208 | the configuration variables must match. |
| 1209 | configs: a list of tuples of values of the configuration variables, |
| 1210 | e.g., [("mac", 0), ("linux", 1)]. The dependencies in |values| |
| 1211 | are added to all of these configurations, and other configurations |
| 1212 | are unchanged. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1213 | """ |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1214 | if not values: |
| 1215 | return |
| 1216 | |
| 1217 | if not self.config_variables: |
| 1218 | self.config_variables = config_variables |
| 1219 | else: |
| 1220 | # See comment in Configs.union(). |
| 1221 | assert self.config_variables == config_variables |
| 1222 | |
| 1223 | for config in configs: |
| 1224 | self.by_config[config] = union( |
| 1225 | self.by_config.get(config), ConfigSettings(config, values)) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1226 | |
| 1227 | def flatten(self): |
| 1228 | """Returns a flat dictionary representation of the configuration. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1229 | """ |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1230 | return dict((k, v.flatten()) for k, v in self.by_config.iteritems()) |
| 1231 | |
| 1232 | def make_isolate_file(self): |
| 1233 | """Returns a dictionary suitable for writing to a .isolate file. |
| 1234 | """ |
| 1235 | dependencies_by_config = self.flatten() |
| 1236 | configs_by_dependency = reduce_inputs(invert_map(dependencies_by_config)) |
| 1237 | return convert_map_to_isolate_dict(configs_by_dependency, |
| 1238 | self.config_variables) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1239 | |
| 1240 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1241 | # TODO(benrg): Remove this function when no old-format files are left. |
| 1242 | def convert_old_to_new_format(value): |
| 1243 | """Converts from the old .isolate format, which only has one variable (OS), |
| 1244 | always includes 'linux', 'mac' and 'win' in the set of valid values for OS, |
| 1245 | and allows conditions that depend on the set of all OSes, to the new format, |
| 1246 | which allows any set of variables, has no hardcoded values, and only allows |
| 1247 | explicit positive tests of variable values. |
| 1248 | """ |
benrg@chromium.org | 7e8e97b | 2013-02-09 03:16:48 +0000 | [diff] [blame] | 1249 | conditions = value.get('conditions', []) |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1250 | if 'variables' not in value and all(len(cond) == 2 for cond in conditions): |
| 1251 | return value # Nothing to change |
| 1252 | |
| 1253 | def parse_condition(cond): |
| 1254 | return re.match(r'OS=="(\w+)"\Z', cond[0]).group(1) |
| 1255 | |
| 1256 | oses = set(map(parse_condition, conditions)) |
| 1257 | default_oses = set(['linux', 'mac', 'win']) |
| 1258 | oses = sorted(oses | default_oses) |
| 1259 | |
| 1260 | def if_not_os(not_os, then): |
| 1261 | expr = ' or '.join('OS=="%s"' % os for os in oses if os != not_os) |
| 1262 | return [expr, then] |
| 1263 | |
benrg@chromium.org | 7e8e97b | 2013-02-09 03:16:48 +0000 | [diff] [blame] | 1264 | conditions = [ |
| 1265 | cond[:2] for cond in conditions if cond[1] |
| 1266 | ] + [ |
| 1267 | if_not_os(parse_condition(cond), cond[2]) |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1268 | for cond in conditions if len(cond) == 3 |
| 1269 | ] |
benrg@chromium.org | 7e8e97b | 2013-02-09 03:16:48 +0000 | [diff] [blame] | 1270 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1271 | if 'variables' in value: |
| 1272 | conditions.append(if_not_os(None, {'variables': value.pop('variables')})) |
| 1273 | conditions.sort() |
| 1274 | |
benrg@chromium.org | 7e8e97b | 2013-02-09 03:16:48 +0000 | [diff] [blame] | 1275 | value = value.copy() |
| 1276 | value['conditions'] = conditions |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1277 | return value |
| 1278 | |
| 1279 | |
| 1280 | def load_isolate_as_config(isolate_dir, value, file_comment): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1281 | """Parses one .isolate file and returns a Configs() instance. |
| 1282 | |
| 1283 | |value| is the loaded dictionary that was defined in the gyp file. |
| 1284 | |
| 1285 | The expected format is strict, anything diverting from the format below will |
| 1286 | throw an assert: |
| 1287 | { |
maruel@chromium.org | 8007b8f | 2012-12-14 15:45:18 +0000 | [diff] [blame] | 1288 | 'includes': [ |
| 1289 | 'foo.isolate', |
| 1290 | ], |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1291 | 'conditions': [ |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1292 | ['OS=="vms" and foo=42', { |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1293 | 'variables': { |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1294 | 'command': [ |
| 1295 | ... |
| 1296 | ], |
| 1297 | 'isolate_dependency_tracked': [ |
| 1298 | ... |
| 1299 | ], |
| 1300 | 'isolate_dependency_untracked': [ |
| 1301 | ... |
| 1302 | ], |
| 1303 | 'read_only': False, |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1304 | }, |
| 1305 | }], |
| 1306 | ... |
| 1307 | ], |
| 1308 | } |
| 1309 | """ |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1310 | value = convert_old_to_new_format(value) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1311 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1312 | variables_and_values = {} |
| 1313 | verify_root(value, variables_and_values) |
| 1314 | if variables_and_values: |
| 1315 | config_variables, config_values = zip( |
| 1316 | *sorted(variables_and_values.iteritems())) |
| 1317 | all_configs = list(itertools.product(*config_values)) |
| 1318 | else: |
| 1319 | config_variables = None |
| 1320 | all_configs = [] |
| 1321 | |
| 1322 | isolate = Configs(file_comment) |
| 1323 | |
| 1324 | # Add configuration-specific variables. |
| 1325 | for expr, then in value.get('conditions', []): |
| 1326 | configs = match_configs(expr, config_variables, all_configs) |
| 1327 | isolate.merge_dependencies(then['variables'], config_variables, configs) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1328 | |
maruel@chromium.org | 8007b8f | 2012-12-14 15:45:18 +0000 | [diff] [blame] | 1329 | # Load the includes. |
| 1330 | for include in value.get('includes', []): |
| 1331 | if os.path.isabs(include): |
| 1332 | raise ExecutionError( |
| 1333 | 'Failed to load configuration; absolute include path \'%s\'' % |
| 1334 | include) |
| 1335 | included_isolate = os.path.normpath(os.path.join(isolate_dir, include)) |
| 1336 | with open(included_isolate, 'r') as f: |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1337 | included_isolate = load_isolate_as_config( |
maruel@chromium.org | 8007b8f | 2012-12-14 15:45:18 +0000 | [diff] [blame] | 1338 | os.path.dirname(included_isolate), |
| 1339 | eval_content(f.read()), |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1340 | None) |
| 1341 | isolate = union(isolate, included_isolate) |
maruel@chromium.org | 8007b8f | 2012-12-14 15:45:18 +0000 | [diff] [blame] | 1342 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1343 | return isolate |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1344 | |
| 1345 | |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1346 | def load_isolate_for_config(isolate_dir, content, variables): |
maruel@chromium.org | 8007b8f | 2012-12-14 15:45:18 +0000 | [diff] [blame] | 1347 | """Loads the .isolate file and returns the information unprocessed but |
| 1348 | filtered for the specific OS. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1349 | |
| 1350 | Returns the command, dependencies and read_only flag. The dependencies are |
| 1351 | fixed to use os.path.sep. |
| 1352 | """ |
| 1353 | # Load the .isolate file, process its conditions, retrieve the command and |
| 1354 | # dependencies. |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1355 | isolate = load_isolate_as_config(isolate_dir, eval_content(content), None) |
| 1356 | try: |
| 1357 | config = tuple(variables[var] for var in isolate.config_variables) |
| 1358 | except KeyError: |
| 1359 | raise ExecutionError( |
| 1360 | 'These configuration variables were missing from the command line: %s' % |
| 1361 | ', '.join(sorted(set(isolate.config_variables) - set(variables)))) |
| 1362 | config = isolate.by_config.get(config) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1363 | if not config: |
csharp@chromium.org | 2a3d7d5 | 2013-03-23 12:54:37 +0000 | [diff] [blame] | 1364 | raise ExecutionError('Failed to load configuration for (%s)' % |
| 1365 | ', '.join(isolate.config_variables)) |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1366 | # Merge tracked and untracked variables, isolate.py doesn't care about the |
| 1367 | # trackability of the variables, only the build tool does. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1368 | dependencies = [ |
| 1369 | f.replace('/', os.path.sep) for f in config.tracked + config.untracked |
| 1370 | ] |
| 1371 | touched = [f.replace('/', os.path.sep) for f in config.touched] |
| 1372 | return config.command, dependencies, touched, config.read_only |
| 1373 | |
| 1374 | |
maruel@chromium.org | dcdbfc8 | 2013-07-25 18:54:57 +0000 | [diff] [blame] | 1375 | def save_isolated(isolated, data): |
| 1376 | """Writes one or multiple .isolated files. |
| 1377 | |
| 1378 | Note: this reference implementation does not create child .isolated file so it |
| 1379 | always returns an empty list. |
| 1380 | |
| 1381 | Returns the list of child isolated files that are included by |isolated|. |
| 1382 | """ |
| 1383 | trace_inputs.write_json(isolated, data, True) |
| 1384 | return [] |
| 1385 | |
| 1386 | |
maruel@chromium.org | d3a1776 | 2012-12-13 14:17:15 +0000 | [diff] [blame] | 1387 | def chromium_save_isolated(isolated, data, variables): |
| 1388 | """Writes one or many .isolated files. |
| 1389 | |
| 1390 | This slightly increases the cold cache cost but greatly reduce the warm cache |
| 1391 | cost by splitting low-churn files off the master .isolated file. It also |
| 1392 | reduces overall isolateserver memcache consumption. |
| 1393 | """ |
| 1394 | slaves = [] |
| 1395 | |
| 1396 | def extract_into_included_isolated(prefix): |
| 1397 | new_slave = {'files': {}, 'os': data['os']} |
| 1398 | for f in data['files'].keys(): |
| 1399 | if f.startswith(prefix): |
| 1400 | new_slave['files'][f] = data['files'].pop(f) |
| 1401 | if new_slave['files']: |
| 1402 | slaves.append(new_slave) |
| 1403 | |
| 1404 | # Split test/data/ in its own .isolated file. |
| 1405 | extract_into_included_isolated(os.path.join('test', 'data', '')) |
| 1406 | |
| 1407 | # Split everything out of PRODUCT_DIR in its own .isolated file. |
| 1408 | if variables.get('PRODUCT_DIR'): |
| 1409 | extract_into_included_isolated(variables['PRODUCT_DIR']) |
| 1410 | |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1411 | files = [] |
maruel@chromium.org | d3a1776 | 2012-12-13 14:17:15 +0000 | [diff] [blame] | 1412 | for index, f in enumerate(slaves): |
| 1413 | slavepath = isolated[:-len('.isolated')] + '.%d.isolated' % index |
| 1414 | trace_inputs.write_json(slavepath, f, True) |
| 1415 | data.setdefault('includes', []).append( |
| 1416 | isolateserver_archive.sha1_file(slavepath)) |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1417 | files.append(os.path.basename(slavepath)) |
maruel@chromium.org | d3a1776 | 2012-12-13 14:17:15 +0000 | [diff] [blame] | 1418 | |
maruel@chromium.org | dcdbfc8 | 2013-07-25 18:54:57 +0000 | [diff] [blame] | 1419 | files.extend(save_isolated(isolated, data)) |
maruel@chromium.org | d3a1776 | 2012-12-13 14:17:15 +0000 | [diff] [blame] | 1420 | return files |
| 1421 | |
| 1422 | |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1423 | class Flattenable(object): |
| 1424 | """Represents data that can be represented as a json file.""" |
| 1425 | MEMBERS = () |
| 1426 | |
| 1427 | def flatten(self): |
| 1428 | """Returns a json-serializable version of itself. |
| 1429 | |
| 1430 | Skips None entries. |
| 1431 | """ |
| 1432 | items = ((member, getattr(self, member)) for member in self.MEMBERS) |
| 1433 | return dict((member, value) for member, value in items if value is not None) |
| 1434 | |
| 1435 | @classmethod |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1436 | def load(cls, data, *args, **kwargs): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1437 | """Loads a flattened version.""" |
| 1438 | data = data.copy() |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1439 | out = cls(*args, **kwargs) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1440 | for member in out.MEMBERS: |
| 1441 | if member in data: |
| 1442 | # Access to a protected member XXX of a client class |
| 1443 | # pylint: disable=W0212 |
| 1444 | out._load_member(member, data.pop(member)) |
| 1445 | if data: |
| 1446 | raise ValueError( |
| 1447 | 'Found unexpected entry %s while constructing an object %s' % |
| 1448 | (data, cls.__name__), data, cls.__name__) |
| 1449 | return out |
| 1450 | |
| 1451 | def _load_member(self, member, value): |
| 1452 | """Loads a member into self.""" |
| 1453 | setattr(self, member, value) |
| 1454 | |
| 1455 | @classmethod |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1456 | def load_file(cls, filename, *args, **kwargs): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1457 | """Loads the data from a file or return an empty instance.""" |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1458 | try: |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1459 | out = cls.load(trace_inputs.read_json(filename), *args, **kwargs) |
| 1460 | logging.debug('Loaded %s(%s)', cls.__name__, filename) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1461 | except (IOError, ValueError): |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1462 | # On failure, loads the default instance. |
| 1463 | out = cls(*args, **kwargs) |
| 1464 | logging.warn('Failed to load %s', filename) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1465 | return out |
| 1466 | |
| 1467 | |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1468 | class SavedState(Flattenable): |
| 1469 | """Describes the content of a .state file. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1470 | |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1471 | This file caches the items calculated by this script and is used to increase |
| 1472 | the performance of the script. This file is not loaded by run_isolated.py. |
| 1473 | This file can always be safely removed. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1474 | |
| 1475 | It is important to note that the 'files' dict keys are using native OS path |
| 1476 | separator instead of '/' used in .isolate file. |
| 1477 | """ |
| 1478 | MEMBERS = ( |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1479 | # Cache of the processed command. This value is saved because .isolated |
| 1480 | # files are never loaded by isolate.py so it's the only way to load the |
| 1481 | # command safely. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1482 | 'command', |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1483 | # Cache of the files found so the next run can skip sha1 calculation. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1484 | 'files', |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1485 | # Path of the original .isolate file. Relative path to isolated_basedir. |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1486 | 'isolate_file', |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1487 | # List of included .isolated files. Used to support/remember 'slave' |
| 1488 | # .isolated files. Relative path to isolated_basedir. |
| 1489 | 'child_isolated_files', |
| 1490 | # If the generated directory tree should be read-only. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1491 | 'read_only', |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1492 | # Relative cwd to use to start the command. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1493 | 'relative_cwd', |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1494 | # GYP variables used to generate the .isolated file. Variables are saved so |
| 1495 | # a user can use isolate.py after building and the GYP variables are still |
| 1496 | # defined. |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1497 | 'variables', |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1498 | ) |
| 1499 | |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1500 | def __init__(self, isolated_basedir): |
| 1501 | """Creates an empty SavedState. |
| 1502 | |
| 1503 | |isolated_basedir| is the directory where the .isolated and .isolated.state |
| 1504 | files are saved. |
| 1505 | """ |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1506 | super(SavedState, self).__init__() |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1507 | assert os.path.isabs(isolated_basedir), isolated_basedir |
| 1508 | assert os.path.isdir(isolated_basedir), isolated_basedir |
| 1509 | self.isolated_basedir = isolated_basedir |
| 1510 | |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1511 | self.command = [] |
| 1512 | self.files = {} |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1513 | self.isolate_file = None |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1514 | self.child_isolated_files = [] |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1515 | self.read_only = None |
| 1516 | self.relative_cwd = None |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1517 | self.variables = {'OS': get_flavor()} |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1518 | |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1519 | def update(self, isolate_file, variables): |
| 1520 | """Updates the saved state with new data to keep GYP variables and internal |
| 1521 | reference to the original .isolate file. |
| 1522 | """ |
maruel@chromium.org | e99c151 | 2013-04-09 20:24:11 +0000 | [diff] [blame] | 1523 | assert os.path.isabs(isolate_file) |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1524 | # Convert back to a relative path. On Windows, if the isolate and |
| 1525 | # isolated files are on different drives, isolate_file will stay an absolute |
| 1526 | # path. |
| 1527 | isolate_file = safe_relpath(isolate_file, self.isolated_basedir) |
| 1528 | |
| 1529 | # The same .isolate file should always be used to generate the .isolated and |
| 1530 | # .isolated.state. |
| 1531 | assert isolate_file == self.isolate_file or not self.isolate_file, ( |
| 1532 | isolate_file, self.isolate_file) |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1533 | self.isolate_file = isolate_file |
| 1534 | self.variables.update(variables) |
| 1535 | |
| 1536 | def update_isolated(self, command, infiles, touched, read_only, relative_cwd): |
| 1537 | """Updates the saved state with data necessary to generate a .isolated file. |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1538 | |
| 1539 | The new files in |infiles| are added to self.files dict but their sha1 is |
| 1540 | not calculated here. |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1541 | """ |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1542 | self.command = command |
| 1543 | # Add new files. |
| 1544 | for f in infiles: |
| 1545 | self.files.setdefault(f, {}) |
| 1546 | for f in touched: |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 1547 | self.files.setdefault(f, {})['T'] = True |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1548 | # Prune extraneous files that are not a dependency anymore. |
| 1549 | for f in set(self.files).difference(set(infiles).union(touched)): |
| 1550 | del self.files[f] |
| 1551 | if read_only is not None: |
| 1552 | self.read_only = read_only |
| 1553 | self.relative_cwd = relative_cwd |
| 1554 | |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1555 | def to_isolated(self): |
| 1556 | """Creates a .isolated dictionary out of the saved state. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1557 | |
maruel@chromium.org | 75c05b4 | 2013-07-25 15:51:48 +0000 | [diff] [blame] | 1558 | https://code.google.com/p/swarming/wiki/IsolateDesign |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1559 | """ |
| 1560 | def strip(data): |
| 1561 | """Returns a 'files' entry with only the whitelisted keys.""" |
| 1562 | return dict((k, data[k]) for k in ('h', 'l', 'm', 's') if k in data) |
| 1563 | |
| 1564 | out = { |
| 1565 | 'files': dict( |
| 1566 | (filepath, strip(data)) for filepath, data in self.files.iteritems()), |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1567 | 'os': self.variables['OS'], |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1568 | } |
| 1569 | if self.command: |
| 1570 | out['command'] = self.command |
| 1571 | if self.read_only is not None: |
| 1572 | out['read_only'] = self.read_only |
| 1573 | if self.relative_cwd: |
| 1574 | out['relative_cwd'] = self.relative_cwd |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1575 | return out |
| 1576 | |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1577 | @property |
| 1578 | def isolate_filepath(self): |
| 1579 | """Returns the absolute path of self.isolate_file.""" |
| 1580 | return os.path.normpath( |
| 1581 | os.path.join(self.isolated_basedir, self.isolate_file)) |
| 1582 | |
| 1583 | # Arguments number differs from overridden method |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1584 | @classmethod |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1585 | def load(cls, data, isolated_basedir): # pylint: disable=W0221 |
| 1586 | """Special case loading to disallow different OS. |
| 1587 | |
| 1588 | It is not possible to load a .isolated.state files from a different OS, this |
| 1589 | file is saved in OS-specific format. |
| 1590 | """ |
| 1591 | out = super(SavedState, cls).load(data, isolated_basedir) |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1592 | if 'os' in data: |
| 1593 | out.variables['OS'] = data['os'] |
| 1594 | if out.variables['OS'] != get_flavor(): |
| 1595 | raise run_isolated.ConfigError( |
| 1596 | 'The .isolated.state file was created on another platform') |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1597 | # The .isolate file must be valid. It could be absolute on Windows if the |
| 1598 | # drive containing the .isolate and the drive containing the .isolated files |
| 1599 | # differ. |
| 1600 | assert not os.path.isabs(out.isolate_file) or sys.platform == 'win32' |
| 1601 | assert os.path.isfile(out.isolate_filepath), out.isolate_filepath |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1602 | return out |
| 1603 | |
| 1604 | def __str__(self): |
| 1605 | out = '%s(\n' % self.__class__.__name__ |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1606 | out += ' command: %s\n' % self.command |
| 1607 | out += ' files: %d\n' % len(self.files) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1608 | out += ' isolate_file: %s\n' % self.isolate_file |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1609 | out += ' read_only: %s\n' % self.read_only |
maruel@chromium.org | 9e9ceaa | 2013-04-05 15:42:42 +0000 | [diff] [blame] | 1610 | out += ' relative_cwd: %s\n' % self.relative_cwd |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1611 | out += ' child_isolated_files: %s\n' % self.child_isolated_files |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1612 | out += ' variables: %s' % ''.join( |
| 1613 | '\n %s=%s' % (k, self.variables[k]) for k in sorted(self.variables)) |
| 1614 | out += ')' |
| 1615 | return out |
| 1616 | |
| 1617 | |
| 1618 | class CompleteState(object): |
| 1619 | """Contains all the state to run the task at hand.""" |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1620 | def __init__(self, isolated_filepath, saved_state): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1621 | super(CompleteState, self).__init__() |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1622 | assert os.path.isabs(isolated_filepath) |
maruel@chromium.org | 4b57f69 | 2012-10-05 20:33:09 +0000 | [diff] [blame] | 1623 | self.isolated_filepath = isolated_filepath |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1624 | # Contains the data to ease developer's use-case but that is not strictly |
| 1625 | # necessary. |
| 1626 | self.saved_state = saved_state |
| 1627 | |
| 1628 | @classmethod |
maruel@chromium.org | 4b57f69 | 2012-10-05 20:33:09 +0000 | [diff] [blame] | 1629 | def load_files(cls, isolated_filepath): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1630 | """Loads state from disk.""" |
maruel@chromium.org | 4b57f69 | 2012-10-05 20:33:09 +0000 | [diff] [blame] | 1631 | assert os.path.isabs(isolated_filepath), isolated_filepath |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1632 | isolated_basedir = os.path.dirname(isolated_filepath) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1633 | return cls( |
maruel@chromium.org | 4b57f69 | 2012-10-05 20:33:09 +0000 | [diff] [blame] | 1634 | isolated_filepath, |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1635 | SavedState.load_file( |
| 1636 | isolatedfile_to_state(isolated_filepath), isolated_basedir)) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1637 | |
maruel@chromium.org | 61a9b3b | 2012-12-12 17:18:52 +0000 | [diff] [blame] | 1638 | def load_isolate(self, cwd, isolate_file, variables, ignore_broken_items): |
maruel@chromium.org | 4b57f69 | 2012-10-05 20:33:09 +0000 | [diff] [blame] | 1639 | """Updates self.isolated and self.saved_state with information loaded from a |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1640 | .isolate file. |
| 1641 | |
| 1642 | Processes the loaded data, deduce root_dir, relative_cwd. |
| 1643 | """ |
| 1644 | # Make sure to not depend on os.getcwd(). |
| 1645 | assert os.path.isabs(isolate_file), isolate_file |
maruel@chromium.org | a3da912 | 2013-03-28 13:27:09 +0000 | [diff] [blame] | 1646 | isolate_file = trace_inputs.get_native_path_case(isolate_file) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1647 | logging.info( |
maruel@chromium.org | 61a9b3b | 2012-12-12 17:18:52 +0000 | [diff] [blame] | 1648 | 'CompleteState.load_isolate(%s, %s, %s, %s)', |
| 1649 | cwd, isolate_file, variables, ignore_broken_items) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1650 | relative_base_dir = os.path.dirname(isolate_file) |
| 1651 | |
| 1652 | # Processes the variables and update the saved state. |
maruel@chromium.org | 61a9b3b | 2012-12-12 17:18:52 +0000 | [diff] [blame] | 1653 | variables = process_variables(cwd, variables, relative_base_dir) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1654 | self.saved_state.update(isolate_file, variables) |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1655 | variables = self.saved_state.variables |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1656 | |
| 1657 | with open(isolate_file, 'r') as f: |
| 1658 | # At that point, variables are not replaced yet in command and infiles. |
| 1659 | # infiles may contain directory entries and is in posix style. |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1660 | command, infiles, touched, read_only = load_isolate_for_config( |
| 1661 | os.path.dirname(isolate_file), f.read(), variables) |
| 1662 | command = [eval_variables(i, variables) for i in command] |
| 1663 | infiles = [eval_variables(f, variables) for f in infiles] |
| 1664 | touched = [eval_variables(f, variables) for f in touched] |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1665 | # root_dir is automatically determined by the deepest root accessed with the |
maruel@chromium.org | 75584e2 | 2013-06-20 01:40:24 +0000 | [diff] [blame] | 1666 | # form '../../foo/bar'. Note that path variables must be taken in account |
| 1667 | # too, add them as if they were input files. |
| 1668 | path_variables = [variables[v] for v in PATH_VARIABLES if v in variables] |
| 1669 | root_dir = determine_root_dir( |
| 1670 | relative_base_dir, infiles + touched + path_variables) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1671 | # The relative directory is automatically determined by the relative path |
| 1672 | # between root_dir and the directory containing the .isolate file, |
| 1673 | # isolate_base_dir. |
| 1674 | relative_cwd = os.path.relpath(relative_base_dir, root_dir) |
benrg@chromium.org | 9ae7286 | 2013-02-11 05:05:51 +0000 | [diff] [blame] | 1675 | # Now that we know where the root is, check that the PATH_VARIABLES point |
| 1676 | # inside it. |
| 1677 | for i in PATH_VARIABLES: |
| 1678 | if i in variables: |
| 1679 | if not path_starts_with( |
| 1680 | root_dir, os.path.join(relative_base_dir, variables[i])): |
| 1681 | raise run_isolated.MappingError( |
maruel@chromium.org | 75584e2 | 2013-06-20 01:40:24 +0000 | [diff] [blame] | 1682 | 'Path variable %s=%r points outside the inferred root directory' |
| 1683 | ' %s' % (i, variables[i], root_dir)) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1684 | # Normalize the files based to root_dir. It is important to keep the |
| 1685 | # trailing os.path.sep at that step. |
| 1686 | infiles = [ |
| 1687 | relpath(normpath(os.path.join(relative_base_dir, f)), root_dir) |
| 1688 | for f in infiles |
| 1689 | ] |
| 1690 | touched = [ |
| 1691 | relpath(normpath(os.path.join(relative_base_dir, f)), root_dir) |
| 1692 | for f in touched |
| 1693 | ] |
csharp@chromium.org | 84d2e2e | 2013-03-27 13:38:42 +0000 | [diff] [blame] | 1694 | follow_symlinks = variables['OS'] != 'win' |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1695 | # Expand the directories by listing each file inside. Up to now, trailing |
| 1696 | # os.path.sep must be kept. Do not expand 'touched'. |
| 1697 | infiles = expand_directories_and_symlinks( |
| 1698 | root_dir, |
| 1699 | infiles, |
csharp@chromium.org | 0185680 | 2012-11-12 17:48:13 +0000 | [diff] [blame] | 1700 | lambda x: re.match(r'.*\.(git|svn|pyc)$', x), |
csharp@chromium.org | 84d2e2e | 2013-03-27 13:38:42 +0000 | [diff] [blame] | 1701 | follow_symlinks, |
csharp@chromium.org | 0185680 | 2012-11-12 17:48:13 +0000 | [diff] [blame] | 1702 | ignore_broken_items) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1703 | |
csharp@chromium.org | bc7c5d1 | 2013-03-21 16:39:15 +0000 | [diff] [blame] | 1704 | # If we ignore broken items then remove any missing touched items. |
| 1705 | if ignore_broken_items: |
| 1706 | original_touched_count = len(touched) |
| 1707 | touched = [touch for touch in touched if os.path.exists(touch)] |
| 1708 | |
| 1709 | if len(touched) != original_touched_count: |
maruel@chromium.org | 1d3a913 | 2013-07-18 20:06:15 +0000 | [diff] [blame] | 1710 | logging.info('Removed %d invalid touched entries', |
csharp@chromium.org | bc7c5d1 | 2013-03-21 16:39:15 +0000 | [diff] [blame] | 1711 | len(touched) - original_touched_count) |
| 1712 | |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1713 | # Finally, update the new data to be able to generate the foo.isolated file, |
| 1714 | # the file that is used by run_isolated.py. |
| 1715 | self.saved_state.update_isolated( |
| 1716 | command, infiles, touched, read_only, relative_cwd) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1717 | logging.debug(self) |
| 1718 | |
maruel@chromium.org | 9268f04 | 2012-10-17 17:36:41 +0000 | [diff] [blame] | 1719 | def process_inputs(self, subdir): |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1720 | """Updates self.saved_state.files with the files' mode and hash. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1721 | |
maruel@chromium.org | 9268f04 | 2012-10-17 17:36:41 +0000 | [diff] [blame] | 1722 | If |subdir| is specified, filters to a subdirectory. The resulting .isolated |
| 1723 | file is tainted. |
| 1724 | |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1725 | See process_input() for more information. |
| 1726 | """ |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1727 | for infile in sorted(self.saved_state.files): |
maruel@chromium.org | 9268f04 | 2012-10-17 17:36:41 +0000 | [diff] [blame] | 1728 | if subdir and not infile.startswith(subdir): |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1729 | self.saved_state.files.pop(infile) |
maruel@chromium.org | 9268f04 | 2012-10-17 17:36:41 +0000 | [diff] [blame] | 1730 | else: |
| 1731 | filepath = os.path.join(self.root_dir, infile) |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1732 | self.saved_state.files[infile] = process_input( |
| 1733 | filepath, |
| 1734 | self.saved_state.files[infile], |
maruel@chromium.org | baa108d | 2013-03-28 13:24:51 +0000 | [diff] [blame] | 1735 | self.saved_state.read_only, |
| 1736 | self.saved_state.variables['OS']) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1737 | |
| 1738 | def save_files(self): |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1739 | """Saves self.saved_state and creates a .isolated file.""" |
maruel@chromium.org | 4b57f69 | 2012-10-05 20:33:09 +0000 | [diff] [blame] | 1740 | logging.debug('Dumping to %s' % self.isolated_filepath) |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1741 | self.saved_state.child_isolated_files = chromium_save_isolated( |
maruel@chromium.org | d3a1776 | 2012-12-13 14:17:15 +0000 | [diff] [blame] | 1742 | self.isolated_filepath, |
| 1743 | self.saved_state.to_isolated(), |
| 1744 | self.saved_state.variables) |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1745 | total_bytes = sum( |
| 1746 | i.get('s', 0) for i in self.saved_state.files.itervalues()) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1747 | if total_bytes: |
maruel@chromium.org | d3a1776 | 2012-12-13 14:17:15 +0000 | [diff] [blame] | 1748 | # TODO(maruel): Stats are missing the .isolated files. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1749 | logging.debug('Total size: %d bytes' % total_bytes) |
maruel@chromium.org | 4b57f69 | 2012-10-05 20:33:09 +0000 | [diff] [blame] | 1750 | saved_state_file = isolatedfile_to_state(self.isolated_filepath) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1751 | logging.debug('Dumping to %s' % saved_state_file) |
| 1752 | trace_inputs.write_json(saved_state_file, self.saved_state.flatten(), True) |
| 1753 | |
| 1754 | @property |
| 1755 | def root_dir(self): |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1756 | """Returns the absolute path of the root_dir to reference the .isolate file |
| 1757 | via relative_cwd. |
| 1758 | |
| 1759 | So that join(root_dir, relative_cwd, basename(isolate_file)) is equivalent |
| 1760 | to isolate_filepath. |
| 1761 | """ |
maruel@chromium.org | b253fb8 | 2012-10-16 21:44:48 +0000 | [diff] [blame] | 1762 | if not self.saved_state.isolate_file: |
| 1763 | raise ExecutionError('Please specify --isolate') |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1764 | isolate_dir = os.path.dirname(self.saved_state.isolate_filepath) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1765 | # Special case '.'. |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1766 | if self.saved_state.relative_cwd == '.': |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1767 | root_dir = isolate_dir |
| 1768 | else: |
| 1769 | assert isolate_dir.endswith(self.saved_state.relative_cwd), ( |
| 1770 | isolate_dir, self.saved_state.relative_cwd) |
| 1771 | # Walk back back to the root directory. |
| 1772 | root_dir = isolate_dir[:-(len(self.saved_state.relative_cwd) + 1)] |
| 1773 | return trace_inputs.get_native_path_case(root_dir) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1774 | |
| 1775 | @property |
| 1776 | def resultdir(self): |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1777 | """Returns the absolute path containing the .isolated file. |
| 1778 | |
| 1779 | It is usually equivalent to the variable PRODUCT_DIR. Uses the .isolated |
| 1780 | path as the value. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1781 | """ |
maruel@chromium.org | 4b57f69 | 2012-10-05 20:33:09 +0000 | [diff] [blame] | 1782 | return os.path.dirname(self.isolated_filepath) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1783 | |
| 1784 | def __str__(self): |
| 1785 | def indent(data, indent_length): |
| 1786 | """Indents text.""" |
| 1787 | spacing = ' ' * indent_length |
| 1788 | return ''.join(spacing + l for l in str(data).splitlines(True)) |
| 1789 | |
| 1790 | out = '%s(\n' % self.__class__.__name__ |
| 1791 | out += ' root_dir: %s\n' % self.root_dir |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1792 | out += ' saved_state: %s)' % indent(self.saved_state, 2) |
| 1793 | return out |
| 1794 | |
| 1795 | |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1796 | def load_complete_state(options, cwd, subdir, skip_update): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1797 | """Loads a CompleteState. |
| 1798 | |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1799 | This includes data from .isolate and .isolated.state files. Never reads the |
| 1800 | .isolated file. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1801 | |
| 1802 | Arguments: |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1803 | options: Options instance generated with OptionParserIsolate. For either |
| 1804 | options.isolate and options.isolated, if the value is set, it is an |
| 1805 | absolute path. |
| 1806 | cwd: base directory to be used when loading the .isolate file. |
| 1807 | subdir: optional argument to only process file in the subdirectory, relative |
| 1808 | to CompleteState.root_dir. |
| 1809 | skip_update: Skip trying to load the .isolate file and processing the |
| 1810 | dependencies. It is useful when not needed, like when tracing. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1811 | """ |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1812 | assert not options.isolate or os.path.isabs(options.isolate) |
| 1813 | assert not options.isolated or os.path.isabs(options.isolated) |
maruel@chromium.org | a3da912 | 2013-03-28 13:27:09 +0000 | [diff] [blame] | 1814 | cwd = trace_inputs.get_native_path_case(unicode(cwd)) |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 1815 | if options.isolated: |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1816 | # Load the previous state if it was present. Namely, "foo.isolated.state". |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1817 | # Note: this call doesn't load the .isolate file. |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 1818 | complete_state = CompleteState.load_files(options.isolated) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1819 | else: |
| 1820 | # Constructs a dummy object that cannot be saved. Useful for temporary |
| 1821 | # commands like 'run'. |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1822 | complete_state = CompleteState(None, SavedState()) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1823 | |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1824 | if not options.isolate: |
| 1825 | if not complete_state.saved_state.isolate_file: |
| 1826 | if not skip_update: |
| 1827 | raise ExecutionError('A .isolate file is required.') |
| 1828 | isolate = None |
| 1829 | else: |
| 1830 | isolate = complete_state.saved_state.isolate_filepath |
| 1831 | else: |
| 1832 | isolate = options.isolate |
| 1833 | if complete_state.saved_state.isolate_file: |
| 1834 | rel_isolate = safe_relpath( |
| 1835 | options.isolate, complete_state.saved_state.isolated_basedir) |
| 1836 | if rel_isolate != complete_state.saved_state.isolate_file: |
| 1837 | raise ExecutionError( |
| 1838 | '%s and %s do not match.' % ( |
| 1839 | options.isolate, complete_state.saved_state.isolate_file)) |
| 1840 | |
| 1841 | if not skip_update: |
| 1842 | # Then load the .isolate and expands directories. |
| 1843 | complete_state.load_isolate( |
| 1844 | cwd, isolate, options.variables, options.ignore_broken_items) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1845 | |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1846 | # Regenerate complete_state.saved_state.files. |
maruel@chromium.org | 9268f04 | 2012-10-17 17:36:41 +0000 | [diff] [blame] | 1847 | if subdir: |
maruel@chromium.org | 306e0e7 | 2012-11-02 18:22:03 +0000 | [diff] [blame] | 1848 | subdir = unicode(subdir) |
maruel@chromium.org | 9268f04 | 2012-10-17 17:36:41 +0000 | [diff] [blame] | 1849 | subdir = eval_variables(subdir, complete_state.saved_state.variables) |
| 1850 | subdir = subdir.replace('/', os.path.sep) |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1851 | |
| 1852 | if not skip_update: |
| 1853 | complete_state.process_inputs(subdir) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1854 | return complete_state |
| 1855 | |
| 1856 | |
maruel@chromium.org | 3683afe | 2013-07-27 00:09:27 +0000 | [diff] [blame] | 1857 | def read_trace_as_isolate_dict(complete_state, trace_blacklist): |
maruel@chromium.org | e27c65f | 2012-10-22 13:56:53 +0000 | [diff] [blame] | 1858 | """Reads a trace and returns the .isolate dictionary. |
| 1859 | |
| 1860 | Returns exceptions during the log parsing so it can be re-raised. |
| 1861 | """ |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1862 | api = trace_inputs.get_api() |
maruel@chromium.org | 4b57f69 | 2012-10-05 20:33:09 +0000 | [diff] [blame] | 1863 | logfile = complete_state.isolated_filepath + '.log' |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1864 | if not os.path.isfile(logfile): |
| 1865 | raise ExecutionError( |
| 1866 | 'No log file \'%s\' to read, did you forget to \'trace\'?' % logfile) |
| 1867 | try: |
maruel@chromium.org | 3683afe | 2013-07-27 00:09:27 +0000 | [diff] [blame] | 1868 | data = api.parse_log(logfile, trace_blacklist, None) |
maruel@chromium.org | e27c65f | 2012-10-22 13:56:53 +0000 | [diff] [blame] | 1869 | exceptions = [i['exception'] for i in data if 'exception' in i] |
| 1870 | results = (i['results'] for i in data if 'results' in i) |
| 1871 | results_stripped = (i.strip_root(complete_state.root_dir) for i in results) |
| 1872 | files = set(sum((result.existent for result in results_stripped), [])) |
| 1873 | tracked, touched = split_touched(files) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1874 | value = generate_isolate( |
| 1875 | tracked, |
| 1876 | [], |
| 1877 | touched, |
| 1878 | complete_state.root_dir, |
| 1879 | complete_state.saved_state.variables, |
maruel@chromium.org | 3683afe | 2013-07-27 00:09:27 +0000 | [diff] [blame] | 1880 | complete_state.saved_state.relative_cwd, |
| 1881 | trace_blacklist) |
maruel@chromium.org | e27c65f | 2012-10-22 13:56:53 +0000 | [diff] [blame] | 1882 | return value, exceptions |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1883 | except trace_inputs.TracingFailure, e: |
| 1884 | raise ExecutionError( |
| 1885 | 'Reading traces failed for: %s\n%s' % |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1886 | (' '.join(complete_state.saved_state.command), str(e))) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1887 | |
| 1888 | |
| 1889 | def print_all(comment, data, stream): |
| 1890 | """Prints a complete .isolate file and its top-level file comment into a |
| 1891 | stream. |
| 1892 | """ |
| 1893 | if comment: |
| 1894 | stream.write(comment) |
| 1895 | pretty_print(data, stream) |
| 1896 | |
| 1897 | |
maruel@chromium.org | 3683afe | 2013-07-27 00:09:27 +0000 | [diff] [blame] | 1898 | def merge(complete_state, trace_blacklist): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1899 | """Reads a trace and merges it back into the source .isolate file.""" |
maruel@chromium.org | 3683afe | 2013-07-27 00:09:27 +0000 | [diff] [blame] | 1900 | value, exceptions = read_trace_as_isolate_dict( |
| 1901 | complete_state, trace_blacklist) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1902 | |
| 1903 | # Now take that data and union it into the original .isolate file. |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1904 | with open(complete_state.saved_state.isolate_filepath, 'r') as f: |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1905 | prev_content = f.read() |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1906 | isolate_dir = os.path.dirname(complete_state.saved_state.isolate_filepath) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1907 | prev_config = load_isolate_as_config( |
maruel@chromium.org | 8007b8f | 2012-12-14 15:45:18 +0000 | [diff] [blame] | 1908 | isolate_dir, |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1909 | eval_content(prev_content), |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1910 | extract_comment(prev_content)) |
| 1911 | new_config = load_isolate_as_config(isolate_dir, value, '') |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1912 | config = union(prev_config, new_config) |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 1913 | data = config.make_isolate_file() |
maruel@chromium.org | ec91af1 | 2012-10-18 20:45:57 +0000 | [diff] [blame] | 1914 | print('Updating %s' % complete_state.saved_state.isolate_file) |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1915 | with open(complete_state.saved_state.isolate_filepath, 'wb') as f: |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1916 | print_all(config.file_comment, data, f) |
maruel@chromium.org | e27c65f | 2012-10-22 13:56:53 +0000 | [diff] [blame] | 1917 | if exceptions: |
| 1918 | # It got an exception, raise the first one. |
| 1919 | raise \ |
| 1920 | exceptions[0][0], \ |
| 1921 | exceptions[0][1], \ |
| 1922 | exceptions[0][2] |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1923 | |
| 1924 | |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 1925 | def CMDcheck(parser, args): |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1926 | """Checks that all the inputs are present and generates .isolated.""" |
maruel@chromium.org | 9268f04 | 2012-10-17 17:36:41 +0000 | [diff] [blame] | 1927 | parser.add_option('--subdir', help='Filters to a subdirectory') |
| 1928 | options, args = parser.parse_args(args) |
| 1929 | if args: |
| 1930 | parser.error('Unsupported argument: %s' % args) |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1931 | complete_state = load_complete_state( |
| 1932 | options, os.getcwd(), options.subdir, False) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1933 | |
| 1934 | # Nothing is done specifically. Just store the result and state. |
| 1935 | complete_state.save_files() |
| 1936 | return 0 |
| 1937 | |
| 1938 | |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 1939 | def CMDhashtable(parser, args): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1940 | """Creates a hash table content addressed object store. |
| 1941 | |
maruel@chromium.org | 4b57f69 | 2012-10-05 20:33:09 +0000 | [diff] [blame] | 1942 | All the files listed in the .isolated file are put in the output directory |
| 1943 | with the file name being the sha-1 of the file's content. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1944 | """ |
maruel@chromium.org | 9268f04 | 2012-10-17 17:36:41 +0000 | [diff] [blame] | 1945 | parser.add_option('--subdir', help='Filters to a subdirectory') |
| 1946 | options, args = parser.parse_args(args) |
| 1947 | if args: |
| 1948 | parser.error('Unsupported argument: %s' % args) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1949 | |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 1950 | with tools.Profiler('GenerateHashtable'): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1951 | success = False |
| 1952 | try: |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1953 | complete_state = load_complete_state( |
| 1954 | options, os.getcwd(), options.subdir, False) |
| 1955 | if not options.outdir: |
| 1956 | options.outdir = os.path.join( |
| 1957 | os.path.dirname(complete_state.isolated_filepath), 'hashtable') |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1958 | # Make sure that complete_state isn't modified until save_files() is |
| 1959 | # called, because any changes made to it here will propagate to the files |
| 1960 | # created (which is probably not intended). |
| 1961 | complete_state.save_files() |
| 1962 | |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 1963 | infiles = complete_state.saved_state.files |
maruel@chromium.org | d3a1776 | 2012-12-13 14:17:15 +0000 | [diff] [blame] | 1964 | # Add all the .isolated files. |
maruel@chromium.org | 87f1196 | 2013-04-10 21:27:28 +0000 | [diff] [blame] | 1965 | isolated_hash = [] |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 1966 | isolated_files = [ |
| 1967 | options.isolated, |
| 1968 | ] + complete_state.saved_state.child_isolated_files |
| 1969 | for item in isolated_files: |
maruel@chromium.org | d3a1776 | 2012-12-13 14:17:15 +0000 | [diff] [blame] | 1970 | item_path = os.path.join( |
| 1971 | os.path.dirname(complete_state.isolated_filepath), item) |
maruel@chromium.org | 87f1196 | 2013-04-10 21:27:28 +0000 | [diff] [blame] | 1972 | # Do not use isolateserver_archive.sha1_file() here because the file is |
| 1973 | # likely smallish (under 500kb) and its file size is needed. |
maruel@chromium.org | d3a1776 | 2012-12-13 14:17:15 +0000 | [diff] [blame] | 1974 | with open(item_path, 'rb') as f: |
| 1975 | content = f.read() |
maruel@chromium.org | 87f1196 | 2013-04-10 21:27:28 +0000 | [diff] [blame] | 1976 | isolated_hash.append(hashlib.sha1(content).hexdigest()) |
maruel@chromium.org | d3a1776 | 2012-12-13 14:17:15 +0000 | [diff] [blame] | 1977 | isolated_metadata = { |
maruel@chromium.org | 87f1196 | 2013-04-10 21:27:28 +0000 | [diff] [blame] | 1978 | 'h': isolated_hash[-1], |
maruel@chromium.org | d3a1776 | 2012-12-13 14:17:15 +0000 | [diff] [blame] | 1979 | 's': len(content), |
| 1980 | 'priority': '0' |
| 1981 | } |
| 1982 | infiles[item_path] = isolated_metadata |
| 1983 | |
| 1984 | logging.info('Creating content addressed object store with %d item', |
| 1985 | len(infiles)) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1986 | |
maruel@chromium.org | b9520b0 | 2013-03-13 18:00:03 +0000 | [diff] [blame] | 1987 | if is_url(options.outdir): |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 1988 | isolateserver_archive.upload_sha1_tree( |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1989 | base_url=options.outdir, |
| 1990 | indir=complete_state.root_dir, |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 1991 | infiles=infiles, |
| 1992 | namespace='default-gzip') |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1993 | else: |
| 1994 | recreate_tree( |
| 1995 | outdir=options.outdir, |
| 1996 | indir=complete_state.root_dir, |
| 1997 | infiles=infiles, |
maruel@chromium.org | ba6489b | 2013-07-11 20:23:33 +0000 | [diff] [blame] | 1998 | action=run_isolated.HARDLINK_WITH_FALLBACK, |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 1999 | as_sha1=True) |
| 2000 | success = True |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 2001 | print('%s %s' % (isolated_hash[0], os.path.basename(options.isolated))) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2002 | finally: |
maruel@chromium.org | 4b57f69 | 2012-10-05 20:33:09 +0000 | [diff] [blame] | 2003 | # If the command failed, delete the .isolated file if it exists. This is |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2004 | # important so no stale swarm job is executed. |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 2005 | if not success and os.path.isfile(options.isolated): |
| 2006 | os.remove(options.isolated) |
maruel@chromium.org | 87f1196 | 2013-04-10 21:27:28 +0000 | [diff] [blame] | 2007 | return not success |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2008 | |
| 2009 | |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 2010 | def CMDmerge(parser, args): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2011 | """Reads and merges the data from the trace back into the original .isolate. |
| 2012 | |
| 2013 | Ignores --outdir. |
| 2014 | """ |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 2015 | parser.require_isolated = False |
maruel@chromium.org | 3683afe | 2013-07-27 00:09:27 +0000 | [diff] [blame] | 2016 | add_trace_option(parser) |
maruel@chromium.org | 9268f04 | 2012-10-17 17:36:41 +0000 | [diff] [blame] | 2017 | options, args = parser.parse_args(args) |
| 2018 | if args: |
| 2019 | parser.error('Unsupported argument: %s' % args) |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 2020 | complete_state = load_complete_state(options, os.getcwd(), None, False) |
maruel@chromium.org | 3683afe | 2013-07-27 00:09:27 +0000 | [diff] [blame] | 2021 | blacklist = trace_inputs.gen_blacklist(options.trace_blacklist) |
| 2022 | merge(complete_state, blacklist) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2023 | return 0 |
| 2024 | |
| 2025 | |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 2026 | def CMDread(parser, args): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2027 | """Reads the trace file generated with command 'trace'. |
| 2028 | |
| 2029 | Ignores --outdir. |
| 2030 | """ |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 2031 | parser.require_isolated = False |
maruel@chromium.org | 3683afe | 2013-07-27 00:09:27 +0000 | [diff] [blame] | 2032 | add_trace_option(parser) |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 2033 | parser.add_option( |
| 2034 | '--skip-refresh', action='store_true', |
| 2035 | help='Skip reading .isolate file and do not refresh the sha1 of ' |
| 2036 | 'dependencies') |
maruel@chromium.org | 9268f04 | 2012-10-17 17:36:41 +0000 | [diff] [blame] | 2037 | options, args = parser.parse_args(args) |
| 2038 | if args: |
| 2039 | parser.error('Unsupported argument: %s' % args) |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 2040 | complete_state = load_complete_state( |
| 2041 | options, os.getcwd(), None, options.skip_refresh) |
maruel@chromium.org | 3683afe | 2013-07-27 00:09:27 +0000 | [diff] [blame] | 2042 | blacklist = trace_inputs.gen_blacklist(options.trace_blacklist) |
| 2043 | value, exceptions = read_trace_as_isolate_dict(complete_state, blacklist) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2044 | pretty_print(value, sys.stdout) |
maruel@chromium.org | e27c65f | 2012-10-22 13:56:53 +0000 | [diff] [blame] | 2045 | if exceptions: |
| 2046 | # It got an exception, raise the first one. |
| 2047 | raise \ |
| 2048 | exceptions[0][0], \ |
| 2049 | exceptions[0][1], \ |
| 2050 | exceptions[0][2] |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2051 | return 0 |
| 2052 | |
| 2053 | |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 2054 | def CMDremap(parser, args): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2055 | """Creates a directory with all the dependencies mapped into it. |
| 2056 | |
| 2057 | Useful to test manually why a test is failing. The target executable is not |
| 2058 | run. |
| 2059 | """ |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 2060 | parser.require_isolated = False |
maruel@chromium.org | 9268f04 | 2012-10-17 17:36:41 +0000 | [diff] [blame] | 2061 | options, args = parser.parse_args(args) |
| 2062 | if args: |
| 2063 | parser.error('Unsupported argument: %s' % args) |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 2064 | complete_state = load_complete_state(options, os.getcwd(), None, False) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2065 | |
| 2066 | if not options.outdir: |
maruel@chromium.org | b8375c2 | 2012-10-05 18:10:01 +0000 | [diff] [blame] | 2067 | options.outdir = run_isolated.make_temp_dir( |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2068 | 'isolate', complete_state.root_dir) |
| 2069 | else: |
maruel@chromium.org | b9520b0 | 2013-03-13 18:00:03 +0000 | [diff] [blame] | 2070 | if is_url(options.outdir): |
| 2071 | raise ExecutionError('Can\'t use url for --outdir with mode remap') |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2072 | if not os.path.isdir(options.outdir): |
| 2073 | os.makedirs(options.outdir) |
maruel@chromium.org | ec91af1 | 2012-10-18 20:45:57 +0000 | [diff] [blame] | 2074 | print('Remapping into %s' % options.outdir) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2075 | if len(os.listdir(options.outdir)): |
| 2076 | raise ExecutionError('Can\'t remap in a non-empty directory') |
| 2077 | recreate_tree( |
| 2078 | outdir=options.outdir, |
| 2079 | indir=complete_state.root_dir, |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 2080 | infiles=complete_state.saved_state.files, |
maruel@chromium.org | ba6489b | 2013-07-11 20:23:33 +0000 | [diff] [blame] | 2081 | action=run_isolated.HARDLINK_WITH_FALLBACK, |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2082 | as_sha1=False) |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 2083 | if complete_state.saved_state.read_only: |
maruel@chromium.org | b8375c2 | 2012-10-05 18:10:01 +0000 | [diff] [blame] | 2084 | run_isolated.make_writable(options.outdir, True) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2085 | |
maruel@chromium.org | 4b57f69 | 2012-10-05 20:33:09 +0000 | [diff] [blame] | 2086 | if complete_state.isolated_filepath: |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2087 | complete_state.save_files() |
| 2088 | return 0 |
| 2089 | |
| 2090 | |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 2091 | def CMDrewrite(parser, args): |
maruel@chromium.org | 9f7f6d4 | 2013-02-04 18:31:17 +0000 | [diff] [blame] | 2092 | """Rewrites a .isolate file into the canonical format.""" |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 2093 | parser.require_isolated = False |
maruel@chromium.org | 9f7f6d4 | 2013-02-04 18:31:17 +0000 | [diff] [blame] | 2094 | options, args = parser.parse_args(args) |
| 2095 | if args: |
| 2096 | parser.error('Unsupported argument: %s' % args) |
| 2097 | |
| 2098 | if options.isolated: |
| 2099 | # Load the previous state if it was present. Namely, "foo.isolated.state". |
| 2100 | complete_state = CompleteState.load_files(options.isolated) |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 2101 | isolate = options.isolate or complete_state.saved_state.isolate_filepath |
maruel@chromium.org | 9f7f6d4 | 2013-02-04 18:31:17 +0000 | [diff] [blame] | 2102 | else: |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 2103 | isolate = options.isolate |
maruel@chromium.org | 9f7f6d4 | 2013-02-04 18:31:17 +0000 | [diff] [blame] | 2104 | if not isolate: |
| 2105 | raise ExecutionError('A .isolate file is required.') |
| 2106 | with open(isolate, 'r') as f: |
| 2107 | content = f.read() |
| 2108 | config = load_isolate_as_config( |
| 2109 | os.path.dirname(os.path.abspath(isolate)), |
| 2110 | eval_content(content), |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 2111 | extract_comment(content)) |
| 2112 | data = config.make_isolate_file() |
maruel@chromium.org | 9f7f6d4 | 2013-02-04 18:31:17 +0000 | [diff] [blame] | 2113 | print('Updating %s' % isolate) |
| 2114 | with open(isolate, 'wb') as f: |
| 2115 | print_all(config.file_comment, data, f) |
| 2116 | return 0 |
| 2117 | |
| 2118 | |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 2119 | def CMDrun(parser, args): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2120 | """Runs the test executable in an isolated (temporary) directory. |
| 2121 | |
| 2122 | All the dependencies are mapped into the temporary directory and the |
maruel@chromium.org | b9520b0 | 2013-03-13 18:00:03 +0000 | [diff] [blame] | 2123 | directory is cleaned up after the target exits. Warning: if --outdir is |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2124 | specified, it is deleted upon exit. |
| 2125 | |
| 2126 | Argument processing stops at the first non-recognized argument and these |
| 2127 | arguments are appended to the command line of the target to run. For example, |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 2128 | use: isolate.py --isolated foo.isolated -- --gtest_filter=Foo.Bar |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2129 | """ |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 2130 | parser.require_isolated = False |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 2131 | parser.add_option( |
| 2132 | '--skip-refresh', action='store_true', |
| 2133 | help='Skip reading .isolate file and do not refresh the sha1 of ' |
| 2134 | 'dependencies') |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2135 | parser.enable_interspersed_args() |
| 2136 | options, args = parser.parse_args(args) |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 2137 | complete_state = load_complete_state( |
| 2138 | options, os.getcwd(), None, options.skip_refresh) |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 2139 | cmd = complete_state.saved_state.command + args |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2140 | if not cmd: |
| 2141 | raise ExecutionError('No command to run') |
maruel@chromium.org | b9520b0 | 2013-03-13 18:00:03 +0000 | [diff] [blame] | 2142 | if options.outdir and is_url(options.outdir): |
| 2143 | raise ExecutionError('Can\'t use url for --outdir with mode run') |
| 2144 | |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 2145 | cmd = tools.fix_python_path(cmd) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2146 | try: |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 2147 | root_dir = complete_state.root_dir |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2148 | if not options.outdir: |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 2149 | if not os.path.isabs(root_dir): |
| 2150 | root_dir = os.path.join(os.path.dirname(options.isolated), root_dir) |
| 2151 | options.outdir = run_isolated.make_temp_dir('isolate', root_dir) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2152 | else: |
| 2153 | if not os.path.isdir(options.outdir): |
| 2154 | os.makedirs(options.outdir) |
| 2155 | recreate_tree( |
| 2156 | outdir=options.outdir, |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 2157 | indir=root_dir, |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 2158 | infiles=complete_state.saved_state.files, |
maruel@chromium.org | ba6489b | 2013-07-11 20:23:33 +0000 | [diff] [blame] | 2159 | action=run_isolated.HARDLINK_WITH_FALLBACK, |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2160 | as_sha1=False) |
| 2161 | cwd = os.path.normpath( |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 2162 | os.path.join(options.outdir, complete_state.saved_state.relative_cwd)) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2163 | if not os.path.isdir(cwd): |
| 2164 | # It can happen when no files are mapped from the directory containing the |
| 2165 | # .isolate file. But the directory must exist to be the current working |
| 2166 | # directory. |
| 2167 | os.makedirs(cwd) |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 2168 | if complete_state.saved_state.read_only: |
maruel@chromium.org | b8375c2 | 2012-10-05 18:10:01 +0000 | [diff] [blame] | 2169 | run_isolated.make_writable(options.outdir, True) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2170 | logging.info('Running %s, cwd=%s' % (cmd, cwd)) |
| 2171 | result = subprocess.call(cmd, cwd=cwd) |
| 2172 | finally: |
| 2173 | if options.outdir: |
maruel@chromium.org | b8375c2 | 2012-10-05 18:10:01 +0000 | [diff] [blame] | 2174 | run_isolated.rmtree(options.outdir) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2175 | |
maruel@chromium.org | 4b57f69 | 2012-10-05 20:33:09 +0000 | [diff] [blame] | 2176 | if complete_state.isolated_filepath: |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2177 | complete_state.save_files() |
| 2178 | return result |
| 2179 | |
| 2180 | |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 2181 | def CMDtrace(parser, args): |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2182 | """Traces the target using trace_inputs.py. |
| 2183 | |
| 2184 | It runs the executable without remapping it, and traces all the files it and |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 2185 | its child processes access. Then the 'merge' command can be used to generate |
| 2186 | an updated .isolate file out of it or the 'read' command to print it out to |
| 2187 | stdout. |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2188 | |
| 2189 | Argument processing stops at the first non-recognized argument and these |
| 2190 | arguments are appended to the command line of the target to run. For example, |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 2191 | use: isolate.py --isolated foo.isolated -- --gtest_filter=Foo.Bar |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2192 | """ |
maruel@chromium.org | 3683afe | 2013-07-27 00:09:27 +0000 | [diff] [blame] | 2193 | add_trace_option(parser) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2194 | parser.enable_interspersed_args() |
| 2195 | parser.add_option( |
| 2196 | '-m', '--merge', action='store_true', |
| 2197 | help='After tracing, merge the results back in the .isolate file') |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 2198 | parser.add_option( |
| 2199 | '--skip-refresh', action='store_true', |
| 2200 | help='Skip reading .isolate file and do not refresh the sha1 of ' |
| 2201 | 'dependencies') |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2202 | options, args = parser.parse_args(args) |
maruel@chromium.org | 8abec8b | 2013-04-16 19:34:20 +0000 | [diff] [blame] | 2203 | complete_state = load_complete_state( |
| 2204 | options, os.getcwd(), None, options.skip_refresh) |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 2205 | cmd = complete_state.saved_state.command + args |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2206 | if not cmd: |
| 2207 | raise ExecutionError('No command to run') |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 2208 | cmd = tools.fix_python_path(cmd) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2209 | cwd = os.path.normpath(os.path.join( |
maruel@chromium.org | f75b0cb | 2012-12-11 21:39:00 +0000 | [diff] [blame] | 2210 | unicode(complete_state.root_dir), |
| 2211 | complete_state.saved_state.relative_cwd)) |
maruel@chromium.org | 808f6af | 2012-10-11 14:08:08 +0000 | [diff] [blame] | 2212 | cmd[0] = os.path.normpath(os.path.join(cwd, cmd[0])) |
| 2213 | if not os.path.isfile(cmd[0]): |
| 2214 | raise ExecutionError( |
| 2215 | 'Tracing failed for: %s\nIt doesn\'t exit' % ' '.join(cmd)) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2216 | logging.info('Running %s, cwd=%s' % (cmd, cwd)) |
| 2217 | api = trace_inputs.get_api() |
maruel@chromium.org | 4b57f69 | 2012-10-05 20:33:09 +0000 | [diff] [blame] | 2218 | logfile = complete_state.isolated_filepath + '.log' |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2219 | api.clean_trace(logfile) |
maruel@chromium.org | b932214 | 2013-01-22 18:49:46 +0000 | [diff] [blame] | 2220 | out = None |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2221 | try: |
| 2222 | with api.get_tracer(logfile) as tracer: |
maruel@chromium.org | b932214 | 2013-01-22 18:49:46 +0000 | [diff] [blame] | 2223 | result, out = tracer.trace( |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2224 | cmd, |
| 2225 | cwd, |
| 2226 | 'default', |
| 2227 | True) |
| 2228 | except trace_inputs.TracingFailure, e: |
| 2229 | raise ExecutionError('Tracing failed for: %s\n%s' % (' '.join(cmd), str(e))) |
| 2230 | |
csharp@chromium.org | 5ab1ca9 | 2012-10-25 13:37:14 +0000 | [diff] [blame] | 2231 | if result: |
maruel@chromium.org | b932214 | 2013-01-22 18:49:46 +0000 | [diff] [blame] | 2232 | logging.error( |
| 2233 | 'Tracer exited with %d, which means the tests probably failed so the ' |
| 2234 | 'trace is probably incomplete.', result) |
| 2235 | logging.info(out) |
csharp@chromium.org | 5ab1ca9 | 2012-10-25 13:37:14 +0000 | [diff] [blame] | 2236 | |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2237 | complete_state.save_files() |
| 2238 | |
| 2239 | if options.merge: |
maruel@chromium.org | 3683afe | 2013-07-27 00:09:27 +0000 | [diff] [blame] | 2240 | blacklist = trace_inputs.gen_blacklist(options.trace_blacklist) |
| 2241 | merge(complete_state, blacklist) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2242 | |
| 2243 | return result |
| 2244 | |
| 2245 | |
maruel@chromium.org | 712454d | 2013-04-04 17:52:34 +0000 | [diff] [blame] | 2246 | def _process_variable_arg(_option, _opt, _value, parser): |
| 2247 | if not parser.rargs: |
| 2248 | raise optparse.OptionValueError( |
| 2249 | 'Please use --variable FOO=BAR or --variable FOO BAR') |
| 2250 | k = parser.rargs.pop(0) |
| 2251 | if '=' in k: |
| 2252 | parser.values.variables.append(tuple(k.split('=', 1))) |
| 2253 | else: |
| 2254 | if not parser.rargs: |
| 2255 | raise optparse.OptionValueError( |
| 2256 | 'Please use --variable FOO=BAR or --variable FOO BAR') |
| 2257 | v = parser.rargs.pop(0) |
| 2258 | parser.values.variables.append((k, v)) |
| 2259 | |
| 2260 | |
maruel@chromium.org | b253fb8 | 2012-10-16 21:44:48 +0000 | [diff] [blame] | 2261 | def add_variable_option(parser): |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 2262 | """Adds --isolated and --variable to an OptionParser.""" |
| 2263 | parser.add_option( |
| 2264 | '-s', '--isolated', |
| 2265 | metavar='FILE', |
| 2266 | help='.isolated file to generate or read') |
| 2267 | # Keep for compatibility. TODO(maruel): Remove once not used anymore. |
maruel@chromium.org | b253fb8 | 2012-10-16 21:44:48 +0000 | [diff] [blame] | 2268 | parser.add_option( |
| 2269 | '-r', '--result', |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 2270 | dest='isolated', |
| 2271 | help=optparse.SUPPRESS_HELP) |
maruel@chromium.org | b253fb8 | 2012-10-16 21:44:48 +0000 | [diff] [blame] | 2272 | default_variables = [('OS', get_flavor())] |
| 2273 | if sys.platform in ('win32', 'cygwin'): |
| 2274 | default_variables.append(('EXECUTABLE_SUFFIX', '.exe')) |
| 2275 | else: |
| 2276 | default_variables.append(('EXECUTABLE_SUFFIX', '')) |
| 2277 | parser.add_option( |
| 2278 | '-V', '--variable', |
maruel@chromium.org | 712454d | 2013-04-04 17:52:34 +0000 | [diff] [blame] | 2279 | action='callback', |
| 2280 | callback=_process_variable_arg, |
maruel@chromium.org | b253fb8 | 2012-10-16 21:44:48 +0000 | [diff] [blame] | 2281 | default=default_variables, |
| 2282 | dest='variables', |
| 2283 | metavar='FOO BAR', |
| 2284 | help='Variables to process in the .isolate file, default: %default. ' |
| 2285 | 'Variables are persistent accross calls, they are saved inside ' |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 2286 | '<.isolated>.state') |
maruel@chromium.org | b253fb8 | 2012-10-16 21:44:48 +0000 | [diff] [blame] | 2287 | |
| 2288 | |
maruel@chromium.org | 3683afe | 2013-07-27 00:09:27 +0000 | [diff] [blame] | 2289 | def add_trace_option(parser): |
| 2290 | """Adds --trace-blacklist to the parser.""" |
| 2291 | parser.add_option( |
| 2292 | '--trace-blacklist', |
| 2293 | action='append', default=list(DEFAULT_BLACKLIST), |
| 2294 | help='List of regexp to use as blacklist filter for files to consider ' |
| 2295 | 'important, not to be confused with --blacklist which blacklists ' |
| 2296 | 'test case.') |
| 2297 | |
| 2298 | |
maruel@chromium.org | 715e3fb | 2013-03-19 15:44:06 +0000 | [diff] [blame] | 2299 | def parse_isolated_option(parser, options, cwd, require_isolated): |
| 2300 | """Processes --isolated.""" |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 2301 | if options.isolated: |
maruel@chromium.org | 61a9b3b | 2012-12-12 17:18:52 +0000 | [diff] [blame] | 2302 | options.isolated = os.path.normpath( |
| 2303 | os.path.join(cwd, options.isolated.replace('/', os.path.sep))) |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 2304 | if require_isolated and not options.isolated: |
maruel@chromium.org | 75c05b4 | 2013-07-25 15:51:48 +0000 | [diff] [blame] | 2305 | parser.error('--isolated is required.') |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 2306 | if options.isolated and not options.isolated.endswith('.isolated'): |
| 2307 | parser.error('--isolated value must end with \'.isolated\'') |
maruel@chromium.org | 715e3fb | 2013-03-19 15:44:06 +0000 | [diff] [blame] | 2308 | |
| 2309 | |
| 2310 | def parse_variable_option(options): |
| 2311 | """Processes --variable.""" |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 2312 | # TODO(benrg): Maybe we should use a copy of gyp's NameValueListToDict here, |
| 2313 | # but it wouldn't be backward compatible. |
| 2314 | def try_make_int(s): |
maruel@chromium.org | e83215b | 2013-02-21 14:16:59 +0000 | [diff] [blame] | 2315 | """Converts a value to int if possible, converts to unicode otherwise.""" |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 2316 | try: |
| 2317 | return int(s) |
| 2318 | except ValueError: |
maruel@chromium.org | e83215b | 2013-02-21 14:16:59 +0000 | [diff] [blame] | 2319 | return s.decode('utf-8') |
benrg@chromium.org | 609b798 | 2013-02-07 16:44:46 +0000 | [diff] [blame] | 2320 | options.variables = dict((k, try_make_int(v)) for k, v in options.variables) |
maruel@chromium.org | b253fb8 | 2012-10-16 21:44:48 +0000 | [diff] [blame] | 2321 | |
| 2322 | |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 2323 | class OptionParserIsolate(tools.OptionParserWithLogging): |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 2324 | """Adds automatic --isolate, --isolated, --out and --variable handling.""" |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 2325 | # Set it to False if it is not required, e.g. it can be passed on but do not |
| 2326 | # fail if not given. |
| 2327 | require_isolated = True |
| 2328 | |
| 2329 | def __init__(self, **kwargs): |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 2330 | tools.OptionParserWithLogging.__init__( |
maruel@chromium.org | 5527690 | 2012-10-05 20:56:19 +0000 | [diff] [blame] | 2331 | self, |
| 2332 | verbose=int(os.environ.get('ISOLATE_DEBUG', 0)), |
| 2333 | **kwargs) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2334 | group = optparse.OptionGroup(self, "Common options") |
| 2335 | group.add_option( |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2336 | '-i', '--isolate', |
| 2337 | metavar='FILE', |
| 2338 | help='.isolate file to load the dependency data from') |
maruel@chromium.org | b253fb8 | 2012-10-16 21:44:48 +0000 | [diff] [blame] | 2339 | add_variable_option(group) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2340 | group.add_option( |
| 2341 | '-o', '--outdir', metavar='DIR', |
| 2342 | help='Directory used to recreate the tree or store the hash table. ' |
maruel@chromium.org | f347c3a | 2012-12-11 19:03:28 +0000 | [diff] [blame] | 2343 | 'Defaults: run|remap: a /tmp subdirectory, others: ' |
| 2344 | 'defaults to the directory containing --isolated') |
csharp@chromium.org | 0185680 | 2012-11-12 17:48:13 +0000 | [diff] [blame] | 2345 | group.add_option( |
| 2346 | '--ignore_broken_items', action='store_true', |
maruel@chromium.org | f347c3a | 2012-12-11 19:03:28 +0000 | [diff] [blame] | 2347 | default=bool(os.environ.get('ISOLATE_IGNORE_BROKEN_ITEMS')), |
| 2348 | help='Indicates that invalid entries in the isolated file to be ' |
| 2349 | 'only be logged and not stop processing. Defaults to True if ' |
| 2350 | 'env var ISOLATE_IGNORE_BROKEN_ITEMS is set') |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2351 | self.add_option_group(group) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2352 | |
| 2353 | def parse_args(self, *args, **kwargs): |
| 2354 | """Makes sure the paths make sense. |
| 2355 | |
| 2356 | On Windows, / and \ are often mixed together in a path. |
| 2357 | """ |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 2358 | options, args = tools.OptionParserWithLogging.parse_args( |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2359 | self, *args, **kwargs) |
| 2360 | if not self.allow_interspersed_args and args: |
| 2361 | self.error('Unsupported argument: %s' % args) |
| 2362 | |
maruel@chromium.org | a3da912 | 2013-03-28 13:27:09 +0000 | [diff] [blame] | 2363 | cwd = trace_inputs.get_native_path_case(unicode(os.getcwd())) |
maruel@chromium.org | 715e3fb | 2013-03-19 15:44:06 +0000 | [diff] [blame] | 2364 | parse_isolated_option(self, options, cwd, self.require_isolated) |
| 2365 | parse_variable_option(options) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2366 | |
| 2367 | if options.isolate: |
maruel@chromium.org | 61a9b3b | 2012-12-12 17:18:52 +0000 | [diff] [blame] | 2368 | # TODO(maruel): Work with non-ASCII. |
| 2369 | # The path must be in native path case for tracing purposes. |
| 2370 | options.isolate = unicode(options.isolate).replace('/', os.path.sep) |
| 2371 | options.isolate = os.path.normpath(os.path.join(cwd, options.isolate)) |
| 2372 | options.isolate = trace_inputs.get_native_path_case(options.isolate) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2373 | |
maruel@chromium.org | b9520b0 | 2013-03-13 18:00:03 +0000 | [diff] [blame] | 2374 | if options.outdir and not is_url(options.outdir): |
maruel@chromium.org | 61a9b3b | 2012-12-12 17:18:52 +0000 | [diff] [blame] | 2375 | options.outdir = unicode(options.outdir).replace('/', os.path.sep) |
| 2376 | # outdir doesn't need native path case since tracing is never done from |
| 2377 | # there. |
| 2378 | options.outdir = os.path.normpath(os.path.join(cwd, options.outdir)) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2379 | |
| 2380 | return options, args |
| 2381 | |
| 2382 | |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2383 | def main(argv): |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 2384 | dispatcher = subcommand.CommandDispatcher(__name__) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2385 | try: |
maruel@chromium.org | 3d67199 | 2013-08-20 00:38:27 +0000 | [diff] [blame] | 2386 | return dispatcher.execute(OptionParserIsolate(version=__version__), argv) |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2387 | except ( |
| 2388 | ExecutionError, |
maruel@chromium.org | b8375c2 | 2012-10-05 18:10:01 +0000 | [diff] [blame] | 2389 | run_isolated.MappingError, |
| 2390 | run_isolated.ConfigError) as e: |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2391 | sys.stderr.write('\nError: ') |
| 2392 | sys.stderr.write(str(e)) |
| 2393 | sys.stderr.write('\n') |
| 2394 | return 1 |
| 2395 | |
| 2396 | |
| 2397 | if __name__ == '__main__': |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 2398 | fix_encoding.fix_encoding() |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 2399 | tools.disable_buffering() |
maruel@chromium.org | e532251 | 2013-08-19 20:17:57 +0000 | [diff] [blame] | 2400 | colorama.init() |
maruel@chromium.org | 8fb47fe | 2012-10-03 20:13:15 +0000 | [diff] [blame] | 2401 | sys.exit(main(sys.argv[1:])) |