"""The versioned layout shared by run writers and review readers.""" import json from collections.abc import Mapping from pathlib import Path RUN_SCHEMA = 'np-run/2' LAYOUT_VERSION = 2 IMPORTS_DIR = 'review-imports' class ReviewLayoutError(ValueError): code = 'REVIEW_IMPORT_LAYOUT' def imports_root(run_dir: Path, run: Mapping | None = None) -> Path: root = Path(run_dir).resolve() message = f'运行 {root.name} 布局无效;仅支持现行布局,请用当前管线重新生成。' try: if run is None: run = json.loads((root / 'run.json').read_text(encoding='utf-8')) target = root / IMPORTS_DIR legacy = root / 'screen3' if (not isinstance(run, Mapping) or run.get('schema') != RUN_SCHEMA or type(run.get('layout_version')) is not int or run['layout_version'] != LAYOUT_VERSION or run.get('review_imports') != IMPORTS_DIR or run.get('id') != root.name or legacy.exists() or legacy.is_symlink() or not target.resolve().is_relative_to(root) or target.exists() and not target.is_dir()): raise ReviewLayoutError(message) return target except (OSError, ValueError) as exc: if isinstance(exc, ReviewLayoutError): raise raise ReviewLayoutError(message) from exc