INNER CODE UNIT · Python
_raw_target
simonlin1212/Vibe-Research · calc/cli.py:131
def _raw_target(rel: str, run_dir: str | None, kind: str) -> Path:
"""raw_ref → 运行目录 raw/ 内的真实文件(相对路径 / realpath / 非符号链接越界 / 普通文件)。"""
if not run_dir:
raise ValueError(f"{kind} 需要 --run-dir")
if not rel:
raise ValueError(f"{kind} 需要 raw_ref")
if Path(rel).is_absolute():
raise ValueError(f"{kind}.raw_ref 必须是运行目录内的相对路径(raw/...),不接受绝对路径:{rel!r}")
# Path.parts erases '.' and duplicate separators before they can be checked.
# Reject ambiguous spelling in both loaders rather than minting another ID.
if any(part in ("..", ".", "") for part in rel.split("/")) or "\\" in rel or not rel.startswith("raw/"):
raise ValueError(f"{kind}.raw_ref 必须使用规范相对路径 raw/<文件>,不得含 ..、.、重复或反向分隔符:{rel!r}")
raw_root = Path(run_dir).resolve(strict=True) / "raw"
target = (Path(run_dir) / rel).resolve(strict=True)
try:
target.relative_to(raw_root.resolve(strict=True))
except ValueError as e:
raise ValueError(f"{kind}.raw_ref 越出运行目录的 raw/:{rel!r}") from e