Files
Agent-Workbench/scripts/verify-archives.py
T

64 lines
3.2 KiB
Python

import hashlib
import json
from datetime import datetime, timezone
from pathlib import Path, PurePosixPath
import sys
import uuid
import zipfile
workspace = Path(__file__).resolve().parent.parent
release_dir = (workspace / 'releases').resolve()
release = json.loads((release_dir / 'latest.json').read_text(encoding='utf-8'))
records = []
extraction_root = None
version = None
for kind, root_key in [('portable', 'portableRoot'), ('source', 'sourceRoot')]:
root = Path(release[root_key]).resolve()
archive = Path(release[kind]['path']).resolve()
assert root.is_relative_to(release_dir) and archive.is_relative_to(release_dir)
archive_bytes = archive.read_bytes()
assert len(archive_bytes) == release[kind]['bytes']
assert hashlib.sha256(archive_bytes).hexdigest() == release[kind]['sha256']
manifest = json.loads((root / 'MANIFEST.json').read_text(encoding='utf-8'))
version = version or manifest['version']
assert manifest['version'] == version and manifest['kind'] == kind
expected = {entry['path']: entry for entry in manifest['files']}
assert len(expected) == len(manifest['files'])
with zipfile.ZipFile(archive) as bundle:
assert bundle.testzip() is None, 'CRC failure'
actual = {}
for entry in bundle.infolist():
name = PurePosixPath(entry.filename.replace('\\', '/'))
assert not name.is_absolute() and '..' not in name.parts and name.parts[0] == root.name
if entry.is_dir():
continue
relative = str(PurePosixPath(*name.parts[1:]))
assert relative not in actual, 'Duplicate archive entry'
actual[relative] = entry
assert set(actual) == set(expected) | {'MANIFEST.json'}
for relative, entry in actual.items():
contents = bundle.read(entry)
target = (root / relative).resolve()
assert target.is_relative_to(root) and contents == target.read_bytes(), relative
if relative != 'MANIFEST.json':
assert len(contents) == expected[relative]['bytes']
assert hashlib.sha256(contents).hexdigest() == expected[relative]['sha256']
if kind == 'source' and '--extract-source' in sys.argv:
isolated = (workspace / '.qa' / ('source-check-' + uuid.uuid4().hex)).resolve()
assert isolated.is_relative_to((workspace / '.qa').resolve())
isolated.mkdir(parents=True, exist_ok=False)
for entry in bundle.infolist():
target = (isolated / entry.filename.replace('\\', '/')).resolve()
assert target.is_relative_to(isolated)
bundle.extractall(isolated)
extraction_root = str(isolated / root.name)
records.append({'kind': kind, 'path': str(archive), 'files': len(expected), 'crcPassed': True,
'manifestAndDirectoryMatch': True, 'sha256': release[kind]['sha256']})
result = {'success': True, 'version': version, 'testedAt': datetime.now(timezone.utc).isoformat(),
'archives': records, 'extractionRoot': extraction_root}
(workspace / '.qa').mkdir(exist_ok=True)
(workspace / '.qa/archives-validation.json').write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8')
print(json.dumps(result, ensure_ascii=True))