Add frozen Main17 features assets (13-13)
Browse files- restore_patch_features.py +48 -0
restore_patch_features.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Reassemble uploaded byte shards into the exact original NPY file."""
|
| 2 |
+
import argparse,hashlib,json,shutil
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
def sha(path):
|
| 6 |
+
h=hashlib.sha256()
|
| 7 |
+
with path.open('rb') as stream:
|
| 8 |
+
while data:=stream.read(8*1024**2): h.update(data)
|
| 9 |
+
return h.hexdigest()
|
| 10 |
+
|
| 11 |
+
def main():
|
| 12 |
+
parser=argparse.ArgumentParser(description=__doc__)
|
| 13 |
+
parser.add_argument('--root',type=Path,default=Path(__file__).resolve().parent)
|
| 14 |
+
args=parser.parse_args();root=args.root.resolve()
|
| 15 |
+
manifest=json.loads((root/'asset_manifest.json').read_text(encoding='utf-8'))
|
| 16 |
+
for item in manifest.get('reassemble',[]):
|
| 17 |
+
target=(root/item['path']).resolve()
|
| 18 |
+
if not target.is_relative_to(root):raise ValueError('Path escapes asset root')
|
| 19 |
+
target.parent.mkdir(parents=True,exist_ok=True)
|
| 20 |
+
if target.exists():
|
| 21 |
+
if target.stat().st_size!=item['bytes'] or sha(target)!=item['sha256']:
|
| 22 |
+
raise ValueError('Existing output differs; refusing overwrite')
|
| 23 |
+
print('Already verified:',item['path']);continue
|
| 24 |
+
if shutil.disk_usage(target.parent).free<item['bytes']:
|
| 25 |
+
raise RuntimeError('Insufficient space for the restored NPY; shards are retained')
|
| 26 |
+
temp=target.with_name(target.name+'.restoring')
|
| 27 |
+
digest=hashlib.sha256();written=0
|
| 28 |
+
try:
|
| 29 |
+
with temp.open('xb') as output:
|
| 30 |
+
for part in item['parts']:
|
| 31 |
+
if part['byte_offset']!=written:raise ValueError('Non-contiguous shards')
|
| 32 |
+
source=(root/part['path']).resolve()
|
| 33 |
+
if not source.is_relative_to(root) or source.stat().st_size!=part['bytes']:
|
| 34 |
+
raise ValueError('Missing or invalid shard')
|
| 35 |
+
part_hash=hashlib.sha256()
|
| 36 |
+
with source.open('rb') as stream:
|
| 37 |
+
while data:=stream.read(8*1024**2):
|
| 38 |
+
output.write(data);part_hash.update(data);digest.update(data);written+=len(data)
|
| 39 |
+
if part_hash.hexdigest()!=part['sha256']:raise ValueError('Shard checksum mismatch')
|
| 40 |
+
if written!=item['bytes'] or digest.hexdigest()!=item['sha256']:
|
| 41 |
+
raise ValueError('Restored NPY checksum mismatch')
|
| 42 |
+
temp.rename(target)
|
| 43 |
+
except Exception:
|
| 44 |
+
# Retain partial output for inspection; never remove existing user files.
|
| 45 |
+
raise
|
| 46 |
+
print('Restored and verified:',item['path'])
|
| 47 |
+
|
| 48 |
+
if __name__=='__main__':main()
|