copy_file.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. import argparse
  9. import os
  10. import sys
  11. import glob
  12. import shutil
  13. def parse_args():
  14. parser = argparse.ArgumentParser()
  15. parser.add_argument('-s', '--src-dir', dest='src_dir', required=True, help='Source directory to copy files from, if not specified, current directory is used.')
  16. parser.add_argument('-r', '--file-regex', dest='file_regex', required=True, help='Globbing pattern used to match file names to copy.')
  17. parser.add_argument('-t', '--target-dir', dest="target_dir", required=True, help='Target directory to copy files to.')
  18. args = parser.parse_args()
  19. if not os.path.isdir(args.src_dir):
  20. print('ERROR: src_dir is not a valid directory.')
  21. exit(1)
  22. return args
  23. def extended_path(path):
  24. """
  25. Maximum Path Length Limitation on Windows is 260 characters, use extended-length path to bypass this limitation
  26. """
  27. if sys.platform in ('win32', 'cli') and len(path) >= 260:
  28. if path.startswith('\\'):
  29. return r'\\?\UNC\{}'.format(path.lstrip('\\'))
  30. else:
  31. return r'\\?\{}'.format(path)
  32. else:
  33. return path
  34. def copy_file(src_dir, file_regex, target_dir):
  35. if not os.path.isdir(args.target_dir):
  36. os.makedirs(target_dir)
  37. for f in glob.glob(os.path.join(src_dir, file_regex), recursive=True):
  38. if os.path.isfile(f):
  39. relative_path = os.path.relpath(f, src_dir)
  40. target_file_path = os.path.join(target_dir, relative_path)
  41. target_file_dir = os.path.dirname(target_file_path)
  42. if not os.path.isdir(target_file_dir):
  43. os.makedirs(target_file_dir)
  44. shutil.copy2(f, extended_path(target_file_path))
  45. print(f'{f} -> {target_file_path}')
  46. if __name__ == "__main__":
  47. args = parse_args()
  48. copy_file(args.src_dir, args.file_regex, args.target_dir)