Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import sys
- import time
- import os
- import uuid
- import shutil
- # log file
- LOGPATH = "/tmp/on-download-completed.log"
- # where to move completed downloads
- DEST_DIR = "/tmp/dl"
- # where download are moved from
- SRC_DIR = "/tmp/.tmp"
- if len(sys.argv) < 4:
- exit()
- logger = open(LOGPATH,'a')
- def end():
- log.close()
- def log(msg):
- timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
- str = "[{}] {}".format(timestamp, msg)
- logger.write("{}\n".format(str))
- print(str)
- def common_start(sa, sb):
- """ returns the longest common substring from the beginning of sa and sb """
- def _iter():
- for a, b in zip(sa, sb):
- if a == b:
- yield a
- else:
- return
- return ''.join(_iter())
- download_id = sys.argv[1]
- num_files = sys.argv[2]
- first_file = sys.argv[3]
- abs_dest_path = os.path.abspath(DEST_DIR)
- abs_src_dir_path = os.path.abspath(SRC_DIR)
- abs_file_path = os.path.abspath(first_file)
- rel_src_dir = os.path.relpath(abs_file_path, abs_src_dir_path)
- rel_src_dir = os.path.normpath(rel_src_dir)
- rel_src_dir = rel_src_dir.split(os.sep)[0]
- src_dir = os.path.join(abs_src_dir_path, rel_src_dir)
- dst_dir = os.path.join(abs_dest_path, rel_src_dir)
- if not os.path.isdir(src_dir):
- log("source dir '{0}' does not exist!".format(src_dir))
- exit()
- if os.path.exists(dst_dir):
- log("destination path already exists, using random suffix")
- dst_dir += "_{}".format(uuid.uuid4().hex)
- log("moving '{}' to '{}'".format(src_dir, dst_dir))
- shutil.move(src_dir, dst_dir)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement