blob: cd23fb29bd5d377f13223b5f6aa932aea1d690ae [file] [log] [blame]
[email protected]da19703b2012-07-17 14:15:341#!/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 Tambre9e24293b2019-05-12 06:11:078from __future__ import print_function
9
[email protected]da19703b2012-07-17 14:15:3410import optparse
11import os
12import shutil
13import sys
14
15class WrongNumberOfArgumentsException(Exception):
16 pass
17
[email protected]92bba242012-11-09 18:04:5018def EscapePath(path):
19 """Returns a path with spaces escaped."""
20 return path.replace(" ", "\\ ")
21
[email protected]da19703b2012-07-17 14:15:3422def ListFilesForPath(path):
23 """Returns a list of all the files under a given path."""
24 output = []
[email protected]6fdd63f62012-11-05 14:22:3025 # Ignore revision control metadata directories.
26 if (os.path.basename(path).startswith('.git') or
27 os.path.basename(path).startswith('.svn')):
[email protected]14275a592012-07-18 11:16:2128 return output
29
[email protected]da19703b2012-07-17 14:15:3430 # 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
42def CalcInputs(inputs):
43 """Computes the full list of input files for a set of command-line arguments.
44 """
[email protected]92bba242012-11-09 18:04:5045 # |inputs| is a list of paths, which may be directories.
[email protected]da19703b2012-07-17 14:15:3446 output = []
47 for input in inputs:
[email protected]92bba242012-11-09 18:04:5048 output.extend(ListFilesForPath(input))
[email protected]da19703b2012-07-17 14:15:3449 return output
50
51def 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
69def 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]92bba242012-11-09 18:04:5082 escaped_files = [EscapePath(x) for x in CalcInputs(arglist)]
[email protected]da19703b2012-07-17 14:15:3483 if options.list_inputs:
[email protected]92bba242012-11-09 18:04:5084 return '\n'.join(escaped_files)
[email protected]da19703b2012-07-17 14:15:3485
86 if not options.output_dir:
87 raise WrongNumberOfArgumentsException('-o required.')
88
89 if options.list_outputs:
[email protected]92bba242012-11-09 18:04:5090 outputs = [os.path.join(options.output_dir, x) for x in escaped_files]
[email protected]da19703b2012-07-17 14:15:3491 return '\n'.join(outputs)
92
93 CopyFiles(files_to_copy, options.output_dir)
94 return
95
96def main(argv):
97 try:
98 result = DoMain(argv[1:])
Raul Tambref7d4453b2019-09-26 19:20:5399 except WrongNumberOfArgumentsException as e:
Raul Tambre9e24293b2019-05-12 06:11:07100 print(e, file=sys.stderr)
[email protected]da19703b2012-07-17 14:15:34101 return 1
102 if result:
Raul Tambre9e24293b2019-05-12 06:11:07103 print(result)
[email protected]da19703b2012-07-17 14:15:34104 return 0
105
106if __name__ == '__main__':
107 sys.exit(main(sys.argv))