ingest.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from argparse import ArgumentParser, Namespace
  2. import glob
  3. import shutil
  4. from typing import List
  5. import os
  6. from dataclasses import dataclass
  7. import time
  8. @dataclass
  9. class Arguments:
  10. source_path: str
  11. dest_path: str
  12. def dir_path(string) -> str:
  13. if os.path.isdir(string):
  14. return string
  15. else:
  16. raise NotADirectoryError(string)
  17. def addArguments(parser: ArgumentParser) -> None:
  18. parser.add_argument('from_path', type=dir_path)
  19. parser.add_argument('to_path', type=dir_path)
  20. parser.set_defaults(func=main)
  21. def parseRawArgs(args: Namespace) -> Arguments:
  22. return Arguments(args.from_path, args.to_path)
  23. def filesByExtension(source_path: str, extension: str) -> List[str]:
  24. upper_extension = extension.upper()
  25. lower_extension = extension.lower()
  26. source_files = []
  27. source_files.extend(glob.glob(f"**/*.{upper_extension}", root_dir=source_path, recursive=True))
  28. source_files.extend(glob.glob(f"**/*.{lower_extension}", root_dir=source_path, recursive=True))
  29. return source_files
  30. def main(raw_args: Namespace) -> None:
  31. args = parseRawArgs(raw_args)
  32. jpegs = filesByExtension(args.source_path, "JPG")
  33. raws = filesByExtension(args.source_path, "RAF")
  34. movs = filesByExtension(args.source_path, "MOV")
  35. total_size = 0
  36. for file in jpegs:
  37. total_size += os.path.getsize(os.path.join(args.source_path, file))
  38. for file in raws:
  39. total_size += os.path.getsize(os.path.join(args.source_path, file))
  40. for file in movs:
  41. total_size += os.path.getsize(os.path.join(args.source_path, file))
  42. block_size = os.statvfs(args.dest_path).f_frsize
  43. blocks_free = os.statvfs(args.dest_path).f_bfree
  44. assert(block_size * blocks_free > total_size)
  45. for (files, subdir) in zip([jpegs, raws, movs], ["JPEG", "RAW", "MOV"]):
  46. try:
  47. os.mkdir(os.path.join(args.dest_path, subdir))
  48. except FileExistsError:
  49. print("Warning: path exists")
  50. pass
  51. time.sleep(1)
  52. for file in files:
  53. source_file = os.path.join(args.source_path, file)
  54. dest_folder = os.path.join(args.dest_path, subdir)
  55. print(f"Copying {source_file} to {dest_folder}")
  56. shutil.copy2(source_file, dest_folder)