okami.mom/strata

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

Niko1221Merge f-ds-out ac306afb2 into f-own-intaff3a132f

main
3.9 KiB86 linesraw
1"""Every tools/*.py that setup.py, strata_cli or strata_control runs BY PATH is in the packaged Terminal (and so in the Control bundle).
2
30.2.4 shipped without tools/dsv41/pack_gguf.py: the tests ran from the checkout, where the file exists, and the packaged Terminal failed at
4"Preparing your model" with "can't open file .../tools/dsv41/pack_gguf.py".  This test reads the by-path references out of the source (an
5AST scan: `ROOT / "tools" / "x" / "y.py"` and the plain string "tools/x/y.py"), stages the bundle the way package_terminal does, and checks that
6each referenced file is in it.  It then runs the DeepSeek scripts from the BUNDLE with --help in a folder that holds nothing else, so an import
7they need (the sibling smoke.py, strata_tokenizer.py) must come from the bundle too."""
8from __future__ import annotations
9
10import ast
11import glob
12import os
13import re
14import shutil
15import subprocess
16import sys
17import tempfile
18import unittest
19from pathlib import Path
20
21ROOT = Path(__file__).resolve().parents[2]
22sys.path.insert(0, str(ROOT / "packaging"))
23import package_terminal as pt  # noqa: E402
24
25#: by-path references that are NOT part of the packaged Terminal on purpose (engine build and install code the bundle never runs). None today.
26NOT_BUNDLED: set[str] = set()
27
28
29def _flatten(node):
30    if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Div):
31        return _flatten(node.left) + _flatten(node.right)
32    return [node]
33
34
35def referenced_tools(root: Path = ROOT) -> dict[str, set[str]]:
36    """{'tools/dsv41/pack_gguf.py': {files that name it}} for setup.py, strata_cli/**, strata_control/**."""
37    files = [root / "setup.py"] + sorted((root / "strata_cli").rglob("*.py")) + sorted((root / "strata_control").rglob("*.py"))
38    found: dict[str, set[str]] = {}
39    for f in files:
40        tree = ast.parse(f.read_text(encoding="utf-8-sig"))
41        for node in ast.walk(tree):
42            if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Div):
43                vals = [p.value if isinstance(p, ast.Constant) and isinstance(p.value, str) else None for p in _flatten(node)]
44                if "tools" in vals:
45                    rest = []
46                    for v in vals[vals.index("tools") + 1:]:
47                        if v is None:
48                            break
49                        rest.append(v)
50                    if rest and rest[-1].endswith(".py"):
51                        found.setdefault("tools/" + "/".join(rest), set()).add(f.relative_to(root).as_posix())
52            elif isinstance(node, ast.Constant) and isinstance(node.value, str):
53                m = re.fullmatch(r"tools[/\\]((?:\w+[/\\])*\w+\.py)", node.value)
54                if m:
55                    found.setdefault("tools/" + m.group(1).replace("\\", "/"), set()).add(f.relative_to(root).as_posix())
56    return found
57
58
59class ByPathToolsAreBundled(unittest.TestCase):
60    @classmethod
61    def setUpClass(cls):
62        cls.temp = tempfile.TemporaryDirectory()
63        cls.root = Path(cls.temp.name) / "bundle"
64        cls.root.mkdir()
65        (cls.root / "tools").mkdir()
66        for item in pt.TOOLS_SUBSET:
67            shutil.copy2(ROOT / "tools" / item, cls.root / "tools" / item)
68        pt.add_installer(cls.root, ROOT)
69
70    @classmethod
71    def tearDownClass(cls):
72        cls.temp.cleanup()
73
74    def test_the_scan_finds_the_by_path_tools(self):
75        # (a scan that finds nothing would make the next test pass for the wrong reason)
76        refs = referenced_tools()
77        for name in ("tools/iq_pack.py", "tools/mtp_fetch.py"):
78            self.assertIn(name, refs)
79        self.assertGreaterEqual(len(refs), 12)
80
81    def test_every_file_run_by_path_is_in_the_bundle(self):
82        missing = {p: sorted(by) for p, by in referenced_tools().items() if p not in NOT_BUNDLED and not (self.root / p).is_file()}
83        self.assertEqual(missing, {}, f"run by path, absent from the packaged Terminal: {missing}")
84
85if __name__ == "__main__":
86    unittest.main()