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 """Copies test data files or directories into a given output directory.""" | 6 """Copies test data files or directories into a given output directory.""" |
7 | 7 |
8 import optparse | 8 import optparse |
9 import os | 9 import os |
10 import shutil | 10 import shutil |
11 import sys | 11 import sys |
12 | 12 |
13 class WrongNumberOfArgumentsException(Exception): | 13 class WrongNumberOfArgumentsException(Exception): |
14 pass | 14 pass |
15 | 15 |
| 16 def EscapePath(path): |
| 17 """Returns a path with spaces escaped.""" |
| 18 return path.replace(" ", "\\ ") |
| 19 |
16 def ListFilesForPath(path): | 20 def ListFilesForPath(path): |
17 """Returns a list of all the files under a given path.""" | 21 """Returns a list of all the files under a given path.""" |
18 output = [] | 22 output = [] |
19 # Ignore revision control metadata directories. | 23 # Ignore revision control metadata directories. |
20 if (os.path.basename(path).startswith('.git') or | 24 if (os.path.basename(path).startswith('.git') or |
21 os.path.basename(path).startswith('.svn')): | 25 os.path.basename(path).startswith('.svn')): |
22 return output | 26 return output |
23 | 27 |
24 # Files get returned without modification. | 28 # Files get returned without modification. |
25 if not os.path.isdir(path): | 29 if not os.path.isdir(path): |
26 output.append(path) | 30 output.append(path) |
27 return output | 31 return output |
28 | 32 |
29 # Directories get recursively expanded. | 33 # Directories get recursively expanded. |
30 contents = os.listdir(path) | 34 contents = os.listdir(path) |
31 for item in contents: | 35 for item in contents: |
32 full_path = os.path.join(path, item) | 36 full_path = os.path.join(path, item) |
33 output.extend(ListFilesForPath(full_path)) | 37 output.extend(ListFilesForPath(full_path)) |
34 return output | 38 return output |
35 | 39 |
36 def CalcInputs(inputs): | 40 def CalcInputs(inputs): |
37 """Computes the full list of input files for a set of command-line arguments. | 41 """Computes the full list of input files for a set of command-line arguments. |
38 """ | 42 """ |
39 # |inputs| is a list of strings, each of which may contain muliple paths | 43 # |inputs| is a list of paths, which may be directories. |
40 # separated by spaces. | |
41 output = [] | 44 output = [] |
42 for input in inputs: | 45 for input in inputs: |
43 tokens = input.split() | 46 output.extend(ListFilesForPath(input)) |
44 for token in tokens: | |
45 output.extend(ListFilesForPath(token)) | |
46 return output | 47 return output |
47 | 48 |
48 def CopyFiles(relative_filenames, output_basedir): | 49 def CopyFiles(relative_filenames, output_basedir): |
49 """Copies files to the given output directory.""" | 50 """Copies files to the given output directory.""" |
50 for file in relative_filenames: | 51 for file in relative_filenames: |
51 relative_dirname = os.path.dirname(file) | 52 relative_dirname = os.path.dirname(file) |
52 output_dir = os.path.join(output_basedir, relative_dirname) | 53 output_dir = os.path.join(output_basedir, relative_dirname) |
53 output_filename = os.path.join(output_basedir, file) | 54 output_filename = os.path.join(output_basedir, file) |
54 | 55 |
55 # In cases where a directory has turned into a file or vice versa, delete it | 56 # In cases where a directory has turned into a file or vice versa, delete it |
(...skipping 13 matching lines...) Expand all Loading... |
69 parser.set_usage(usage) | 70 parser.set_usage(usage) |
70 parser.add_option('-o', dest='output_dir') | 71 parser.add_option('-o', dest='output_dir') |
71 parser.add_option('--inputs', action='store_true', dest='list_inputs') | 72 parser.add_option('--inputs', action='store_true', dest='list_inputs') |
72 parser.add_option('--outputs', action='store_true', dest='list_outputs') | 73 parser.add_option('--outputs', action='store_true', dest='list_outputs') |
73 options, arglist = parser.parse_args(argv) | 74 options, arglist = parser.parse_args(argv) |
74 | 75 |
75 if len(arglist) == 0: | 76 if len(arglist) == 0: |
76 raise WrongNumberOfArgumentsException('<input_files> required.') | 77 raise WrongNumberOfArgumentsException('<input_files> required.') |
77 | 78 |
78 files_to_copy = CalcInputs(arglist) | 79 files_to_copy = CalcInputs(arglist) |
| 80 escaped_files = [EscapePath(x) for x in CalcInputs(arglist)] |
79 if options.list_inputs: | 81 if options.list_inputs: |
80 return '\n'.join(files_to_copy) | 82 return '\n'.join(escaped_files) |
81 | 83 |
82 if not options.output_dir: | 84 if not options.output_dir: |
83 raise WrongNumberOfArgumentsException('-o required.') | 85 raise WrongNumberOfArgumentsException('-o required.') |
84 | 86 |
85 if options.list_outputs: | 87 if options.list_outputs: |
86 outputs = [os.path.join(options.output_dir, x) for x in files_to_copy] | 88 outputs = [os.path.join(options.output_dir, x) for x in escaped_files] |
87 return '\n'.join(outputs) | 89 return '\n'.join(outputs) |
88 | 90 |
89 CopyFiles(files_to_copy, options.output_dir) | 91 CopyFiles(files_to_copy, options.output_dir) |
90 return | 92 return |
91 | 93 |
92 def main(argv): | 94 def main(argv): |
93 try: | 95 try: |
94 result = DoMain(argv[1:]) | 96 result = DoMain(argv[1:]) |
95 except WrongNumberOfArgumentsException, e: | 97 except WrongNumberOfArgumentsException, e: |
96 print >>sys.stderr, e | 98 print >>sys.stderr, e |
97 return 1 | 99 return 1 |
98 if result: | 100 if result: |
99 print result | 101 print result |
100 return 0 | 102 return 0 |
101 | 103 |
102 if __name__ == '__main__': | 104 if __name__ == '__main__': |
103 sys.exit(main(sys.argv)) | 105 sys.exit(main(sys.argv)) |
OLD | NEW |