Resolve the workspace root from the injected runtime context.
| 36 | |
| 37 | |
| 38 | class WorkspaceCommandContext: |
| 39 | """Resolve the workspace root from the injected runtime context.""" |
| 40 | |
| 41 | def __init__(self, ctx: Dict[str, Any] | None): |
| 42 | if ctx is None: |
| 43 | raise ValueError("_context is required for uv tools") |
| 44 | self.workspace_root = self._require_workspace(ctx.get("python_workspace_root")) |
| 45 | self._raw_ctx = ctx |
| 46 | |
| 47 | @staticmethod |
| 48 | def _require_workspace(raw_path: Any) -> Path: |
| 49 | if raw_path is None: |
| 50 | raise ValueError("python_workspace_root missing from _context") |
| 51 | path = Path(raw_path).expanduser().resolve() |
| 52 | path.mkdir(parents=True, exist_ok=True) |
| 53 | return path |
| 54 | |
| 55 | def resolve_under_workspace(self, relative_path: str | Path) -> Path: |
| 56 | candidate = Path(relative_path) |
| 57 | absolute = candidate if candidate.is_absolute() else self.workspace_root / candidate |
| 58 | absolute = absolute.expanduser().resolve() |
| 59 | if self.workspace_root not in absolute.parents and absolute != self.workspace_root: |
| 60 | raise ValueError("script path is outside workspace root") |
| 61 | return absolute |
| 62 | |
| 63 | |
| 64 | def _validate_packages(packages: Sequence[str]) -> List[str]: |
no outgoing calls
no test coverage detected