Guest User

Untitled

a guest
May 27th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. import argparse
  2. import gzip
  3. import os
  4. import shutil
  5. import sys
  6. import threading
  7.  
  8. def parse_input():
  9. parser = argparse.ArgumentParser()
  10. parser.add_argument('-target', nargs=1, required=True,
  11. help='Target Backup folder')
  12. parser.add_argument('-source', nargs='+', required=True,
  13. help='Source Files to be added')
  14. parser.add_argument('-compress', nargs=1, type=int,
  15. help='Gzip threshold in bytes', default=[100000])
  16.  
  17. # no input means show me the help
  18. if len(sys.argv) == 1:
  19. parser.print_help()
  20. sys.exit()
  21.  
  22. return parser.parse_args()
  23.  
  24.  
  25. def size_if_newer(source, target):
  26. """ If newer it returns size, otherwise it returns False """
  27.  
  28. src_stat = os.stat(source)
  29. try:
  30. target_ts = os.stat(target).st_mtime
  31. except FileNotFoundError:
  32. try:
  33. target_ts = os.stat(target + '.gz').st_mtime
  34. except FileNotFoundError:
  35. target_ts = 0
  36.  
  37. # The time difference of one second is necessary since subsecond accuracy
  38. # of os.st_mtime is striped by copy2
  39. return src_stat.st_size if (src_stat.st_mtime - target_ts > 1) else False
  40.  
  41. def threaded_sync_file(source, target, compress):
  42. size = size_if_newer(source, target)
  43.  
  44. if size:
  45. thread = threading.Thread(target=transfer_file,
  46. args=(source, target, size > compress))
  47. thread.start()
  48. return thread
  49.  
  50. def sync_file(source, target, compress):
  51. size = size_if_newer(source, target)
  52.  
  53. if size:
  54. transfer_file(source, target, size > compress)
  55.  
  56.  
  57. def transfer_file(source, target, compress):
  58. """ Either copy or compress and copies the file """
  59.  
  60. try:
  61. if compress:
  62. with gzip.open(target + '.gz', 'wb') as target_fid:
  63. with open(source, 'rb') as source_fid:
  64. target_fid.writelines(source_fid)
  65. print('Compress {}'.format(source))
  66. else:
  67. shutil.copy2(source, target)
  68. print('Copy {}'.format(source))
  69. except FileNotFoundError:
  70. os.makedirs(os.path.dirname(target))
  71. transfer_file(source, target, compress)
  72.  
  73.  
  74. def sync_root(root, arg):
  75. target = arg.target[0]
  76. compress = arg.compress[0]
  77. threads = []
  78.  
  79. for path, _, files in os.walk(root):
  80. for source in files:
  81. source = path + '/' + source
  82. threads.append(threaded_sync_file(source,
  83. target + source, compress))
  84. # sync_file(source, target + source, compress)
  85. for thread in threads:
  86. thread.join()
  87.  
  88.  
  89. if __name__ == '__main__':
  90. arg = parse_input()
  91. print('### Start copy ####')
  92. for root in arg.source:
  93. sync_root(root, arg)
  94. print('### Done ###')
Add Comment
Please, Sign In to add comment