mirror of
https://github.com/kuhyx/steam-backlog-enforcer.git
synced 2026-11-26 07:07:26 +01:00
The pinned howlongtobeatpy (1.0.23) takes a user agent in send_website_request_getcode and dropped parse_all_scripts; the call still matched 1.0.20, which only worked because the deployed env was behind its own pin. Now on the pinned API, and the env upgraded. Three modules carried the same executable/path constant block since the 250-line split; it is _store_tools. Four carried the same "restore the hours a refetch lost" loop; it is restore_prior_hours. Both detached launches go through one helper that returns the process. Thirteen try/except/else blocks are flat, dataclass fields come from dataclasses.fields, and the rest are one-line fixes. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HHCbkJWXHjtTUaqvnHRAqA
90 lines
2.4 KiB
Python
90 lines
2.4 KiB
Python
"""Toggling the protected/immutable flag on /etc/hosts around edits.
|
|
|
|
Leaf helpers: they call nothing else in the blocker, so extracting them
|
|
introduces no cycle. Split to keep both files under the 250-line cap.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import subprocess
|
|
|
|
from steam_backlog_enforcer._store_tools import (
|
|
GUARDCTL,
|
|
SUDO,
|
|
TEE,
|
|
)
|
|
from steam_backlog_enforcer.config import (
|
|
BLOCKED_DOMAINS,
|
|
HOSTS_FILE,
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def _disable_hosts_protection() -> None:
|
|
"""Temporarily unlock /etc/hosts so its content can be edited.
|
|
|
|
Guard-lib: stop watcher, collapse bind mount, chattr -i.
|
|
"""
|
|
subprocess.run(
|
|
[SUDO, GUARDCTL, "file-guard", "pacman-unlock", "hosts"],
|
|
capture_output=True,
|
|
timeout=10,
|
|
check=False,
|
|
)
|
|
|
|
|
|
def _enable_hosts_protection() -> None:
|
|
"""Re-lock /etc/hosts, adopting its current content as the new canonical.
|
|
|
|
Guard-lib: chattr +i, reapply bind mount, restart watcher.
|
|
"""
|
|
subprocess.run(
|
|
[SUDO, GUARDCTL, "file-guard", "sync", "hosts"],
|
|
capture_output=True,
|
|
timeout=10,
|
|
check=False,
|
|
)
|
|
|
|
|
|
def _sudo_write_hosts(content: str) -> None:
|
|
"""Write *content* to /etc/hosts via ``sudo tee``."""
|
|
subprocess.run(
|
|
[SUDO, TEE, str(HOSTS_FILE)],
|
|
input=content.encode(),
|
|
stdout=subprocess.DEVNULL,
|
|
timeout=10,
|
|
check=True,
|
|
)
|
|
|
|
|
|
def _reblock_hosts() -> bool:
|
|
"""Uncomment Steam Store entries in /etc/hosts."""
|
|
try:
|
|
_disable_hosts_protection()
|
|
content = HOSTS_FILE.read_text(encoding="utf-8")
|
|
new_lines = []
|
|
changed = False
|
|
for line in content.splitlines(keepends=True):
|
|
stripped = line.strip()
|
|
if stripped.startswith("# ") and any(
|
|
d in stripped for d in BLOCKED_DOMAINS
|
|
):
|
|
# Remove the '# ' prefix.
|
|
uncommented = line.replace("# ", "", 1)
|
|
new_lines.append(uncommented)
|
|
changed = True
|
|
else:
|
|
new_lines.append(line)
|
|
|
|
if changed:
|
|
_sudo_write_hosts("".join(new_lines))
|
|
logger.info("Re-enabled Steam Store entries in /etc/hosts.")
|
|
|
|
_enable_hosts_protection()
|
|
except OSError:
|
|
logger.exception("Failed to modify /etc/hosts")
|
|
return False
|
|
return True
|