okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
be32c7f3f
main
1"""Normalization: the golden files must be byte-identical on Windows, Linux and macOS and on every developer's machine. 2 3Done here: the scratch folder, the repository folder and the home folder become <ROOT>, <REPO>, <HOME>; every backslash in a path becomes a slash; the engine 4stub's executable suffix is dropped; random API keys, run ids and times become fixed words. Nothing else is touched, so a real difference stays visible. 5""" 6 7from __future__import annotations 8 9import re 10from pathlib import Path 11 12KEY = re .compile (r"\b(sk-[A-Za-z0-9_-]{8,}|strata-[A-Za-z0-9]{24,})\b" ) 13 14 15def _roots (world )-> list [tuple [str ,str ]]: 16from .world import ROOT 17pairs = [] 18for path ,word in ((world .root ,"<ROOT>" ), (ROOT ,"<REPO>" )): 19for form in {str (path ),path .as_posix (),str (path ).replace ("\\" ,"/" )}: 20pairs .append ((form ,word )) 21pairs .sort (key = lambda p :- len (p [0 ]))# the longest first: <ROOT> sits inside no other, but be safe 22return pairs 23 24 25def normalize (value ,world ): 26pairs = _roots (world ) 27 28def text (s :str )-> str : 29for form ,word in pairs : 30s = s .replace (form ,word ) 31s = s .replace (form .replace ("/" ,"\\" ),word ) 32if "<" in s or "\\" in s : 33s = re .sub (r"(<ROOT>|<REPO>)([^\s\"',;]*)" ,lambda m :m .group (1 )+ m .group (2 ).replace ("\\" ,"/" ),s ) 34s = re .sub (r"<ROOT>/(local/Strata|state/strata)/" ,"<ROOT>/<STATE>/" ,s )# the per-user state folder is named by the host OS 35s = KEY .sub ("<KEY>" ,s ) 36s = re .sub (r"/strata-engine\.exe\b" ,"/strata-engine" ,s ) 37return s 38 39def walk (v ,key = None ): 40if isinstance (v ,dict ): 41out = {} 42for k ,x in v .items (): 43if k in ("api_key" ,)and x : 44out [k ]= "<KEY>" 45elif k in ("created" ,"updated" ,"at" ,"stamp" ,"saved_at" ,"written_at" ,"ts" ,"time" )and isinstance (x , (int ,float )): 46out [k ]= "<TIME>" 47else : 48out [k ]= walk (x ,k ) 49return out 50if isinstance (v ,list ): 51return [walk (x )for x in v ] 52if isinstance (v ,str ): 53return text (v ) 54return v 55 56out = walk (value ) 57# the run config carries the argument list three times (args, and the stamps of what the launch derived and wrote); an identical copy is written as "=args" 58cfg = out .get ("config_file" )if isinstance (out ,dict )else None 59if isinstance (cfg ,dict ): 60for k in ("args_derived" ,"args_written" ): 61if k in cfg and cfg [k ]== cfg .get ("args" ): 62cfg [k ]= "=args" 63return out