mirror of
https://github.com/kuhyx/steam-backlog-enforcer.git
synced 2026-11-24 00:46:17 +01:00
Move store check report types to their own module
_store_verify.py grew past the 250-line cap. The report, its checks and the error now live in _store_check_report.py. Todo: 85af719b Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
7fa0ebad6a
commit
9311405bc8
@ -28,7 +28,7 @@ from steam_backlog_enforcer._ctl_reset import RESET_OPS
|
|||||||
from steam_backlog_enforcer._ctl_restart import RESTART_OPS, restart_available_at
|
from steam_backlog_enforcer._ctl_restart import RESTART_OPS, restart_available_at
|
||||||
from steam_backlog_enforcer._fast_install_progress import fast_install_status
|
from steam_backlog_enforcer._fast_install_progress import fast_install_status
|
||||||
from steam_backlog_enforcer._playtime_block import release_block
|
from steam_backlog_enforcer._playtime_block import release_block
|
||||||
from steam_backlog_enforcer._store_verify import StoreVerifyError
|
from steam_backlog_enforcer._store_check_report import StoreVerifyError
|
||||||
from steam_backlog_enforcer._store_window import (
|
from steam_backlog_enforcer._store_window import (
|
||||||
MAX_WINDOW_MINUTES,
|
MAX_WINDOW_MINUTES,
|
||||||
open_store_window,
|
open_store_window,
|
||||||
|
|||||||
90
steam_backlog_enforcer/_store_check_report.py
Normal file
90
steam_backlog_enforcer/_store_check_report.py
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
"""The result of a store unblock / re-block check, and its failure report.
|
||||||
|
|
||||||
|
Kept apart from the checks themselves (``_store_verify``) so the report a
|
||||||
|
todo task is filed from has one plain home: which checks ran, which failed,
|
||||||
|
and the evidence gathered for a failure.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from typing import Final
|
||||||
|
|
||||||
|
UNBLOCK: Final = "unblock"
|
||||||
|
REBLOCK: Final = "re-block"
|
||||||
|
NETWORK_DOWN: Final = "network down"
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class Check:
|
||||||
|
"""One check's outcome: its name, whether it held, and what was seen."""
|
||||||
|
|
||||||
|
name: str
|
||||||
|
ok: bool
|
||||||
|
detail: str
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class StoreCheckReport:
|
||||||
|
"""Every check of one action, plus the evidence a failure report needs."""
|
||||||
|
|
||||||
|
action: str
|
||||||
|
at: str
|
||||||
|
checks: list[Check]
|
||||||
|
network_down: bool = False
|
||||||
|
repaired: bool = False
|
||||||
|
evidence: dict[str, str] = field(default_factory=dict)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def ok(self) -> bool:
|
||||||
|
"""Whether every check held."""
|
||||||
|
return not self.network_down and all(c.ok for c in self.checks)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def failed_check(self) -> str | None:
|
||||||
|
"""The failure: "network down", the first failed check, or None."""
|
||||||
|
if self.network_down:
|
||||||
|
return NETWORK_DOWN
|
||||||
|
return next((c.name for c in self.checks if not c.ok), None)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def title(self) -> str:
|
||||||
|
"""The todo task title for this failure."""
|
||||||
|
return f"Store {self.action} verification failed: {self.failed_check}"
|
||||||
|
|
||||||
|
def body(self) -> str:
|
||||||
|
"""The failure report as markdown, for the todo task."""
|
||||||
|
rows = [
|
||||||
|
f"- {c.name}: {'ok' if c.ok else 'FAILED'} ({c.detail})"
|
||||||
|
for c in self.checks
|
||||||
|
]
|
||||||
|
parts = [
|
||||||
|
f"Time: {self.at}",
|
||||||
|
f"Action: {self.action}",
|
||||||
|
f"Repair pass run: {'yes' if self.repaired else 'no'}",
|
||||||
|
f"Result: {self.failed_check or 'ok'}",
|
||||||
|
"",
|
||||||
|
"## Checks",
|
||||||
|
*rows,
|
||||||
|
]
|
||||||
|
for name, text in self.evidence.items():
|
||||||
|
parts += ["", f"## {name}", "```", text.rstrip() or "(empty)", "```"]
|
||||||
|
return "\n".join(parts) + "\n"
|
||||||
|
|
||||||
|
def to_wire(self) -> dict[str, object]:
|
||||||
|
"""What the daemon hands the web server for filing."""
|
||||||
|
return {
|
||||||
|
"action": self.action,
|
||||||
|
"check": self.failed_check,
|
||||||
|
"title": self.title,
|
||||||
|
"body": self.body(),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class StoreVerifyError(RuntimeError):
|
||||||
|
"""A store action ran but its checks failed, even after the repair pass."""
|
||||||
|
|
||||||
|
def __init__(self, report: StoreCheckReport) -> None:
|
||||||
|
"""Keep *report* for the caller to show and file."""
|
||||||
|
super().__init__(f"Store {report.action} failed: {report.failed_check}")
|
||||||
|
self.report = report
|
||||||
@ -19,7 +19,6 @@ Re-block checks (the reverse):
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass, field
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
@ -31,15 +30,18 @@ from steam_backlog_enforcer._hosts_lines import (
|
|||||||
live_store_lines,
|
live_store_lines,
|
||||||
store_lines,
|
store_lines,
|
||||||
)
|
)
|
||||||
|
from steam_backlog_enforcer._store_check_report import (
|
||||||
|
REBLOCK,
|
||||||
|
UNBLOCK,
|
||||||
|
Check,
|
||||||
|
StoreCheckReport,
|
||||||
|
)
|
||||||
from steam_backlog_enforcer._store_tools import IPTABLES, IPTABLES_CHAIN, SUDO
|
from steam_backlog_enforcer._store_tools import IPTABLES, IPTABLES_CHAIN, SUDO
|
||||||
from steam_backlog_enforcer.config import BLOCKED_DOMAINS, HOSTS_FILE
|
from steam_backlog_enforcer.config import BLOCKED_DOMAINS, HOSTS_FILE
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
|
|
||||||
UNBLOCK: Final = "unblock"
|
|
||||||
REBLOCK: Final = "re-block"
|
|
||||||
NETWORK_DOWN: Final = "network down"
|
|
||||||
STORE_URL: Final = "https://store.steampowered.com/"
|
STORE_URL: Final = "https://store.steampowered.com/"
|
||||||
CONTROL_URL: Final = "https://steamcommunity.com/"
|
CONTROL_URL: Final = "https://steamcommunity.com/"
|
||||||
|
|
||||||
@ -60,81 +62,6 @@ SETTLE_ATTEMPTS: Final = 3
|
|||||||
_SETTLE_GAP_SECONDS: Final = 2.0
|
_SETTLE_GAP_SECONDS: Final = 2.0
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class Check:
|
|
||||||
"""One check's outcome: its name, whether it held, and what was seen."""
|
|
||||||
|
|
||||||
name: str
|
|
||||||
ok: bool
|
|
||||||
detail: str
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class StoreCheckReport:
|
|
||||||
"""Every check of one action, plus the evidence a failure report needs."""
|
|
||||||
|
|
||||||
action: str
|
|
||||||
at: str
|
|
||||||
checks: list[Check]
|
|
||||||
network_down: bool = False
|
|
||||||
repaired: bool = False
|
|
||||||
evidence: dict[str, str] = field(default_factory=dict)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def ok(self) -> bool:
|
|
||||||
"""Whether every check held."""
|
|
||||||
return not self.network_down and all(c.ok for c in self.checks)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def failed_check(self) -> str | None:
|
|
||||||
"""The failure: "network down", the first failed check, or None."""
|
|
||||||
if self.network_down:
|
|
||||||
return NETWORK_DOWN
|
|
||||||
return next((c.name for c in self.checks if not c.ok), None)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def title(self) -> str:
|
|
||||||
"""The todo task title for this failure."""
|
|
||||||
return f"Store {self.action} verification failed: {self.failed_check}"
|
|
||||||
|
|
||||||
def body(self) -> str:
|
|
||||||
"""The failure report as markdown, for the todo task."""
|
|
||||||
rows = [
|
|
||||||
f"- {c.name}: {'ok' if c.ok else 'FAILED'} ({c.detail})"
|
|
||||||
for c in self.checks
|
|
||||||
]
|
|
||||||
parts = [
|
|
||||||
f"Time: {self.at}",
|
|
||||||
f"Action: {self.action}",
|
|
||||||
f"Repair pass run: {'yes' if self.repaired else 'no'}",
|
|
||||||
f"Result: {self.failed_check or 'ok'}",
|
|
||||||
"",
|
|
||||||
"## Checks",
|
|
||||||
*rows,
|
|
||||||
]
|
|
||||||
for name, text in self.evidence.items():
|
|
||||||
parts += ["", f"## {name}", "```", text.rstrip() or "(empty)", "```"]
|
|
||||||
return "\n".join(parts) + "\n"
|
|
||||||
|
|
||||||
def to_wire(self) -> dict[str, object]:
|
|
||||||
"""What the daemon hands the web server for filing."""
|
|
||||||
return {
|
|
||||||
"action": self.action,
|
|
||||||
"check": self.failed_check,
|
|
||||||
"title": self.title,
|
|
||||||
"body": self.body(),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class StoreVerifyError(RuntimeError):
|
|
||||||
"""A store action ran but its checks failed, even after the repair pass."""
|
|
||||||
|
|
||||||
def __init__(self, report: StoreCheckReport) -> None:
|
|
||||||
"""Keep *report* for the caller to show and file."""
|
|
||||||
super().__init__(f"Store {report.action} failed: {report.failed_check}")
|
|
||||||
self.report = report
|
|
||||||
|
|
||||||
|
|
||||||
def _run(cmd: list[str], timeout: float) -> tuple[int, str]:
|
def _run(cmd: list[str], timeout: float) -> tuple[int, str]:
|
||||||
"""Run *cmd*; its exit code and combined output (``-1`` if it never ran)."""
|
"""Run *cmd*; its exit code and combined output (``-1`` if it never ran)."""
|
||||||
try:
|
try:
|
||||||
|
|||||||
@ -20,8 +20,8 @@ import logging
|
|||||||
import threading
|
import threading
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from steam_backlog_enforcer._store_check_report import StoreVerifyError
|
||||||
from steam_backlog_enforcer._store_verify import (
|
from steam_backlog_enforcer._store_verify import (
|
||||||
StoreVerifyError,
|
|
||||||
check_reblock,
|
check_reblock,
|
||||||
check_unblock,
|
check_unblock,
|
||||||
verify,
|
verify,
|
||||||
|
|||||||
@ -18,7 +18,7 @@ from steam_backlog_enforcer._serve_startup import (
|
|||||||
ensure_port_available,
|
ensure_port_available,
|
||||||
parse_serve_args,
|
parse_serve_args,
|
||||||
)
|
)
|
||||||
from steam_backlog_enforcer._store_verify import StoreVerifyError
|
from steam_backlog_enforcer._store_check_report import StoreVerifyError
|
||||||
from steam_backlog_enforcer._store_window import (
|
from steam_backlog_enforcer._store_window import (
|
||||||
DEFAULT_WINDOW_MINUTES,
|
DEFAULT_WINDOW_MINUTES,
|
||||||
MAX_WINDOW_MINUTES,
|
MAX_WINDOW_MINUTES,
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import pytest
|
|||||||
|
|
||||||
from steam_backlog_enforcer import _ctl_ops
|
from steam_backlog_enforcer import _ctl_ops
|
||||||
from steam_backlog_enforcer._ctl_protocol import CtlError
|
from steam_backlog_enforcer._ctl_protocol import CtlError
|
||||||
from steam_backlog_enforcer._store_verify import (
|
from steam_backlog_enforcer._store_check_report import (
|
||||||
Check,
|
Check,
|
||||||
StoreCheckReport,
|
StoreCheckReport,
|
||||||
StoreVerifyError,
|
StoreVerifyError,
|
||||||
|
|||||||
@ -8,6 +8,7 @@ from unittest.mock import MagicMock, patch
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from steam_backlog_enforcer import _store_verify as sv
|
from steam_backlog_enforcer import _store_verify as sv
|
||||||
|
from steam_backlog_enforcer._store_check_report import NETWORK_DOWN, StoreVerifyError
|
||||||
from steam_backlog_enforcer._store_verify import _run as run_command
|
from steam_backlog_enforcer._store_verify import _run as run_command
|
||||||
from steam_backlog_enforcer.config import BLOCKED_DOMAINS
|
from steam_backlog_enforcer.config import BLOCKED_DOMAINS
|
||||||
|
|
||||||
@ -114,7 +115,7 @@ def test_unblock_both_unreachable_is_network_down(host: FakeHost) -> None:
|
|||||||
host.reachable = dict.fromkeys(host.reachable, False)
|
host.reachable = dict.fromkeys(host.reachable, False)
|
||||||
report = sv.check_unblock()
|
report = sv.check_unblock()
|
||||||
assert not report.ok
|
assert not report.ok
|
||||||
assert report.failed_check == sv.NETWORK_DOWN
|
assert report.failed_check == NETWORK_DOWN
|
||||||
assert report.title == "Store unblock verification failed: network down"
|
assert report.title == "Store unblock verification failed: network down"
|
||||||
|
|
||||||
|
|
||||||
@ -201,7 +202,7 @@ def test_run_survives_missing_binary() -> None:
|
|||||||
def test_error_carries_report(host: FakeHost) -> None:
|
def test_error_carries_report(host: FakeHost) -> None:
|
||||||
"""StoreVerifyError keeps the report and names the check."""
|
"""StoreVerifyError keeps the report and names the check."""
|
||||||
host.chain = True
|
host.chain = True
|
||||||
err = sv.StoreVerifyError(sv.check_unblock())
|
err = StoreVerifyError(sv.check_unblock())
|
||||||
assert str(err) == "Store unblock failed: iptables"
|
assert str(err) == "Store unblock failed: iptables"
|
||||||
assert err.report.failed_check == "iptables"
|
assert err.report.failed_check == "iptables"
|
||||||
|
|
||||||
|
|||||||
@ -9,7 +9,7 @@ from unittest.mock import MagicMock, patch
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from steam_backlog_enforcer import _store_window
|
from steam_backlog_enforcer import _store_window
|
||||||
from steam_backlog_enforcer._store_verify import (
|
from steam_backlog_enforcer._store_check_report import (
|
||||||
Check,
|
Check,
|
||||||
StoreCheckReport,
|
StoreCheckReport,
|
||||||
StoreVerifyError,
|
StoreVerifyError,
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import pytest
|
|||||||
|
|
||||||
from steam_backlog_enforcer import _web_privileged, _web_store_report
|
from steam_backlog_enforcer import _web_privileged, _web_store_report
|
||||||
from steam_backlog_enforcer._ctl_protocol import CtlError, DaemonUnreachableError
|
from steam_backlog_enforcer._ctl_protocol import CtlError, DaemonUnreachableError
|
||||||
from steam_backlog_enforcer._store_verify import (
|
from steam_backlog_enforcer._store_check_report import (
|
||||||
Check,
|
Check,
|
||||||
StoreCheckReport,
|
StoreCheckReport,
|
||||||
StoreVerifyError,
|
StoreVerifyError,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user