Skip to content
Merged

web #282

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions selfdrive/carrot/server/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
ROOT_DIR = os.path.dirname(BASE_DIR)

DEFAULT_SETTINGS_PATH = "/data/openpilot/selfdrive/carrot_settings.json"
CARROT_DATA_DIR = "/data/openpilot/selfdrive/carrot/data"
CARROT_STATE_DIR = os.path.join(CARROT_DATA_DIR, "state")
CARROT_GIT_STATE_PATH = os.path.join(CARROT_STATE_DIR, "git.json")

WEB_DIR = os.path.join(ROOT_DIR, "web")
CSS_DIR = os.path.join(WEB_DIR, "css")
Expand Down Expand Up @@ -442,7 +445,61 @@ def _clamp_numeric(value: float, p: Optional[Dict[str, Any]]) -> float:
pass
return value

def _read_git_state() -> Dict[str, Any]:
try:
with open(CARROT_GIT_STATE_PATH, "r", encoding="utf-8") as f:
data = json.load(f)
return data if isinstance(data, dict) else {}
except Exception:
return {}

def _write_git_state(data: Dict[str, Any]) -> None:
try:
os.makedirs(CARROT_STATE_DIR, exist_ok=True)
tmp_path = f"{CARROT_GIT_STATE_PATH}.tmp"
with open(tmp_path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=True, separators=(",", ":"))
os.replace(tmp_path, CARROT_GIT_STATE_PATH)
except Exception:
pass

def _read_custom_meta_value(name: str) -> Optional[str]:
if name != "GitPullTime":
return None

try:
value = _read_git_state().get("git_pull_time")
if value is None:
return None
return str(value).strip()
except Exception:
return None

def _write_git_pull_time(ts: Optional[int] = None) -> None:
value = int(ts if ts is not None else time.time())
data = _read_git_state()
data["git_pull_time"] = value
data["git_pull_ok"] = True
_write_git_state(data)

def _did_git_pull_update(output: str) -> bool:
body = str(output or "").strip().lower()
if not body:
return False
if "already up to date" in body or "already up-to-date" in body:
return False
return (
"fast-forward" in body or
"merge made by" in body or
"updating " in body or
bool(re.search(r"[0-9]+\s+files?\s+changed", body))
)

def _get_param_value(name: str, default: Any) -> Any:
custom_value = _read_custom_meta_value(name)
if custom_value is not None:
return custom_value

if not HAS_PARAMS:
# mem store (string) fallback
s = _mem_store.get(name, None)
Expand Down Expand Up @@ -881,6 +938,8 @@ async def _run_tool_job(job: Dict[str, Any]) -> None:
if action == "git_pull":
_tool_job_progress(job, message="git pull", current=1, total=1)
rc = await _tool_stream_exec(job, ["git", "pull"], cwd=repo_dir, timeout=180)
if rc == 0 and _did_git_pull_update(job.get("log") or ""):
_write_git_pull_time()
result = _tool_result_from_log(job, rc)
_tool_job_finish(job, ok=rc == 0, result=result)
return
Expand Down Expand Up @@ -1298,6 +1357,8 @@ def run(cmd: List[str], cwd: Optional[str] = None) -> Tuple[int, str]:

if action == "git_pull":
rc, out = run(["git", "pull"], cwd=REPO_DIR)
if rc == 0 and _did_git_pull_update(out):
_write_git_pull_time()
return web.json_response({"ok": rc == 0, "rc": rc, "out": out})

if action == "git_sync":
Expand Down
Loading
Loading