-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortHelper.py
More file actions
135 lines (101 loc) · 4.47 KB
/
SortHelper.py
File metadata and controls
135 lines (101 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
import shutil
import sys
import os
import argparse
import platform
from HashUtil import HashList
from HashUtil import Utils
def MoveFileToQuarantine(root, fl, args):
Utils.Quarantine(root, fl, args, "../.!Quarantine")
# path, ext = fl
# absp = os.path.join(root, path)
# movTarPath = os.path.abspath(os.path.join(os.path.join(root, "../.!Quarantine".encode()), path))
# print(movTarPath)
# lxPath = b'/'.join(movTarPath.split(b"\\")) # Linuxise
# splitPath = lxPath.split(b'/')
# print(splitPath)
# currentPath = b'/'.join(splitPath[0:-1])
# print(currentPath)
# #currentFile = split[-1]
# if not os.path.exists(currentPath):
# os.makedirs(currentPath)
# print("[INFO] Moving {} to {}".format(absp, movTarPath))
# #shutil.move(absp, movTarPath)
def IsDriveSafe(a,b):
# Check path isn't our parent
# I should check this!
absa = os.path.abspath(a)
absb = os.path.abspath(b)
if platform.system() == "Windows":
drivea = absa.split("\\")[0]
driveb = absb.split("\\")[0]
if not drivea == driveb:
return True
relp = os.path.relpath(absa, absb)
relpsl = relp.split("\\")
if(relpsl[-1] == ".."):
# Can get to this directory :(
return False
else:
relp = os.path.relpath(absa, absb)
relpsl = relp.split("/")
if(relpsl[-1] == ".."):
# Can get to this directory :(
return False
return True
def GetExtension(filename: str):
return filename.split(".")[-1].lower().encode()
excludeDirs = [".git"]
excludeFileTypes = [b"gitignore"]
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generates File Identities with an option to quarantine duplicates")
parser.add_argument("--allow-quarantine", action="store_true", help='Enable moving files - Dangerous')
parser.add_argument("-lh", "--long-hash", action="store_true", help='Enable full file Hashes being generated')
parser.add_argument("-r", "--raw", action="store_true", help='Prevent hashing the contents of files; instead hash the container')
parser.add_argument("--silent", action="store_true", help='Silence output')
parser.add_argument('-t', '--hashtable', nargs=1, type=str, help='Location of hashtable')
parser.add_argument("path", metavar="path", type=str)
args = parser.parse_args()
if not os.path.exists(args.path):
raise IOError("Directory \"{}\" does not exist".format(
args.path
))
#if args.hashtable and not os.path.exists(args.hashtable[0]):
# raise IOError("Directory \"{}\" does not exist".format(
# args.hashtable
#))
if not IsDriveSafe(args.path, "./") and args.allow_quarantine:
raise Exception("Path is a parent of the directory this script is in!")
pathAsBytes = args.path.encode()
encodedHashtable = args.hashtable[0].encode() if args.hashtable else None
hashlist = HashList.CHashList(encodedHashtable)
for r, d, p in os.walk(args.path):
d[:] = [x for x in d if x not in excludeDirs]
p[:] = [x for x in p if GetExtension(x) not in excludeFileTypes]
if ".skipfolder" in p:
d[:] = []#[x for x in d]
print("[IGNORE] Skipping Below {}".format(r))
continue
for fi in p:
# Let's catagorise these
f = fi.split(".")
path = os.path.join(r, fi)
relp = os.path.relpath(path, os.path.abspath(args.path)).encode()
ext = f[len(f) - 1].lower().encode()
try:
if not hashlist.IsElementKnown(args.path.encode(), relp, ext, allowLongHashes=args.long_hash, silent=args.silent, useRawHashes=args.raw):
hashlist.AddElement(args.path.encode(), relp, ext, silent=args.silent, useLongHash=args.long_hash, useRawHashes=args.raw, disableCheckpoint=True)
if not args.silent:
print("[CLEAR] File: {}".format(relp))#.decode()))
pass
else:
#print("Wanting to move {}".format(relp))
if args.allow_quarantine:
MoveFileToQuarantine(args.path.encode(), (relp, ext), args)
except KeyboardInterrupt as kbi:
raise kbi
except Exception as e:
print("Error on file {}. Reason: {}".format(relp, e), file=sys.stderr)
#raise e
continue