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