summaryrefslogtreecommitdiffstats
path: root/media/webrtc/trunk/build/copy_test_data_ios.py
blob: 51307358355ecdbf078e5e15d624829ea40ed8cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Copies test data files or directories into a given output directory."""

import optparse
import os
import shutil
import sys

class WrongNumberOfArgumentsException(Exception):
  pass

def ListFilesForPath(path):
  """Returns a list of all the files under a given path."""
  output = []
  # Ignore dotfiles and dot directories.
  # TODO(rohitrao): This will fail to exclude cases where the initial argument
  # is a relative path that starts with a dot.
  if os.path.basename(path).startswith('.'):
    return output

  # Files get returned without modification.
  if not os.path.isdir(path):
    output.append(path)
    return output

  # Directories get recursively expanded.
  contents = os.listdir(path)
  for item in contents:
    full_path = os.path.join(path, item)
    output.extend(ListFilesForPath(full_path))
  return output

def CalcInputs(inputs):
  """Computes the full list of input files for a set of command-line arguments.
  """
  # |inputs| is a list of strings, each of which may contain muliple paths
  # separated by spaces.
  output = []
  for input in inputs:
    tokens = input.split()
    for token in tokens:
      output.extend(ListFilesForPath(token))
  return output

def CopyFiles(relative_filenames, output_basedir):
  """Copies files to the given output directory."""
  for file in relative_filenames:
    relative_dirname = os.path.dirname(file)
    output_dir = os.path.join(output_basedir, relative_dirname)
    output_filename = os.path.join(output_basedir, file)

    # In cases where a directory has turned into a file or vice versa, delete it
    # before copying it below.
    if os.path.exists(output_dir) and not os.path.isdir(output_dir):
      os.remove(output_dir)
    if os.path.exists(output_filename) and os.path.isdir(output_filename):
      shutil.rmtree(output_filename)

    if not os.path.exists(output_dir):
      os.makedirs(output_dir)
    shutil.copy(file, output_filename)

def DoMain(argv):
  parser = optparse.OptionParser()
  usage = 'Usage: %prog -o <output_dir> [--inputs] [--outputs] <input_files>'
  parser.set_usage(usage)
  parser.add_option('-o', dest='output_dir')
  parser.add_option('--inputs', action='store_true', dest='list_inputs')
  parser.add_option('--outputs', action='store_true', dest='list_outputs')
  options, arglist = parser.parse_args(argv)

  if len(arglist) == 0:
    raise WrongNumberOfArgumentsException('<input_files> required.')

  files_to_copy = CalcInputs(arglist)
  if options.list_inputs:
    return '\n'.join(files_to_copy)

  if not options.output_dir:
    raise WrongNumberOfArgumentsException('-o required.')

  if options.list_outputs:
    outputs = [os.path.join(options.output_dir, x) for x in files_to_copy]
    return '\n'.join(outputs)

  CopyFiles(files_to_copy, options.output_dir)
  return

def main(argv):
  try:
    result = DoMain(argv[1:])
  except WrongNumberOfArgumentsException, e:
    print >>sys.stderr, e
    return 1
  if result:
    print result
  return 0

if __name__ == '__main__':
  sys.exit(main(sys.argv))