#!/usr/bin/env python3 # SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC # SPDX-License-Identifier: Apache-2.0 """Dafny, pinned: fetch the release this project uses, check it, and run it. pixi run dafny-fetch download Dafny into .tools/ once (network), checked by SHA-256 pixi run dafny verify file.dfy run the pinned Dafny (fetches first if it is missing) pixi run dafny --version Dafny is not on conda-forge, so pixi cannot install it directly; this script is the pin instead. It downloads the official release zip for this machine from GitHub, refuses it unless its SHA-256 is the one written below, and unpacks it into .tools/dafny-/ (ignored by git). The zip is self-contained: it carries its own .NET runtime and the Z3 builds Dafny was tested with, so the solver is pinned too. To move to another Dafny, change VERSION and every hash in RELEASES in one commit, and expect every stored proof answer to be asked again. One thing the zip does not carry is ICU, which .NET loads for culture-aware text; on a machine without it (a slim container image) Dafny aborts before it prints its version. Dafny is run here with DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1 (`env()` below), which tells .NET not to look for ICU: verifying a program compares no text by culture, so the verdicts are the same and the machine needs nothing installed. Whatever else runs the executable (scripts/library.mjs, tests/dafny.test.mjs) sets the same variable. Standard library only, like the corpus pipeline. """ from __future__ import annotations import hashlib import os import platform import shutil import stat import subprocess import sys import urllib.request import zipfile from pathlib import Path VERSION = "4.11.0" # platform -> (release asset, SHA-256 of the zip) RELEASES = { ("Linux", "x86_64"): ("dafny-4.11.0-x64-ubuntu-22.04.zip", "a46a9ff7cdd720f7955854c78e95df13f4cfe6b80691b05f8654fe19e8267179"), ("Darwin", "arm64"): ("dafny-4.11.0-arm64-macos-13.zip", "c90c75e7d5db9c6ccbb7127840dfe43f0ac938b039a7ebed146d8ead383a572f"), } URL = "https://github.com/dafny-lang/dafny/releases/download/v{version}/{asset}" ROOT = Path(__file__).resolve().parent.parent TOOLS = ROOT / ".tools" HOME = TOOLS / f"dafny-{VERSION}" EXE = HOME / "dafny" / "dafny" def release(system: str | None = None, machine: str | None = None) -> tuple[str, str]: key = (system or platform.system(), machine or platform.machine()) if key not in RELEASES: raise SystemExit(f"dafny: no pinned Dafny {VERSION} for {key[0]} {key[1]}; the pinned platforms are " + ", ".join(f"{s} {m}" for s, m in RELEASES)) return RELEASES[key] def sha256(path: Path) -> str: h = hashlib.sha256() with path.open("rb") as f: for block in iter(lambda: f.read(1 << 20), b""): h.update(block) return h.hexdigest() def unpack(zip_path: Path, into: Path) -> None: """Unzip, keeping each file's Unix permission bits (zipfile drops them), so dafny and z3 stay executable.""" with zipfile.ZipFile(zip_path) as z: for info in z.infolist(): target = into / info.filename z.extract(info, into) mode = (info.external_attr >> 16) & 0o777 if mode and not info.is_dir(): os.chmod(target, mode | stat.S_IRUSR) def fetch(quiet: bool = False) -> Path: """Make sure the pinned Dafny is in .tools/, and return its executable.""" if EXE.exists(): return EXE asset, want = release() TOOLS.mkdir(exist_ok=True) zip_path = TOOLS / asset if not zip_path.exists() or sha256(zip_path) != want: url = URL.format(version=VERSION, asset=asset) if not quiet: print(f"dafny: downloading {url}", file=sys.stderr) part = zip_path.with_suffix(".part") with urllib.request.urlopen(url) as r, part.open("wb") as f: shutil.copyfileobj(r, f) part.replace(zip_path) got = sha256(zip_path) if got != want: zip_path.unlink() raise SystemExit(f"dafny: {asset} has SHA-256 {got}, not the pinned {want}; refusing it") staging = TOOLS / f".dafny-{VERSION}.unpacking" shutil.rmtree(staging, ignore_errors=True) unpack(zip_path, staging) staging.replace(HOME) zip_path.unlink() if not quiet: print(f"dafny: Dafny {VERSION} is in {HOME.relative_to(ROOT)}", file=sys.stderr) return EXE def env() -> dict[str, str]: """The environment Dafny runs in: this one, and .NET told not to look for ICU (the module's docstring).""" return {**os.environ, "DOTNET_SYSTEM_GLOBALIZATION_INVARIANT": "1"} def main(argv: list[str]) -> int: if argv[:1] == ["fetch"]: fetch() v = subprocess.run([str(EXE), "--version"], capture_output=True, text=True, env=env()) if v.returncode != 0 or not v.stdout.startswith(VERSION): raise SystemExit(f"dafny: the fetched Dafny does not start here (exit {v.returncode}): {(v.stderr or v.stdout).strip()[:400]}") print(v.stdout.strip()) return 0 if argv[:1] == ["path"]: print(fetch(quiet=True)) return 0 exe = fetch() return subprocess.run([str(exe), *argv], env=env()).returncode if __name__ == "__main__": sys.exit(main(sys.argv[1:]))