From 9b8436df4e7d91257644d49f2a2ddb11a777a030 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 19 Oct 2026 23:26:32 +0200 Subject: [PATCH] tools: gguf-py is also looked up in the install folder (STRATA_INSTALL_DIR, the app's folder), so tools run by hand from the Control package find what setup.py downloaded; the error names STRATA_GGUF_PY (#3269) --- tools/_paths.py | 28 +++++++++++++++++++++++++--- tools/test_gguf_paths.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 tools/test_gguf_paths.py diff --git a/tools/_paths.py b/tools/_paths.py index 336719ec..097b7d1c 100644 --- a/tools/_paths.py +++ b/tools/_paths.py @@ -1,7 +1,9 @@ """tools/_paths.py - where the tools find llama.cpp's gguf-py (for the MTP packer and the fixtures). Order: $STRATA_GGUF_PY, then /third_party/llama.cpp/gguf-py (what setup.py clones), then the development -tree's ../../.ref/llama.cpp/gguf-py. +tree's ../../.ref/llama.cpp/gguf-py, then (#3269) the install folders: in the Control package setup.py writes third_party/ +to the per-user install folder ($STRATA_INSTALL_DIR, else the app's folder, the Terminal's remembered installs), not +beside the tools, so a tool run by hand from the package finds it there. """ from __future__ import annotations @@ -12,14 +14,34 @@ from pathlib import Path ENGINE = Path(__file__).resolve().parents[1] +def _install_dirs() -> list[Path]: + """The per-user install folders a Control-mode setup.py writes third_party/ into. Best effort: never raises.""" + out: list[Path] = [] + raw = os.environ.get("STRATA_INSTALL_DIR") + if raw: + out.append(Path(raw).expanduser()) + try: + if str(ENGINE) not in sys.path: + sys.path.append(str(ENGINE)) + from strata_control import discovery + out.append(discovery.app_install_dir()) + out += [pl.path for pl in discovery.places(here=ENGINE)] + except Exception: + pass + return out + + def gguf_py() -> str: cands = [os.environ.get("STRATA_GGUF_PY"), ENGINE / "third_party" / "llama.cpp" / "gguf-py", ENGINE.parents[min(1, len(ENGINE.parents) - 1)] / ".ref" / "llama.cpp" / "gguf-py"] # the dev tree; a shallow install (G:\Strata) has no grandparent + cands += [d / "third_party" / "llama.cpp" / "gguf-py" for d in _install_dirs()] for c in cands: if c and (Path(c) / "gguf").is_dir(): return str(c) - sys.exit("llama.cpp's gguf-py was not found: run setup.py (it clones llama.cpp into third_party/), " - "or set STRATA_GGUF_PY") + sys.exit("llama.cpp's gguf-py was not found. Set STRATA_GGUF_PY to the gguf-py folder of any llama.cpp checkout " + "(/gguf-py), or run setup.py once (it downloads llama.cpp into third_party/ of the install " + "folder; in the desktop app or the Terminal package that folder is STRATA_INSTALL_DIR, which this tool also " + "looks in)") def add_gguf_py() -> None: diff --git a/tools/test_gguf_paths.py b/tools/test_gguf_paths.py new file mode 100644 index 00000000..1a73ffff --- /dev/null +++ b/tools/test_gguf_paths.py @@ -0,0 +1,39 @@ +"""#3269: a tool run by hand from the Control package finds gguf-py in the install folder setup.py wrote it to.""" +import os +import sys +import tempfile +import unittest +from pathlib import Path +from unittest import mock + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import _paths # noqa: E402 + + +class GgufPyLookup(unittest.TestCase): + def test_install_dir_is_searched(self): + with tempfile.TemporaryDirectory() as d: + gp = Path(d) / "third_party" / "llama.cpp" / "gguf-py" + (gp / "gguf").mkdir(parents=True) + env = {k: v for k, v in os.environ.items() if k != "STRATA_GGUF_PY"} + env["STRATA_INSTALL_DIR"] = d + with mock.patch.dict(os.environ, env, clear=True), \ + mock.patch.object(_paths, "ENGINE", Path(d) / "nowhere" / "pkg"): + self.assertEqual(_paths.gguf_py(), str(gp)) + + def test_env_wins_and_message_names_it(self): + with tempfile.TemporaryDirectory() as d: + (Path(d) / "gguf").mkdir() + with mock.patch.dict(os.environ, {"STRATA_GGUF_PY": d}): + self.assertEqual(_paths.gguf_py(), d) + env = {k: v for k, v in os.environ.items() if k not in ("STRATA_GGUF_PY", "STRATA_INSTALL_DIR")} + with tempfile.TemporaryDirectory() as d, mock.patch.dict(os.environ, env, clear=True), \ + mock.patch.object(_paths, "ENGINE", Path(d) / "a" / "b"), \ + mock.patch.object(_paths, "_install_dirs", return_value=[]): + with self.assertRaises(SystemExit) as cm: + _paths.gguf_py() + self.assertIn("STRATA_GGUF_PY", str(cm.exception)) + + +if __name__ == "__main__": + unittest.main()