98 lines
4.9 KiB
Python
98 lines
4.9 KiB
Python
import hashlib
|
|
import json
|
|
import os
|
|
import shutil
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path, PurePosixPath
|
|
import sys
|
|
import uuid
|
|
import zipfile
|
|
|
|
if '--help' in sys.argv:
|
|
print('python scripts/verify-archives.py [--extract-source] - verify current immutable ZIPs; AGENT_WORKBENCH_ARCHIVE_QA_ROOT selects the extraction root.')
|
|
sys.exit(0)
|
|
if any(value != '--extract-source' for value in sys.argv[1:]):
|
|
raise SystemExit('Unsupported argument; use --help.')
|
|
|
|
workspace = Path(__file__).resolve().parent.parent
|
|
release_dir = (workspace / 'releases').resolve()
|
|
release = json.loads((release_dir / 'latest.json').read_text(encoding='utf-8'))
|
|
archive_qa_root = Path(os.environ.get('AGENT_WORKBENCH_ARCHIVE_QA_ROOT', workspace / '.qa')).resolve()
|
|
archive_qa_root.mkdir(parents=True, exist_ok=True)
|
|
records = []
|
|
extraction_root = None
|
|
version = None
|
|
|
|
def filesystem_path(path):
|
|
"""Use the Windows extended path form for source archives with deep dependency trees."""
|
|
value = str(path)
|
|
if os.name == 'nt' and not value.startswith('\\\\?\\'):
|
|
return '\\\\?\\' + value
|
|
return value
|
|
|
|
def extract_archive(bundle, root):
|
|
for entry in bundle.infolist():
|
|
target = (root / entry.filename.replace('\\\\', '/')).resolve()
|
|
if entry.is_dir():
|
|
os.makedirs(filesystem_path(target), exist_ok=True)
|
|
continue
|
|
os.makedirs(filesystem_path(target.parent), exist_ok=True)
|
|
with bundle.open(entry, 'r') as source, open(filesystem_path(target), 'wb') as destination:
|
|
shutil.copyfileobj(source, destination)
|
|
|
|
for kind, root_key in [('portable', 'portableRoot'), ('source', 'sourceRoot')] + ([('evidence', 'evidenceRoot')] if 'evidenceRoot' in release else []):
|
|
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']
|
|
if kind == 'evidence':
|
|
assert manifest['app'] == 'agent-workbench-evidence'
|
|
assert manifest['sourceCommit'] == release['documentation']['sourceCommit']
|
|
assert hashlib.sha256((root / 'MANIFEST.json').read_bytes()).hexdigest() == release['documentation']['evidenceManifestSha256']
|
|
else:
|
|
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), relative
|
|
with open(filesystem_path(target), 'rb') as original:
|
|
assert contents == original.read(), 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 = (archive_qa_root / ('source-check-' + uuid.uuid4().hex)).resolve()
|
|
assert isolated.is_relative_to(archive_qa_root)
|
|
isolated.mkdir(parents=True, exist_ok=False)
|
|
for entry in bundle.infolist():
|
|
target = (isolated / entry.filename.replace('\\', '/')).resolve()
|
|
assert target.is_relative_to(isolated)
|
|
extract_archive(bundle, 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))
|