mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-28 00:29:09 +01:00
Compare commits
2 Commits
e6812f7b4f
...
0976c84254
| Author | SHA1 | Date | |
|---|---|---|---|
| 0976c84254 | |||
| be94b4e147 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -14,3 +14,6 @@ coverage.lcov
|
||||
htmlcov/
|
||||
*.log
|
||||
.DS_Store
|
||||
|
||||
# ci_mirror.sh cached requirements.txt venv (rebuilt on demand)
|
||||
.ci-mirror-venv/
|
||||
|
||||
@ -42,6 +42,12 @@ repos:
|
||||
entry: '(?i)#\s*ruff:\s*noqa'
|
||||
language: pygrep
|
||||
types: [python]
|
||||
- id: requirements-sync
|
||||
name: requirements.txt covers all package imports
|
||||
entry: python3 scripts/check_requirements_sync.py
|
||||
language: system
|
||||
pass_filenames: false
|
||||
files: ^(diet_guard/.*\.py|requirements\.txt)$
|
||||
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
rev: v0.15.2
|
||||
@ -115,12 +121,12 @@ repos:
|
||||
|
||||
- repo: local
|
||||
hooks:
|
||||
- id: pytest-coverage
|
||||
name: pytest with coverage enforcement
|
||||
entry: python -m pytest
|
||||
- id: ci-mirror
|
||||
name: CI mirror (clean-venv pytest + pre-commit --all-files)
|
||||
entry: bash scripts/ci_mirror.sh
|
||||
language: system
|
||||
types: [python]
|
||||
pass_filenames: false
|
||||
always_run: true
|
||||
require_serial: true
|
||||
stages: [pre-push]
|
||||
|
||||
|
||||
@ -13,17 +13,36 @@ from typing import TYPE_CHECKING
|
||||
from diet_guard._gate import gate_is_due
|
||||
from diet_guard._gatelock import MealGate, acquire_gate_lock, release_gate_lock
|
||||
from diet_guard._gatelock_support import wait_for_display
|
||||
from diet_guard._sync import pull_shared_log
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Callable
|
||||
|
||||
|
||||
def _is_due(emit: Callable[[str], None]) -> bool:
|
||||
"""Return the gate decision, refreshing from sync only when a lock is due.
|
||||
|
||||
The cheap local check runs first; the network pull happens solely on the
|
||||
rare tick a lock would otherwise fire (via :func:`pull_shared_log`, which
|
||||
fails closed), then the decision is re-read from the freshly written log.
|
||||
"""
|
||||
if not gate_is_due():
|
||||
return False
|
||||
reason = pull_shared_log() # only pay the network cost when about to lock
|
||||
if reason is not None:
|
||||
emit(f"{reason}; using local log.")
|
||||
return gate_is_due()
|
||||
|
||||
|
||||
def cmd_gate(emit: Callable[[str], None], *, check: bool, demo: bool) -> int:
|
||||
"""Run the log-to-unlock gate.
|
||||
|
||||
Three modes: ``--check`` is a headless decision (no window) whose exit code
|
||||
a timer reads; ``--demo`` always shows a safe demo window; bare ``gate``
|
||||
shows the real lock only when one is due. A flock guard stops a second
|
||||
shows the real lock only when one is due. Both real modes route through
|
||||
:func:`_is_due`, which -- only when a lock is otherwise due -- pulls the
|
||||
shared log first (see :func:`_refresh_from_sync`) so a meal just logged on
|
||||
the phone unlocks without a manual re-entry. A flock guard stops a second
|
||||
window from stacking on top of the first, and a window-opening mode first
|
||||
waits for the X display so a session-start launch never crashes unshown.
|
||||
|
||||
@ -37,10 +56,10 @@ def cmd_gate(emit: Callable[[str], None], *, check: bool, demo: bool) -> int:
|
||||
For ``--check``: 0 if not due, 1 if a lock is due. Otherwise 0.
|
||||
"""
|
||||
if check:
|
||||
due = gate_is_due()
|
||||
due = _is_due(emit)
|
||||
emit("due (a lock is warranted)" if due else "ok (no lock needed)")
|
||||
return 1 if due else 0
|
||||
if not demo and not gate_is_due():
|
||||
if not demo and not _is_due(emit):
|
||||
emit("ok - no lock needed right now.")
|
||||
return 0
|
||||
handle = acquire_gate_lock()
|
||||
|
||||
@ -155,6 +155,7 @@ class MealGate(_GateMealFlow):
|
||||
on_submit=self._on_submit,
|
||||
on_close=self.close,
|
||||
on_add_item=self._on_add_item,
|
||||
on_fetch_sync=self._on_fetch_sync,
|
||||
)
|
||||
self._widgets = build_layout(
|
||||
self.root,
|
||||
|
||||
@ -15,6 +15,7 @@ from typing import TYPE_CHECKING
|
||||
|
||||
from diet_guard._budget import BudgetError, daily_budget, protein_target_g
|
||||
from diet_guard._foodbank import remember_food, remember_meal
|
||||
from diet_guard._gate import due_slots
|
||||
from diet_guard._gatelock_nutrition import _GateNutrition
|
||||
from diet_guard._gatelock_ui import ERR, FG, UNIT_GRAMS
|
||||
from diet_guard._meal import MealItem, item_to_component, meal_total
|
||||
@ -27,6 +28,7 @@ from diet_guard._state import (
|
||||
today_total_kcal,
|
||||
today_total_macros,
|
||||
)
|
||||
from diet_guard._sync import pull_shared_log
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from diet_guard._estimator import Nutrition
|
||||
@ -257,6 +259,46 @@ class _GateMealFlow(_GateNutrition):
|
||||
self._set_status(f"{logged} — all meals logged, unlocking…")
|
||||
self.root.after(_UNLOCK_DELAY_MS, self.close)
|
||||
|
||||
# -- manual sync ------------------------------------------------------------
|
||||
|
||||
def _on_fetch_sync(self) -> None:
|
||||
"""Pull the shared log on demand and unlock any slots it now satisfies.
|
||||
|
||||
For a meal already logged on another device (typically the phone) that
|
||||
has not yet propagated here: rather than re-entering it to unlock, the
|
||||
user pulls it in. The pull blocks the window briefly (bounded by the
|
||||
sync timeout) and fails closed -- a failure leaves the lock untouched.
|
||||
"""
|
||||
if self.demo_mode:
|
||||
self._set_status("Fetch from sync is only available on the real lock.")
|
||||
return
|
||||
self._set_status("Fetching from sync…")
|
||||
self.root.update_idletasks()
|
||||
reason = pull_shared_log()
|
||||
if reason is not None:
|
||||
self._set_status(f"{reason} — still locked.", error=True)
|
||||
return
|
||||
self._reconcile_after_fetch()
|
||||
|
||||
def _reconcile_after_fetch(self) -> None:
|
||||
"""Drop pending slots a freshly pulled meal now covers; unlock if none."""
|
||||
still_due = set(due_slots())
|
||||
satisfied = [slot for slot in self._pending if slot not in still_due]
|
||||
self._refresh_dashboard()
|
||||
if not satisfied:
|
||||
self._set_status("No new meals found in sync.")
|
||||
return
|
||||
self._pending = [slot for slot in self._pending if slot in still_due]
|
||||
if not self._pending:
|
||||
self._unlock("Synced from another device")
|
||||
return
|
||||
self._clear_inputs()
|
||||
self._refresh_slot_header()
|
||||
count = len(satisfied)
|
||||
meal_word = "meal" if count == 1 else "meals"
|
||||
self._set_status(f"Pulled {count} {meal_word} — next meal, please.")
|
||||
self._widgets.desc_text.focus_set()
|
||||
|
||||
# -- dashboard --------------------------------------------------------------
|
||||
|
||||
def _refresh_dashboard(self) -> None:
|
||||
|
||||
@ -93,6 +93,7 @@ class GateCallbacks:
|
||||
on_submit: Callable[[], None]
|
||||
on_close: Callable[[], None]
|
||||
on_add_item: Callable[[], None]
|
||||
on_fetch_sync: Callable[[], None]
|
||||
|
||||
|
||||
def make_vars(root: tk.Misc) -> GateVars:
|
||||
@ -422,6 +423,19 @@ def build_layout(
|
||||
command=callbacks.on_submit,
|
||||
).pack(pady=(4, 6))
|
||||
|
||||
# Manual pull for a meal already logged on another device (the phone) but
|
||||
# not yet propagated to this machine -- saves re-typing it to unlock.
|
||||
tk.Button(
|
||||
frame,
|
||||
text="⟳ Fetch from sync",
|
||||
font=("Arial", 12),
|
||||
bg="#334455",
|
||||
fg="white",
|
||||
activebackground="#445566",
|
||||
cursor="hand2",
|
||||
command=callbacks.on_fetch_sync,
|
||||
).pack(pady=(0, 6))
|
||||
|
||||
status_label = tk.Label(
|
||||
frame,
|
||||
textvariable=vars_.status,
|
||||
|
||||
@ -13,6 +13,7 @@ from __future__ import annotations
|
||||
from datetime import datetime, timezone
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
from typing import TYPE_CHECKING
|
||||
import uuid
|
||||
|
||||
@ -89,10 +90,21 @@ def _read_raw_log() -> DayLog:
|
||||
|
||||
|
||||
def _write_log(log: DayLog) -> None:
|
||||
"""Persist the full log to disk, creating the data directory if needed."""
|
||||
"""Persist the full log to disk, creating the data directory if needed.
|
||||
|
||||
Written atomically -- a temp file in the same directory, then
|
||||
:func:`os.replace` -- so a concurrent reader (the gate now syncs while the
|
||||
15-min timer may also be writing) never sees a half-written file and
|
||||
mistakes a torn read for an empty log.
|
||||
"""
|
||||
FOOD_LOG_FILE.parent.mkdir(parents=True, exist_ok=True)
|
||||
with FOOD_LOG_FILE.open("w") as handle:
|
||||
json.dump(log, handle, indent=2)
|
||||
tmp_path = FOOD_LOG_FILE.with_name(f"{FOOD_LOG_FILE.name}.{os.getpid()}.tmp")
|
||||
try:
|
||||
with tmp_path.open("w") as handle:
|
||||
json.dump(log, handle, indent=2)
|
||||
tmp_path.replace(FOOD_LOG_FILE)
|
||||
finally:
|
||||
tmp_path.unlink(missing_ok=True)
|
||||
|
||||
|
||||
def _hmac_key_available() -> bool:
|
||||
|
||||
@ -130,3 +130,20 @@ def run_sync() -> DayLog:
|
||||
message="diet_guard sync",
|
||||
)
|
||||
return resigned
|
||||
|
||||
|
||||
def pull_shared_log() -> str | None:
|
||||
"""Run a sync tick, failing closed instead of raising.
|
||||
|
||||
A thin wrapper over :func:`run_sync` for callers that must never crash on a
|
||||
sync error: the gate's automatic pre-lock refresh and the lock screen's
|
||||
manual "Fetch from sync" button. Returns ``None`` on success, or a short
|
||||
human-readable reason when the pull could not complete (no token, network
|
||||
down, a disk error writing the merged log back), so the caller keeps its
|
||||
own lock decision rather than failing open.
|
||||
"""
|
||||
try:
|
||||
run_sync()
|
||||
except Exception as exc: # pylint: disable=broad-exception-caught
|
||||
return f"sync unavailable ({exc})"
|
||||
return None
|
||||
|
||||
@ -4,10 +4,19 @@ alongside its source module (see _cli_gate.py's module docstring).
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from diet_guard import _cli_gate
|
||||
from diet_guard._cli_gate import cmd_gate
|
||||
from diet_guard._cli_gate import _is_due, cmd_gate
|
||||
from diet_guard._estimator import Nutrition
|
||||
from diet_guard._gate import gate_is_due
|
||||
from diet_guard._state import log_meal
|
||||
|
||||
|
||||
def _no_sync() -> object:
|
||||
"""Neutralize the pre-lock pull (as if it found nothing) for mode tests."""
|
||||
return patch.object(_cli_gate, "pull_shared_log", return_value=None)
|
||||
|
||||
|
||||
class TestCmdGate:
|
||||
@ -16,7 +25,7 @@ class TestCmdGate:
|
||||
def test_check_due(self) -> None:
|
||||
"""--check exits 1 and announces a due lock."""
|
||||
lines: list[str] = []
|
||||
with patch.object(_cli_gate, "gate_is_due", return_value=True):
|
||||
with patch.object(_cli_gate, "gate_is_due", return_value=True), _no_sync():
|
||||
assert cmd_gate(lines.append, check=True, demo=False) == 1
|
||||
assert "due" in lines[0]
|
||||
|
||||
@ -46,10 +55,11 @@ class TestCmdGate:
|
||||
assert "no lock needed" in lines[0]
|
||||
|
||||
def test_bare_gate_due_opens_window(self) -> None:
|
||||
"""A bare gate that is due opens the real window."""
|
||||
"""A bare gate that is still due after a sync opens the real window."""
|
||||
gate = MagicMock()
|
||||
with (
|
||||
patch.object(_cli_gate, "gate_is_due", return_value=True),
|
||||
_no_sync(),
|
||||
patch.object(_cli_gate, "MealGate", return_value=gate),
|
||||
patch.object(_cli_gate, "acquire_gate_lock", return_value=MagicMock()),
|
||||
patch.object(_cli_gate, "release_gate_lock"),
|
||||
@ -58,11 +68,24 @@ class TestCmdGate:
|
||||
assert cmd_gate([].append, check=False, demo=False) == 0
|
||||
gate.run.assert_called_once()
|
||||
|
||||
def test_bare_gate_cleared_by_sync_opens_no_window(self) -> None:
|
||||
"""A meal pulled in by the pre-lock sync clears the lock: no window."""
|
||||
lines: list[str] = []
|
||||
with (
|
||||
patch.object(_cli_gate, "gate_is_due", side_effect=[True, False]),
|
||||
patch.object(_cli_gate, "pull_shared_log", return_value=None),
|
||||
patch.object(_cli_gate, "MealGate") as factory,
|
||||
):
|
||||
assert cmd_gate(lines.append, check=False, demo=False) == 0
|
||||
factory.assert_not_called()
|
||||
assert "no lock needed" in lines[0]
|
||||
|
||||
def test_gate_already_running(self) -> None:
|
||||
"""A held single-instance lock means a second window is not opened."""
|
||||
lines: list[str] = []
|
||||
with (
|
||||
patch.object(_cli_gate, "gate_is_due", return_value=True),
|
||||
_no_sync(),
|
||||
patch.object(_cli_gate, "acquire_gate_lock", return_value=None),
|
||||
patch.object(_cli_gate, "MealGate") as factory,
|
||||
):
|
||||
@ -75,6 +98,7 @@ class TestCmdGate:
|
||||
lines: list[str] = []
|
||||
with (
|
||||
patch.object(_cli_gate, "gate_is_due", return_value=True),
|
||||
_no_sync(),
|
||||
patch.object(_cli_gate, "acquire_gate_lock", return_value=MagicMock()),
|
||||
patch.object(_cli_gate, "release_gate_lock"),
|
||||
patch.object(_cli_gate, "wait_for_display", return_value=False),
|
||||
@ -83,3 +107,70 @@ class TestCmdGate:
|
||||
assert cmd_gate(lines.append, check=False, demo=False) == 0
|
||||
factory.assert_not_called()
|
||||
assert "display not ready" in lines[0]
|
||||
|
||||
|
||||
class TestIsDue:
|
||||
"""The decision helper only pays the network cost when a lock is due."""
|
||||
|
||||
def test_not_due_skips_sync(self) -> None:
|
||||
"""When nothing is due locally, no pull is attempted."""
|
||||
pull = MagicMock(return_value=None)
|
||||
with (
|
||||
patch.object(_cli_gate, "gate_is_due", return_value=False),
|
||||
patch.object(_cli_gate, "pull_shared_log", pull),
|
||||
):
|
||||
assert _is_due([].append) is False
|
||||
pull.assert_not_called()
|
||||
|
||||
def test_due_then_cleared_by_sync(self) -> None:
|
||||
"""A due lock that the pull clears re-reads as not due."""
|
||||
pull = MagicMock(return_value=None)
|
||||
with (
|
||||
patch.object(_cli_gate, "gate_is_due", side_effect=[True, False]),
|
||||
patch.object(_cli_gate, "pull_shared_log", pull),
|
||||
):
|
||||
assert _is_due([].append) is False
|
||||
pull.assert_called_once()
|
||||
|
||||
def test_due_and_still_due_after_sync(self) -> None:
|
||||
"""A due lock the pull cannot satisfy stays due."""
|
||||
pull = MagicMock(return_value=None)
|
||||
with (
|
||||
patch.object(_cli_gate, "gate_is_due", side_effect=[True, True]),
|
||||
patch.object(_cli_gate, "pull_shared_log", pull),
|
||||
):
|
||||
assert _is_due([].append) is True
|
||||
pull.assert_called_once()
|
||||
|
||||
def test_pull_failure_is_reported(self) -> None:
|
||||
"""A failed pull emits its reason but the local decision still stands."""
|
||||
lines: list[str] = []
|
||||
with (
|
||||
patch.object(_cli_gate, "gate_is_due", side_effect=[True, True]),
|
||||
patch.object(
|
||||
_cli_gate, "pull_shared_log", return_value="sync unavailable (x)"
|
||||
),
|
||||
):
|
||||
assert _is_due(lines.append) is True
|
||||
assert "sync unavailable (x)" in lines[0]
|
||||
|
||||
|
||||
class TestGateReadsFreshState:
|
||||
"""The fix's whole correctness rests on the re-check reading fresh disk.
|
||||
|
||||
Mocked ``gate_is_due`` tests would pass whether or not production re-reads
|
||||
state, so this one exercises the real uncached read path: a slot written to
|
||||
``food_log.json`` (what ``run_sync`` does when it pulls a phone meal) must be
|
||||
visible to the very next ``gate_is_due`` call.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def _nutrition() -> Nutrition:
|
||||
return Nutrition(200.0, 10.0, 20.0, 5.0, 100.0, "test")
|
||||
|
||||
def test_written_slot_clears_the_gate_immediately(self) -> None:
|
||||
"""At 09:00 only the 08:00 slot is due; logging it flips the decision."""
|
||||
now = datetime(2026, 1, 1, 9, 0, tzinfo=timezone.utc)
|
||||
assert gate_is_due(now) is True
|
||||
log_meal("oatmeal", self._nutrition(), slot=8)
|
||||
assert gate_is_due(now) is False
|
||||
|
||||
@ -423,3 +423,75 @@ class TestMealFlow:
|
||||
with patch.object(gate, "_unlock") as unlock:
|
||||
gate._finish_slot("done")
|
||||
unlock.assert_called_once()
|
||||
|
||||
|
||||
class TestFetchFromSync:
|
||||
"""The manual "Fetch from sync" button on the lock screen."""
|
||||
|
||||
def test_demo_mode_does_not_sync(self, gate: MealGate) -> None:
|
||||
"""In demo the button is inert and never touches the network."""
|
||||
with patch.object(_gatelock_mealflow, "pull_shared_log") as pull:
|
||||
gate._on_fetch_sync()
|
||||
pull.assert_not_called()
|
||||
assert "only available on the real lock" in gate._vars.status.get()
|
||||
|
||||
def test_pull_failure_keeps_lock(self, gate: MealGate) -> None:
|
||||
"""A failed pull shows the reason and leaves pending slots intact."""
|
||||
gate.demo_mode = False
|
||||
gate._pending = [8, 12]
|
||||
with patch.object(
|
||||
_gatelock_mealflow,
|
||||
"pull_shared_log",
|
||||
return_value="sync unavailable (x)",
|
||||
):
|
||||
gate._on_fetch_sync()
|
||||
assert gate._pending == [8, 12]
|
||||
assert "still locked" in gate._vars.status.get()
|
||||
|
||||
def test_no_new_meals_keeps_all_slots(self, gate: MealGate) -> None:
|
||||
"""A clean pull that satisfies nothing reports so and keeps the slots."""
|
||||
gate.demo_mode = False
|
||||
gate._pending = [8, 12]
|
||||
with (
|
||||
patch.object(_gatelock_mealflow, "pull_shared_log", return_value=None),
|
||||
patch.object(_gatelock_mealflow, "due_slots", return_value=(8, 12)),
|
||||
):
|
||||
gate._on_fetch_sync()
|
||||
assert gate._pending == [8, 12]
|
||||
assert "No new meals" in gate._vars.status.get()
|
||||
|
||||
def test_partial_advances_to_next_slot(self, gate: MealGate) -> None:
|
||||
"""One slot pulled in leaves the rest; the window advances (singular)."""
|
||||
gate.demo_mode = False
|
||||
gate._pending = [8, 12]
|
||||
with (
|
||||
patch.object(_gatelock_mealflow, "pull_shared_log", return_value=None),
|
||||
patch.object(_gatelock_mealflow, "due_slots", return_value=(12,)),
|
||||
):
|
||||
gate._on_fetch_sync()
|
||||
assert gate._pending == [12]
|
||||
assert "Pulled 1 meal " in gate._vars.status.get()
|
||||
|
||||
def test_partial_plural_wording(self, gate: MealGate) -> None:
|
||||
"""Two slots pulled in uses the plural 'meals'."""
|
||||
gate.demo_mode = False
|
||||
gate._pending = [8, 12, 16]
|
||||
with (
|
||||
patch.object(_gatelock_mealflow, "pull_shared_log", return_value=None),
|
||||
patch.object(_gatelock_mealflow, "due_slots", return_value=(16,)),
|
||||
):
|
||||
gate._on_fetch_sync()
|
||||
assert gate._pending == [16]
|
||||
assert "Pulled 2 meals" in gate._vars.status.get()
|
||||
|
||||
def test_all_satisfied_unlocks(self, gate: MealGate) -> None:
|
||||
"""When the pull satisfies every pending slot, the gate unlocks."""
|
||||
gate.demo_mode = False
|
||||
gate._pending = [8, 12]
|
||||
with (
|
||||
patch.object(_gatelock_mealflow, "pull_shared_log", return_value=None),
|
||||
patch.object(_gatelock_mealflow, "due_slots", return_value=()),
|
||||
):
|
||||
gate._on_fetch_sync()
|
||||
assert gate._pending == []
|
||||
assert "unlocking" in gate._vars.status.get()
|
||||
|
||||
@ -357,6 +357,27 @@ class TestRawLogAccess:
|
||||
write_raw_log(log)
|
||||
assert read_raw_log() == log
|
||||
|
||||
def test_write_leaves_no_temp_file(self) -> None:
|
||||
"""A successful atomic write cleans up its temp file."""
|
||||
write_raw_log({"2026-06-22": [{"id": "x"}]})
|
||||
assert list(_state.FOOD_LOG_FILE.parent.glob("*.tmp")) == []
|
||||
|
||||
def test_write_failure_preserves_prior_log(self) -> None:
|
||||
"""A failed replace leaves the old log intact and no temp behind.
|
||||
|
||||
This is the point of the atomic write: a concurrent reader (the gate
|
||||
now syncs while the timer may also write) never sees a torn or empty
|
||||
log just because a write was interrupted.
|
||||
"""
|
||||
write_raw_log({"2026-06-22": [{"id": "original"}]})
|
||||
with (
|
||||
patch("pathlib.Path.replace", side_effect=OSError("no space")),
|
||||
pytest.raises(OSError, match="no space"),
|
||||
):
|
||||
write_raw_log({"2026-06-22": [{"id": "clobbered"}]})
|
||||
assert read_raw_log() == {"2026-06-22": [{"id": "original"}]}
|
||||
assert list(_state.FOOD_LOG_FILE.parent.glob("*.tmp")) == []
|
||||
|
||||
|
||||
class TestResignEntry:
|
||||
"""resign_entry recomputes the hmac so a merged entry validates again."""
|
||||
|
||||
@ -202,3 +202,20 @@ class TestRunSync:
|
||||
with patch.object(_sync, "GitHubSyncClient", return_value=client):
|
||||
_sync.run_sync()
|
||||
assert lookup_food("oatmeal") is not None
|
||||
|
||||
|
||||
class TestPullSharedLog:
|
||||
"""The fail-closed wrapper the gate and the lock-screen button share."""
|
||||
|
||||
def test_returns_none_on_success(self) -> None:
|
||||
"""A clean pull returns None (no reason to report)."""
|
||||
with patch.object(_sync, "run_sync") as run_sync:
|
||||
assert _sync.pull_shared_log() is None
|
||||
run_sync.assert_called_once_with()
|
||||
|
||||
def test_returns_reason_on_any_failure(self) -> None:
|
||||
"""Any run_sync error becomes a reason string, never an exception."""
|
||||
with patch.object(_sync, "run_sync", side_effect=RuntimeError("boom")):
|
||||
reason = _sync.pull_shared_log()
|
||||
assert reason is not None
|
||||
assert "boom" in reason
|
||||
|
||||
@ -42,6 +42,10 @@ unfixable = []
|
||||
[tool.ruff.lint.per-file-ignores]
|
||||
"**/tests/**/*.py" = ["ARG", "D", "PLC0415", "PLR2004", "S101", "S107", "SLF001"]
|
||||
"**/test_*.py" = ["ARG", "D", "PLC0415", "PLR2004", "S101", "S107", "SLF001"]
|
||||
# pull_shared_log must fail closed: any run_sync error (no token, network, disk
|
||||
# write-back) is returned as a reason string so the gate keeps locking rather
|
||||
# than failing open. A deliberately broad catch, so BLE001 does not apply here.
|
||||
"**/_sync.py" = ["BLE001"]
|
||||
|
||||
[tool.ruff.lint.pydocstyle]
|
||||
convention = "google"
|
||||
|
||||
@ -8,6 +8,7 @@ gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.1.0#subdirectory=gatel
|
||||
mcp>=1.9
|
||||
mypy>=1.8.0
|
||||
pre-commit>=3.6.0
|
||||
pydantic>=2
|
||||
pylint>=3.0.0
|
||||
pytest>=8.0.0
|
||||
pytest-cov>=4.1.0
|
||||
|
||||
196
scripts/check_requirements_sync.py
Executable file
196
scripts/check_requirements_sync.py
Executable file
@ -0,0 +1,196 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Fail if the package imports a third-party dist not declared in requirements.
|
||||
|
||||
CI installs *only* ``requirements.txt`` (never the pyproject dependency list),
|
||||
so an import added to the source but not mirrored into ``requirements.txt``
|
||||
passes locally — where the dev environment already has it — and only blows up
|
||||
in CI with ``ModuleNotFoundError``. This static check closes that gap at commit
|
||||
time: it walks the package's own modules, collects every top-level third-party
|
||||
import, and verifies each one's distribution is listed in ``requirements.txt``.
|
||||
|
||||
It is intentionally conservative — it only flags *direct* imports (things the
|
||||
source explicitly ``import``s), never transitive dependencies — because you
|
||||
should declare what you import rather than lean on someone else's dependency
|
||||
tree. The pre-push clean-venv run is the definitive backstop for anything this
|
||||
heuristic misses.
|
||||
|
||||
Usage (defaults auto-detect the single top-level package and repo-root
|
||||
requirements.txt)::
|
||||
|
||||
python3 scripts/check_requirements_sync.py
|
||||
python3 scripts/check_requirements_sync.py --package screen_locker \
|
||||
--requirements requirements.txt
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import ast
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
|
||||
# Import-name -> distribution-name overrides for the rare cases where PEP 503
|
||||
# normalisation (lowercase, runs of -/_/. collapsed to '-') is not enough.
|
||||
# Most packages import under their normalised dist name (crdt_sync ->
|
||||
# crdt-sync); add an entry here only when they genuinely diverge (the classic
|
||||
# example being ``import dateutil`` shipped by ``python-dateutil``).
|
||||
_IMPORT_TO_DIST: dict[str, str] = {
|
||||
"dateutil": "python-dateutil",
|
||||
"yaml": "pyyaml",
|
||||
"kasa": "python-kasa",
|
||||
}
|
||||
|
||||
|
||||
def _normalise(name: str) -> str:
|
||||
"""Return the PEP 503 canonical form of a distribution/import name."""
|
||||
return re.sub(r"[-_.]+", "-", name).lower()
|
||||
|
||||
|
||||
def _requirement_names(requirements: Path) -> set[str]:
|
||||
"""Parse distribution names from a requirements.txt file.
|
||||
|
||||
Handles the forms this monorepo uses: ``name``, ``name>=x``, ``name==x``,
|
||||
``name[extra]``, and ``name @ git+https://...``. Comments, blank lines and
|
||||
``-r``/``-c`` include directives are ignored.
|
||||
"""
|
||||
names: set[str] = set()
|
||||
for raw_line in requirements.read_text(encoding="utf-8").splitlines():
|
||||
line = raw_line.strip()
|
||||
if not line or line.startswith(("#", "-")):
|
||||
continue
|
||||
# The distribution name is the leading token before any version
|
||||
# specifier, extras bracket, environment marker or ``@`` URL.
|
||||
token = re.split(r"[\s@<>=!~;\[]", line, maxsplit=1)[0]
|
||||
if token:
|
||||
names.add(_normalise(token))
|
||||
return names
|
||||
|
||||
|
||||
def _iter_package_modules(package_dir: Path) -> list[Path]:
|
||||
"""Return the package's own .py files, excluding any tests directory."""
|
||||
return [
|
||||
path
|
||||
for path in sorted(package_dir.rglob("*.py"))
|
||||
if "tests" not in path.relative_to(package_dir).parts
|
||||
]
|
||||
|
||||
|
||||
def _top_level_imports(module: Path) -> set[str]:
|
||||
"""Collect top-level module names imported by a single source file.
|
||||
|
||||
``import a.b`` and ``from a.b import c`` both contribute ``a``. Relative
|
||||
imports (``from . import x``) are skipped — they are first-party by
|
||||
definition.
|
||||
"""
|
||||
tree = ast.parse(module.read_text(encoding="utf-8"), filename=str(module))
|
||||
imported: set[str] = set()
|
||||
for node in ast.walk(tree):
|
||||
if isinstance(node, ast.Import):
|
||||
for alias in node.names:
|
||||
imported.add(alias.name.split(".", 1)[0])
|
||||
elif isinstance(node, ast.ImportFrom) and node.level == 0 and node.module:
|
||||
imported.add(node.module.split(".", 1)[0])
|
||||
return imported
|
||||
|
||||
|
||||
def _third_party_imports(package_dir: Path, package_name: str) -> set[str]:
|
||||
"""Return third-party top-level imports across the whole package.
|
||||
|
||||
Standard-library modules (via ``sys.stdlib_module_names``) and the package's
|
||||
own name are filtered out, leaving only external dependencies.
|
||||
"""
|
||||
stdlib = sys.stdlib_module_names
|
||||
external: set[str] = set()
|
||||
for module in _iter_package_modules(package_dir):
|
||||
for name in _top_level_imports(module):
|
||||
if name != package_name and name not in stdlib:
|
||||
external.add(name)
|
||||
return external
|
||||
|
||||
|
||||
def _distribution_for(import_name: str) -> str:
|
||||
"""Map a top-level import name to its (normalised) distribution name."""
|
||||
override = _IMPORT_TO_DIST.get(import_name)
|
||||
return _normalise(override if override is not None else import_name)
|
||||
|
||||
|
||||
def _detect_package(root: Path) -> str:
|
||||
"""Find the single top-level importable package directory under ``root``.
|
||||
|
||||
Raises SystemExit with a helpful message if zero or several candidates
|
||||
exist, in which case ``--package`` must be given explicitly.
|
||||
"""
|
||||
candidates = [
|
||||
child.name
|
||||
for child in sorted(root.iterdir())
|
||||
if child.is_dir()
|
||||
and (child / "__init__.py").is_file()
|
||||
and child.name != "tests"
|
||||
]
|
||||
if len(candidates) != 1:
|
||||
sys.exit(
|
||||
"Could not auto-detect a single package "
|
||||
f"(found {candidates or 'none'}); pass --package explicitly.",
|
||||
)
|
||||
return candidates[0]
|
||||
|
||||
|
||||
def _parse_args(argv: list[str] | None) -> argparse.Namespace:
|
||||
"""Parse command-line arguments."""
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument(
|
||||
"--package",
|
||||
help="Top-level package name (auto-detected when omitted).",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--requirements",
|
||||
default="requirements.txt",
|
||||
help="Path to requirements.txt (default: %(default)s).",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--root",
|
||||
default=".",
|
||||
help="Repository root to resolve paths against (default: %(default)s).",
|
||||
)
|
||||
return parser.parse_args(argv)
|
||||
|
||||
|
||||
def check(package: str, package_dir: Path, requirements: Path) -> list[str]:
|
||||
"""Return a sorted list of imported dists missing from requirements.txt."""
|
||||
declared = _requirement_names(requirements)
|
||||
missing = {
|
||||
name
|
||||
for name in _third_party_imports(package_dir, package)
|
||||
if _distribution_for(name) not in declared
|
||||
}
|
||||
return sorted(missing)
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
"""Entry point: exit non-zero (with a report) when deps are missing."""
|
||||
args = _parse_args(argv)
|
||||
root = Path(args.root).resolve()
|
||||
package = args.package or _detect_package(root)
|
||||
package_dir = root / package
|
||||
requirements = root / args.requirements
|
||||
|
||||
if not package_dir.is_dir():
|
||||
sys.exit(f"Package directory not found: {package_dir}")
|
||||
if not requirements.is_file():
|
||||
sys.exit(f"Requirements file not found: {requirements}")
|
||||
|
||||
missing = check(package, package_dir, requirements)
|
||||
if missing:
|
||||
joined = "\n - ".join(missing)
|
||||
sys.stderr.write(
|
||||
f"{package}: these third-party imports are not declared in "
|
||||
f"{requirements.name} (CI installs only that file, so they will "
|
||||
f"fail with ModuleNotFoundError):\n - {joined}\n",
|
||||
)
|
||||
return 1
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
93
scripts/ci_mirror.sh
Executable file
93
scripts/ci_mirror.sh
Executable file
@ -0,0 +1,93 @@
|
||||
#!/bin/bash
|
||||
# ============================================================================
|
||||
# ci_mirror.sh — run the exact CI gates locally before a push can leave.
|
||||
#
|
||||
# Why: the installed pre-commit hook only checks *staged* files, and the local
|
||||
# dev environment already has every dependency installed. CI does neither — it
|
||||
# runs `pre-commit run --all-files` and installs a fresh environment from
|
||||
# requirements.txt. That gap is why green-locally repos went red in CI
|
||||
# (missing runtime deps, --all-files lint debt, order-dependent test flakes).
|
||||
#
|
||||
# This script reproduces both CI workflows on the developer machine:
|
||||
# 1. a venv built ONLY from requirements.txt (mirrors the Tests workflow's
|
||||
# `pip install -r requirements.txt`), rebuilt only when requirements.txt
|
||||
# changes (hash-gated so day-to-day pushes stay fast);
|
||||
# 2. `pre-commit run --all-files` (mirrors the pre-commit workflow);
|
||||
# 3. `python -m pytest` inside that clean venv (mirrors Tests).
|
||||
#
|
||||
# Wired as the pre-push hook, so a red result blocks the push before CI ever
|
||||
# sees it. Escape hatch for genuine emergencies: `git push --no-verify`.
|
||||
# ============================================================================
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Requirements file may be overridden (e.g. testsAndMisc uses meta/…).
|
||||
REQUIREMENTS_FILE="${REQUIREMENTS_FILE:-requirements.txt}"
|
||||
readonly REQUIREMENTS_FILE
|
||||
|
||||
ROOT="$(git rev-parse --show-toplevel)"
|
||||
readonly ROOT
|
||||
cd "$ROOT"
|
||||
|
||||
readonly VENV_DIR="$ROOT/.ci-mirror-venv"
|
||||
readonly HASH_FILE="$VENV_DIR/.requirements.sha256"
|
||||
readonly REQ_PATH="$ROOT/$REQUIREMENTS_FILE"
|
||||
|
||||
log() { printf 'ci-mirror: %s\n' "$1" >&2; }
|
||||
|
||||
fail() {
|
||||
log "FAILED — $1"
|
||||
log "CI would be red. Fix the above, or 'git push --no-verify' to override."
|
||||
exit 1
|
||||
}
|
||||
|
||||
require_file() {
|
||||
if [[ ! -f "$REQ_PATH" ]]; then
|
||||
fail "requirements file not found: $REQ_PATH"
|
||||
fi
|
||||
}
|
||||
|
||||
# Rebuild the venv only when requirements.txt changed since the last build.
|
||||
ensure_venv() {
|
||||
local current stored
|
||||
current="$(sha256sum "$REQ_PATH" | cut -d' ' -f1)"
|
||||
stored=""
|
||||
if [[ -f "$HASH_FILE" ]]; then
|
||||
stored="$(cat "$HASH_FILE")"
|
||||
fi
|
||||
|
||||
if [[ -x "$VENV_DIR/bin/python" && "$current" == "$stored" ]]; then
|
||||
log "venv up to date (requirements.txt unchanged)"
|
||||
return
|
||||
fi
|
||||
|
||||
log "requirements.txt changed — rebuilding clean venv (mirrors CI install)"
|
||||
rm -rf "$VENV_DIR"
|
||||
python3 -m venv "$VENV_DIR"
|
||||
"$VENV_DIR/bin/python" -m pip install --quiet --upgrade pip \
|
||||
|| fail "pip self-upgrade in the clean venv"
|
||||
"$VENV_DIR/bin/python" -m pip install --quiet -r "$REQ_PATH" \
|
||||
|| fail "pip install -r $REQUIREMENTS_FILE (a dep may be undeclared)"
|
||||
printf '%s' "$current" > "$HASH_FILE"
|
||||
log "clean venv ready"
|
||||
}
|
||||
|
||||
run_precommit_all_files() {
|
||||
log "pre-commit run --all-files (mirrors the pre-commit workflow)"
|
||||
pre-commit run --all-files || fail "pre-commit --all-files"
|
||||
}
|
||||
|
||||
run_pytest_clean_venv() {
|
||||
log "pytest in the clean venv (mirrors the Tests workflow)"
|
||||
"$VENV_DIR/bin/python" -m pytest "$@" || fail "pytest (clean requirements.txt venv)"
|
||||
}
|
||||
|
||||
main() {
|
||||
require_file
|
||||
ensure_venv
|
||||
run_precommit_all_files
|
||||
run_pytest_clean_venv "$@"
|
||||
log "all CI gates passed locally — safe to push"
|
||||
}
|
||||
|
||||
main "$@"
|
||||
Loading…
Reference in New Issue
Block a user