37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
"""Git repository location resolver with validation.
|
|
|
|
Prevents hard-coded repository names from silently pointing to stale paths.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
|
|
class GitRepoLocator:
|
|
def __init__(self, root: str | None = None):
|
|
self.root = Path(root or os.getenv("AIOS_GITOPS_ROOT", "/data/aios-gitops/runtime/gitea-bin/data/repositories"))
|
|
|
|
def candidates(self, owner: str = "aios", names: tuple[str, ...] = ("aios.git", "aios-v23.git")):
|
|
return [self.root / owner / name for name in names]
|
|
|
|
def locate(self, owner: str = "aios") -> dict:
|
|
found = [str(path) for path in self.candidates(owner) if path.exists()]
|
|
return {
|
|
"root": str(self.root),
|
|
"owner": owner,
|
|
"found": found,
|
|
"healthy": len(found) == 1,
|
|
"warning": "multiple repositories found" if len(found) > 1 else None,
|
|
}
|
|
|
|
def require(self, owner: str = "aios") -> Path:
|
|
result = self.locate(owner)
|
|
if not result["healthy"]:
|
|
raise RuntimeError(f"Git repo resolution failed: {result}")
|
|
return Path(result["found"][0])
|
|
|
|
|
|
__all__ = ["GitRepoLocator"]
|