forge
Explore
Log in

Don't you dare go Hollow

hezarfen_rag

Clone
★ 0

0 watching0 forksPython2.4 MiB184b84dAhmet Kaan Gümüş2 months ago189 commits

verify_backup.py36 lines, 1.1 KiBHistoryRawDownload
1"""Verify that every backed-up data/model file matches its committed LFS hash.
2Run after `git lfs pull`: python verify_backup.py
3"""
4import hashlib
5import pathlib
6import subprocess
7import sys
8
9root = pathlib.Path(__file__).resolve().parent
10result = subprocess.run(
11 ['git', 'lfs', 'ls-files', '--long', '--null'], cwd=root,
12 check=True, stdout=subprocess.PIPE,
13)
14failed = []
15count = 0
16for entry in result.stdout.decode('utf-8').split('\0'):
17 if not entry:
18 continue
19 digest, marker, name = entry.split(' ', 2)
20 path = root / name
21 if not path.is_file():
22 failed.append(name + ': missing')
23 continue
24 actual = hashlib.sha256()
25 with path.open('rb') as stream:
26 for chunk in iter(lambda: stream.read(1024 * 1024), b''):
27 actual.update(chunk)
28 if actual.hexdigest() != digest:
29 failed.append(name + ': hash mismatch (run git lfs pull)')
30 count += 1
31if count == 0:
32 failed.append('No LFS files found')
33print(f'Checked {count} files; {len(failed)} errors.')
34for error in failed:
35 print(error)
36sys.exit(bool(failed))