Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import sys
  4. import time
  5. import os
  6. import uuid
  7. import shutil
  8.  
  9. # log file
  10. LOGPATH = "/tmp/on-download-completed.log"
  11.  
  12. # where to move completed downloads
  13. DEST_DIR = "/tmp/dl"
  14.  
  15. # where download are moved from
  16. SRC_DIR = "/tmp/.tmp"
  17.  
  18. if len(sys.argv) < 4:
  19. exit()
  20.  
  21. logger = open(LOGPATH,'a')
  22.  
  23. def end():
  24. log.close()
  25.  
  26. def log(msg):
  27. timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
  28. str = "[{}] {}".format(timestamp, msg)
  29. logger.write("{}\n".format(str))
  30. print(str)
  31.  
  32. def common_start(sa, sb):
  33. """ returns the longest common substring from the beginning of sa and sb """
  34. def _iter():
  35. for a, b in zip(sa, sb):
  36. if a == b:
  37. yield a
  38. else:
  39. return
  40.  
  41. return ''.join(_iter())
  42.  
  43. download_id = sys.argv[1]
  44. num_files = sys.argv[2]
  45. first_file = sys.argv[3]
  46.  
  47. abs_dest_path = os.path.abspath(DEST_DIR)
  48. abs_src_dir_path = os.path.abspath(SRC_DIR)
  49. abs_file_path = os.path.abspath(first_file)
  50.  
  51. rel_src_dir = os.path.relpath(abs_file_path, abs_src_dir_path)
  52. rel_src_dir = os.path.normpath(rel_src_dir)
  53. rel_src_dir = rel_src_dir.split(os.sep)[0]
  54.  
  55. src_dir = os.path.join(abs_src_dir_path, rel_src_dir)
  56. dst_dir = os.path.join(abs_dest_path, rel_src_dir)
  57.  
  58. if not os.path.isdir(src_dir):
  59. log("source dir '{0}' does not exist!".format(src_dir))
  60. exit()
  61.  
  62. if os.path.exists(dst_dir):
  63. log("destination path already exists, using random suffix")
  64. dst_dir += "_{}".format(uuid.uuid4().hex)
  65.  
  66. log("moving '{}' to '{}'".format(src_dir, dst_dir))
  67.  
  68. shutil.move(src_dir, dst_dir)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement