Spaces:
Sleeping

File size: 1,329 Bytes
526950e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# scripts/lib/local_state.py
"""Read/write .me-hub/local-state.json — per-machine, gitignored."""
from __future__ import annotations
import json
from datetime import datetime, timezone
from pathlib import Path
from typing import Optional

from lib.paths import repo_root


def _default_local_state_path() -> Path:
    return repo_root() / ".me-hub" / "local-state.json"


_LOCAL_STATE_PATH_FN = _default_local_state_path


SCHEMA_VERSION = 1


def write(data: dict) -> None:
    """Write the local-state file. Adds schema_version."""
    path = _LOCAL_STATE_PATH_FN()
    path.parent.mkdir(parents=True, exist_ok=True)
    payload = {"schema_version": SCHEMA_VERSION, **data}
    path.write_text(json.dumps(payload, indent=2, sort_keys=True), encoding="utf-8")


def read() -> Optional[dict]:
    """Return the parsed local-state dict, or None if the file is missing."""
    path = _LOCAL_STATE_PATH_FN()
    if not path.is_file():
        return None
    return json.loads(path.read_text(encoding="utf-8"))


def update_last_refresh(state_commit: str) -> None:
    """Stamp the current UTC time and the source-state commit we synced against."""
    cur = read() or {}
    cur["last_local_refresh_utc"] = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
    cur["last_seen_state_commit"] = state_commit
    write(cur)