-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashes_tools.py
More file actions
41 lines (35 loc) · 1.39 KB
/
hashes_tools.py
File metadata and controls
41 lines (35 loc) · 1.39 KB
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
import os
import unicodedata
def load_hashes(hashes_file_path):
hashes = {}
try:
with open(hashes_file_path, 'r', encoding="utf-8", errors="replace") as f:
for line in f:
parts = line.strip().rsplit(' ', 1)
file_path, file_hash = parts
file_name = os.path.basename(file_path)
hashes[file_hash] = {
'file_path': file_path,
'file_name': file_name
}
except FileNotFoundError:
print(f"Errore: File {hashes_file_path} non trovato.")
return hashes
def compare_hashes(original_hashes, hashes):
for hash in list(original_hashes.keys()):
try:
file = hashes[hash]
# Normalize names in order to avoid encoding mismatch
original_file_name = unicodedata.normalize('NFC', original_hashes[hash]['file_name'])
file_name = unicodedata.normalize('NFC', file['file_name'])
if original_file_name == file_name:
original_hashes.pop(hash)
hashes.pop(hash)
except KeyError:
continue
return original_hashes, hashes
def save_hashes(hashes, output_file_path):
""" Save the hashes to a text file."""
with open(output_file_path, 'w') as f:
for file_hash, data in hashes.items():
f.write(f"{data['file_path']} {file_hash}\n")