diff --git a/mailman_sync.py b/mailman_sync.py index c99d9d8..f025695 100755 --- a/mailman_sync.py +++ b/mailman_sync.py @@ -25,24 +25,30 @@ class ListManager: list_name: str dry_run: bool + def _call_script(self, script: str, args: list[str], **kwargs): + output = subprocess.run( + [self.mailman_bin / script, *args], + encoding="ascii", + capture_output=True, + check=True, + **kwargs, + ) + for line in output.stdout.splitlines(): + print(f"[{script} {self.list_name}] {line}") + def newlist(self, urlhost: str, admin: str): password = "".join(secrets.choice(PASSWORD_CHARS) for i in range(PASSWORD_LEN)) - output = subprocess.run( + self._call_script( + "newlist", [ - self.mailman_bin / "newlist", "--quiet", f"--urlhost={urlhost}", self.list_name, admin, password, ], - encoding="ascii", - capture_output=True, - check=True, ) - for line in output.stdout.splitlines(): - print(f"[Creating {self.list_name}] {line}") def config_list(self): config_changes = """ @@ -70,27 +76,18 @@ forward_auto_discards = 0 # don't notify admin about discards config_file.write(config_changes) config_file.flush() - command = [ - self.mailman_bin / "config_list", + args = [ "--inputfile", config_file.name, self.list_name, ] if self.dry_run: - command.append("--checkonly") + args.append("--checkonly") - output = subprocess.run( - command, - encoding="ascii", - capture_output=True, - check=True, - ) - for line in output.stdout.splitlines(): - print(f"[Configuring {self.list_name}] {line}") + self._call_script("config_list", args) def sync_members(self, members: list[str]): - command = [ - self.mailman_bin / "sync_members", + args = [ "--welcome-msg=no", "--goodbye-msg=no", "--notifyadmin=no", @@ -99,18 +96,10 @@ forward_auto_discards = 0 # don't notify admin about discards self.list_name, ] if self.dry_run: - command.append("--no-change") + args.append("--no-change") members_data = "\n".join(members) - output = subprocess.run( - command, - input=members_data, - encoding="ascii", - capture_output=True, - check=True, - ) - for line in output.stdout.splitlines(): - print(f"[Syncing {self.list_name}] {line}") + self._call_script("sync_members", args, input=members_data) def main(