OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Does one of the following depending on the --mode argument: | 6 """Does one of the following depending on the --mode argument: |
7 check Verifies all the inputs exist, touches the file specified with | 7 check Verifies all the inputs exist, touches the file specified with |
8 --result and exits. | 8 --result and exits. |
9 hashtable Puts a manifest file and hard links each of the inputs into the | 9 hashtable Puts a manifest file and hard links each of the inputs into the |
10 output directory. | 10 output directory. |
(...skipping 13 matching lines...) Expand all Loading... |
24 import logging | 24 import logging |
25 import optparse | 25 import optparse |
26 import os | 26 import os |
27 import re | 27 import re |
28 import stat | 28 import stat |
29 import subprocess | 29 import subprocess |
30 import sys | 30 import sys |
31 import tempfile | 31 import tempfile |
32 | 32 |
33 import trace_inputs | 33 import trace_inputs |
34 import tree_creator | 34 import run_test_from_archive |
35 | 35 |
36 | 36 |
37 def relpath(path, root): | 37 def relpath(path, root): |
38 """os.path.relpath() that keeps trailing slash.""" | 38 """os.path.relpath() that keeps trailing slash.""" |
39 out = os.path.relpath(path, root) | 39 out = os.path.relpath(path, root) |
40 if path.endswith(os.path.sep): | 40 if path.endswith(os.path.sep): |
41 out += os.path.sep | 41 out += os.path.sep |
42 return out | 42 return out |
43 | 43 |
44 | 44 |
(...skipping 10 matching lines...) Expand all Loading... |
55 logging.info('%s not under %s' % (path, root)) | 55 logging.info('%s not under %s' % (path, root)) |
56 return path | 56 return path |
57 | 57 |
58 | 58 |
59 def expand_directories(indir, infiles, blacklist): | 59 def expand_directories(indir, infiles, blacklist): |
60 """Expands the directories, applies the blacklist and verifies files exist.""" | 60 """Expands the directories, applies the blacklist and verifies files exist.""" |
61 logging.debug('expand_directories(%s, %s, %s)' % (indir, infiles, blacklist)) | 61 logging.debug('expand_directories(%s, %s, %s)' % (indir, infiles, blacklist)) |
62 outfiles = [] | 62 outfiles = [] |
63 for relfile in infiles: | 63 for relfile in infiles: |
64 if os.path.isabs(relfile): | 64 if os.path.isabs(relfile): |
65 raise tree_creator.MappingError('Can\'t map absolute path %s' % relfile) | 65 raise run_test_from_archive.MappingError( |
| 66 'Can\'t map absolute path %s' % relfile) |
66 infile = os.path.normpath(os.path.join(indir, relfile)) | 67 infile = os.path.normpath(os.path.join(indir, relfile)) |
67 if not infile.startswith(indir): | 68 if not infile.startswith(indir): |
68 raise tree_creator.MappingError( | 69 raise run_test_from_archive.MappingError( |
69 'Can\'t map file %s outside %s' % (infile, indir)) | 70 'Can\'t map file %s outside %s' % (infile, indir)) |
70 | 71 |
71 if relfile.endswith(os.path.sep): | 72 if relfile.endswith(os.path.sep): |
72 if not os.path.isdir(infile): | 73 if not os.path.isdir(infile): |
73 raise tree_creator.MappingError( | 74 raise run_test_from_archive.MappingError( |
74 'Input directory %s must have a trailing slash' % infile) | 75 'Input directory %s must have a trailing slash' % infile) |
75 for dirpath, dirnames, filenames in os.walk(infile): | 76 for dirpath, dirnames, filenames in os.walk(infile): |
76 # Convert the absolute path to subdir + relative subdirectory. | 77 # Convert the absolute path to subdir + relative subdirectory. |
77 reldirpath = dirpath[len(indir)+1:] | 78 reldirpath = dirpath[len(indir)+1:] |
78 outfiles.extend(os.path.join(reldirpath, f) for f in filenames) | 79 outfiles.extend(os.path.join(reldirpath, f) for f in filenames) |
79 for index, dirname in enumerate(dirnames): | 80 for index, dirname in enumerate(dirnames): |
80 # Do not process blacklisted directories. | 81 # Do not process blacklisted directories. |
81 if blacklist(os.path.join(reldirpath, dirname)): | 82 if blacklist(os.path.join(reldirpath, dirname)): |
82 del dirnames[index] | 83 del dirnames[index] |
83 else: | 84 else: |
84 if not os.path.isfile(infile): | 85 if not os.path.isfile(infile): |
85 raise tree_creator.MappingError('Input file %s doesn\'t exist' % infile) | 86 raise run_test_from_archive.MappingError( |
| 87 'Input file %s doesn\'t exist' % infile) |
86 outfiles.append(relfile) | 88 outfiles.append(relfile) |
87 return outfiles | 89 return outfiles |
88 | 90 |
89 | 91 |
90 def process_inputs(indir, infiles, need_hash, read_only): | 92 def process_inputs(indir, infiles, need_hash, read_only): |
91 """Returns a dictionary of input files, populated with the files' mode and | 93 """Returns a dictionary of input files, populated with the files' mode and |
92 hash. | 94 hash. |
93 | 95 |
94 The file mode is manipulated if read_only is True. In practice, we only save | 96 The file mode is manipulated if read_only is True. In practice, we only save |
95 one of 4 modes: 0755 (rwx), 0644 (rw), 0555 (rx), 0444 (r). | 97 one of 4 modes: 0755 (rwx), 0644 (rw), 0555 (rx), 0444 (r). |
(...skipping 28 matching lines...) Expand all Loading... |
124 outdir: Output directory to create the files in. | 126 outdir: Output directory to create the files in. |
125 indir: Root directory the infiles are based in. | 127 indir: Root directory the infiles are based in. |
126 infiles: List of files to map from |indir| to |outdir|. | 128 infiles: List of files to map from |indir| to |outdir|. |
127 action: See assert below. | 129 action: See assert below. |
128 """ | 130 """ |
129 logging.debug( | 131 logging.debug( |
130 'recreate_tree(%s, %s, %s, %s)' % (outdir, indir, infiles, action)) | 132 'recreate_tree(%s, %s, %s, %s)' % (outdir, indir, infiles, action)) |
131 logging.info('Mapping from %s to %s' % (indir, outdir)) | 133 logging.info('Mapping from %s to %s' % (indir, outdir)) |
132 | 134 |
133 assert action in ( | 135 assert action in ( |
134 tree_creator.HARDLINK, tree_creator.SYMLINK, tree_creator.COPY) | 136 run_test_from_archive.HARDLINK, |
| 137 run_test_from_archive.SYMLINK, |
| 138 run_test_from_archive.COPY) |
135 outdir = os.path.normpath(outdir) | 139 outdir = os.path.normpath(outdir) |
136 if not os.path.isdir(outdir): | 140 if not os.path.isdir(outdir): |
137 logging.info ('Creating %s' % outdir) | 141 logging.info ('Creating %s' % outdir) |
138 os.makedirs(outdir) | 142 os.makedirs(outdir) |
139 # Do not call abspath until the directory exists. | 143 # Do not call abspath until the directory exists. |
140 outdir = os.path.abspath(outdir) | 144 outdir = os.path.abspath(outdir) |
141 | 145 |
142 for relfile in infiles: | 146 for relfile in infiles: |
143 infile = os.path.join(indir, relfile) | 147 infile = os.path.join(indir, relfile) |
144 outfile = os.path.join(outdir, relfile) | 148 outfile = os.path.join(outdir, relfile) |
145 outsubdir = os.path.dirname(outfile) | 149 outsubdir = os.path.dirname(outfile) |
146 if not os.path.isdir(outsubdir): | 150 if not os.path.isdir(outsubdir): |
147 os.makedirs(outsubdir) | 151 os.makedirs(outsubdir) |
148 tree_creator.link_file(outfile, infile, action) | 152 run_test_from_archive.link_file(outfile, infile, action) |
149 | 153 |
150 | 154 |
151 def separate_inputs_command(args, root, files): | 155 def separate_inputs_command(args, root, files): |
152 """Strips off the command line from the inputs. | 156 """Strips off the command line from the inputs. |
153 | 157 |
154 gyp provides input paths relative to cwd. Convert them to be relative to root. | 158 gyp provides input paths relative to cwd. Convert them to be relative to root. |
155 OptionParser kindly strips off '--' from sys.argv if it's provided and that's | 159 OptionParser kindly strips off '--' from sys.argv if it's provided and that's |
156 the first non-arg value. Manually look up if it was present in sys.argv. | 160 the first non-arg value. Manually look up if it was present in sys.argv. |
157 """ | 161 """ |
158 cmd = [] | 162 cmd = [] |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 outdir, indir, dictfiles, _read_only, _cmd, _relative_cwd, resultfile): | 246 outdir, indir, dictfiles, _read_only, _cmd, _relative_cwd, resultfile): |
243 outdir = outdir or os.path.dirname(resultfile) | 247 outdir = outdir or os.path.dirname(resultfile) |
244 for relfile, properties in dictfiles.iteritems(): | 248 for relfile, properties in dictfiles.iteritems(): |
245 infile = os.path.join(indir, relfile) | 249 infile = os.path.join(indir, relfile) |
246 outfile = os.path.join(outdir, properties['sha-1']) | 250 outfile = os.path.join(outdir, properties['sha-1']) |
247 if os.path.isfile(outfile): | 251 if os.path.isfile(outfile): |
248 # Just do a quick check that the file size matches. | 252 # Just do a quick check that the file size matches. |
249 if os.stat(infile).st_size == os.stat(outfile).st_size: | 253 if os.stat(infile).st_size == os.stat(outfile).st_size: |
250 continue | 254 continue |
251 # Otherwise, an exception will be raised. | 255 # Otherwise, an exception will be raised. |
252 tree_creator.link_file(outfile, infile, tree_creator.HARDLINK) | 256 run_test_from_archive.link_file( |
| 257 outfile, infile, run_test_from_archive.HARDLINK) |
253 return 0 | 258 return 0 |
254 | 259 |
255 | 260 |
256 def MODEremap( | 261 def MODEremap( |
257 outdir, indir, dictfiles, read_only, _cmd, _relative_cwd, _resultfile): | 262 outdir, indir, dictfiles, read_only, _cmd, _relative_cwd, _resultfile): |
258 if not outdir: | 263 if not outdir: |
259 outdir = tempfile.mkdtemp(prefix='isolate') | 264 outdir = tempfile.mkdtemp(prefix='isolate') |
260 print 'Remapping into %s' % outdir | 265 print 'Remapping into %s' % outdir |
261 if len(os.listdir(outdir)): | 266 if len(os.listdir(outdir)): |
262 print 'Can\'t remap in a non-empty directory' | 267 print 'Can\'t remap in a non-empty directory' |
263 return 1 | 268 return 1 |
264 recreate_tree(outdir, indir, dictfiles.keys(), tree_creator.HARDLINK) | 269 recreate_tree(outdir, indir, dictfiles.keys(), run_test_from_archive.HARDLINK) |
265 if read_only: | 270 if read_only: |
266 tree_creator.make_writable(outdir, True) | 271 run_test_from_archive.make_writable(outdir, True) |
267 return 0 | 272 return 0 |
268 | 273 |
269 | 274 |
270 def MODErun( | 275 def MODErun( |
271 _outdir, indir, dictfiles, read_only, cmd, relative_cwd, _resultfile): | 276 _outdir, indir, dictfiles, read_only, cmd, relative_cwd, _resultfile): |
272 """Always uses a temporary directory.""" | 277 """Always uses a temporary directory.""" |
273 try: | 278 try: |
274 outdir = tempfile.mkdtemp(prefix='isolate') | 279 outdir = tempfile.mkdtemp(prefix='isolate') |
275 recreate_tree(outdir, indir, dictfiles.keys(), tree_creator.HARDLINK) | 280 recreate_tree( |
| 281 outdir, indir, dictfiles.keys(), run_test_from_archive.HARDLINK) |
276 cwd = os.path.join(outdir, relative_cwd) | 282 cwd = os.path.join(outdir, relative_cwd) |
277 if not os.path.isdir(cwd): | 283 if not os.path.isdir(cwd): |
278 os.makedirs(cwd) | 284 os.makedirs(cwd) |
279 if read_only: | 285 if read_only: |
280 tree_creator.make_writable(outdir, True) | 286 run_test_from_archive.make_writable(outdir, True) |
281 | 287 |
282 logging.info('Running %s, cwd=%s' % (cmd, cwd)) | 288 logging.info('Running %s, cwd=%s' % (cmd, cwd)) |
283 return subprocess.call(cmd, cwd=cwd) | 289 return subprocess.call(cmd, cwd=cwd) |
284 finally: | 290 finally: |
285 tree_creator.rmtree(outdir) | 291 run_test_from_archive.rmtree(outdir) |
286 | 292 |
287 | 293 |
288 def MODEtrace( | 294 def MODEtrace( |
289 _outdir, indir, _dictfiles, _read_only, cmd, relative_cwd, resultfile): | 295 _outdir, indir, _dictfiles, _read_only, cmd, relative_cwd, resultfile): |
290 """Shortcut to use trace_inputs.py properly. | 296 """Shortcut to use trace_inputs.py properly. |
291 | 297 |
292 It constructs the equivalent of dictfiles. It is hardcoded to base the | 298 It constructs the equivalent of dictfiles. It is hardcoded to base the |
293 checkout at src/. | 299 checkout at src/. |
294 """ | 300 """ |
295 logging.info('Running %s, cwd=%s' % (cmd, os.path.join(indir, relative_cwd))) | 301 logging.info('Running %s, cwd=%s' % (cmd, os.path.join(indir, relative_cwd))) |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 try: | 401 try: |
396 return isolate( | 402 return isolate( |
397 options.outdir, | 403 options.outdir, |
398 options.result, | 404 options.result, |
399 indir, | 405 indir, |
400 infiles, | 406 infiles, |
401 options.mode, | 407 options.mode, |
402 options.read_only, | 408 options.read_only, |
403 cmd, | 409 cmd, |
404 options.from_results) | 410 options.from_results) |
405 except tree_creator.MappingError, e: | 411 except run_test_from_archive.MappingError, e: |
406 print >> sys.stderr, str(e) | 412 print >> sys.stderr, str(e) |
407 return 1 | 413 return 1 |
408 | 414 |
409 | 415 |
410 if __name__ == '__main__': | 416 if __name__ == '__main__': |
411 sys.exit(main()) | 417 sys.exit(main()) |
OLD | NEW |