zhiyuanhucs's picture
Publish clean five-benchmark evaluation code, fixed tasks and metric definitions
fb361ec verified
Raw History Blame Contribute Delete
1.99 kB
#!/usr/bin/env python3
"""Verify and unpack the task, vendor and agent-runtime payloads."""
import hashlib
import json
from pathlib import Path
import subprocess
import tarfile
ROOT = Path(__file__).resolve().parent
def main():
manifest = json.loads((ROOT / 'ARTIFACTS.json').read_text())
for name, record in manifest['archives'].items():
archive = (ROOT / name).resolve(strict=True)
with archive.open('rb') as handle:
if hashlib.file_digest(handle, 'sha256').hexdigest() != record['sha256']:
raise ValueError('Archive checksum mismatch: ' + name)
process = subprocess.Popen(['zstd', '-dc', str(archive)], stdout=subprocess.PIPE)
try:
with tarfile.open(fileobj=process.stdout, mode='r|') as stream:
for member in stream:
relative = Path(member.name)
if relative.is_absolute() or '..' in relative.parts or member.issym() or member.islnk():
raise ValueError('Unsafe archive member')
if member.isdir():
(ROOT / relative).mkdir(parents=True, exist_ok=True)
elif member.isfile():
target = ROOT / relative
if not target.resolve().is_relative_to(ROOT):
raise ValueError('Unsafe destination')
target.parent.mkdir(parents=True, exist_ok=True)
with stream.extractfile(member) as source, target.open('wb') as destination:
import shutil
shutil.copyfileobj(source, destination)
target.chmod(member.mode)
if process.wait():
raise RuntimeError('Decompression failed')
finally:
if process.poll() is None:
process.terminate()
print('Verified and unpacked:', name)
if __name__ == '__main__':
main()