Advertisement
Guest User

Mirror folder for utorrent

a guest
Dec 20th, 2014
2,353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.55 KB | None | 0 0
  1. import argparse, os, errno
  2.  
  3. def main(sources, dest_folder, utorrent=False):
  4.     """
  5.     Script takes a list of paths (sources) and creates a matching links/folders
  6.     hierarchy in a destination folder (dest_folder).
  7.    
  8.     You can include '*' in the sources to add everything in the current directory.
  9.     """
  10.     if utorrent:
  11.         # utorrent behaves like this:
  12.         # 1. single-file torrent: we'll receive the file and its parent dir (which probably contains all other downloads!!)
  13.         # 2. dir-torrent: we'll only receive the folder containing the files.
  14.         assert len(sources) == 2
  15.        
  16.         if '' in sources:
  17.             # dir
  18.             src = next(f for f in sources if f != '')
  19.         else:
  20.             # file
  21.             src = next(f for f in sources if os.path.isfile(f))
  22.        
  23.         sources = [src]
  24.    
  25.     dest_folder = os.path.abspath(dest_folder)
  26.     paths = set()
  27.     for source in sources: 
  28.         if source == '*':
  29.             # wildcard mode! grab everything in the current directory
  30.             # ...except self and the destination; true story!
  31.             current_filename = os.path.realpath(__file__)
  32.             current_folder = os.getcwd()
  33.             for source in walk(os.getcwd()):
  34.                 if source not in [current_filename, dest_folder, current_folder]:
  35.                     print(source)
  36.                     paths.add(source)
  37.         else:
  38.             assert os.path.exists(source), "File doesn't exist: " + source
  39.             paths.add(os.path.abspath(source))
  40.            
  41.     # create the destination path
  42.     skip_on_exists(lambda: os.makedirs(args.dest_folder))
  43.    
  44.     # all good, create the links & folders!
  45.     for f in paths:
  46.         mirror_path(f, args.dest_folder)
  47.    
  48. def mirror_path(path, dest_folder):
  49.     """
  50.     Mirrors path into dest_folder, according to these rules:
  51.         path is a file: a link is created in dest_folder
  52.         path is directory: a directory is created in dest_folder,
  53.         then everything inside is mirrored too.
  54.     """
  55.     path = os.path.abspath(path)
  56.     dest_folder = os.path.abspath(dest_folder)
  57.     # ensure we have a base directory to work with
  58.     skip_on_exists(lambda: os.makedirs(dest_folder))
  59.     # compute the emplacement of the mirror item
  60.     dest = os.path.join(dest_folder, os.path.basename(path))
  61.    
  62.     if os.path.isdir(path):
  63.         # source is a directory: mirror it with a normal directory
  64.         skip_on_exists(lambda: os.makedirs(dest))
  65.         # now we'll go into that source and mirror all items within,
  66.         # setting the destination to the directory we just created
  67.         for p in walk(path):
  68.             mirror_path(p, dest)
  69.     else:
  70.         # this is a file; just create a symlink!
  71.         skip_on_exists(lambda: os.symlink(path, dest))
  72.    
  73. def walk(path):
  74.     """
  75.     Returns everything (files+dirs) in path as absolute paths.
  76.     Non-recursive.
  77.     """
  78.     for dirpath, dirnames, filenames in os.walk(path):
  79.         return [os.path.join(dirpath, path) for path in dirnames + filenames]
  80.    
  81. def skip_on_exists(action):
  82.     """
  83.     Performs callable (action), ignoring any OSError indicating that
  84.     the resource already exists.
  85.     """
  86.     try:
  87.         action()
  88.     except OSError as err:
  89.         if err.errno == errno.EEXIST:
  90.             pass  # directory already exists
  91.         else:
  92.             raise
  93.  
  94.  
  95. if __name__ == '__main__':
  96.     """
  97.     Process command-line arguments and call main()
  98.     """
  99.     parser = argparse.ArgumentParser()
  100.     parser.add_argument('sources', nargs='+', help='the files or dirs to process')
  101.     parser.add_argument('--dest_folder', required=True, help='the destination folder')
  102.     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.')
  103.     args = parser.parse_args()
  104.     main(args.sources, args.dest_folder, args.utorrent)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement