Move list management functions to a class to reuse shared arguments

This commit is contained in:
Adam Goldsmith 2023-01-09 21:48:02 -05:00
parent c49f3c7635
commit 9e3a042a58

View File

@ -5,6 +5,7 @@ Update Mailman 2 lists via a json API of the form {"LIST": ["ADDRESS", ...]}
"""
import argparse
from dataclasses import dataclass
import os
from pathlib import Path
import secrets
@ -18,15 +19,21 @@ PASSWORD_CHARS = string.ascii_letters + string.digits + string.punctuation
PASSWORD_LEN = 18
def newlist(mailman_bin: Path, listname: str, urlhost: str, admin: str):
@dataclass
class ListManager:
mailman_bin: Path
list_name: str
dry_run: bool
def newlist(self, urlhost: str, admin: str):
password = "".join(secrets.choice(PASSWORD_CHARS) for i in range(PASSWORD_LEN))
output = subprocess.run(
[
mailman_bin / "newlist",
self.mailman_bin / "newlist",
"--quiet",
f"--urlhost={urlhost}",
listname,
self.list_name,
admin,
password,
],
@ -35,10 +42,9 @@ def newlist(mailman_bin: Path, listname: str, urlhost: str, admin: str):
check=True,
)
for line in output.stdout.splitlines():
print(f"[Creating {listname}] {line}")
print(f"[Creating {self.list_name}] {line}")
def config_list(mailman_bin: Path, mailing_list: str, dry_run: bool):
def config_list(self):
config_changes = """
# TODO: set real_name, moderator, subject prefix, and reply-to address
from_is_list = 1
@ -65,12 +71,12 @@ forward_auto_discards = 0 # don't notify admin about discards
config_file.flush()
command = [
mailman_bin / "config_list",
self.mailman_bin / "config_list",
"--inputfile",
config_file.name,
mailing_list,
self.list_name,
]
if dry_run:
if self.dry_run:
command.append("--checkonly")
output = subprocess.run(
@ -80,22 +86,19 @@ forward_auto_discards = 0 # don't notify admin about discards
check=True,
)
for line in output.stdout.splitlines():
print(f"[Configuring {mailing_list}] {line}")
print(f"[Configuring {self.list_name}] {line}")
def sync_members(
mailman_bin: Path, mailing_list: str, members: list[str], dry_run: bool
):
def sync_members(self, members: list[str]):
command = [
mailman_bin / "sync_members",
self.mailman_bin / "sync_members",
"--welcome-msg=no",
"--goodbye-msg=no",
"--notifyadmin=no",
"--file",
"-",
mailing_list,
self.list_name,
]
if dry_run:
if self.dry_run:
command.append("--no-change")
members_data = "\n".join(members)
@ -107,7 +110,7 @@ def sync_members(
check=True,
)
for line in output.stdout.splitlines():
print(f"[Syncing {mailing_list}] {line}")
print(f"[Syncing {self.list_name}] {line}")
def main(
@ -133,16 +136,17 @@ def main(
certification_lists = r.json()
for name, members in certification_lists.items():
list_name = name + list_suffix
list_manager = ListManager(mailman_bin, list_name, dry_run)
if list_name not in existing_lists:
if dry_run:
print(f"Skipping non-existing list {list_name} in dry run mode")
continue
else:
newlist(mailman_bin, list_name, urlhost, admin)
list_manager.newlist(urlhost, admin)
print(f"Configuring/syncing {list_name}...")
config_list(mailman_bin, list_name, dry_run)
sync_members(mailman_bin, list_name, members, dry_run)
list_manager.config_list()
list_manager.sync_members(members)
def parse_arguments():