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 output_filename = os.path.join(output_basedir, file) |
| 49 |
| 50 # In cases where a directory has turned into a file or vice versa, delete it |
| 51 # before copying it below. |
| 52 if os.path.exists(output_dir) and not os.path.isdir(output_dir): |
| 53 os.remove(output_dir) |
| 54 if os.path.exists(output_filename) and os.path.isdir(output_filename): |
| 55 shutil.rmtree(output_filename) |
| 56 |
| 57 if not os.path.exists(output_dir): |
| 58 os.makedirs(output_dir) |
| 59 shutil.copy(file, output_filename) |
| 60 |
| 61 def DoMain(argv): |
| 62 parser = optparse.OptionParser() |
| 63 usage = 'Usage: %prog -o <output_dir> [--inputs] [--outputs] <input_files>' |
| 64 parser.set_usage(usage) |
| 65 parser.add_option('-o', dest='output_dir') |
| 66 parser.add_option('--inputs', action='store_true', dest='list_inputs') |
| 67 parser.add_option('--outputs', action='store_true', dest='list_outputs') |
| 68 options, arglist = parser.parse_args(argv) |
| 69 |
| 70 if len(arglist) == 0: |
| 71 raise WrongNumberOfArgumentsException('<input_files> required.') |
| 72 |
| 73 files_to_copy = CalcInputs(arglist) |
| 74 if options.list_inputs: |
| 75 return '\n'.join(files_to_copy) |
| 76 |
| 77 if not options.output_dir: |
| 78 raise WrongNumberOfArgumentsException('-o required.') |
| 79 |
| 80 if options.list_outputs: |
| 81 outputs = [os.path.join(options.output_dir, x) for x in files_to_copy] |
| 82 return '\n'.join(outputs) |
| 83 |
| 84 CopyFiles(files_to_copy, options.output_dir) |
| 85 return |
| 86 |
| 87 def main(argv): |
| 88 try: |
| 89 result = DoMain(argv[1:]) |
| 90 except WrongNumberOfArgumentsException, e: |
| 91 print >>sys.stderr, e |
| 92 return 1 |
| 93 if result: |
| 94 print result |
| 95 return 0 |
| 96 |
| 97 if __name__ == '__main__': |
| 98 sys.exit(main(sys.argv)) |
OLD | NEW |