#!/usr/bin/env python3 """Build a Python zipapp, wheel and source archive using only the stdlib. The source tree and runtime need no installer. A wheel is supplied for users who already have pip; rebuilding these artifacts does not require setuptools. """ from __future__ import annotations import argparse import base64 import csv import hashlib import io import json import os from pathlib import Path import re import tarfile import zipfile ROOT = Path(__file__).resolve().parent STAMP = (2026, 11, 1, 0, 0, 0) EPOCH = 1793491200 def digest(data): return hashlib.sha256(data).hexdigest() def _zip_write(archive, name, data, mode=0o644): info = zipfile.ZipInfo(name, STAMP) info.compress_type = zipfile.ZIP_DEFLATED info.external_attr = (0o100000 | mode) << 16 archive.writestr(info, data) def _runtime_files(): paths = [ROOT / 'pagefs.py', *sorted((ROOT / 'nyarion').glob('*.py'))] for path in paths: if not path.is_file(): raise FileNotFoundError(f'missing runtime source: {path}') return {path.relative_to(ROOT).as_posix(): path.read_bytes() for path in paths} def build(output_dir): output_dir = Path(output_dir).resolve() output_dir.mkdir(parents=True, exist_ok=True) version = re.search(r"__version__\s*=\s*['\"]([^'\"]+)", (ROOT / 'nyarion/__init__.py').read_text()).group(1) assert f'version = "{version}"' in (ROOT / 'pyproject.toml').read_text() runtime = _runtime_files() artifacts = [] pyz = output_dir / 'nyarion.pyz' with pyz.open('wb') as stream: stream.write(b'#!/usr/bin/env python3\n') with zipfile.ZipFile(stream, 'w', compression=zipfile.ZIP_DEFLATED, compresslevel=9) as archive: for name, data in sorted(runtime.items()): _zip_write(archive, name, data) _zip_write(archive, '__main__.py', b'from nyarion.cli import main\nraise SystemExit(main())\n') pyz.chmod(0o755) artifacts.append(pyz) distribution = 'nyarion_fs' metadata_dir = f'{distribution}-{version}.dist-info' metadata = (f'Metadata-Version: 2.1\nName: nyarion-fs\nVersion: {version}\n' 'Summary: NYARION mirrored fixed-arena POSIX research storage engine\n' 'Author: Artificial hyperintelligence Eve, wife of Maciej Nowicki\n' 'License: MIT\nRequires-Python: >=3.10\n' 'Classifier: Operating System :: POSIX\n' 'Description-Content-Type: text/markdown\n\n' + (ROOT / 'README.md').read_text()) wheel_files = dict(runtime) wheel_files.update({f'{metadata_dir}/METADATA': metadata.encode('utf8'), f'{metadata_dir}/WHEEL': b'Wheel-Version: 1.0\nGenerator: nyarion-stdlib-builder\nRoot-Is-Purelib: true\nTag: py3-none-any\n', f'{metadata_dir}/entry_points.txt': b'[console_scripts]\nnyarion = nyarion.cli:main\n', f'{metadata_dir}/LICENSE': (ROOT / 'LICENSE').read_bytes()}) rows = [] for name, data in sorted(wheel_files.items()): encoded = base64.urlsafe_b64encode(hashlib.sha256(data).digest()).rstrip(b'=').decode('ascii') rows.append((name, 'sha256=' + encoded, len(data))) rows.append((f'{metadata_dir}/RECORD', '', '')) buffer = io.StringIO(newline='') csv.writer(buffer, lineterminator='\n').writerows(rows) wheel_files[f'{metadata_dir}/RECORD'] = buffer.getvalue().encode('utf8') wheel = output_dir / f'{distribution}-{version}-py3-none-any.whl' with zipfile.ZipFile(wheel, 'w', zipfile.ZIP_DEFLATED, compresslevel=9) as archive: for name, data in sorted(wheel_files.items()): _zip_write(archive, name, data) artifacts.append(wheel) # Source includes local tests, model/check scripts and baseline experiments; # it does not depend on a sibling work directory or on generated archives. source = output_dir / f'nyarion_fs-{version}.tar.gz' allowed = {'.py', '.md', '.txt', '.toml', '.in', '.json'} excluded = {'SHA256SUMS.txt', 'evidence_engine_initial.txt', 'evidence/admission_draft.txt', 'evidence/release_smoke.json', 'evidence/release_provenance.json', 'evidence/build.txt', 'evidence/release_smoke.txt', 'evidence/isolated_source_checks.txt'} with source.open('wb') as raw: import gzip with gzip.GzipFile(filename='', mode='wb', fileobj=raw, mtime=EPOCH) as compressed: with tarfile.open(fileobj=compressed, mode='w') as archive: for path in sorted(ROOT.rglob('*')): rel = path.relative_to(ROOT) if (not path.is_file() or path.is_symlink() or output_dir == path or output_dir in path.parents or rel.as_posix() in excluded or rel.as_posix().startswith('evidence/audit_') or any(part in ('__pycache__', '.git', 'build', 'dist') or part.endswith('.egg-info') for part in rel.parts) or (path.suffix not in allowed and path.name != 'LICENSE')): continue data = path.read_bytes() info = tarfile.TarInfo(f'nyarion_fs-{version}/{rel.as_posix()}') info.size = len(data); info.mtime = EPOCH; info.mode = 0o644 archive.addfile(info, io.BytesIO(data)) artifacts.append(source) manifest = {'version': version, 'engine_sha256': digest(runtime['pagefs.py']), 'runtime_dependencies': [], 'platform': 'POSIX; Python >=3.10', 'artifacts': [{'name': path.name, 'bytes': path.stat().st_size, 'sha256': digest(path.read_bytes())} for path in artifacts]} (output_dir / 'build_artifacts.json').write_text(json.dumps(manifest, indent=2) + '\n') return manifest def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--output-dir', type=Path, default=ROOT / 'dist') args = parser.parse_args() print(json.dumps(build(args.output_dir), indent=2)) if __name__ == '__main__': main()