1 # PyNSC: Generic interface for generating daemon configuration
2 # (c) 2024 Martin Mareš <mj@ucw.cz>
4 from io import StringIO
5 from pathlib import Path
6 from typing import TextIO
10 from nsconfig.core import Nsc, NscZone
16 def __init__(self) -> None:
19 def setup(self, nsc: Nsc) -> None:
22 def dump_config(self, file: TextIO = sys.stdout) -> None:
25 def write_config(self) -> None:
28 def reload_zone(self, z: NscZone) -> None:
31 def reload_daemon(self) -> None:
34 def _install_config(self, path: Path, new_contents: str) -> bool:
36 old_new_contents = path.read_text()
37 except FileNotFoundError:
38 old_new_contents = None
39 if new_contents == old_new_contents:
42 new_path = Path(str(path) + '.new')
43 with open(new_path, 'w') as f:
45 new_path.replace(path)
48 def _write_config(self, config_path: Path) -> bool:
49 string_stream = StringIO()
50 self.dump_config(string_stream)
51 if self._install_config(config_path, string_stream.getvalue()):
52 print('Wrote new daemon configuration')
55 print('Daemon configuration not changed')
58 def _run_command(self, argv, **kwargs) -> None:
59 res = subprocess.run(argv, **kwargs)
60 if res.returncode > 0:
61 print(f'Command failed: {argv}')
65 class NscDaemonNull(NscDaemon):