Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 6 """Copies test data files or directories into a given output directory.""" | |
| 7 | |
| 8 import optparse | |
| 9 import os | |
| 10 import shutil | |
| 11 import sys | |
| 12 | |
| 13 class WrongNumberOfArgumentsException(Exception): | |
| 14 pass | |
| 15 | |
| 16 def ListFilesForPath(path): | |
| 17 """Returns a list of all the files under a given path.""" | |
| 18 output = [] | |
| 19 # Files get returned without modification. | |
| 20 if not os.path.isdir(path): | |
| 21 output.append(path) | |
| 22 return output | |
| 23 | |
| 24 # Directories get recursively expanded. | |
| 25 contents = os.listdir(path) | |
| 26 for item in contents: | |
| 27 full_path = os.path.join(path, item) | |
| 28 output.extend(ListFilesForPath(full_path)) | |
| 29 return output | |
| 30 | |
| 31 def CalcInputs(inputs): | |
| 32 """Computes the full list of input files for a set of command-line arguments. | |
| 33 """ | |
| 34 # |inputs| is a list of strings, each of which may contain muliple paths | |
| 35 # separated by spaces. | |
| 36 output = [] | |
| 37 for input in inputs: | |
| 38 tokens = input.split() | |
| 39 for token in tokens: | |
| 40 output.extend(ListFilesForPath(token)) | |
| 41 return output | |
| 42 | |
| 43 def CopyFiles(relative_filenames, output_basedir): | |
| 44 """Copies files to the given output directory.""" | |
| 45 for file in relative_filenames: | |
| 46 relative_dirname = os.path.dirname(file) | |
| 47 output_dir = os.path.join(output_basedir, relative_dirname) | |
| 48 if not os.path.exists(output_dir): | |
|
Mark Mentovai
2012/07/16 17:48:45
This doesn’t really handle the case where what was
| |
| 49 os.makedirs(output_dir) | |
| 50 output_filename = os.path.join(output_basedir, file) | |
| 51 shutil.copy(file, output_filename) | |
| 52 | |
| 53 def DoMain(argv): | |
| 54 parser = optparse.OptionParser() | |
| 55 usage = 'Usage: %prog -o <output_dir> [--inputs] [--outputs] <input_files>' | |
| 56 parser.set_usage(usage) | |
| 57 parser.add_option('-o', dest='output_dir') | |
| 58 parser.add_option('--inputs', action='store_true', dest='list_inputs') | |
| 59 parser.add_option('--outputs', action='store_true', dest='list_outputs') | |
| 60 options, arglist = parser.parse_args(argv) | |
| 61 | |
| 62 if len(arglist) == 0: | |
| 63 raise WrongNumberOfArgumentsException('<input_files> required.') | |
| 64 | |
| 65 files_to_copy = CalcInputs(arglist) | |
| 66 if options.list_inputs: | |
| 67 return '\n'.join(files_to_copy) | |
| 68 | |
| 69 if not options.output_dir: | |
| 70 raise WrongNumberOfArgumentsException('-o required.') | |
| 71 | |
| 72 if options.list_outputs: | |
| 73 outputs = [os.path.join(options.output_dir, x) for x in files_to_copy] | |
| 74 return '\n'.join(outputs) | |
| 75 | |
| 76 CopyFiles(files_to_copy, options.output_dir) | |
| 77 return 0 | |
| 78 | |
| 79 def main(argv): | |
| 80 try: | |
| 81 result = DoMain(argv[1:]) | |
| 82 except WrongNumberOfArguments, e: | |
| 83 print >>sys.stderr, e | |
| 84 return 1 | |
| 85 print result | |
| 86 return 0 | |
| 87 | |
| 88 if __name__ == '__main__': | |
| 89 sys.exit(main(sys.argv)) | |
| OLD | NEW |