Files
caption-test/src/utils/path_utils.py

33 lines
981 B
Python

"""Project root resolution via marker files."""
from __future__ import annotations
from pathlib import Path
# Markers identifying the project root (per REQUIREMENTS_GIN_STYLE.md §5).
_MARKERS: tuple[str, ...] = ("pyproject.toml", ".git", "in")
def get_proj_dir() -> str:
"""Return absolute project root with trailing slash.
Walks up from this file's directory until finding pyproject.toml,
.git, or in/. Searches up to 10 levels.
Returns:
Project root path with trailing slash, e.g. '/home/user/caption-test/'.
Raises:
RuntimeError: If no marker found within 10 parent directories.
"""
current = Path(__file__).resolve().parent
for _ in range(10):
if any((current / m).exists() for m in _MARKERS):
return str(current) + "/"
current = current.parent
raise RuntimeError(
f"Project root not found. Looked for {_MARKERS} starting at "
f"{Path(__file__).resolve().parent}",
)