Add dry run argument

This commit is contained in:
Adam Goldsmith 2022-12-25 02:25:24 -05:00
parent fa366e7e63
commit b5bc574433

View File

@ -11,19 +11,24 @@ import subprocess
import requests import requests
def sync_members(mailman_bin: Path, mailing_list: str, members: list[str]): def sync_members(
members_data = "\n".join(members) mailman_bin: Path, mailing_list: str, members: list[str], dry_run: bool
output = subprocess.run( ):
[ command = [
mailman_bin / "sync_members", mailman_bin / "sync_members",
"--no-change",
"--welcome-msg=no", "--welcome-msg=no",
"--goodbye-msg=no", "--goodbye-msg=no",
"--notifyadmin=no", "--notifyadmin=no",
"--file", "--file",
"-", "-",
mailing_list, mailing_list,
], ]
if dry_run:
command.append("--no-change")
members_data = "\n".join(members)
output = subprocess.run(
command,
input=members_data, input=members_data,
encoding="ascii", encoding="ascii",
capture_output=True, capture_output=True,
@ -32,7 +37,7 @@ def sync_members(mailman_bin: Path, mailing_list: str, members: list[str]):
print(output.stdout) print(output.stdout)
def main(mailman_bin: Path, api: str, token: str, list_suffix: str): def main(mailman_bin: Path, api: str, token: str, list_suffix: str, dry_run: bool):
r = requests.get(api, headers={"Authorization": "Token " + token}) r = requests.get(api, headers={"Authorization": "Token " + token})
if not r.ok: if not r.ok:
print(f"Failed to get mailing list data from api: {r.status_code} {r.text}") print(f"Failed to get mailing list data from api: {r.status_code} {r.text}")
@ -48,7 +53,7 @@ def main(mailman_bin: Path, api: str, token: str, list_suffix: str):
for name, members in certification_lists.items(): for name, members in certification_lists.items():
list_name = name + list_suffix list_name = name + list_suffix
if list_name in existing_lists: if list_name in existing_lists:
sync_members(mailman_bin, list_name, members) sync_members(mailman_bin, list_name, members, dry_run)
else: else:
print(f"Skipping {list_name}, as it does not exist in Mailman") print(f"Skipping {list_name}, as it does not exist in Mailman")
@ -64,10 +69,16 @@ if __name__ == "__main__":
argp.add_argument("--api", required=True, help="API endpoint to retrieve JSON from") argp.add_argument("--api", required=True, help="API endpoint to retrieve JSON from")
argp.add_argument("--token", required=True, help="Authorization Token for API") argp.add_argument("--token", required=True, help="Authorization Token for API")
argp.add_argument("--list-suffix", help="Suffix for mailing lists") argp.add_argument("--list-suffix", help="Suffix for mailing lists")
argp.add_argument(
"-n",
"--dry-run",
action="store_true",
help="Don't make changes, just print what would happen",
)
args = argp.parse_args() args = argp.parse_args()
try: try:
main(args.bin, args.api, args.token, args.list_suffix) main(args.bin, args.api, args.token, args.list_suffix, args.dry_run)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
print(e.stderr) print(e.stderr)
raise raise