Commits

Anton Shestakov committed 6374bfc

Verbose mode. Additional checks.

Comments (0)

Files changed (1)

 
 import libtorrent as lt
 
+
+dry_run = False
+verbose = 1
+source_file = None
+dirty_directory = None
+
+header_printed = False
+
 def show_usage():
     print("""Remove files bigger than declared in .torrent file.
 Usage: %s -s TORRENT_FILE -d DIRECTORY [-n]
  -n, --dry-run               Don't delete anything, just print what will be done
  
  -q, --quiet                 Print only errors
+
+ -v, --verbose               Print information about every file in TORRENT_FILE
     """ % os.path.split(sys.argv[0])[1])
 
+
+def info(message, level):
+    global header_printed
+
+    if level > verbose:
+        return
+
+    if not header_printed:
+        print('%s %27s %19s %20s' 
+            % ('OPERATION', 'FILE', 'SIZE DECLARED', 'SIZE FOUND'))
+        header_printed = True
+        
+    print(message)
+
+
 def main():
+    global source_file, dirty_directory, dry_run, verbose
+    
     try:
-        opts, args = getopt.getopt(sys.argv[1:], 's:t:d:nq', 
-            ['source=', 'torrent=', 'directory=', 'dry-run', 'quiet'])
+        opts, args = getopt.getopt(sys.argv[1:], 's:t:d:nqv', 
+            ['source=', 'torrent=', 'directory=', 'dry-run', 'quiet', 'verbose'])
     except getopt.GetoptError as err:
         print(err)
         show_usage()
         sys.exit(2)
         
-    dry_run = False
-    quiet = False
-    source_file = None
-    dirty_directory = None
-
     for o, a in opts:
         if o in ('-s', '--source', '-t', '--torrent'):
             source_file = a
         elif o in ('-n', '--dry-run'):
             dry_run = True
         elif o in ('-q', '--quiet'):
-            quiet = True
+            verbose = 0
+        elif o in ('-v', '--verbose'):
+            verbose = 2
         
     if source_file is not None and dirty_directory is not None:
-        clean(source_file, dirty_directory, dry_run, quiet)
+        clean()
     else:
         show_usage()
-    
-def clean(source_file, dirty_directory, dry_run, quiet):
-    header_printed = False
-    
+
+
+def clean():
     with open(source_file) as torrent_src:
         bulk = lt.bdecode(torrent_src.read())
-    src_sizes = bulk['info']['files']
+
+    try:
+        src_sizes = bulk['info']['files']
+    except TypeError as error:
+        print('Invalid .torrent format. Couldn\'t get files list.')
+        sys.exit(4)
     
     for src_file in src_sizes:
         filename = os.path.join(*src_file['path'])
         real_path = os.path.join(dirty_directory, filename)
+
         try:
             real_size = os.stat(real_path).st_size
         except OSError:
-            continue
-        if real_size > src_file['length']:
-            if not quiet:
-                if not header_printed:
-                    print('%s %27s %19s %20s' % (
-                        'OPERATION', 'FILE', 'SIZE DECLARED', 'SIZE FOUND'))
-                    header_printed = True
-                print('unlink %30s: %12d bytes > %12d bytes' % (
-                    filename, real_size, src_file['length']))
+            real_size = None
+            
+        op = 'N/A'
+        level = 2
+        
+        if real_size is None:
+            comp = ''
+            real_size = 0
+        elif src_file['length'] < real_size:
+            op = 'unlink'
+            comp = '<'
+            level = 1
+        elif src_file['length'] > real_size:
+            comp = '>'
+        else:
+            comp = '='
+            
+        info('%6s %30s: %12d bytes %1s %12d bytes' % (
+            op, filename, src_file['length'], comp, real_size), level)
+            
+        if op == 'unlink' and not dry_run:
+            try:
+                os.unlink(real_path)
+            except OSError as error:
+                print(error)
+    return 0
 
-            if not dry_run:
-                try:
-                    os.unlink(real_path)
-                except OSError as error:
-                    print(error)
-    return 0
 
 if __name__ == '__main__':
     main()