-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collector.py
227 lines (176 loc) · 6.78 KB
/
Collector.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
import os
import constant
import common
from HashDB import *
from PyQt5.QtCore import QThread
from enum import Enum
from DuplicateDB import *
class CollectorCmd(Enum):
load = 0
scan = 1
verify = 2
unload = 3
class Collector(QThread):
def __init__(self, ui):
QThread.__init__(self)
self.map = {}
self.duplicate_db = DuplicateDB(ui)
self.ui = ui
def get_db(self, path):
db = self.map.get(path)
if None == db:
db = HashDB(path, self.ui)
self.map[path] = db
return db
def clear(self):
self.map = {}
self.ui.hash_cnt(0)
def add_dir(self, path, recursive, cmd, skipExisting):
self.argPath = path
self.argRecursive = recursive
self.argCmd = cmd
self.argSkipExisting = skipExisting
self.worker = self.add_dir_worker
self.exec()
def find_hash(self, hash):
for path, db in self.map.items():
name = db.find_hash(hash)
if None != name:
name = os.path.join(db.path, name)
return name
return None
def find_hash_all(self, hash):
found = []
for path, db in self.map.items():
name = db.find_hash(hash)
if None != name:
name = os.path.join(db.path, name)
found.append(name)
return found
def __del__(self):
self.wait()
def run(self):
self.worker()
def is_skip_dir(self, path):
return os.path.isfile(os.path.join(path, constant.NOHASHFILE))
def add_dir_worker(self):
self.ui.reset()
self.add_dir_impl(self.argPath, self.argRecursive, self.argSkipExisting, self.argCmd)
self.ui.hash_cnt(len(self.map))
self.ui.stats()
def add_dir_impl2(self, path, recursive, skipExisting, cmd):
dir = os.path.normpath(path)
self.ui.status(dir)
if self.is_skip_dir(dir):
self.ui.info("Skipping dir: %s" % dir)
self.ui.inc_dir_skipped()
return
db = self.get_db(dir)
if db.load():
self.ui.inc_hash_db_loaded()
pass
if cmd is CollectorCmd.scan:
db.scan(self.duplicate_db, skipExisting)
self.ui.inc_dir_scanned()
db.save()
elif cmd is CollectorCmd.verify:
db.verify()
if recursive:
dirList = []
dirList.extend(common.get_dir_list_absolute(path, False))
for dir in dirList:
self.add_dir_impl2(dir, recursive, skipExisting, cmd)
if self.ui.is_abort():
return
self.ui.hash_cnt(len(self.map))
def remove_dir_from_hash_db(self, path, recursive):
to_remove = []
if recursive:
for db_path, db in self.map.items():
if db_path.startswith(path):
to_remove.append(db_path)
else:
for db_path, db in self.map.items():
if db_path == path:
to_remove.append(db_path)
break
for p in to_remove:
self.ui.info("Remove hashDB: %s" % p)
self.map.pop(p, None)
def add_dir_impl(self, path, recursive, skipExisting, cmd):
if cmd is CollectorCmd.unload:
self.ui.info("Unloading HashDB %sfrom: %s" % ('recursively ' if recursive else '', path))
self.remove_dir_from_hash_db(path, recursive)
self.ui.debug("Finished unloading %d HashDB." % (len(self.map)))
else:
self.duplicate_db.reset()
self.ui.info("Loading HashDB %sfrom: %s" % ('recursively ' if recursive else '', path))
self.add_dir_impl2(path, recursive, skipExisting, cmd)
self.ui.debug("Finished loading %d HashDB." % (len(self.map)))
self.duplicate_db.show_duplicates(None, False)
def remove_hash(self, path, hash):
db = self.map.get(path)
if None == db:
self.ui.error("Collector.remove_hash: Error db not found: %s" % path)
else:
db.remove(hash)
def remove_file(self, filepath):
path = os.path.dirname(filepath)
filename = os.path.basename(filepath)
db = self.map.get(path)
if db:
db.remove_filename(filename)
else:
self.ui.debug("remove_file: HashDB not found: %s" % path)
def save_hashes(self, forceSave = False):
self.ui.info("Start saving HashDB")
for path, db in self.map.items():
db.save(forceSave)
self.ui.info("Finished saving HashDB")
pass
def find_duplicates_in_hashDB_impl(self):
self.ui.info("Start finding duplicates in HashDB...")
self.duplicate_db.reset()
for path, db in self.map.items():
for hash, name in db.map.items():
filepath = os.path.normpath(os.path.join(db.path, name))
self.duplicate_db.add_hash(hash, filepath)
self.ui.inc_file_processed()
self.ui.debug("Finished finding duplicates")
def find_and_show_duplicates_in_hashDB_worker(self):
self.find_duplicates_in_hashDB_impl()
self.duplicate_db.show_duplicates(self.arg_path, self.arg_recursive)
self.ui.stats()
def find_and_show_duplicates_in_hashDB(self, path, recursive):
self.arg_path = path
self.arg_recursive = recursive
self.worker = self.find_and_show_duplicates_in_hashDB_worker
self.exec()
def exec(self):
if constant.USE_THREADS:
self.start()
else:
self.worker()
def move_duplicates_with_master_dir(self, master_path, dest_dir, move_flat, with_subfolders, simulate):
self.arg_master_path = master_path
self.arg_dest_dir = dest_dir
self.arg_move_flat = move_flat
self.arg_simulate = simulate
self.arg_with_subfolders = with_subfolders
self.worker = self.move_duplicates_with_master_dir_worker
self.exec()
def move_duplicates_with_master_dir_worker(self):
filenames = self.duplicate_db.get_list_with_files_to_move_keep_master_path(self.arg_master_path, self.arg_with_subfolders)
self.move_files(filenames, self.arg_move_flat, self.arg_dest_dir, self.arg_simulate)
def move_files(self, filenames, move_flat, dest_dir, is_simulation):
self.ui.reset()
for filename in filenames:
if self.ui.is_abort():
break
path = common.create_duplicate_dest_path(filename, dest_dir, move_flat)
common.move_file(filename, path, False, is_simulation, self.ui)
if not is_simulation:
self.remove_file(filename)
if not is_simulation:
self.save_hashes()
self.ui.stats()