okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2e3085a55
main
1"""The line between serving and acquiring. 2 3Starting a model and serving an API never install, download or compile anything. Everything that 4reaches the network or runs pip, git, cmake or a package manager lives behind an explicit command: 5SETUP, UPDATE or `strata install`. This module is that line, in one place: 6 7* `forbidden()` marks a stretch of code (a start, a serve) in which acquiring is not allowed; 8* `require(what, command)` is called by every acquiring primitive (a download, a pip install, a 9network probe, a compile) and raises `AcquisitionRefused` inside such a stretch; 10* `AcquisitionRefused` is the typed error: it says what is missing and names the explicit command. 11 12The environment variable `STRATA_NO_ACQUIRE=1` forbids acquiring for a whole process. A process 13that serves (`strata serve`, `strata runtime start`, the API server) never imports the installer, so 14for it this is a second line of defence, not the first. 15 16Nothing here imports anything outside the standard library. 17""" 18 19from __future__import annotations 20 21import contextlib 22import os 23import sys 24import threading 25 26ENV = "STRATA_NO_ACQUIRE" 27 28_lock = threading .Lock () 29_depth = 0 30 31 32class AcquisitionRefused (RuntimeError ): 33"""Something would have been downloaded or installed during a start or a serve.""" 34 35code = "acquisition_refused" 36exit_code = 5 37 38def __init__ (self ,what :str ,command :str | None = None ): 39self .what = what 40self .command = command or explicit_commands () 41super ().__init__ ( 42f" { what } is missing or out of date. Starting and serving never install or download " 43f"anything. Run: { self . command } " ) 44 45 46def explicit_commands ()-> str : 47"""The commands that acquire, named for this platform.""" 48if os .name == "nt" : 49return "SETUP.bat (first-time setup), UPDATE.bat (update) or `strata install`" 50return "./setup.sh (first-time setup), ./update.sh (update) or `strata install`" 51 52 53def is_forbidden ()-> bool : 54return _depth > 0 or os .environ .get (ENV ,"" ).strip ().lower ()in ("1" ,"true" ,"yes" ,"on" ) 55 56 57@ contextlib . contextmanager 58def forbidden (): 59"""No acquisition inside this block. Nests.""" 60global _depth 61with _lock : 62_depth += 1 63try : 64yield 65finally : 66with _lock : 67_depth -= 1 68 69 70def require (what :str ,command :str | None = None )-> None : 71"""Call before acquiring `what`; raises AcquisitionRefused where acquiring is forbidden.""" 72if is_forbidden (): 73raise AcquisitionRefused (what ,command ) 74 75 76def main_guard (fn ,* ,fail = None ): 77"""Run `fn()`; a refused acquisition becomes `fail(message, hint)` (or an exit-5 message).""" 78try : 79return fn () 80except AcquisitionRefused as exc : 81if fail is not None : 82return fail (str (exc ),None ) 83f"strata: { exc } " ,file = sys .stderr ) 84return exc .exit_code