7c9d33f952
state.py: per-instance sync state. <pack>/.cloud-sync/state.json
(mode 600) records last_pulled_snapshot_id + last_pulled_at +
host_tag. Versioned schema. clear() on remote-empty.
sync.pull decision tree (replaces the unconditional restore):
no token file
→ prompt_login_qt; on Skip return 0 (don't block launch)
no state + remote empty
→ no-op
no state + remote non-empty
→ restore (first-run on this machine)
state.id == remote.id
→ skip restore (up to date)
state.id != remote.id, no in-scope local edits since state.at
→ restore (fast-forward)
state.id != remote.id, in-scope local edits since state.at
→ prompt_conflict_qt
keep_local → don't restore; push will overwrite cloud
use_remote → restore + update state
cancel → exit 1
sync.push: --json output parsed for snapshot_id; state.json updated
to that id after a successful backup. Skips silently if no token.
_find_modified_in_scope: walks include roots, filters via
_matches_any (restic-style globs: dir/, **/dir/, **/*.glob).
Stops at 50 hits; we only need 'any' + a sample for the dialog.
_format_dt: hand-rolled (no GNU-vs-Windows strftime quirks) →
'Thursday, October 21, 2021 at 7:12 PM'.
Restic JSON parsing helpers: _parse_snapshots, _parse_restic_time
(handles nanosecond precision), _parse_backup_summary.
tests/test_state.py: 19 new tests covering state read/write, scope-
aware mtime walk, exclude glob matching, restic output parsers.
Total: 52 green.
85 lines
2.5 KiB
Python
85 lines
2.5 KiB
Python
"""Per-instance sync state.
|
|
|
|
Tracks the snapshot id this pack was last synced to and when. Lives at
|
|
``<pack-folder>/.cloud-sync/state.json`` (mode 600).
|
|
|
|
Purpose: divergence detection. On ``pull``, if the remote latest id
|
|
differs from ``last_pulled_snapshot_id`` AND any in-scope local file
|
|
has mtime > ``last_pulled_at``, the local and remote diverged from a
|
|
common ancestor — surface the conflict dialog.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from dataclasses import dataclass
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
|
|
|
|
SCHEMA_VERSION = 1
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class State:
|
|
last_pulled_snapshot_id: str
|
|
last_pulled_at: datetime
|
|
host_tag: str = "cloud-sync"
|
|
|
|
|
|
def state_path(pack_folder: Path) -> Path:
|
|
return pack_folder / ".cloud-sync" / "state.json"
|
|
|
|
|
|
def read(pack_folder: Path) -> State | None:
|
|
"""Return parsed state or None if file missing / unreadable / wrong schema."""
|
|
p = state_path(pack_folder)
|
|
if not p.exists():
|
|
return None
|
|
try:
|
|
data = json.loads(p.read_text(encoding="utf-8"))
|
|
except (OSError, json.JSONDecodeError):
|
|
return None
|
|
if data.get("schema") != SCHEMA_VERSION:
|
|
return None
|
|
try:
|
|
return State(
|
|
last_pulled_snapshot_id=data["last_pulled_snapshot_id"],
|
|
last_pulled_at=_parse_iso(data["last_pulled_at"]),
|
|
host_tag=data.get("host_tag", "cloud-sync"),
|
|
)
|
|
except (KeyError, ValueError):
|
|
return None
|
|
|
|
|
|
def write(pack_folder: Path, state: State) -> None:
|
|
"""Persist state. Creates parent dir + sets mode 600."""
|
|
p = state_path(pack_folder)
|
|
p.parent.mkdir(parents=True, exist_ok=True)
|
|
payload = {
|
|
"schema": SCHEMA_VERSION,
|
|
"last_pulled_snapshot_id": state.last_pulled_snapshot_id,
|
|
"last_pulled_at": state.last_pulled_at.astimezone(timezone.utc)
|
|
.isoformat()
|
|
.replace("+00:00", "Z"),
|
|
"host_tag": state.host_tag,
|
|
}
|
|
p.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8")
|
|
p.chmod(0o600)
|
|
|
|
|
|
def clear(pack_folder: Path) -> None:
|
|
"""Remove state.json if present. Used when remote has zero snapshots."""
|
|
p = state_path(pack_folder)
|
|
p.unlink(missing_ok=True)
|
|
|
|
|
|
def _parse_iso(s: str) -> datetime:
|
|
"""Parse ISO-8601 with trailing Z or +HH:MM, return tz-aware UTC."""
|
|
if s.endswith("Z"):
|
|
s = s[:-1] + "+00:00"
|
|
dt = datetime.fromisoformat(s)
|
|
if dt.tzinfo is None:
|
|
dt = dt.replace(tzinfo=timezone.utc)
|
|
return dt.astimezone(timezone.utc)
|