d41236c376
tracker-policy-pr / check-pr (pull_request) Successful in 8s
test / integration-docker (pull_request) Successful in 36s
test / unit (pull_request) Successful in 43s
lint / lint (push) Successful in 56s
test / integration-firecracker (pull_request) Successful in 3m27s
test / coverage (pull_request) Successful in 21s
test / publish-infra (pull_request) Has been skipped
Generalize render_diff so it isn't supervise-flavored: the "(current)" / "(proposed)" side titles are no longer baked in — callers pass `before_title` and `after_label` (required). Move it from orchestrator/supervisor/util.py to the base bot_bottle.util alongside sha256_hex, and delete the now-empty supervisor/util.py. The supervise callsite (Orchestrator.supervise_respond) assigns before_title="current", after_label="proposed"; the facade no longer re-exports render_diff (callers import from bot_bottle.util). Behavior at the supervise callsite is unchanged. Full unit suite green (2243). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
70 lines
2.0 KiB
Python
70 lines
2.0 KiB
Python
"""Cross-cutting utility helpers used by multiple modules.
|
|
|
|
Top-level (i.e. backend-agnostic) — backend-specific helpers live one
|
|
level deeper, under their backend package."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import difflib
|
|
import hashlib
|
|
import ipaddress
|
|
import os
|
|
import sys
|
|
|
|
|
|
def sha256_hex(content: str) -> str:
|
|
"""Hex SHA-256 of a UTF-8 string."""
|
|
return hashlib.sha256(content.encode("utf-8")).hexdigest()
|
|
|
|
|
|
def render_diff(
|
|
before: str,
|
|
after: str,
|
|
*,
|
|
label: str = "config",
|
|
before_title: str,
|
|
after_label: str,
|
|
) -> str:
|
|
"""Unified diff of `before` vs `after`, with the two sides headed
|
|
`{label} ({before_title})` and `{label} ({after_label})`. Empty diff (no
|
|
changes) renders as the empty string. The side titles are the caller's —
|
|
e.g. "current"/"proposed" for a supervise proposal."""
|
|
diff = difflib.unified_diff(
|
|
before.splitlines(keepends=True),
|
|
after.splitlines(keepends=True),
|
|
fromfile=f"{label} ({before_title})",
|
|
tofile=f"{label} ({after_label})",
|
|
lineterm="",
|
|
)
|
|
parts = list(diff)
|
|
if not parts:
|
|
return ""
|
|
return "".join(p if p.endswith("\n") else p + "\n" for p in parts).rstrip("\n")
|
|
|
|
|
|
def is_ip_literal(value: str) -> bool:
|
|
try:
|
|
ipaddress.ip_address(value)
|
|
except ValueError:
|
|
return False
|
|
return True
|
|
|
|
|
|
def read_tty_line() -> str:
|
|
"""Mirror `IFS= read -r REPLY </dev/tty`. Falls back to stdin."""
|
|
try:
|
|
with open("/dev/tty", "r", encoding="utf-8") as tty:
|
|
return tty.readline().rstrip("\n")
|
|
except OSError:
|
|
return sys.stdin.readline().rstrip("\n")
|
|
|
|
|
|
def expand_tilde(path: str) -> str:
|
|
"""Expand a leading '~' to $HOME. Leaves paths without a leading
|
|
tilde unchanged. Falls back to the empty string if $HOME is unset
|
|
(callers should already have checked HOME if they care)."""
|
|
if path.startswith("~"):
|
|
home = os.environ.get("HOME", "")
|
|
return home + path[1:]
|
|
return path
|