Files
CoCoCapitalQuiz/tools/package_source.py
T

74 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""Create a deterministic, source-only Capital Quest starter ZIP.
No compiler, network, Git mutation, private inputs or game execution is involved.
"""
import argparse
import hashlib
import json
from pathlib import Path
from zipfile import ZIP_DEFLATED, ZipFile, ZipInfo
ROOT = Path(__file__).resolve().parents[1]
ROOT_FILES = ('AGENTS.md', 'LICENSE', 'Makefile', 'README.md', '.gitignore',
'vccplus-project.json', 'main.c', 'quiz.h', 'ui.h', 'map_data.h')
DIRECTORIES = ('src', 'data', 'assets', 'tests', 'tools', 'docs/manual')
EXTRA_FILES = ('docs/PROVENANCE.md', 'docs/PUBLIC_RELEASE.md')
def source_files():
names = set(ROOT_FILES + EXTRA_FILES)
for directory in DIRECTORIES:
for path in (ROOT / directory).rglob('*'):
if path.is_dir():
continue
name = path.relative_to(ROOT).as_posix()
if '__pycache__' in path.parts or path.suffix == '.pyc' or path.name == '.DS_Store':
continue
if path.is_symlink() or not path.is_file():
raise ValueError('Only regular source files are permitted: ' + name)
names.add(name)
result = {}
for name in sorted(names):
path = ROOT / name
if path.is_symlink() or not path.is_file():
raise ValueError('Missing regular source input: ' + name)
if any(word in name.casefold() for word in ('connection', 'vccgrant')):
raise ValueError('Private-connection filename is not a source input: ' + name)
if path.suffix.casefold() in ('.dsk', '.rom', '.bin', '.o', '.a', '.pdf', '.zip'):
raise ValueError('Binary, archive or generated output is not source: ' + name)
result['CapitalQuest/' + name] = path.read_bytes()
return result
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--destination', type=Path, required=True)
args = parser.parse_args()
destination = args.destination.resolve()
if destination.is_relative_to(ROOT):
parser.error('Keep package output outside the source project.')
if destination.exists():
parser.error('Destination already exists; retain completed packages unchanged.')
files = source_files()
destination.parent.mkdir(parents=True, exist_ok=True)
with ZipFile(destination, 'x', compression=ZIP_DEFLATED, compresslevel=9) as archive:
for name, data in files.items():
info = ZipInfo(name, date_time=(1980, 1, 1, 0, 0, 0))
info.create_system = 3
info.external_attr = 0o100644 << 16
info.compress_type = ZIP_DEFLATED
archive.writestr(info, data)
manifest = dict(schemaVersion=1,projectDirectory='CapitalQuest',
configurationFile='CapitalQuest/vccplus-project.json',entrySource='main.c',
archive=destination.name,byteSize=destination.stat().st_size,
sha256=hashlib.sha256(destination.read_bytes()).hexdigest(),
files=[dict(path=name,byteSize=len(data),sha256=hashlib.sha256(data).hexdigest())
for name,data in files.items()])
destination.with_suffix('.manifest.json').write_text(json.dumps(manifest,indent=2)+'\n')
print(json.dumps({k:v for k,v in manifest.items() if k!='files'},indent=2))
if __name__ == '__main__':
main()