2022-12-24 14:04:29 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
"""
|
|
|
|
Update Mailman 2 lists via a json API of the form {"LIST": ["ADDRESS", ...]}
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
2023-01-05 21:47:43 -05:00
|
|
|
import os
|
2022-12-24 14:04:29 -05:00
|
|
|
from pathlib import Path
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
2022-12-25 02:25:24 -05:00
|
|
|
def sync_members(
|
|
|
|
mailman_bin: Path, mailing_list: str, members: list[str], dry_run: bool
|
|
|
|
):
|
|
|
|
command = [
|
|
|
|
mailman_bin / "sync_members",
|
|
|
|
"--welcome-msg=no",
|
|
|
|
"--goodbye-msg=no",
|
|
|
|
"--notifyadmin=no",
|
|
|
|
"--file",
|
|
|
|
"-",
|
|
|
|
mailing_list,
|
|
|
|
]
|
|
|
|
if dry_run:
|
|
|
|
command.append("--no-change")
|
|
|
|
|
2022-12-25 02:19:14 -05:00
|
|
|
members_data = "\n".join(members)
|
2022-12-24 14:04:29 -05:00
|
|
|
output = subprocess.run(
|
2022-12-25 02:25:24 -05:00
|
|
|
command,
|
2022-12-25 02:14:36 -05:00
|
|
|
input=members_data,
|
2022-12-25 02:19:14 -05:00
|
|
|
encoding="ascii",
|
2022-12-24 14:04:29 -05:00
|
|
|
capture_output=True,
|
|
|
|
check=True,
|
|
|
|
)
|
2022-12-25 02:19:14 -05:00
|
|
|
print(output.stdout)
|
2022-12-24 14:04:29 -05:00
|
|
|
|
|
|
|
|
2023-01-05 21:47:43 -05:00
|
|
|
def main(mailman_bin: Path, api: str, api_auth: str, list_suffix: str, dry_run: bool):
|
|
|
|
r = requests.get(api, headers={"Authorization": api_auth})
|
2022-12-24 14:04:29 -05:00
|
|
|
if not r.ok:
|
|
|
|
print(f"Failed to get mailing list data from api: {r.status_code} {r.text}")
|
|
|
|
return
|
|
|
|
|
|
|
|
existing_lists = subprocess.run(
|
2022-12-25 02:19:14 -05:00
|
|
|
[mailman_bin / "list_lists", "-b"],
|
|
|
|
encoding="ascii",
|
|
|
|
capture_output=True,
|
|
|
|
check=True,
|
|
|
|
).stdout.split("\n")
|
2022-12-24 14:04:29 -05:00
|
|
|
certification_lists = r.json()
|
|
|
|
for name, members in certification_lists.items():
|
2022-12-25 02:12:08 -05:00
|
|
|
list_name = name + list_suffix
|
|
|
|
if list_name in existing_lists:
|
2022-12-25 02:25:24 -05:00
|
|
|
sync_members(mailman_bin, list_name, members, dry_run)
|
2022-12-25 02:12:08 -05:00
|
|
|
else:
|
|
|
|
print(f"Skipping {list_name}, as it does not exist in Mailman")
|
2022-12-24 14:04:29 -05:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
argp = argparse.ArgumentParser(description=__doc__)
|
|
|
|
argp.add_argument(
|
|
|
|
"--bin",
|
|
|
|
default="/usr/local/mailman",
|
|
|
|
type=Path,
|
|
|
|
help="Path to Mailman site admin scripts (default %(default)s)",
|
|
|
|
)
|
|
|
|
argp.add_argument("--api", required=True, help="API endpoint to retrieve JSON from")
|
|
|
|
argp.add_argument("--list-suffix", help="Suffix for mailing lists")
|
2022-12-25 02:25:24 -05:00
|
|
|
argp.add_argument(
|
|
|
|
"-n",
|
|
|
|
"--dry-run",
|
|
|
|
action="store_true",
|
|
|
|
help="Don't make changes, just print what would happen",
|
|
|
|
)
|
2022-12-24 14:04:29 -05:00
|
|
|
args = argp.parse_args()
|
|
|
|
|
2023-01-05 21:47:43 -05:00
|
|
|
if "API_AUTH" in os.environ:
|
|
|
|
api_auth = os.environ.get('API_AUTH')
|
|
|
|
else:
|
|
|
|
print("Missing API_AUTH environment variable")
|
|
|
|
exit(-1)
|
|
|
|
|
2022-12-25 02:16:54 -05:00
|
|
|
try:
|
2023-01-05 21:47:43 -05:00
|
|
|
main(args.bin, args.api, api_auth, args.list_suffix, args.dry_run)
|
2022-12-25 02:16:54 -05:00
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
print(e.stderr)
|
|
|
|
raise
|