-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduplicatesmanager.py
541 lines (398 loc) · 19.7 KB
/
duplicatesmanager.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#####################################################################################################
##
## duplicatesmanager.py - a script for comicrack
##
## Author: perezmu, pescuma
##
## Copyleft perezmu 2011.
##
## Detailed credits: "http://code.google.com/p/comicrack-duplicates-manager/wiki/FellowCredits"
##
######################################################################################################
#########
#
# Import section
import sys, traceback
import re
import clr
import System
import System.IO
from System.IO import Path, Directory, File, FileInfo
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import DialogResult, MessageBox, MessageBoxButtons, MessageBoxIcon
from itertools import groupby
from dmBookWrapper import *
from utilsbycory import cleanupseries
from processfunctions import *
from dmParser import *
from utilsbycory import *
from constants import *
#
#
##########
##'''---------------------------------------------------------'''
############
#
# MAIN FUNCTION
#@Name DuplicatesManager
#@Hook Books
#@Image duplicatesmanager.png
def DuplicatesManager(books):
########################################
#
# Starting log file
#
if not Directory.Exists(DUPESDIRECTORY):
try:
Directory.CreateDirectory(DUPESDIRECTORY)
except Exception, ex:
MessageBox.Show('ERROR: '+ str(ex), "ERROR creating dump directory" + DUPESDIRECTORY, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
return
logfile = open(LOGFILE,'w')
logfile.write('COMICRACK DUPLICATES MANAGER V '+VERSION+'\n\n')
''' Logfile initialized '''
#
#
#########################################
try:
ProcessDuplicates(books, logfile)
except Exception, ex:
logfile.write('\n\nSTOPPED PROCESSING BECAUSE OF EXCEPTION:\n')
traceback.print_exc(None, logfile, False)
raise ex
finally:
logfile.close()
def ProcessDuplicates(books, logfile):
#########################################
#
# Getting comics info
comiclist = []
for book in books:
b = dmBookWrapper(book)
# re.sub(r'^0+','',b.Number) -> removes leading 0's
series = b.Series
if b.Volume:
series += ' Vol.' + b.Volume
series += ' ' + b.Format
comiclist.append((cleanupseries(series),re.sub(r'^0+','',b.Number),b.Volume,b.FileName,b.PageCount,b.FileSize/1048576.0,b.ID,b.CVDB_ID,b.FilePath,book.Tags,book.Notes,b.FileFormat,b.ScanInformation,book))
logfile.write('Parsing '+str(len(comiclist))+ ' ecomics\n')
#
#
########################################
#########################################
#
# Setting intial options values
options = {"movefiles":MOVEFILES,
"removefromlib":REMOVEFROMLIB,
"updateinfo":UPDATEINFO,
"verbose":VERBOSE,
"debug":DEBUG,
"sizemargin":SIZEMARGIN,
"coverpages":COVERPAGES,
"c2c_noads_gap":C2C_NOADS_GAP}
########################################
#
# Main Loop
#
try:
###########################################
#
# Load rules file
rules = LoadRules(logfile, options)
#
############################################
############################################
#
# Massage comics to get a list of dupes groups
''' Now we group books looking for dupes! '''
comiclist.sort()
''' begin sorting and sort the list '''
# TODO: I need to cleanup the series names and issues 1/2, 0.5, etc...
# TODO: Also, check for CVDB items first!
cl = {}
''' temp dictionary'''
for key, group in groupby(comiclist, lambda x: x[SERIES]):
cl[key] = list(group)
'''groups by series'''
''' cl is a dictionary that now has 'series' as keys'''
''' we remove series with only one ecomic '''
logfile.write('============= Begining dupes identification ==================================\n\n')
logfile.write('Parsing '+str(len(comiclist))+ ' ecomics\n')
logfile.write('Found '+str(len(cl))+ ' different series\n')
if options["verbose"]:
for series in sorted(cl.keys()):
logfile.write('\t'+series+'\n')
remove = []
for series in cl.keys():
if len(cl[series])==1:
remove.append(series)
for series in remove:
del cl[series]
logfile.write('Found '+str(len(cl))+ ' different series with more than one issue\n')
if options["verbose"]:
for series in sorted(cl.keys()):
logfile.write('\t'+series+'\n')
''' we now regroup each series looking for dupe issues '''
# We need to use a different list or sometimes python duplicates entries
temp_cl = {}
for series in cl.keys():
cl[series].sort()
temp_dict = {}
for key, group in groupby(cl[series], lambda x: x[NUMBER]):
temp_dict[key] = list(group)
temp_cl[series] = temp_dict
cl = temp_cl
''' cleaning issues without dupes '''
remove = []
for series in cl.keys():
for number in cl[series]:
if len(cl[series][number])==1:
remove.append([series,number])
for a in remove:
del cl[a[0]][a[1]]
''' now a second go for series without issues after non-dupe removal '''
remove = []
for i in cl:
if len(cl[i])==0:
remove.append(i)
for i in remove:
del cl[i]
logfile.write('Found '+str(len(cl))+ ' different series with dupes\n')
if options["verbose"]:
for series in sorted(cl.keys()):
logfile.write('\t'+series+'\t('+str(cl[series].keys())+')\n')
''' Now I have them sorted, I convert them to a simple list of lists (groups)...
each item in this list is a list of dupes '''
dupe_groups = []
for i in cl:
for j in cl[i]:
dupe_groups.append(cl[i][j])
logfile.write('Found '+str(len(dupe_groups)) +' groups of dupes, with a total of '+ str(len(reduce(list.__add__, dupe_groups, [])))+ ' ecomics.\n')
if options["verbose"]:
for group in sorted(dupe_groups):
logfile.write('\t'+group[0][SERIES]+' #'+group[0][NUMBER]+'\n')
for comic in group:
logfile.write('\t\t'+comic[FILENAME]+'\n')
dupe_groups.sort()
logfile.write('\n============= End of dupes identification=====================================\n\n\n\n')
logfile.write('============= Beginning dupes processing =====================================\n\n')
del cl
#
##########################################################
#
# Exception handling
#
except NoRulesFileException, ex:
MessageBox.Show('ERROR: '+ str(ex), "ERROR in Rules File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
logfile.write('\n\nERROR in Rules File:\n')
traceback.print_exc(None, logfile, False)
return
###################### processing ########################################
movedcomics = 0
new_groups = []
# fix for issue 4 - if there are no dupes, end gracefully
if len(dupe_groups) == 0:
MessageBox.Show('Script execution completed: No duplicates found in the comics selected', 'Success', MessageBoxButtons.OK, MessageBoxIcon.Information)
logfile.write('\n\n\n\ ########################################################### \n\n\n')
logfile.write('Scritp execution completed: No duplicates found in the comics selected')
del dupe_groups
del new_groups
return
for group in dupe_groups:
t_group = group[:]
logfile.write('\n= PROCESSING GROUP_____\n')
logfile.write('= '+ t_group[0][SERIES] + ' #'+str(t_group[0][NUMBER])+'\n')
i_rules = 0
while (len(t_group)> 1) and (i_rules < len(rules)):
t_rule = rules[i_rules][:]
line = t_rule[0]
t_rule = t_rule[1:]
logfile.write('\n_________________ ')
logfile.write(line)
logfile.write(' _________________\n')
logfile.flush()
if options["debug"]:
logfile.write(' ' + str(t_rule) + '\n')
t_rule.append(t_group[:])
t_rule.append(logfile)
t_rule.insert(1,ComicRack)
t_rule.insert(1,options)
t_group = globals()[t_rule[0]](*t_rule[1:]) ### this is the trick to call a function using a string with its name
i_rules = i_rules+1
new_groups.append(t_group)
dupe_groups = new_groups[:]
remain_comics = len(reduce(list.__add__, new_groups))
for group in dupe_groups:
if len(group) == 1: new_groups.remove(group)
# new_groups holds now the remaining groups for logging purposes
#if len(dupe_groups)>=1:
# print 'Found ',len(dupe_groups), ' groups of dupes, with a total of ', len(reduce(list.__add__, dupe_groups)), ' comics.'
#
# End of Main Loop
#
###########################################################
#### End report
MessageBox.Show('Script execution completed correctly on: '+ str(len(books))+ ' books.\n - '+str(len(dupe_groups))+' duplicated groups processed.\n - '+str(len(new_groups))+' duplicated groups remain.\n - '+str(remain_comics)+' comics remain', 'Success', MessageBoxButtons.OK, MessageBoxIcon.Information)
logfile.write('\n\n\n\ ########################################################### \n\n\n')
logfile.write('Script execution completed correctly on: '+ str(len(books))+ ' books.\n'+str(len(dupe_groups))+' duplicated groups processed.\n'+str(len(new_groups))+' duplicated groups remain..\n'+str(remain_comics)+' comics remain')
#### Garbage collecting
del dupe_groups
del new_groups
return
#### ============================================================================================================================
###############################
#
# Read and parse rules files dmrules.dat
#
def LoadRules(logfile, options):
# Check if file exists
if not File.Exists(RULESFILE):
raise NoRulesFileException('Rules File (dmrules.dat) could not be found in the script directory ('+ SCRIPTDIRECTORY +')')
# Read file
f = open(RULESFILE, 'r')
all_lines = f.readlines()
f.close()
# Parse rules and filter out options
options_list = []
rules = []
for line in Parse(all_lines):
if line[2] == '@':
options_list.append(line)
elif line[2][0] == '@':
options_list.append(line[:2] + ['@', line[2][1:]] + line[3:])
else:
rules.append(line)
logfile.write('\n\n============= Beginning options reading ==================================\n\n')
logfile.write('Successfully read the following options: \n\n')
for option in options_list:
logfile.write('\tLine ' + str(option[0]) + ': ' + str(option[3:]) + '\n')
logfile.write('\n')
logfile.write('\n\n============= Beginning rules reading ==================================\n\n')
logfile.write('Successfully read the following rules: \n\n')
for rule in rules:
logfile.write('\tLine ' + str(rule[0]) + ': ' + str(rule[2:]) + '\n')
logfile.write('\n')
bool_options = ("movefiles", "removefromlib", "updateinfo", "verbose", "debug")
int_options = ("sizemargin", "coverspages", "c2c_noads_gap")
#
# Parse options
#
# Checks if options need to have (and in fact do have) boolean or integer values
bDict = {"false":False, "true":True}
for option in options_list:
opLineNum = option[0]
opLine = option[1]
if len(option) != 5:
raise NoRulesFileException('Line ' + str(opLineNum) + ': Option "' + opLine + '" has wrong format')
opName = option[3].lower()
opVal = option[4].lower()
# boolean option
if opName in bool_options:
opVal = opVal
if opVal in bDict.keys():
options[opName] = bDict[opVal]
else:
raise NoRulesFileException('Line ' + str(opLineNum) + ': Option "'+ opLine +'" value is invalid ("True" or "False" required)')
# integer option
elif opName in int_options:
try:
options[opName] = int(opVal)
except:
raise NoRulesFileException('Line ' + str(opLineNum) + ': Option "'+ opLine +'" value is invalid (integer required)')
# failure
else:
raise NoRulesFileException('Line ' + str(opLineNum) + ': Option "'+ opLine +'" not recognized (' + str(opName) + ')')
logfile.write('\n\n============= Beginning options parsing ==================================\n\n')
logfile.write('Using the following options: \n\n')
for option in options:
logfile.write('\t'+option.upper() + " = " + str(options[option]).upper()+'\n')
#
# Parse rules
#
logfile.write('\n\n============= Beginning rules parsing ==================================\n\n')
parsed_rules = []
for rule in rules:
parsed_rules.append(ParseRule(rule))
if VERBOSE:
logfile.write('\nParsed rules:\n\n')
for rule in parsed_rules:
logfile.write('\t\t'+str(rule)+'\n')
logfile.write('\n============= End of rules parsing ======================================\n\n\n\n')
return parsed_rules
def AsPercentage(args, index, defVal):
if index < len(args):
text = args[index]
else:
text = defVal
if text[-1] == '%':
num = text[:-1]
else:
num = text
try:
return int(num)
except:
raise Exception('Invalid percentage value: ' + text)
known_rules = [
[ ["pagecount", "keep", "fileless"], lambda args: ["keep_pagecount_fileless"] ],
[ ["pagecount", "remove", "fileless"],lambda args: ["remove_pagecount_fileless"] ],
[ ["pagecount", "keep", "largest"], lambda args: ["keep_pagecount_largest", AsPercentage(args, 0, "0%")] ],
[ ["pagecount", "remove", "largest"], lambda args: ["remove_pagecount_largest", AsPercentage(args, 0, "0%")] ],
[ ["pagecount", "keep", "smallest"], lambda args: ["keep_pagecount_smallest", AsPercentage(args, 0, "0%")] ],
[ ["pagecount", "remove", "smallest"],lambda args: ["remove_pagecount_smallest", AsPercentage(args, 0, "0%")] ],
[ ["pagecount", "keep", "noads"], lambda args: ["keep_pagecount_noads"] ],
[ ["pagecount", "keep", "c2c"], lambda args: ["keep_pagecount_c2c"] ],
[ ["filesize", "keep", "largest"], lambda args: ["keep_filesize_largest", AsPercentage(args, 0, "0%")] ],
[ ["filesize", "remove", "largest"], lambda args: ["remove_filesize_largest", AsPercentage(args, 0, "0%")] ],
[ ["filesize", "keep", "smallest"], lambda args: ["keep_filesize_smallest", AsPercentage(args, 0, "0%")] ],
[ ["filesize", "remove", "smallest"], lambda args: ["remove_filesize_smallest", AsPercentage(args, 0, "0%")] ],
[ ["pagesize", "keep", "largest"], lambda args: ["keep_pagesize_largest", AsPercentage(args, 0, "0%")] ],
[ ["pagesize", "remove", "largest"], lambda args: ["remove_pagesize_largest", AsPercentage(args, 0, "0%")] ],
[ ["pagesize", "keep", "smallest"], lambda args: ["keep_pagesize_smallest", AsPercentage(args, 0, "0%")] ],
[ ["pagesize", "remove", "smallest"], lambda args: ["remove_pagesize_smallest", AsPercentage(args, 0, "0%")] ],
[ ["covers", "keep", "some"], lambda args: ["keep_covers_all", False] ],
[ ["covers", "keep", "all"], lambda args: ["keep_covers_all", True] ],
[ ["filename", "keep"], lambda args: ["keep_with_words", args, [FILENAME]] ],
[ ["filename", "remove"], lambda args: ["remove_with_words", args, [FILENAME]] ],
[ ["filepath", "keep"], lambda args: ["keep_with_words", args, [FILEPATH]] ],
[ ["filepath", "remove"], lambda args: ["remove_with_words", args, [FILEPATH]] ],
[ ["tags", "keep"], lambda args: ["keep_with_words", args, [TAGS]] ],
[ ["tags", "remove"], lambda args: ["remove_with_words", args, [TAGS]] ],
[ ["notes", "keep"], lambda args: ["keep_with_words", args, [NOTES]] ],
[ ["notes", "remove"], lambda args: ["remove_with_words", args, [NOTES]] ],
[ ["text", "keep"], lambda args: ["keep_with_words", args, [FILENAME, FILEPATH, TAGS, NOTES, SCAN]] ],
[ ["text", "remove"], lambda args: ["remove_with_words", args, [FILENAME, FILEPATH, TAGS, NOTES, SCAN]] ],
[ ["scan", "keep"], lambda args: ["keep_with_words", args, [SCAN]] ],
[ ["scan", "remove"], lambda args: ["remove_with_words", args, [SCAN]] ],
[ ["filetype", "keep"], lambda args: ["keep_with_words", args, [FILETYPE]] ],
[ ["filetype", "remove"], lambda args: ["remove_with_words", args, [FILETYPE]] ],
[ ["keep", "first"], lambda args: ["keep_first"] ],
]
def ParseRule(rule):
line_num = rule[0]
line = rule[1]
rule_tokens = rule[2:]
# Try to match to a known command
for cmd in known_rules:
tokens = cmd[0]
action = cmd[1]
if len(rule_tokens) < len(tokens):
continue
# Check it the rule matches the command
matches = True
for i in range(len(tokens)):
if tokens[i] != rule_tokens[i]:
matches = False
break;
if not matches:
continue
args = rule_tokens[len(tokens):]
try:
result = [line]
result.extend(action(args))
return result
except Exception, ex:
raise NoRulesFileException('Line ' + str(line_num) + ': ' + str(ex) + '\n' + line)
# If got here not command was matched
raise NoRulesFileException('Line ' + str(line_num) + ': Rule could not be parsed:\n' + line)
class NoRulesFileException(Exception):
pass