[email protected] | da19703b | 2012-07-17 14:15:34 | [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 | |
| 6 | """Copies test data files or directories into a given output directory.""" |
| 7 | |
Raul Tambre | 9e24293b | 2019-05-12 06:11:07 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
[email protected] | da19703b | 2012-07-17 14:15:34 | [diff] [blame] | 10 | import optparse |
| 11 | import os |
| 12 | import shutil |
| 13 | import sys |
| 14 | |
| 15 | class WrongNumberOfArgumentsException(Exception): |
| 16 | pass |
| 17 | |
[email protected] | 92bba24 | 2012-11-09 18:04:50 | [diff] [blame] | 18 | def EscapePath(path): |
| 19 | """Returns a path with spaces escaped.""" |
| 20 | return path.replace(" ", "\\ ") |
| 21 | |
[email protected] | da19703b | 2012-07-17 14:15:34 | [diff] [blame] | 22 | def ListFilesForPath(path): |
| 23 | """Returns a list of all the files under a given path.""" |
| 24 | output = [] |
[email protected] | 6fdd63f6 | 2012-11-05 14:22:30 | [diff] [blame] | 25 | # Ignore revision control metadata directories. |
| 26 | if (os.path.basename(path).startswith('.git') or |
| 27 | os.path.basename(path).startswith('.svn')): |
[email protected] | 14275a59 | 2012-07-18 11:16:21 | [diff] [blame] | 28 | return output |
| 29 | |
[email protected] | da19703b | 2012-07-17 14:15:34 | [diff] [blame] | 30 | # Files get returned without modification. |
| 31 | if not os.path.isdir(path): |
| 32 | output.append(path) |
| 33 | return output |
| 34 | |
| 35 | # Directories get recursively expanded. |
| 36 | contents = os.listdir(path) |
| 37 | for item in contents: |
| 38 | full_path = os.path.join(path, item) |
| 39 | output.extend(ListFilesForPath(full_path)) |
| 40 | return output |
| 41 | |
| 42 | def CalcInputs(inputs): |
| 43 | """Computes the full list of input files for a set of command-line arguments. |
| 44 | """ |
[email protected] | 92bba24 | 2012-11-09 18:04:50 | [diff] [blame] | 45 | # |inputs| is a list of paths, which may be directories. |
[email protected] | da19703b | 2012-07-17 14:15:34 | [diff] [blame] | 46 | output = [] |
| 47 | for input in inputs: |
[email protected] | 92bba24 | 2012-11-09 18:04:50 | [diff] [blame] | 48 | output.extend(ListFilesForPath(input)) |
[email protected] | da19703b | 2012-07-17 14:15:34 | [diff] [blame] | 49 | return output |
| 50 | |
| 51 | def CopyFiles(relative_filenames, output_basedir): |
| 52 | """Copies files to the given output directory.""" |
| 53 | for file in relative_filenames: |
| 54 | relative_dirname = os.path.dirname(file) |
| 55 | output_dir = os.path.join(output_basedir, relative_dirname) |
| 56 | output_filename = os.path.join(output_basedir, file) |
| 57 | |
| 58 | # In cases where a directory has turned into a file or vice versa, delete it |
| 59 | # before copying it below. |
| 60 | if os.path.exists(output_dir) and not os.path.isdir(output_dir): |
| 61 | os.remove(output_dir) |
| 62 | if os.path.exists(output_filename) and os.path.isdir(output_filename): |
| 63 | shutil.rmtree(output_filename) |
| 64 | |
| 65 | if not os.path.exists(output_dir): |
| 66 | os.makedirs(output_dir) |
| 67 | shutil.copy(file, output_filename) |
| 68 | |
| 69 | def DoMain(argv): |
| 70 | parser = optparse.OptionParser() |
| 71 | usage = 'Usage: %prog -o <output_dir> [--inputs] [--outputs] <input_files>' |
| 72 | parser.set_usage(usage) |
| 73 | parser.add_option('-o', dest='output_dir') |
| 74 | parser.add_option('--inputs', action='store_true', dest='list_inputs') |
| 75 | parser.add_option('--outputs', action='store_true', dest='list_outputs') |
| 76 | options, arglist = parser.parse_args(argv) |
| 77 | |
| 78 | if len(arglist) == 0: |
| 79 | raise WrongNumberOfArgumentsException('<input_files> required.') |
| 80 | |
| 81 | files_to_copy = CalcInputs(arglist) |
[email protected] | 92bba24 | 2012-11-09 18:04:50 | [diff] [blame] | 82 | escaped_files = [EscapePath(x) for x in CalcInputs(arglist)] |
[email protected] | da19703b | 2012-07-17 14:15:34 | [diff] [blame] | 83 | if options.list_inputs: |
[email protected] | 92bba24 | 2012-11-09 18:04:50 | [diff] [blame] | 84 | return '\n'.join(escaped_files) |
[email protected] | da19703b | 2012-07-17 14:15:34 | [diff] [blame] | 85 | |
| 86 | if not options.output_dir: |
| 87 | raise WrongNumberOfArgumentsException('-o required.') |
| 88 | |
| 89 | if options.list_outputs: |
[email protected] | 92bba24 | 2012-11-09 18:04:50 | [diff] [blame] | 90 | outputs = [os.path.join(options.output_dir, x) for x in escaped_files] |
[email protected] | da19703b | 2012-07-17 14:15:34 | [diff] [blame] | 91 | return '\n'.join(outputs) |
| 92 | |
| 93 | CopyFiles(files_to_copy, options.output_dir) |
| 94 | return |
| 95 | |
| 96 | def main(argv): |
| 97 | try: |
| 98 | result = DoMain(argv[1:]) |
Raul Tambre | f7d4453b | 2019-09-26 19:20:53 | [diff] [blame] | 99 | except WrongNumberOfArgumentsException as e: |
Raul Tambre | 9e24293b | 2019-05-12 06:11:07 | [diff] [blame] | 100 | print(e, file=sys.stderr) |
[email protected] | da19703b | 2012-07-17 14:15:34 | [diff] [blame] | 101 | return 1 |
| 102 | if result: |
Raul Tambre | 9e24293b | 2019-05-12 06:11:07 | [diff] [blame] | 103 | print(result) |
[email protected] | da19703b | 2012-07-17 14:15:34 | [diff] [blame] | 104 | return 0 |
| 105 | |
| 106 | if __name__ == '__main__': |
| 107 | sys.exit(main(sys.argv)) |