"""Architecture guard for the package-layout refactor. The backend's 37 top-level modules moved under ``clipwright/``. These tests pin the invariants that made the move worth doing: 1. the repo root holds no importable ``*.py`` module any more; 2. every package ``__init__`` is a pure docstring (no code, no re-exports); 3. nothing under ``clipwright/`` or ``tests/`` still imports a module by its old bare top-level name. Failure messages name the offending files. """ import ast from pathlib import Path REPO_ROOT = Path(__file__).resolve().parents[1] PACKAGE = REPO_ROOT / "clipwright" TESTS = REPO_ROOT / "tests" # The old top-level module names. ``from clipwright. import `` is # the supported spelling; importing any of these bare names is a regression. LEGACY_MODULES = frozenset({ "active_speaker", "app", "camera_inset", "clip_selection", "edit_builder", "editor", "ffmpeg_utils", "file_hosts", "frame_sampler", "free_tools", "gemini_worker", "hook_grounding", "hooks", "layout_picker", "layout_ranges", "llm_backend", "main", "media_auth", "postiz", "punch_in", "quality_probe", "recut", "reframe_v2", "restoring_static", "scene_detection", "screencast_layout", "security_utils", "split_layout", "subtitles", "thumbnail", "transcribe_backends", "verify_aesthetic", "verify_custom_hook", "verify_hooks", "watermarked", "yt_clients", }) def _legacy_imports(path): """The legacy bare module names imported by the source at ``path``.""" tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path)) found = set() for node in ast.walk(tree): if isinstance(node, ast.Import): for alias in node.names: root = alias.name.split(".")[0] if root in LEGACY_MODULES: found.add(root) elif isinstance(node, ast.ImportFrom): # A relative import inside the package (``from . import x``) is # not a bare top-level import; only absolute ones can be legacy. if node.level: continue root = (node.module or "").split(".")[0] if root in LEGACY_MODULES: found.add(root) return found def test_repo_root_has_no_python_modules(): offenders = sorted(p.name for p in REPO_ROOT.glob("*.py")) assert not offenders, ( "stray top-level modules at the repo root (move them under clipwright/): " + ", ".join(offenders) ) def test_package_inits_are_docstring_only(): offenders = [] for init in sorted(PACKAGE.rglob("__init__.py")): tree = ast.parse(init.read_text(encoding="utf-8"), filename=str(init)) for node in tree.body: is_docstring = ( isinstance(node, ast.Expr) and isinstance(node.value, ast.Constant) and isinstance(node.value.value, str) ) if not is_docstring: offenders.append(f"{init.relative_to(REPO_ROOT)}:{node.lineno}") assert not offenders, ( "package __init__ files must contain only a docstring: " + ", ".join(offenders) ) def test_no_legacy_bare_module_imports(): offenders = {} sources = sorted(PACKAGE.rglob("*.py")) + sorted(TESTS.glob("*.py")) for path in sources: found = _legacy_imports(path) if found: offenders[str(path.relative_to(REPO_ROOT))] = sorted(found) assert not offenders, ( "legacy bare module imports (use clipwright. instead): " + ", ".join( f"{name} -> {mods}" for name, mods in sorted(offenders.items()) ) )