Procházet zdrojové kódy

refactor; use of --sizes and --threshold together seem to work

dennisro před 5 roky
rodič
revize
ac6ca4c7ef
1 změnil soubory, kde provedl 35 přidání a 36 odebrání
  1. 35 36
      2.py

+ 35 - 36
2.py

@@ -1,7 +1,6 @@
 #!/usr/local/bin/python3
-import os, sys
+import os, sys, argparse
 import hashlib
-import argparse
 import humanfriendly
 
 parser = argparse.ArgumentParser(description='What the dupe!?')
@@ -12,15 +11,15 @@ required = parser.add_argument_group('required arguments')
 optional.add_argument('--threshold', type=str,
                     help='Only output files greater than \'size\', e.g. 100M')
 
-optional.add_argument('--exclude', type=str, nargs='?', action='append',
-                    help='Only output files greater than \'size\', e.g. 100M')
-
 optional.add_argument('--sizes', type=str, nargs='+',
                     help='Only output files greater than \'size\', e.g. 100M')
 
 required.add_argument('--dir', type=str, nargs='?', required=True, action='append',
                     help='Directory to scan. Can be issued multiple times.')
 
+optional.add_argument('--exclude', type=str, nargs='?', action='append',
+                    help='Only output files greater than \'size\', e.g. 100M')
+
 parser._action_groups.append(optional)
 
 args = parser.parse_args()
@@ -99,11 +98,7 @@ def printResults(dict1):
     results = list(filter(lambda x: len(x) > 1, dict1.values()))
     for result in results:
         file_size = os.path.getsize(result[0])
-        if threshold > 0:
-            if file_size >= threshold:
-                final[threshold].append(result)
-
-        else:
+        if not args.threshold or (args.threshold and args.sizes):
             count = 0
             while count+1 < len(sizes):
                 try:
@@ -112,23 +107,23 @@ def printResults(dict1):
                 except:
                     final[sizes[-1]].append(result)
                 count += 1
-            if file_size < humanfriendly.parse_size(sizes[0]):
-                final[sizes[0]].append(result)
-            final[threshold]=[False]
-    if len(results) > 0 and len(final[threshold]) > 0:
+                if args.threshold and args.sizes:
+                    pass
+                elif file_size < humanfriendly.parse_size(sizes[0]):
+                    print('Hi')
+                    final[sizes[0]].append(result)
+        else:
+            print('Hi')
+            if file_size >= threshold:
+                final[threshold].append(result)
+
+    if len(results) > 0:
         print('___________________')
         print('\n\033[1;34m\033[1;34m\u25b6 Duplicates Found\033[0m\n')
         print('  The following files are identical. The name could differ, but the content is identical')
         print('___________________')
         new = ['0']
-        if threshold > 0:
-             print("\n\033[1;34m\u25b6 Files bigger than %s\033[0m" % humanfriendly.format_size(threshold, binary=True))
-             for dupe in final[threshold]:
-                 print('___________________\n')
-                 for file in dupe:
-                     print('  %s' % str(file))
-             print('___________________')
-        else:
+        if not args.threshold or (args.threshold and args.sizes):
             for size in sizes:
                 new.append(size)
                 if len(final[size]) > 0:
@@ -141,22 +136,26 @@ def printResults(dict1):
                         for file in dupe:
                             print('  %s' % str(file))
                     print('___________________')
+        else:
+             print("\n\033[1;34m\u25b6 Files bigger than %s\033[0m" % humanfriendly.format_size(threshold, binary=True))
+             for dupe in final[threshold]:
+                 print('___________________\n')
+                 for file in dupe:
+                     print('  %s' % str(file))
+             print('___________________')
 
     else:
         print('\n\033[1mNo duplicate files found.\033[0m')
 
 if __name__ == '__main__':
-    if len(sys.argv) > 1:
-        dups = {}
-        folders = args.dir
-        for i in folders:
-            # Iterate the folders given
-            if os.path.exists(i):
-                # Find the duplicated files and append them to the dups
-                joinDicts(dups, findDup(i))
-            else:
-                print('%s is not a valid path, please verify' % i)
-                sys.exit()
-        printResults(dups)
-    else:
-        print('Usage: python dupFinder.py folder or python dupFinder.py folder1 folder2 folder3')
+    dups = {}
+    folders = args.dir
+    for i in folders:
+        # Iterate the folders given
+        if os.path.exists(i):
+            # Find the duplicated files and append them to the dups
+            joinDicts(dups, findDup(i))
+        else:
+            print('%s is not a valid path, please verify' % i)
+            sys.exit()
+    printResults(dups)