Download RESTORE.py from jiajun0613/act-datasets: direct link, hf CLI and curl.
- Browser
- Download file 4.06 kB
-
https://huggingface.co/datasets/jiajun0613/act-datasets/resolve/main/RESTORE.py
- Command line
-
hf download hf://datasets/jiajun0613/act-datasets/RESTORE.py
-
curl -L -o RESTORE.py https://huggingface.co/datasets/jiajun0613/act-datasets/resolve/main/RESTORE.py
4.06 kB
| """Restore selected downloaded ACT data; preserve bytes and source timestamps. | |
| Example: python RESTORE.py --snapshot . --output /home/qianpusun/data --prefix vit/v3 | |
| Run once for each desired prefix, or omit --prefix for all non-LIBERO artifacts. | |
| Original LIBERO HDF5 files already have their final layout and are downloaded separately. | |
| """ | |
| from pathlib import Path | |
| import argparse | |
| import gzip | |
| import hashlib | |
| import json | |
| import os | |
| import shutil | |
| import tarfile | |
| def digest(path): | |
| h=hashlib.sha256() | |
| with path.open('rb') as f: | |
| for b in iter(lambda:f.read(8*1024**2),b''): | |
| h.update(b) | |
| return h.hexdigest() | |
| def target(root, rel): | |
| p=Path(rel) | |
| if p.is_absolute() or '..' in p.parts: | |
| raise ValueError(f'Invalid relative path: {rel}') | |
| out=root/p | |
| if not out.resolve().is_relative_to(root.resolve()): | |
| raise ValueError(f'Path leaves output directory: {rel}') | |
| return out | |
| def restore(snapshot, output, prefix=''): | |
| manifest=json.loads((snapshot/'DATA_MANIFEST.json').read_text()) | |
| selected=[i for i in manifest['artifacts'] if i['source_files'] and | |
| (not prefix or i['path']==prefix or i['path'].startswith(prefix.rstrip('/')+'/'))] | |
| if not selected: | |
| raise ValueError(f'No source artifacts match prefix: {prefix}') | |
| for i,item in enumerate(selected,1): | |
| src=target(snapshot,item['path']) | |
| if not src.is_file() or src.stat().st_size!=item['bytes'] or digest(src)!=item['sha256']: | |
| raise ValueError(f'Missing or corrupt downloaded artifact: {src}') | |
| if not item.get('archive'): | |
| dest=target(output,item['path']);dest.parent.mkdir(parents=True,exist_ok=True) | |
| if src.resolve()!=dest.resolve(): | |
| tmp=dest.with_name(dest.name+'.restore-tmp');shutil.copyfile(src,tmp);tmp.replace(dest) | |
| os.utime(dest,ns=(item['mtime_ns'],item['mtime_ns'])) | |
| else: | |
| with gzip.open(target(snapshot,item['inventory']),'rt') as f: | |
| expected={j['path']:j for j in map(json.loads,f)} | |
| seen=set() | |
| with tarfile.open(src,'r|*') as tar: | |
| for member in tar: | |
| if member.isdir(): | |
| continue | |
| if not member.isfile(): | |
| raise ValueError(f'Unsupported member: {member.name}') | |
| rel=member.name | |
| for old,new in item.get('archive_prefix_mapping',{}).items(): | |
| if rel.startswith(old): | |
| rel=new+rel[len(old):];break | |
| if rel not in expected or rel in seen: | |
| raise ValueError(f'Unexpected archive member: {rel}') | |
| seen.add(rel);record=expected[rel] | |
| if member.size!=record['bytes']: | |
| raise ValueError(f'Size mismatch: {rel}') | |
| dest=target(output,rel);dest.parent.mkdir(parents=True,exist_ok=True) | |
| tmp=dest.with_name(dest.name+'.restore-tmp');h=hashlib.sha256() | |
| with tar.extractfile(member) as incoming,tmp.open('wb') as outgoing: | |
| for block in iter(lambda:incoming.read(4*1024**2),b''): | |
| h.update(block);outgoing.write(block) | |
| if h.hexdigest()!=record['sha256']: | |
| tmp.unlink();raise ValueError(f'Hash mismatch: {rel}') | |
| tmp.replace(dest);os.utime(dest,ns=(record['mtime_ns'],record['mtime_ns'])) | |
| if seen!=set(expected): | |
| raise ValueError(f'Incomplete archive: {src}') | |
| print(f'{i}/{len(selected)} restored {item["path"]}',flush=True) | |
| if __name__=='__main__': | |
| parser=argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument('--snapshot',type=Path,default=Path(__file__).resolve().parent) | |
| parser.add_argument('--output',type=Path,required=True) | |
| parser.add_argument('--prefix',default='') | |
| args=parser.parse_args();restore(args.snapshot,args.output,args.prefix) | |