Datasets:
File size: 2,532 Bytes
8271cc3 | 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 42 43 44 45 46 47 48 49 50 51 52 53 54 | """Load Test records and safely extract the exact referenced images; stdlib only."""
from __future__ import annotations
import argparse
import json
from pathlib import Path
import tempfile
import zipfile
from verify_release import file_hash, safe_image_path, verify
def load_test(root=None, extract_dir=None):
root = Path(root or Path(__file__).resolve().parent).resolve()
result = verify(root)
if result['errors']:
raise ValueError('Release verification failed: ' + '; '.join(result['errors']))
archive_path = root/'images.zip'
destination = (Path(extract_dir).resolve() if extract_dir else
Path(tempfile.gettempdir())/'magar-bench-test'/file_hash(archive_path)[:16])
index = json.loads((root/'image_manifest.json').read_text(encoding='utf-8'))['images']
destination.mkdir(parents=True, exist_ok=True)
with zipfile.ZipFile(archive_path) as archive:
for entry in index:
name = entry['path']
if not safe_image_path(name):
raise ValueError(f'Unsafe archive path: {name}')
target = (destination/name).resolve()
if not target.is_relative_to(destination.resolve()):
raise ValueError(f'Path escapes extraction directory: {name}')
if target.is_file() and file_hash(target) == entry['sha256']:
continue
if target.exists():
raise ValueError(f'Existing cache file differs from release: {target}')
target.parent.mkdir(parents=True, exist_ok=True)
with target.open('xb') as stream:
stream.write(archive.read(name))
if file_hash(target) != entry['sha256']:
raise ValueError(f'Extracted image mismatch: {target}')
rows = [json.loads(line) for line in (root/'test.jsonl').read_text(encoding='utf-8').splitlines() if line.strip()]
for row in rows:
row['images'] = [str((destination/name).resolve()) for name in row['images']]
return rows
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--root', type=Path, default=Path(__file__).resolve().parent)
parser.add_argument('--extract-dir', type=Path)
args = parser.parse_args()
loaded = load_test(args.root, args.extract_dir)
print(json.dumps({'test_rows':len(loaded), 'resolved_unique_images':len({p for r in loaded for p in r['images']}),
'first_sample_id':loaded[0]['id'], 'first_image':loaded[0]['images'][0]}, indent=2))
|