okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ffb3d9b31
main
1"""Paths a run config names inside a Control folder that no longer exists, pointed at this Control. 2 3Setup writes absolute paths into a run config (``cwd``, ``--expert-profile``) that lie inside the Control it ran from. That folder can go away: 4the Linux AppImage mounts its bundled Control at ``/tmp/.mount_<random>/usr/lib/Strata/control``, a new name at every start, and an app 5reinstall or a Control update moves it too. A path that ends up inside a folder called ``control`` which no longer exists is pointed at the 6same file in this Control when the file is there; any other value is left as it is. The app host does the same for the saved files 7(apps/host/server/native-control.mjs ``repairControlPaths``); this is for a start that reads a run config itself (the update's test start). 8""" 9from __future__import annotations 10 11import os 12import re 13from pathlib import Path 14from typing import Any 15 16_INSIDE = re .compile (r"^(.+?[/\\]control)(?=[/\\]|$)(.*)$" ,re .IGNORECASE ) 17 18 19def this_control ()-> Path : 20"""The Control folder this code runs from (the folder that holds ``strata_control``).""" 21return Path (__file__ ).resolve ().parent .parent 22 23 24def _same (a :str ,b :Path )-> bool : 25try : 26return os .path .normcase (os .path .abspath (a ))== os .path .normcase (str (b .resolve ())) 27except OSError : 28return False 29 30 31def repoint_value (value :Any ,here :Path | None = None )-> Any : 32"""``value`` pointed at ``here`` (default: this Control) when it names a missing path inside another ``control`` folder; else unchanged.""" 33if not isinstance (value ,str ): 34return value 35m = _INSIDE .match (value ) 36if not m : 37return value 38here = here or this_control () 39try : 40if os .path .exists (value )or _same (m .group (1 ),here ): 41return value 42except OSError : 43return value 44rest = m .group (2 ).lstrip ("/\\" ) 45candidate = here .joinpath (* re .split (r"[/\\]" ,rest ))if rest else here 46return str (candidate )if candidate .exists ()else value 47 48 49def repoint (cfg :dict ,here :Path | None = None )-> dict : 50"""A copy of the run config with ``cwd`` and every ``args`` value repointed (see repoint_value).""" 51out = dict (cfg ) 52if "cwd" in out : 53out ["cwd" ]= repoint_value (out ["cwd" ],here ) 54if isinstance (out .get ("args" ),list ): 55out ["args" ]= [repoint_value (a ,here )for a in out ["args" ]] 56return out