copy_test_data_ios.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.
  5. """Copies test data files or directories into a given output directory."""
  6. import optparse
  7. import os
  8. import shutil
  9. import sys
  10. class WrongNumberOfArgumentsException(Exception):
  11. pass
  12. def ListFilesForPath(path):
  13. """Returns a list of all the files under a given path."""
  14. output = []
  15. # Ignore dotfiles and dot directories.
  16. # TODO(rohitrao): This will fail to exclude cases where the initial argument
  17. # is a relative path that starts with a dot.
  18. if os.path.basename(path).startswith('.'):
  19. return output
  20. # Files get returned without modification.
  21. if not os.path.isdir(path):
  22. output.append(path)
  23. return output
  24. # Directories get recursively expanded.
  25. contents = os.listdir(path)
  26. for item in contents:
  27. full_path = os.path.join(path, item)
  28. output.extend(ListFilesForPath(full_path))
  29. return output
  30. def CalcInputs(inputs):
  31. """Computes the full list of input files for a set of command-line arguments.
  32. """
  33. # |inputs| is a list of strings, each of which may contain muliple paths
  34. # separated by spaces.
  35. output = []
  36. for input in inputs:
  37. tokens = input.split()
  38. for token in tokens:
  39. output.extend(ListFilesForPath(token))
  40. return output
  41. def CopyFiles(relative_filenames, output_basedir):
  42. """Copies files to the given output directory."""
  43. for file in relative_filenames:
  44. relative_dirname = os.path.dirname(file)
  45. output_dir = os.path.join(output_basedir, relative_dirname)
  46. output_filename = os.path.join(output_basedir, file)
  47. # In cases where a directory has turned into a file or vice versa, delete it
  48. # before copying it below.
  49. if os.path.exists(output_dir) and not os.path.isdir(output_dir):
  50. os.remove(output_dir)
  51. if os.path.exists(output_filename) and os.path.isdir(output_filename):
  52. shutil.rmtree(output_filename)
  53. if not os.path.exists(output_dir):
  54. os.makedirs(output_dir)
  55. shutil.copy(file, output_filename)
  56. def DoMain(argv):
  57. parser = optparse.OptionParser()
  58. usage = 'Usage: %prog -o <output_dir> [--inputs] [--outputs] <input_files>'
  59. parser.set_usage(usage)
  60. parser.add_option('-o', dest='output_dir')
  61. parser.add_option('--inputs', action='store_true', dest='list_inputs')
  62. parser.add_option('--outputs', action='store_true', dest='list_outputs')
  63. options, arglist = parser.parse_args(argv)
  64. if len(arglist) == 0:
  65. raise WrongNumberOfArgumentsException('<input_files> required.')
  66. files_to_copy = CalcInputs(arglist)
  67. if options.list_inputs:
  68. return '\n'.join(files_to_copy)
  69. if not options.output_dir:
  70. raise WrongNumberOfArgumentsException('-o required.')
  71. if options.list_outputs:
  72. outputs = [os.path.join(options.output_dir, x) for x in files_to_copy]
  73. return '\n'.join(outputs)
  74. CopyFiles(files_to_copy, options.output_dir)
  75. return
  76. def main(argv):
  77. try:
  78. result = DoMain(argv[1:])
  79. except WrongNumberOfArgumentsException, e:
  80. print >>sys.stderr, e
  81. return 1
  82. if result:
  83. print result
  84. return 0
  85. if __name__ == '__main__':
  86. sys.exit(main(sys.argv))