Fix #3613 #3467: the Terminal/Control bundle ships tools/calibrate.py; a setup crash now says what it was
setup.py imports tools/calibrate.py after a tune, but INSTALLER_TOOLS did not list it, so every launch after a tune died with ModuleNotFoundError and only 'The model could not be installed'. The list now names it, and add_installer also reads the tools/ modules setup.py imports so the list cannot go stale again (stage-control.py uses the same list). A new test stages the bundle the way package_terminal.py does and imports setup.py and its tools with tools/ limited to the bundle. strata launch keeps the last 400 lines of setup's output in launch/<name>.setup.log, shows the Python error line in plain words when setup crashed without an [X] line, and names the file.
This commit is contained in:
1 parent
27a5707de5
commit
2a22daa4f0
4 files changed
+238
-5
No files matched your search
@@ -115,8 +115,11 @@ TOOLS_SUBSET = (
|
||||
#: (STRATA_CONTROL_INSTALL=1: no pip, no engine fetch, no compiler). Entry points; their local imports under tools/ come with them.
|
||||
#: The same list the app's Control component stages (apps/desktop/scripts/stage-control.py uses these names).
|
||||
INSTALLER_FILES = ("setup.py", "ref/load.py") # ref/load.py: strata_pack.py (the Q2_0 AVX-512 repack) imports it
|
||||
#: calibrate: setup.py imports it after a tune (calibrate_config, and the earlier tuning that a rerun applies); it was missing from this
|
||||
#: list in 0.1.47, so every launch after a tune failed (#3613, #3467). setup_tool_imports() below also reads setup.py itself, so a tools/
|
||||
#: 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")
|
||||
"strata_tokenizer", "gguf_reader", "calibrate")
|
||||
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).
|
||||
@@ -247,9 +250,22 @@ def local_imports(folder: Path, entry: tuple[str, ...]) -> list[str]:
|
||||
return sorted(seen)
|
||||
|
||||
|
||||
def setup_tool_imports(source: Path = ROOT) -> list[str]:
|
||||
"""The `tools/<name>.py` modules that the installer files import (any import statement, at any depth: setup.py imports most of them
|
||||
inside functions after putting tools/ on sys.path). tools/hip and tools/image hold engine-install code that Control's mode never runs."""
|
||||
import ast
|
||||
found: set[str] = set()
|
||||
for name in INSTALLER_FILES:
|
||||
for node in ast.walk(ast.parse((source / name).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 []
|
||||
found.update(m for m in names if (source / "tools" / f"{m}.py").exists())
|
||||
return sorted(found)
|
||||
|
||||
|
||||
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, INSTALLER_TOOLS)
|
||||
tools = local_imports(source, tuple(sorted(set(INSTALLER_TOOLS) | set(setup_tool_imports(source)))))
|
||||
copied = []
|
||||
for name in INSTALLER_FILES:
|
||||
(folder / name).parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
@@ -34,6 +34,23 @@ ROOT = Path(__file__).resolve().parents[2]
|
||||
WINDOWS = os.name == "nt"
|
||||
|
||||
|
||||
def python_failure(lines: list[str]) -> str | None:
|
||||
"""The last line of a Python traceback in setup.py's output (ModuleNotFoundError: No module named 'calibrate'), or None when the
|
||||
output holds no traceback. Frames are skipped; the exception line is the first unindented line after the last 'Traceback'."""
|
||||
for i in range(len(lines) - 1, -1, -1):
|
||||
if lines[i].startswith("Traceback (most recent call last)"):
|
||||
for line in lines[i + 1:]:
|
||||
if line.strip() and not line[:1].isspace():
|
||||
return line.strip()[:300]
|
||||
return None
|
||||
return None
|
||||
|
||||
|
||||
def missing_file(failure: str) -> bool:
|
||||
"""An import or file error: the install itself is short of a file, which an update of Strata brings back."""
|
||||
return failure.startswith(("ModuleNotFoundError", "ImportError", "FileNotFoundError"))
|
||||
|
||||
|
||||
class LaunchError(RuntimeError):
|
||||
"""A step could not be done. `text` says what it means; `next` says what to do."""
|
||||
|
||||
@@ -42,6 +59,23 @@ class LaunchError(RuntimeError):
|
||||
self.text, self.next, self.code = text, next, code
|
||||
|
||||
|
||||
def setup_failure(lines: list[str], err: str, hint: str, details: str | None, own_files: bool) -> LaunchError:
|
||||
"""The error for a setup.py run that exited non-zero. setup.py's own `[X]` line (err) is the message when there is one; when it
|
||||
stopped with a Python error instead, that error's last line is (#3613, #3467: a file missing from the Terminal's tools/ ended every
|
||||
launch after a tune with only "The model could not be installed"). `details` is the file that holds the rest of its output."""
|
||||
crashed = python_failure(lines) if not err else None
|
||||
if crashed:
|
||||
where = f" The whole setup output is in {details}." if details else ""
|
||||
if missing_file(crashed):
|
||||
return LaunchError(f"The model could not be installed. A file that setup needs is missing from this Strata install ({crashed}).",
|
||||
"Update Strata (`strata update`) and run it again." + where, 6)
|
||||
return LaunchError(f"The model could not be installed. Setup stopped with an error ({crashed}).", "Run it again." + where, 6)
|
||||
return LaunchError("The model could not be installed." + (f" {err}." if err else ""),
|
||||
(hint or ("Run it again: finished steps are skipped." if own_files else
|
||||
"Run it again: finished steps are skipped and a download continues where it stopped."))
|
||||
+ (f" Details: {details}" if details else ""), 6)
|
||||
|
||||
|
||||
class Cancelled(LaunchError):
|
||||
def __init__(self):
|
||||
super().__init__("Stopped. Nothing was changed on your computer beyond files already downloaded.", "", 130)
|
||||
@@ -810,9 +844,7 @@ class Runner:
|
||||
adopted = self.adopt_prepared_files(err)
|
||||
if adopted is not None:
|
||||
return adopted
|
||||
raise LaunchError("The model could not be installed." + (f" {err}." if err else ""),
|
||||
hint or ("Run it again: finished steps are skipped." if s.model_path else
|
||||
"Run it again: finished steps are skipped and a download continues where it stopped."), 6)
|
||||
raise setup_failure(lines, err, hint, self.keep_setup_output(lines), bool(s.model_path))
|
||||
path, _desc = self.installed_state()
|
||||
if path is None:
|
||||
raise LaunchError("Setup finished but no run config was written for this model.",
|
||||
@@ -821,6 +853,18 @@ class Runner:
|
||||
self.check_pictures(path)
|
||||
return path
|
||||
|
||||
def keep_setup_output(self, lines: list[str]) -> str | None:
|
||||
"""The last lines setup.py printed, kept next to the launch's other logs so a failed install can be read afterwards (the message
|
||||
shows one line; a traceback needs the rest). Best effort: None when it cannot be written."""
|
||||
try:
|
||||
folder = launch_dir()
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
path = folder / f"{self.name}.setup.log"
|
||||
path.write_text("\n".join(lines[-400:]) + "\n", encoding="utf-8", errors="replace")
|
||||
return str(path)
|
||||
except (OSError, ValueError):
|
||||
return None
|
||||
|
||||
def adopt_prepared_files(self, setup_error: str) -> Path | None:
|
||||
"""The installer cannot set this graphics card up (Intel Arc, for one): if the model's files and prepared pack are already on disk,
|
||||
write the run config here (strata_cli/launch/adopt.py); else say what is missing."""
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
"""The Terminal / Control bundle holds every tools/ module that setup.py can import (#3613, #3467).
|
||||
|
||||
0.1.47 shipped without tools/calibrate.py: setup.py imports it after a tune, so every launch after a tune ended in
|
||||
ModuleNotFoundError. The bundle is staged here the way packaging/package_terminal.py stages it (the payload, the read-only tools,
|
||||
then add_installer), and setup.py's tool imports are resolved against tools/ as the bundle has it, not against the source tree.
|
||||
"""
|
||||
|
||||
import ast
|
||||
import os
|
||||
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
|
||||
|
||||
|
||||
def imports_of(path: Path) -> set[str]:
|
||||
found: set[str] = set()
|
||||
for node in ast.walk(ast.parse(path.read_text(encoding="utf-8-sig"))):
|
||||
if isinstance(node, ast.Import):
|
||||
found.update(a.name.split(".")[0] for a in node.names)
|
||||
elif isinstance(node, ast.ImportFrom) and node.module and node.level == 0:
|
||||
found.add(node.module.split(".")[0])
|
||||
return found
|
||||
|
||||
|
||||
class BundleImports(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.temp = tempfile.TemporaryDirectory(prefix="strata-bundle-")
|
||||
cls.root = Path(cls.temp.name) / "strata-terminal"
|
||||
cls.root.mkdir()
|
||||
ignore = shutil.ignore_patterns("__pycache__", "*.pyc")
|
||||
for item in pt.PAYLOAD:
|
||||
src, dst = ROOT / item, cls.root / item
|
||||
dst.parent.mkdir(parents=True, exist_ok=True)
|
||||
if src.is_dir():
|
||||
shutil.copytree(src, dst, ignore=ignore)
|
||||
else:
|
||||
shutil.copy2(src, dst)
|
||||
(cls.root / "tools").mkdir(exist_ok=True)
|
||||
for item in pt.TOOLS_SUBSET:
|
||||
shutil.copy2(ROOT / "tools" / item, cls.root / "tools" / item)
|
||||
cls.installer = pt.add_installer(cls.root, ROOT)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.temp.cleanup()
|
||||
|
||||
def test_calibrate_is_in_the_bundle(self):
|
||||
# the exact file #3613 and #3467 lacked
|
||||
self.assertTrue((self.root / "tools" / "calibrate.py").is_file())
|
||||
self.assertIn("calibrate", pt.INSTALLER_TOOLS)
|
||||
|
||||
def test_every_tools_module_setup_py_imports_is_in_the_bundle(self):
|
||||
in_bundle = {p.stem for p in (self.root / "tools").glob("*.py")}
|
||||
in_source = {p.stem for p in (ROOT / "tools").glob("*.py")}
|
||||
wanted = imports_of(self.root / "setup.py") & in_source
|
||||
self.assertIn("calibrate", wanted, "setup.py no longer imports calibrate: update this test")
|
||||
self.assertEqual(sorted(wanted - in_bundle), [], "setup.py imports tools/ modules that the bundle does not carry")
|
||||
|
||||
def test_the_bundled_tools_import_nothing_the_bundle_lacks(self):
|
||||
in_bundle = {p.stem for p in (self.root / "tools").glob("*.py")}
|
||||
in_source = {p.stem for p in (ROOT / "tools").glob("*.py")}
|
||||
for module in sorted(in_bundle):
|
||||
missing = sorted((imports_of(self.root / "tools" / f"{module}.py") & in_source) - in_bundle)
|
||||
self.assertEqual(missing, [], f"tools/{module}.py imports tools/ modules the bundle does not carry")
|
||||
|
||||
def test_setup_py_and_each_of_its_tools_import_with_tools_limited_to_the_bundle(self):
|
||||
modules = sorted(set(pt.setup_tool_imports(self.root)) | set(pt.INSTALLER_TOOLS))
|
||||
code = ("import sys, os, importlib\n"
|
||||
f"sys.path[:0] = [{str(self.root)!r}, {str(self.root / 'tools')!r}]\n"
|
||||
"import setup\n"
|
||||
f"for name in {modules!r}:\n"
|
||||
" module = importlib.import_module(name)\n"
|
||||
f" assert os.path.samefile(os.path.dirname(module.__file__), {str(self.root / 'tools')!r}), (name, module.__file__)\n"
|
||||
"print('imports ok')\n")
|
||||
env = {**os.environ, "STRATA_CONTROL_INSTALL": "1", "PYTHONNOUSERSITE": "1"}
|
||||
for key in ("PYTHONPATH", "PYTHONHOME"):
|
||||
env.pop(key, None)
|
||||
done = subprocess.run([sys.executable, "-I", "-c", code], cwd=str(self.root.parent), env=env,
|
||||
capture_output=True, text=True, timeout=120)
|
||||
self.assertEqual(done.returncode, 0, (done.stdout or "") + (done.stderr or "")[-1500:])
|
||||
self.assertIn("imports ok", done.stdout)
|
||||
|
||||
def test_a_tool_setup_starts_to_import_is_added_without_editing_the_list(self):
|
||||
# setup_tool_imports reads setup.py, so INSTALLER_TOOLS cannot go stale the way it did for calibrate
|
||||
self.assertTrue(set(pt.setup_tool_imports(ROOT)) <= set(pt.local_imports(ROOT, tuple(sorted(
|
||||
set(pt.INSTALLER_TOOLS) | set(pt.setup_tool_imports(ROOT)))))))
|
||||
without = tuple(x for x in pt.INSTALLER_TOOLS if x != "calibrate")
|
||||
self.assertNotIn("calibrate", pt.local_imports(ROOT, without)) # the old list really lacked it
|
||||
self.assertIn("calibrate", pt.setup_tool_imports(ROOT)) # and the scan finds it anyway
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,72 @@
|
||||
"""When setup.py stops with a Python error, `strata launch` says what it was and where the rest of the output is (#3613, #3467)."""
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from strata_cli.launch import runner
|
||||
from strata_cli.launch.runner import LaunchError, Runner, missing_file, python_failure, setup_failure
|
||||
|
||||
TRACEBACK = [
|
||||
" [..] Tuning Strata for this PC",
|
||||
"Traceback (most recent call last):",
|
||||
' File "C:\\Strata\\setup.py", line 6414, in calibrate_config',
|
||||
" import calibrate as CAL",
|
||||
"ModuleNotFoundError: No module named 'calibrate'",
|
||||
]
|
||||
|
||||
|
||||
class SetupFailureWords(unittest.TestCase):
|
||||
def test_the_last_line_of_a_traceback_is_found(self):
|
||||
self.assertEqual(python_failure(TRACEBACK), "ModuleNotFoundError: No module named 'calibrate'")
|
||||
self.assertIsNone(python_failure(["[X] Not enough disk space", " Free some space"]))
|
||||
self.assertIsNone(python_failure(["Traceback (most recent call last):", ' File "x.py", line 1, in <module>']))
|
||||
# the last traceback wins, frames and blank lines are skipped
|
||||
two = TRACEBACK[:1] + ["Traceback (most recent call last):", " frame", "ValueError: old"] + TRACEBACK[1:]
|
||||
self.assertEqual(python_failure(two), "ModuleNotFoundError: No module named 'calibrate'")
|
||||
|
||||
def test_a_missing_module_is_said_as_a_missing_file_with_the_next_step_and_the_output_path(self):
|
||||
error = setup_failure(TRACEBACK, "", "", r"C:\Users\x\launch\a.setup.log", False)
|
||||
self.assertIsInstance(error, LaunchError)
|
||||
self.assertIn("The model could not be installed.", error.text)
|
||||
self.assertIn("missing from this Strata install", error.text)
|
||||
self.assertIn("No module named 'calibrate'", error.text)
|
||||
self.assertIn("strata update", error.next)
|
||||
self.assertIn(r"C:\Users\x\launch\a.setup.log", error.next)
|
||||
|
||||
def test_another_python_error_keeps_its_line_and_the_path(self):
|
||||
error = setup_failure(["Traceback (most recent call last):", " f", "KeyError: 'cuda'"], "", "", "/tmp/a.setup.log", True)
|
||||
self.assertIn("KeyError: 'cuda'", error.text)
|
||||
self.assertNotIn("missing", error.text)
|
||||
self.assertIn("/tmp/a.setup.log", error.next)
|
||||
|
||||
def test_a_setup_error_line_still_wins_and_gets_the_details_path(self):
|
||||
error = setup_failure(TRACEBACK, "Not enough disk space", "Free some space.", "/tmp/a.setup.log", False)
|
||||
self.assertEqual(error.text, "The model could not be installed. Not enough disk space.")
|
||||
self.assertEqual(error.next, "Free some space. Details: /tmp/a.setup.log")
|
||||
plain = setup_failure([], "", "", None, False)
|
||||
self.assertEqual(plain.text, "The model could not be installed.")
|
||||
self.assertIn("a download continues where it stopped", plain.next)
|
||||
self.assertNotIn("Details", plain.next)
|
||||
|
||||
def test_missing_file_kinds(self):
|
||||
self.assertTrue(missing_file("ModuleNotFoundError: No module named 'x'"))
|
||||
self.assertTrue(missing_file("ImportError: cannot import name 'y'"))
|
||||
self.assertFalse(missing_file("ValueError: bad"))
|
||||
|
||||
def test_the_setup_output_is_kept_in_the_launch_folder(self):
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
run = Runner.__new__(Runner)
|
||||
with mock.patch.object(Runner, "name", new_callable=mock.PropertyMock, return_value="gyro-s-32k"), mock.patch.object(runner, "launch_dir", return_value=Path(folder)):
|
||||
kept = run.keep_setup_output([f"line {i}" for i in range(1000)] + TRACEBACK)
|
||||
self.assertEqual(Path(kept), Path(folder) / "gyro-s-32k.setup.log")
|
||||
text = Path(kept).read_text(encoding="utf-8")
|
||||
self.assertIn("ModuleNotFoundError: No module named 'calibrate'", text)
|
||||
self.assertEqual(len(text.splitlines()), 400) # the tail, not a runaway file
|
||||
with mock.patch.object(Runner, "name", new_callable=mock.PropertyMock, return_value="gyro-s-32k"), mock.patch.object(runner, "launch_dir", side_effect=OSError("read-only")):
|
||||
self.assertIsNone(run.keep_setup_output(["x"]))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user