bonelag/voice / hash.py
bonelag's picture
download
raw
2.8 kB
import hashlib
from collections import defaultdict
from pathlib import Path
def calculate_hash(file_path: Path, chunk_size: int = 1024 * 1024) -> str:
hasher = hashlib.sha256()
with open(file_path, "rb") as f:
while chunk := f.read(chunk_size):
hasher.update(chunk)
return hasher.hexdigest()
def inspect_all_files_hash(
root_path: str = ".", extensions: set = {".onnx", ".json"}
):
root = Path(root_path).resolve()
print(f"🔍 Bắt đầu quét các file {extensions} từ: {root}")
# Lấy toàn bộ file có đuôi nằm trong danh sách
target_files = sorted(
[
f
for f in root.rglob("*")
if f.is_file() and f.suffix.lower() in extensions
]
)
if not target_files:
print(f"🤷‍♂️ Không tìm thấy file nào có đuôi {extensions}!")
return
print(f"📦 Tìm thấy {len(target_files)} file. Đang đọc hash...")
hash_map = defaultdict(list)
for file in target_files:
try:
f_hash = calculate_hash(file)
hash_map[f_hash].append(file)
except Exception as e:
print(f"⚠️ Lỗi đọc file {file.name}: {e}")
duplicates = {h: paths for h, paths in hash_map.items() if len(paths) > 1}
uniques = {h: paths[0] for h, paths in hash_map.items() if len(paths) == 1}
# 1. Nhóm trùng nhau
if duplicates:
print("\n" + "=" * 75)
print(f"🚨 PHÁT HIỆN {len(duplicates)} NHÓM FILE TRÙNG NHAU:")
print("=" * 75)
for idx, (h, paths) in enumerate(duplicates.items(), 1):
print(f"\n[Trùng {idx}] SHA-256: {h}")
for p in paths:
size_kb = p.stat().st_size / 1024
# Nếu file > 1MB thì hiện MB, nhỏ hơn thì hiện KB cho dễ nhìn
size_str = (
f"{size_kb / 1024:.2f} MB"
if size_kb >= 1024
else f"{size_kb:.2f} KB"
)
print(f" 👉 ./{p.relative_to(root)} ({size_str})")
# 2. Toàn bộ file độc nhất
print("\n" + "=" * 75)
print(f"✨ DANH SÁCH {len(uniques)} FILE ĐỘC NHẤT (KHÔNG TRÙNG):")
print("=" * 75)
for idx, (h, p) in enumerate(uniques.items(), 1):
size_kb = p.stat().st_size / 1024
size_str = (
f"{size_kb / 1024:.2f} MB"
if size_kb >= 1024
else f"{size_kb:.2f} KB"
)
print(f"{idx:02d}. [{h}]")
print(f" 📁 ./{p.relative_to(root)} ({size_str})")
# Quét cả .onnx và .json tại thư mục hiện tại
inspect_all_files_hash(".", extensions={".onnx", ".json"})

Xet Storage Details

Size:
2.8 kB
·
Xet hash:
ed2415163640e8b0dcb6846e2a09532a5efb5e172ce5ae63b2882d57aff5e186

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.