Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import argparse, os, errno
- def main(sources, dest_folder, utorrent=False):
- """
- Script takes a list of paths (sources) and creates a matching links/folders
- hierarchy in a destination folder (dest_folder).
- You can include '*' in the sources to add everything in the current directory.
- """
- if utorrent:
- # utorrent behaves like this:
- # 1. single-file torrent: we'll receive the file and its parent dir (which probably contains all other downloads!!)
- # 2. dir-torrent: we'll only receive the folder containing the files.
- assert len(sources) == 2
- if '' in sources:
- # dir
- src = next(f for f in sources if f != '')
- else:
- # file
- src = next(f for f in sources if os.path.isfile(f))
- sources = [src]
- dest_folder = os.path.abspath(dest_folder)
- paths = set()
- for source in sources:
- if source == '*':
- # wildcard mode! grab everything in the current directory
- # ...except self and the destination; true story!
- current_filename = os.path.realpath(__file__)
- current_folder = os.getcwd()
- for source in walk(os.getcwd()):
- if source not in [current_filename, dest_folder, current_folder]:
- print(source)
- paths.add(source)
- else:
- assert os.path.exists(source), "File doesn't exist: " + source
- paths.add(os.path.abspath(source))
- # create the destination path
- skip_on_exists(lambda: os.makedirs(args.dest_folder))
- # all good, create the links & folders!
- for f in paths:
- mirror_path(f, args.dest_folder)
- def mirror_path(path, dest_folder):
- """
- Mirrors path into dest_folder, according to these rules:
- path is a file: a link is created in dest_folder
- path is directory: a directory is created in dest_folder,
- then everything inside is mirrored too.
- """
- path = os.path.abspath(path)
- dest_folder = os.path.abspath(dest_folder)
- # ensure we have a base directory to work with
- skip_on_exists(lambda: os.makedirs(dest_folder))
- # compute the emplacement of the mirror item
- dest = os.path.join(dest_folder, os.path.basename(path))
- if os.path.isdir(path):
- # source is a directory: mirror it with a normal directory
- skip_on_exists(lambda: os.makedirs(dest))
- # now we'll go into that source and mirror all items within,
- # setting the destination to the directory we just created
- for p in walk(path):
- mirror_path(p, dest)
- else:
- # this is a file; just create a symlink!
- skip_on_exists(lambda: os.symlink(path, dest))
- def walk(path):
- """
- Returns everything (files+dirs) in path as absolute paths.
- Non-recursive.
- """
- for dirpath, dirnames, filenames in os.walk(path):
- return [os.path.join(dirpath, path) for path in dirnames + filenames]
- def skip_on_exists(action):
- """
- Performs callable (action), ignoring any OSError indicating that
- the resource already exists.
- """
- try:
- action()
- except OSError as err:
- if err.errno == errno.EEXIST:
- pass # directory already exists
- else:
- raise
- if __name__ == '__main__':
- """
- Process command-line arguments and call main()
- """
- parser = argparse.ArgumentParser()
- parser.add_argument('sources', nargs='+', help='the files or dirs to process')
- parser.add_argument('--dest_folder', required=True, help='the destination folder')
- parser.add_argument('--utorrent', action='store_true', help='when calling from utorrent\'s advanced properties, set this switch so that you can pass both %F and %D for all torrents. DO NOT use this outside utorrent or it will not process all your files correctly.')
- args = parser.parse_args()
- main(args.sources, args.dest_folder, args.utorrent)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement