media/webrtc/trunk/build/copy_test_data_ios.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/trunk/build/copy_test_data_ios.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,104 @@
     1.4 +#!/usr/bin/env python
     1.5 +# Copyright (c) 2012 The Chromium Authors. All rights reserved.
     1.6 +# Use of this source code is governed by a BSD-style license that can be
     1.7 +# found in the LICENSE file.
     1.8 +
     1.9 +"""Copies test data files or directories into a given output directory."""
    1.10 +
    1.11 +import optparse
    1.12 +import os
    1.13 +import shutil
    1.14 +import sys
    1.15 +
    1.16 +class WrongNumberOfArgumentsException(Exception):
    1.17 +  pass
    1.18 +
    1.19 +def ListFilesForPath(path):
    1.20 +  """Returns a list of all the files under a given path."""
    1.21 +  output = []
    1.22 +  # Ignore dotfiles and dot directories.
    1.23 +  # TODO(rohitrao): This will fail to exclude cases where the initial argument
    1.24 +  # is a relative path that starts with a dot.
    1.25 +  if os.path.basename(path).startswith('.'):
    1.26 +    return output
    1.27 +
    1.28 +  # Files get returned without modification.
    1.29 +  if not os.path.isdir(path):
    1.30 +    output.append(path)
    1.31 +    return output
    1.32 +
    1.33 +  # Directories get recursively expanded.
    1.34 +  contents = os.listdir(path)
    1.35 +  for item in contents:
    1.36 +    full_path = os.path.join(path, item)
    1.37 +    output.extend(ListFilesForPath(full_path))
    1.38 +  return output
    1.39 +
    1.40 +def CalcInputs(inputs):
    1.41 +  """Computes the full list of input files for a set of command-line arguments.
    1.42 +  """
    1.43 +  # |inputs| is a list of strings, each of which may contain muliple paths
    1.44 +  # separated by spaces.
    1.45 +  output = []
    1.46 +  for input in inputs:
    1.47 +    tokens = input.split()
    1.48 +    for token in tokens:
    1.49 +      output.extend(ListFilesForPath(token))
    1.50 +  return output
    1.51 +
    1.52 +def CopyFiles(relative_filenames, output_basedir):
    1.53 +  """Copies files to the given output directory."""
    1.54 +  for file in relative_filenames:
    1.55 +    relative_dirname = os.path.dirname(file)
    1.56 +    output_dir = os.path.join(output_basedir, relative_dirname)
    1.57 +    output_filename = os.path.join(output_basedir, file)
    1.58 +
    1.59 +    # In cases where a directory has turned into a file or vice versa, delete it
    1.60 +    # before copying it below.
    1.61 +    if os.path.exists(output_dir) and not os.path.isdir(output_dir):
    1.62 +      os.remove(output_dir)
    1.63 +    if os.path.exists(output_filename) and os.path.isdir(output_filename):
    1.64 +      shutil.rmtree(output_filename)
    1.65 +
    1.66 +    if not os.path.exists(output_dir):
    1.67 +      os.makedirs(output_dir)
    1.68 +    shutil.copy(file, output_filename)
    1.69 +
    1.70 +def DoMain(argv):
    1.71 +  parser = optparse.OptionParser()
    1.72 +  usage = 'Usage: %prog -o <output_dir> [--inputs] [--outputs] <input_files>'
    1.73 +  parser.set_usage(usage)
    1.74 +  parser.add_option('-o', dest='output_dir')
    1.75 +  parser.add_option('--inputs', action='store_true', dest='list_inputs')
    1.76 +  parser.add_option('--outputs', action='store_true', dest='list_outputs')
    1.77 +  options, arglist = parser.parse_args(argv)
    1.78 +
    1.79 +  if len(arglist) == 0:
    1.80 +    raise WrongNumberOfArgumentsException('<input_files> required.')
    1.81 +
    1.82 +  files_to_copy = CalcInputs(arglist)
    1.83 +  if options.list_inputs:
    1.84 +    return '\n'.join(files_to_copy)
    1.85 +
    1.86 +  if not options.output_dir:
    1.87 +    raise WrongNumberOfArgumentsException('-o required.')
    1.88 +
    1.89 +  if options.list_outputs:
    1.90 +    outputs = [os.path.join(options.output_dir, x) for x in files_to_copy]
    1.91 +    return '\n'.join(outputs)
    1.92 +
    1.93 +  CopyFiles(files_to_copy, options.output_dir)
    1.94 +  return
    1.95 +
    1.96 +def main(argv):
    1.97 +  try:
    1.98 +    result = DoMain(argv[1:])
    1.99 +  except WrongNumberOfArgumentsException, e:
   1.100 +    print >>sys.stderr, e
   1.101 +    return 1
   1.102 +  if result:
   1.103 +    print result
   1.104 +  return 0
   1.105 +
   1.106 +if __name__ == '__main__':
   1.107 +  sys.exit(main(sys.argv))

mercurial