Files
xray-lists/xray_lists/dnsmasq_gen.py
T
2026-08-16 23:21:59 +00:00

33 lines
924 B
Python

from __future__ import annotations
import logging
from pathlib import Path
log = logging.getLogger("xray-lists.dnsmasq")
def render_nftset_file(domain_to_targets: dict[str, list[str]]) -> str:
lines = []
for domain in sorted(domain_to_targets):
targets = ",".join(domain_to_targets[domain])
lines.append(f"nftset=/{domain}/{targets}")
return "\n".join(lines) + ("\n" if lines else "")
def write_if_changed(path: Path, content: str, dry_run: bool = False) -> bool:
old = path.read_text() if path.is_file() else None
if old == content:
log.debug("%s: unchanged", path)
return False
if dry_run:
log.info("[dry-run] would write %s", path)
return True
path.parent.mkdir(parents=True, exist_ok=True)
tmp = path.with_suffix(path.suffix + ".tmp")
tmp.write_text(content)
tmp.replace(path)
log.info("wrote %s", path)
return True