58 lines
2.0 KiB
Python
58 lines
2.0 KiB
Python
import os
|
|
import xxhash
|
|
|
|
class FileHash:
|
|
def __init__(self,
|
|
path_file,
|
|
chunk_size=1024 * 1024,
|
|
*args,
|
|
**kwargs):
|
|
super(FileHash,self).__init__(*args,**kwargs)
|
|
self.path_file = path_file
|
|
self._path = os.path.dirname(self.path_file)
|
|
self.chunk_size = chunk_size
|
|
self.path_hasher = xxhash.xxh64(self.path_file)
|
|
# noinspection PyArgumentList
|
|
# self.file_hash = self.get_hash(self.path_file)
|
|
self.path_hash = self.hash_path()
|
|
|
|
def get_hash(self,f,
|
|
progress_callback,
|
|
current_file_progress_callback,
|
|
imported_file_count_callback,
|
|
found_file_callback,
|
|
total_file_count_callback,
|
|
checksum_file_callback,
|
|
checksum_progress_callback,
|
|
checksum_dialog_open_callback,
|
|
compare_checksums_source_file_callback,
|
|
compare_checksums_dest_file_callback,
|
|
compare_checksums_source_hash_callback,
|
|
compare_checksums_dest_hash_callback,
|
|
compare_checksums_add_row_callback):
|
|
|
|
checksum_dialog_open_callback.emit(True)
|
|
|
|
size = os.path.getsize(f)
|
|
hasher = xxhash.xxh64()
|
|
|
|
chunk_count = 0
|
|
|
|
checksum_file_callback.emit(f)
|
|
|
|
with open(f, 'rb') as f:
|
|
for chunk in iter(lambda: f.read(self.chunk_size),b""):
|
|
hasher.update(chunk)
|
|
chunk_count += 1
|
|
hashed_size = chunk_count * self.chunk_size
|
|
checksum_progress_callback.emit(round((hashed_size / size) * 100, 1))
|
|
|
|
file_hash = hasher.hexdigest()
|
|
checksum_dialog_open_callback.emit(False)
|
|
|
|
return file_hash
|
|
|
|
def hash_path(self):
|
|
""" hashes a string passed as a path """
|
|
|
|
return self.path_hasher.hexdigest() |