diff --git a/packaging/package_terminal.py b/packaging/package_terminal.py index aa368651..55b29b1a 100644 --- a/packaging/package_terminal.py +++ b/packaging/package_terminal.py @@ -121,6 +121,11 @@ INSTALLER_FILES = ("setup.py", "ref/load.py") # ref/load.py: strata_pack.py (t #: module it starts to import is never forgotten again; tests/terminal/test_bundle_imports.py checks the staged bundle. INSTALLER_TOOLS = ("strata_mcp", "iq_pack", "strata_pack", "pack_index", "mtp_fetch", "mtp_pack", "mtp_rt", "draft_vocab", "strata_tokenizer", "gguf_reader", "calibrate") +#: tools run BY PATH (`python tools/.../x.py`) by setup.py, strata_cli or strata_control: no import names them, so setup_tool_imports() cannot find +#: them. DeepSeek's setup and launch run dsv41/pack_gguf.py (the model folder), dsv41/smoke.py (the engine's first answer) and dsv41/tune.py +#: (the short tune); setup.py's other by-path tools (nvfp4_local_pack, strata_bench, strata_inspect) were missing from the bundle the same way. +#: tests/terminal/test_bundle_tools_paths.py reads the by-path references out of the source and fails when one is missing from the bundle. +INSTALLER_SCRIPTS = ("dsv41/pack_gguf", "dsv41/smoke", "dsv41/tune", "nvfp4_local_pack", "strata_bench", "strata_inspect") INSTALLER_DATA = "data" # draft vocabularies, expert profiles: 2 MB #: packages the bundle must import after the install (the API server's and the CLI's). @@ -245,9 +250,15 @@ def local_imports(folder: Path, entry: tuple[str, ...]) -> list[str]: source = folder / "tools" / f"{name}.py" if not source.exists(): raise SystemExit(f"installer tool missing: tools/{name}.py") + sub = name.rsplit("/", 1)[0] + "/" if "/" in name else "" # (tools/dsv41/tune.py imports its neighbour smoke.py) for node in ast.walk(ast.parse(source.read_text(encoding="utf-8-sig"))): names = [a.name.split(".")[0] for a in node.names] if isinstance(node, ast.Import) else [node.module.split(".")[0]] if isinstance(node, ast.ImportFrom) and node.module and node.level == 0 else [] - todo += [m for m in names if (folder / "tools" / f"{m}.py").exists() and m not in seen] + for m in names: + for rel in ((sub + m,) if sub else ()) + (m,): + if (folder / "tools" / f"{rel}.py").exists(): + if rel not in seen: + todo.append(rel) + break return sorted(seen) @@ -266,7 +277,7 @@ def setup_tool_imports(source: Path = ROOT) -> list[str]: def add_installer(folder: Path, source: Path = ROOT) -> dict: """Copy the installer's files from the source tree into a staged terminal folder (after the terminal payload is in place).""" - tools = local_imports(source, tuple(sorted(set(INSTALLER_TOOLS) | set(setup_tool_imports(source))))) + tools = local_imports(source, tuple(sorted(set(INSTALLER_TOOLS) | set(INSTALLER_SCRIPTS) | set(setup_tool_imports(source))))) copied = [] for name in INSTALLER_FILES: (folder / name).parent.mkdir(parents=True, exist_ok=True) @@ -274,6 +285,7 @@ def add_installer(folder: Path, source: Path = ROOT) -> dict: copied.append(name) (folder / "tools").mkdir(exist_ok=True) for name in tools: + (folder / "tools" / f"{name}.py").parent.mkdir(parents=True, exist_ok=True) shutil.copy2(source / "tools" / f"{name}.py", folder / "tools" / f"{name}.py") copied.append(f"tools/{name}.py") if (folder / INSTALLER_DATA).exists(): diff --git a/tests/terminal/test_bundle_tools_paths.py b/tests/terminal/test_bundle_tools_paths.py new file mode 100644 index 00000000..64e4b6d5 --- /dev/null +++ b/tests/terminal/test_bundle_tools_paths.py @@ -0,0 +1,94 @@ +"""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). + +0.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 +"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 +AST 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 +each 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 +they need (the sibling smoke.py, strata_tokenizer.py) must come from the bundle too.""" +from __future__ import annotations + +import ast +import glob +import os +import re +import shutil +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[2] +sys.path.insert(0, str(ROOT / "packaging")) +import package_terminal as pt # noqa: E402 + +#: by-path references that are NOT part of the packaged Terminal on purpose (engine build and install code the bundle never runs). None today. +NOT_BUNDLED: set[str] = set() + + +def _flatten(node): + if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Div): + return _flatten(node.left) + _flatten(node.right) + return [node] + + +def referenced_tools(root: Path = ROOT) -> dict[str, set[str]]: + """{'tools/dsv41/pack_gguf.py': {files that name it}} for setup.py, strata_cli/**, strata_control/**.""" + files = [root / "setup.py"] + sorted((root / "strata_cli").rglob("*.py")) + sorted((root / "strata_control").rglob("*.py")) + found: dict[str, set[str]] = {} + for f in files: + tree = ast.parse(f.read_text(encoding="utf-8-sig")) + for node in ast.walk(tree): + if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Div): + vals = [p.value if isinstance(p, ast.Constant) and isinstance(p.value, str) else None for p in _flatten(node)] + if "tools" in vals: + rest = [] + for v in vals[vals.index("tools") + 1:]: + if v is None: + break + rest.append(v) + if rest and rest[-1].endswith(".py"): + found.setdefault("tools/" + "/".join(rest), set()).add(f.relative_to(root).as_posix()) + elif isinstance(node, ast.Constant) and isinstance(node.value, str): + m = re.fullmatch(r"tools[/\\]((?:\w+[/\\])*\w+\.py)", node.value) + if m: + found.setdefault("tools/" + m.group(1).replace("\\", "/"), set()).add(f.relative_to(root).as_posix()) + return found + + +class ByPathToolsAreBundled(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.temp = tempfile.TemporaryDirectory() + cls.root = Path(cls.temp.name) / "bundle" + cls.root.mkdir() + (cls.root / "tools").mkdir() + for item in pt.TOOLS_SUBSET: + shutil.copy2(ROOT / "tools" / item, cls.root / "tools" / item) + pt.add_installer(cls.root, ROOT) + + @classmethod + def tearDownClass(cls): + cls.temp.cleanup() + + def test_the_scan_finds_the_deepseek_scripts(self): + # (a scan that finds nothing would make the next test pass for the wrong reason) + refs = referenced_tools() + for name in ("tools/dsv41/pack_gguf.py", "tools/dsv41/smoke.py", "tools/dsv41/tune.py", "tools/iq_pack.py", "tools/mtp_fetch.py"): + self.assertIn(name, refs) + self.assertGreaterEqual(len(refs), 12) + + def test_every_file_run_by_path_is_in_the_bundle(self): + missing = {p: sorted(by) for p, by in referenced_tools().items() if p not in NOT_BUNDLED and not (self.root / p).is_file()} + self.assertEqual(missing, {}, f"run by path, absent from the packaged Terminal: {missing}") + + def test_the_deepseek_scripts_start_from_the_bundle_alone(self): + env = {k: v for k, v in os.environ.items() if k not in ("PYTHONPATH",)} + for rel in ("tools/dsv41/pack_gguf.py", "tools/dsv41/smoke.py", "tools/dsv41/tune.py"): + with tempfile.TemporaryDirectory() as cwd: + done = subprocess.run([sys.executable, str(self.root / rel), "--help"], capture_output=True, text=True, cwd=cwd, env=env, timeout=120) + self.assertEqual(done.returncode, 0, f"{rel} --help from the bundle: {done.stderr[-400:]}") + + +if __name__ == "__main__": + unittest.main()