-
Notifications
You must be signed in to change notification settings - Fork 0
/
d.py
executable file
·363 lines (289 loc) · 10.9 KB
/
d.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/python3
#!/usr/bin/env python3
import os
import hashlib
from optparse import OptionParser
from utils import *
# Flags
#---------------------------------------------
test = False
#test = True
execute = True
#debug = True # Dev Test
debug = False # in Release !
# Ratings chars sorted
# used: ` _ ^ - , ) ( !
# all : ` _ ^ ] [ @ = ; . - , + ) ( ' & % # !
# bad sort: } { ~ $ .
# not on windows: < > : " / \ | ? *
# prefixes for filename, with more duplicates
ratings = ['`','_','^','-',',','+',')','(','!','!!'] # 10
tab = ' '
# Options
#------------------------------------------------------------------------------------------
op = OptionParser(description='Find and delete duplicated files. '+
'Rename with added rating from duplicates count. Detect extesions.') #, usage='',epilog='')
def opt_bool(sh,long, dest,help, default):
if default != 0:
op.add_option(sh,long, dest=dest, help=help, action='store_false', default=True)
else:
op.add_option(sh,long, dest=dest, help=help, action='store_true', default=False)
opt_bool('-t', '--test', 'test', 'Test only and show stats', 0)
opt_bool('-o', '--opt', 'only', 'Only show options and quit', 0)
opt_bool('-l', '--list', 'list', 'List files, not just only stats', 0)
opt_bool('-x', '--exec', 'noexecute', 'Excecute (delete or rename)', 0)
opt_bool('-e', '--ext', 'autoext', 'Detect and set files extesions (few)', 1)
opt_bool('-n', '--sub', 'recursive', 'Check subdirs', 1)
opt_bool('-a', '--across', 'across', 'Test duplicates across dirs', 1) # 1
op.add_option('-d', '--dir', dest='dir', help='Path. If not set, uses current', default='')
op.add_option('-s', '--size', dest='h_size', type='int', default = -4096,
help='Size to read, 1 full (slow), 0 none, default -4096, - from end, + from beginning')
opt_bool('-p', '--prefix', 'prefix',
'Add prefix rating symbol, from duplicate count ` _ ^ - , ) ( ! !!', 1)
opt_bool('-u', '--suffix', 'suffix', 'Add suffix ratings _1 to _9', 0)
op.add_option('-i', '--offset', dest='offset',
help='Offset value to add to rating count', type='int', default = 0)
(opt, args) = op.parse_args()
h_size = abs(opt.h_size)
if opt.noexecute:
execute = False
if opt.test:
test = True
if debug:
opt.list = True
#test = True # force -t for testing only
execute = False # don't execute
print('Options: test '+yn(test)+' execute '+yn(execute)+' list '+yn(opt.list))
print(' subdirs '+yn(opt.recursive)+' across '+yn(opt.across)+' size '+str(opt.h_size))
print(' ext '+yn(opt.autoext)+' prefix '+yn(opt.prefix)+' suffix '+yn(opt.suffix))
# Start dir
#---------------------------------------------
if opt.dir == '':
start_dir = os.getcwd()
if debug: # test only
#start_dir += '/test-dirs/1simple'
#start_dir += '/test-dirs/2same-name'
#start_dir += '/test-dirs/3two-dir'
start_dir += '/test-dirs/4clean-name'
else:
start_dir = opt.dir
if opt.only:
print('Path: ' + start_dir)
exit(0)
if opt.list:
print('---------------------------------------------')
# Var
#---------------------------------------------
file_list = list() # all files
file_count = dict() # same file properties count for unique
# stats
class cStats:
all_dirs = 0
all_files = 0
all_size = 0
left_files = 0 # left after deleting duplicates
left_size = 0
wrong_ext = 0 # auto changed ext
class cFile:
def __init__(self, fpath, dir, fneu, fne, ext, size, hash):
self.fpath = fpath # full path with file
self.dir = dir # just dir path
self.fneu = fneu # filename, no ext, no ratings, for unique test
self.fne = fne # filename, no ext
self.ext = ext # file extension
self.size = size # file size B
self.hash = hash # hash from file contents, 0 if not used
self.unique = True
def unique_attrs(self, across):
# file properties needed to be different for file being unique
#return (self.ext, self.size, self.hash)) # test .. different names but same content
return ((self.fneu, self.ext, self.size, self.hash) if across
else (self.dir, self.fneu, self.ext, self.size, self.hash))
stats = cStats()
# Hash
#---------------------------------------------
def get_hash(file):
hasher = hashlib.md5()
#print(file+' '+str(opt.h_size))
with open(file, "rb") as f:
if opt.h_size < -1: # from end
try: # fails: h_size > f.sizeof
f.seek(opt.h_size, os.SEEK_END)
except:
pass
#print('xx')
buf = f.read()
else:
if h_size == 1: # read full
buf = f.read()
else: # from begin
buf = f.read(h_size)
#print(buf)
hasher.update(buf)
return hasher.hexdigest()
# Process 1 file
#------------------------------------------------------------------------------------------
def process_file(dir, fname):
#print(fpath)
fpath = os.path.join(dir, fname)
if not os.path.isfile(fpath):
return
fspl = os.path.splitext(fname)
fne = fspl[0] # fname no ext
ext = fspl[1]
size = os.path.getsize(fpath)
stats.all_files += 1
stats.all_size += size
#print(fname + ' ' + str(size) + ' ' + fne + ' ' + ext)
# find (n) in name --
# this will ignore any suffix chars after (n) e.g. (n)ab^`
l = fne.rfind('(')
r = fne.rfind(')')
fneu = fne
if l+1 < r and l > 0 and r > 0:
num = fne[l+1 : r] # num (n)
fnb = fne[ : l] # fname no ()
fneu = fnb
#print('l '+str(l)+' r '+str(r))
# ignore rating prefixes, if already --
if opt.prefix:
for r in ratings:
while fneu.startswith(r):
fneu = fneu[len(r) : ]
# get hash
hash = get_hash(fpath) if opt.h_size != 0 else 0
# cfile
cf = cFile(fpath, dir, fneu, fne, ext, size, hash)
unique_at = cf.unique_attrs(opt.across)
in_count = file_count.get(unique_at, 0)
file_count[unique_at] = in_count + 1 # inc count
unique = True if in_count == 0 else False
if unique:
stats.left_files += 1
stats.left_size += size
# file info
if opt.list:
u = ' # ' if unique else ' - '
# align size right, unique +, fname
print('{:>12}'.format(str_size(size)) +
' ' + u + ' ' + fname)# + ' ' + hash)
# add
cf.unique = unique
file_list.append(cf)
return
# Process 1 file ext
#------------------------------------------------------------------------------------------
def process_file_ext(dir, fname):
fpath = os.path.join(dir, fname)
if not os.path.isfile(fpath):
return
size = os.path.getsize(fpath)
if size < 16:
return
fspl = os.path.splitext(fname)
fne = fspl[0] # fname no ext
ext = fspl[1]
ex = get_file_ext(fpath)
if ex == '?':
return # unknown
if ext == '' or ex != ext[1:]:
stats.wrong_ext += 1
if execute:
new_fpath = os.path.join(dir, fne+'.'+ex)
try:
os.rename(fpath, new_fpath)
except Exception as exc:
print(tab + 'rename ext failed: ' + fpath + ' ' + str(exc))
# Main loop for ext
#------------------------------------------------------------------------------------------
if opt.autoext:
if not opt.recursive:
files = os.listdir(start_dir)
for fname in files:
process_file_ext(start_dir, fname)
else:
for dir, subdirs, files in os.walk(start_dir):
if dir.find('/.') != -1: # skip hidden
continue
for fname in files:
process_file_ext(dir, fname)
# Main get loop
#------------------------------------------------------------------------------------------
if not opt.recursive:
if opt.list:
print(start_dir)
files = os.listdir(start_dir)
files.sort()
for fname in files:
process_file(start_dir, fname)
else:
for dir, subdirs, files in os.walk(start_dir):
if dir.find('/.') != -1: # skip hidden
continue
if opt.list:
print(dir)
stats.all_dirs += 1
# each dir separate
# don't clear for across dirs
if not opt.across:
#file_list.clear
file_count.clear
files.sort()
for fname in files:
process_file(dir, fname)
print('----- Stats -----')
print(' Dirs: ' + str(stats.all_dirs))
if stats.all_files > 0:
print('Files: {:.2f}'.format(100.0 * stats.left_files / stats.all_files) + '% ' + str(stats.left_files) + ' / ' + str(stats.all_files))
if stats.all_size > 0:
print(' Size: {:.2f}'.format(100.0 * stats.left_size / stats.all_size) + '% ' + str_size(stats.left_size) + ' / ' + str_size(stats.all_size) + ' B')
print(' Free: ' + str_size(stats.all_size - stats.left_size) + ' B')
if stats.wrong_ext > 0:
print(' Ext: ' + str(stats.wrong_ext))
if test:
exit(0)
# Delete, Rename files
print('----- Executing')
dir = ''
for f in file_list:
if dir != f.dir:
dir = f.dir
if opt.list:
print(dir)
unique_at = f.unique_attrs(opt.across)
count = file_count.get(unique_at, 0) + opt.offset
if not f.unique: # delete
ch = '-'
name = f.fpath[ len(dir)+1 : ]
if execute:
try:
os.remove(f.fpath)
except Exception as ex:
print(tab + 'delete failed: ' + f.fne + ' ' + str(ex))
else:
if count < 2:
continue
# rename with rating prefix
pref = ''
sufx = ''
if opt.prefix:
pref = ratings[min( max(0, count-2), len(ratings)-1 )]
if opt.suffix:
sufx = '_' + str(count)
ch = '#' # dont rename to existing file(s)
add = ''
while True:
new_file = pref + f.fne + sufx + add + f.ext
new_fpath = os.path.join(f.dir, new_file)
name = new_file
if not os.path.exists(new_fpath):
break
print(tab + 'exists: ' + new_file)
add = 'a' if add == '' else chr(ord(add[0]) + 1) # a,b,c..
if execute:
try:
os.rename(f.fpath, new_fpath)
except Exception as ex:
print(tab + 'rename failed: ' + f.fne + ' ' + str(ex))
if opt.list:
print(tab + ch + ' ' + str(count) + ' ' + name)