copy_test_data_ios.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. from __future__ import print_function
  7. import optparse
  8. import os
  9. import shutil
  10. import sys
  11. class WrongNumberOfArgumentsException(Exception):
  12. pass
  13. def EscapePath(path):
  14. """Returns a path with spaces escaped."""
  15. return path.replace(" ", "\\ ")
  16. def ListFilesForPath(path):
  17. """Returns a list of all the files under a given path."""
  18. output = []
  19. # Ignore revision control metadata directories.
  20. if (os.path.basename(path).startswith('.git') or
  21. os.path.basename(path).startswith('.svn')):
  22. return output
  23. # Files get returned without modification.
  24. if not os.path.isdir(path):
  25. output.append(path)
  26. return output
  27. # Directories get recursively expanded.
  28. contents = os.listdir(path)
  29. for item in contents:
  30. full_path = os.path.join(path, item)
  31. output.extend(ListFilesForPath(full_path))
  32. return output
  33. def CalcInputs(inputs):
  34. """Computes the full list of input files for a set of command-line arguments.
  35. """
  36. # |inputs| is a list of paths, which may be directories.
  37. output = []
  38. for input in inputs:
  39. output.extend(ListFilesForPath(input))
  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. escaped_files = [EscapePath(x) for x in CalcInputs(arglist)]
  68. if options.list_inputs:
  69. return '\n'.join(escaped_files)
  70. if not options.output_dir:
  71. raise WrongNumberOfArgumentsException('-o required.')
  72. if options.list_outputs:
  73. outputs = [os.path.join(options.output_dir, x) for x in escaped_files]
  74. return '\n'.join(outputs)
  75. CopyFiles(files_to_copy, options.output_dir)
  76. return
  77. def main(argv):
  78. try:
  79. result = DoMain(argv[1:])
  80. except WrongNumberOfArgumentsException as e:
  81. print(e, file=sys.stderr)
  82. return 1
  83. if result:
  84. print(result)
  85. return 0
  86. if __name__ == '__main__':
  87. sys.exit(main(sys.argv))