paths.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from __future__ import annotations
  2. from pathlib import Path
  3. # Package location: supply_agent/paths.py → project root is parent of supply_agent/
  4. _PACKAGE_DIR = Path(__file__).resolve().parent
  5. _DEFAULT_PROJECT_ROOT = _PACKAGE_DIR.parent
  6. def find_project_root(start: Path | None = None) -> Path:
  7. """
  8. Find the project root directory.
  9. Walks upward looking for pyproject.toml or a skills/ directory.
  10. Falls back to the package's parent directory.
  11. """
  12. current = (start or Path.cwd()).resolve()
  13. for directory in [current, *current.parents]:
  14. if (directory / "pyproject.toml").exists():
  15. return directory
  16. if (directory / "skills").is_dir():
  17. return directory
  18. return _DEFAULT_PROJECT_ROOT
  19. def resolve_path(path: Path | str, *, project_root: Path | None = None) -> Path:
  20. """
  21. Resolve a path relative to CWD or project root.
  22. Priority:
  23. 1. Absolute paths — used as-is
  24. 2. Relative path exists under CWD — use CWD-relative
  25. 3. Otherwise — resolve against project root
  26. """
  27. resolved = Path(path)
  28. if resolved.is_absolute():
  29. return resolved
  30. root = project_root or find_project_root()
  31. cwd_candidate = (Path.cwd() / resolved).resolve()
  32. if cwd_candidate.exists():
  33. return cwd_candidate
  34. return (root / resolved).resolve()