#!/usr/bin/env python3 """Install checksum-pinned, project-local runtimes from official distributions.""" from concurrent.futures import ThreadPoolExecutor import argparse from pathlib import Path import hashlib import json import platform import shutil import subprocess import tarfile ROOT = Path(__file__).resolve().parents[1] RUNTIME = ROOT / '.runtime' def install(name, item): target = RUNTIME / name marker = target / '.quality-runtime-version' if marker.exists() and marker.read_text().strip() == item['version']: print(f'{name}: already installed ({item["version"]})', flush=True) return if target.exists(): raise RuntimeError(f'{target} already exists without the expected version; preserve it and inspect manually') archive = RUNTIME / 'downloads' / (name + '.tar.gz') algorithm = next(a for a in ('sha512', 'sha256', 'md5') if a in item) if not archive.exists() or hashlib.file_digest(archive.open('rb'), algorithm).hexdigest() != item[algorithm]: print(f'{name}: downloading official distribution', flush=True) partial = archive.with_suffix('.partial') subprocess.run(['curl', '--fail', '--location', '--silent', '--show-error', '--retry', '2', '--connect-timeout', '30', '--max-time', '900', item['url'], '-o', str(partial)], check=True) with partial.open('rb') as stream: actual = hashlib.file_digest(stream, algorithm).hexdigest() if actual != item[algorithm]: raise RuntimeError(f'{name}: checksum mismatch; installation stopped') partial.replace(archive) stage = RUNTIME / 'staging' / name stage.mkdir(parents=True, exist_ok=True) with tarfile.open(archive) as tar: top_names = {Path(m.name).parts[0] for m in tar.getmembers() if m.name and m.name != '.'} if len(top_names) != 1: raise RuntimeError(f'{name}: expected one distribution root') tar.extractall(stage, filter='data') extracted = stage / next(iter(top_names)) shutil.move(str(extracted), target) marker.write_text(item['version'] + '\n') print(f'{name}: installed and checksum verified ({item["version"]})', flush=True) def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--without-ai', action='store_true', help='Explicitly install only the database and Java runtimes') parser.add_argument('--without-dsh', action='store_true', help='Keep only the existing nao analysis runtime') parser.add_argument('--dsh-node', help='Use this Node 24.9+ executable for the project-local DSH runtime') args = parser.parse_args() if platform.system() != 'Darwin' or platform.machine() != 'arm64': raise SystemExit('The locked local runtime packages target Apple Silicon macOS. Use a supported system JDK 21/MySQL 8.4 on other hosts.') (RUNTIME / 'downloads').mkdir(parents=True, exist_ok=True) lock = json.loads((ROOT / 'runtime.lock.json').read_text()) with ThreadPoolExecutor(max_workers=3) as pool: futures = [pool.submit(install, name, item) for name, item in lock.items()] for future in futures: future.result() if not args.without_ai: from setup_nao_runtime import install as install_assistant install_assistant() if not args.without_dsh: from setup_dsh_runtime import install as install_dsh install_dsh(args.dsh_node) print('All local runtimes are ready.', flush=True) if __name__ == '__main__': main()