Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 3 | # Use of this source code is governed by a BSD-style license that can be |
michael@0 | 4 | # found in the LICENSE file. |
michael@0 | 5 | |
michael@0 | 6 | """Copies test data files or directories into a given output directory.""" |
michael@0 | 7 | |
michael@0 | 8 | import optparse |
michael@0 | 9 | import os |
michael@0 | 10 | import shutil |
michael@0 | 11 | import sys |
michael@0 | 12 | |
michael@0 | 13 | class WrongNumberOfArgumentsException(Exception): |
michael@0 | 14 | pass |
michael@0 | 15 | |
michael@0 | 16 | def ListFilesForPath(path): |
michael@0 | 17 | """Returns a list of all the files under a given path.""" |
michael@0 | 18 | output = [] |
michael@0 | 19 | # Ignore dotfiles and dot directories. |
michael@0 | 20 | # TODO(rohitrao): This will fail to exclude cases where the initial argument |
michael@0 | 21 | # is a relative path that starts with a dot. |
michael@0 | 22 | if os.path.basename(path).startswith('.'): |
michael@0 | 23 | return output |
michael@0 | 24 | |
michael@0 | 25 | # Files get returned without modification. |
michael@0 | 26 | if not os.path.isdir(path): |
michael@0 | 27 | output.append(path) |
michael@0 | 28 | return output |
michael@0 | 29 | |
michael@0 | 30 | # Directories get recursively expanded. |
michael@0 | 31 | contents = os.listdir(path) |
michael@0 | 32 | for item in contents: |
michael@0 | 33 | full_path = os.path.join(path, item) |
michael@0 | 34 | output.extend(ListFilesForPath(full_path)) |
michael@0 | 35 | return output |
michael@0 | 36 | |
michael@0 | 37 | def CalcInputs(inputs): |
michael@0 | 38 | """Computes the full list of input files for a set of command-line arguments. |
michael@0 | 39 | """ |
michael@0 | 40 | # |inputs| is a list of strings, each of which may contain muliple paths |
michael@0 | 41 | # separated by spaces. |
michael@0 | 42 | output = [] |
michael@0 | 43 | for input in inputs: |
michael@0 | 44 | tokens = input.split() |
michael@0 | 45 | for token in tokens: |
michael@0 | 46 | output.extend(ListFilesForPath(token)) |
michael@0 | 47 | return output |
michael@0 | 48 | |
michael@0 | 49 | def CopyFiles(relative_filenames, output_basedir): |
michael@0 | 50 | """Copies files to the given output directory.""" |
michael@0 | 51 | for file in relative_filenames: |
michael@0 | 52 | relative_dirname = os.path.dirname(file) |
michael@0 | 53 | output_dir = os.path.join(output_basedir, relative_dirname) |
michael@0 | 54 | output_filename = os.path.join(output_basedir, file) |
michael@0 | 55 | |
michael@0 | 56 | # In cases where a directory has turned into a file or vice versa, delete it |
michael@0 | 57 | # before copying it below. |
michael@0 | 58 | if os.path.exists(output_dir) and not os.path.isdir(output_dir): |
michael@0 | 59 | os.remove(output_dir) |
michael@0 | 60 | if os.path.exists(output_filename) and os.path.isdir(output_filename): |
michael@0 | 61 | shutil.rmtree(output_filename) |
michael@0 | 62 | |
michael@0 | 63 | if not os.path.exists(output_dir): |
michael@0 | 64 | os.makedirs(output_dir) |
michael@0 | 65 | shutil.copy(file, output_filename) |
michael@0 | 66 | |
michael@0 | 67 | def DoMain(argv): |
michael@0 | 68 | parser = optparse.OptionParser() |
michael@0 | 69 | usage = 'Usage: %prog -o <output_dir> [--inputs] [--outputs] <input_files>' |
michael@0 | 70 | parser.set_usage(usage) |
michael@0 | 71 | parser.add_option('-o', dest='output_dir') |
michael@0 | 72 | parser.add_option('--inputs', action='store_true', dest='list_inputs') |
michael@0 | 73 | parser.add_option('--outputs', action='store_true', dest='list_outputs') |
michael@0 | 74 | options, arglist = parser.parse_args(argv) |
michael@0 | 75 | |
michael@0 | 76 | if len(arglist) == 0: |
michael@0 | 77 | raise WrongNumberOfArgumentsException('<input_files> required.') |
michael@0 | 78 | |
michael@0 | 79 | files_to_copy = CalcInputs(arglist) |
michael@0 | 80 | if options.list_inputs: |
michael@0 | 81 | return '\n'.join(files_to_copy) |
michael@0 | 82 | |
michael@0 | 83 | if not options.output_dir: |
michael@0 | 84 | raise WrongNumberOfArgumentsException('-o required.') |
michael@0 | 85 | |
michael@0 | 86 | if options.list_outputs: |
michael@0 | 87 | outputs = [os.path.join(options.output_dir, x) for x in files_to_copy] |
michael@0 | 88 | return '\n'.join(outputs) |
michael@0 | 89 | |
michael@0 | 90 | CopyFiles(files_to_copy, options.output_dir) |
michael@0 | 91 | return |
michael@0 | 92 | |
michael@0 | 93 | def main(argv): |
michael@0 | 94 | try: |
michael@0 | 95 | result = DoMain(argv[1:]) |
michael@0 | 96 | except WrongNumberOfArgumentsException, e: |
michael@0 | 97 | print >>sys.stderr, e |
michael@0 | 98 | return 1 |
michael@0 | 99 | if result: |
michael@0 | 100 | print result |
michael@0 | 101 | return 0 |
michael@0 | 102 | |
michael@0 | 103 | if __name__ == '__main__': |
michael@0 | 104 | sys.exit(main(sys.argv)) |