-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpydedupersql.py
268 lines (225 loc) · 7.91 KB
/
pydedupersql.py
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# Pydeduper
# http://www.github.com/shafirahmad/pydeduper
import os
import sys
import pandas
import time
import hashlib
import sqlite3
CHUNK_SIZE = 1024
# Specify start folder here - pls do make sure this path exists
startfolder = os.path.normpath( 'C:/testdir/' )
numFiles = 0
numDirs = 0
numCount=0
con = sqlite3.connect("pydeduper.db")
cur = con.cursor()
if 0:
cur.execute(''' DROP TABLE pdfolder ''')
cur.execute(''' CREATE TABLE pdfolder
(id INTEGER PRIMARY KEY, parent text, name text, full text, numfiles integer)
''')
cur.execute(''' DROP TABLE pdfiles ''')
cur.execute(''' CREATE TABLE pdfiles
(id INTEGER PRIMARY KEY, parent integer, name text, size integer, hash text, hashfull text)
''')
con.commit()
if 1:
cur.execute(''' DELETE FROM pdfiles WHERE 1 ''')
cur.execute(''' DELETE FROM pdfolder WHERE 1 ''')
con.commit()
if 0:
cur.execute(''' INSERT INTO pdfiles
(parent, name, hash, size)
VALUES ("parent", "file", "000000", 99)
''')
cur.execute(''' INSERT INTO pdfolder
(parent, name, full, numfiles)
VALUES ("parent", "", "parent", 0)
''')
cur.execute(''' INSERT INTO pdfolder
(parent, name, full, numfiles)
VALUES ("parent2", "", "parent2", 0)
''')
con.commit()
NEWFOLDERSQL= ''' INSERT INTO pdfolder
(parent, name, full, numfiles)
VALUES (?,?,?,?)
'''
UPDATEFOLDERSQL= ''' UPDATE pdfolder
SET numfiles = ?
WHERE full = ?
'''
NEWFILESQL= ''' INSERT INTO pdfiles
(name, hash, size, parent)
VALUES (?,?,?,
(SELECT id from pdfolder WHERE full = ?))
'''
print ("SQL pdfolder")
for row in cur.execute(''' SELECT * from pdfolder '''):
print(row)
print ("SQL sort")
for row in cur.execute(''' SELECT * from pdfiles ORDER by size, hash '''):
print(row)
def getfolderid(folder):
for row in cur.execute("SELECT id from pdfolder WHERE full = ?", (folder,)):
return row[0]
def getfoldername(id):
for row in cur.execute("SELECT full FROM pdfolder where id=?", (id,)):
return row[0]
def getallfolders():
rows = []
for row in cur.execute("SELECT id, full FROM pdfolder ORDER BY id"):
rows.append(row)
return rows
def getfilesfromfolder(folder):
folderid = getfolderid(folder)
rows = []
for row in cur.execute("SELECT id, name, size, hash FROM pdfiles WHERE parent = ? ORDER BY id",(folderid,)):
rows.append(row)
return rows
def getfileswithhash(hash):
#print("<<<",hash)
rows = []
for row in cur.execute("SELECT id, name, size, parent FROM pdfiles WHERE hash = ? ORDER BY id",(hash,)):
rows.append(row)
rows2=[]
for row in rows:
foldername = getfoldername(row[3])
#row = list(row).append(foldername)
row2 = list(row)
row2.append(foldername)
rows2.append(row2)
return rows2
def newfile(parent, name, hash, size):
print("FILE: ",parent, name, hash, size)
cur.execute(NEWFILESQL, (name, hash, size, parent))
con.commit()
def newfolder(parent, name, full, numfiles):
print("FOLDER:", parent, name, full, numfiles)
cur.execute(NEWFOLDERSQL, (parent, name, full, numfiles))
con.commit()
def updatefolder(full, numfiles):
print("UPFOLDER:", full, numfiles)
cur.execute(UPDATEFOLDERSQL, (numfiles, full))
con.commit()
def getfileHash(fileName):
with open(fileName, 'rb') as fh:
#filehash = hashlib.md5()
filehash = hashlib.sha1()
while chunk := fh.read(CHUNK_SIZE):
filehash.update(chunk)
# digest for binary, hexdigest for string
return filehash.hexdigest()
def getListOfFiles(dirName, depth=0):
DirsAndFiles = os.listdir(dirName)
if depth==0:
print("depth 0 sql")
newfolder(dirName, "", dirName, 0)
allFiles = []
numFiles = 0
totSize = 0
for entry in DirsAndFiles:
# get full path
fullPath = os.path.normpath( os.path.join(dirName, entry) )
# Dir or file?
if os.path.isdir(fullPath):
newfolder(dirName, entry, fullPath, 0)
allF, numF, totS = getListOfFiles(fullPath, depth=depth+1)
allFiles = allFiles + allF
updatefolder(dirName, numF)
numFiles += numF
totSize += totS
else:
fileSize = os.path.getsize(fullPath)
#filehash = getfileHash(fullPath)
filehash = ""
newfile(dirName, entry, filehash, fileSize)
#allFiles.append(fullPath+", "+str(depth))
allFiles.append(fullPath+", "+str(fileSize)+", "+filehash)
#allFiles.append(fullPath)
totSize += fileSize
numFiles += 1
#print(numFiles, totSize, dirName)
if depth==0:
print("depth 0 sql")
updatefolder(dirName, numFiles)
return allFiles, numFiles, totSize
thelist, numFiles, totSize =getListOfFiles(startfolder)
print(numFiles, totSize)
#print(thelist)
items = []
for item in thelist:
item = item.split(", ")
items.append(item)
# print(item)
# Sort items by filesize (as str) and hash
items.sort(key=lambda x: (int(x[1]),x[2]))
print("")
#for item in items:
# print(item)
print ("SQL pdfolder")
for row in cur.execute(''' SELECT * FROM pdfolder '''):
print(row)
print ("SQL sort")
for row in cur.execute(''' SELECT * FROM pdfiles ORDER by size, hash '''):
print(row)
dupesizes = []
print ("SQL group by size")
for row in cur.execute(''' SELECT size,count(*) AS c FROM pdfiles GROUP BY size HAVING c > 1 '''):
print(row)
dupesizes.append(row[0])
print(dupesizes)
print("")
print("Start calculating Hashes")
for size in dupesizes:
rows=[]
for row in cur.execute(''' SELECT id, parent, name FROM pdfiles WHERE size=? ''',(size,)): rows.append(row)
for row in rows:
print("SIZE ", size, row)
id,parentid,name = row
parent=getfoldername(parentid)
hash = getfileHash(os.path.join(parent,name))
# path,name = os.path.split(full)
cur.execute(''' UPDATE pdfiles set hash=? WHERE id=? ''',(hash,id))
print("Updating file ",id,parent,name,size,hash)
con.commit()
print ("SQL sort")
for row in cur.execute(''' SELECT * FROM pdfiles ORDER by size, hash '''):
print(row)
print ("SQL size dupes")
for row in cur.execute(''' SELECT * FROM pdfiles WHERE hash!='' ORDER by size, hash '''):
print(row)
print ("SQL hash dupes")
for row in cur.execute(''' SELECT hash, count(*) as c FROM pdfiles WHERE hash!='' GROUP BY hash HAVING c>1 '''):
print(row)
print ("SQL hash dupes 2")
rows=[]
for row in cur.execute(''' SELECT hash, count(*) as c FROM pdfiles WHERE hash!='' GROUP BY hash HAVING c>1 '''): rows.append(row)
for row in rows:
for rrow in cur.execute(''' SELECT * FROM pdfiles WHERE hash=? ''',(row[0],)):
print(rrow)
# NEXT
# - Go folder by folder, get hashes,
# - with hashes, find other files with same hashes
# - so we can find % duplication in folder (no of files iwth dupe hashes/total files in folder)
print("")
print("FOLDER DUPLICATION %")
folders=getallfolders()
for folder in folders:
files = getfilesfromfolder(folder[1])
filecount, hashcount = 0, 0
for row in files:
#print(folder, row)
if row[3] != "":
hashfiles = getfileswithhash(row[3])
first=True
for hashfile in hashfiles:
if row[0] != hashfile[0]:
if first:
#print(" ",folder[1],row)
first=False
hashcount +=1
#print(" ",hashfile)
filecount+=1
if filecount>0: print("% DUPES:", folder[1], 100*hashcount/filecount)