Abstract out mailman script calls into a method

This commit is contained in:
Adam Goldsmith 2023-01-09 21:55:04 -05:00
parent 9e3a042a58
commit ff944231d8

View File

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