okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge main (87dfca0e7) into f-own-int: the 0.2.14 app branch, fixtures and CI fixes, the Qwen4-readiness tools, the shape-engine build scriptsffb3d9b31

main
2.3 KiB56 linesraw
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``)."""
21    return Path(__file__).resolve().parent.parent
22
23
24def _same(a: str, b: Path) -> bool:
25    try:
26        return os.path.normcase(os.path.abspath(a)) == os.path.normcase(str(b.resolve()))
27    except OSError:
28        return 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."""
33    if not isinstance(value, str):
34        return value
35    m = _INSIDE.match(value)
36    if not m:
37        return value
38    here = here or this_control()
39    try:
40        if os.path.exists(value) or _same(m.group(1), here):
41            return value
42    except OSError:
43        return value
44    rest = m.group(2).lstrip("/\\")
45    candidate = here.joinpath(*re.split(r"[/\\]", rest)) if rest else here
46    return 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)."""
51    out = dict(cfg)
52    if "cwd" in out:
53        out["cwd"] = repoint_value(out["cwd"], here)
54    if isinstance(out.get("args"), list):
55        out["args"] = [repoint_value(a, here) for a in out["args"]]
56    return out