#!/usr/bin/python from argparse import ArgumentParser from pathlib import Path from sys import exit MAILBOX_SUBDIRS = {"cur", "new", "tmp"} def get_mailbox_from_dir(path: Path, mailboxes: list[Path]) -> None: for current_path in path.iterdir(): if current_path.is_dir() and current_path.name not in MAILBOX_SUBDIRS: subdirs = list(current_path.iterdir()) if MAILBOX_SUBDIRS & set(subdir.name for subdir in subdirs) and current_path not in mailboxes: mailboxes.append(current_path) for subdir in subdirs: if subdir.name not in MAILBOX_SUBDIRS: get_mailbox_from_dir(path=current_path, mailboxes=mailboxes) def main(output: Path, paths: list[Path]) -> None: file_string = "" if len(paths) == 0: exit(1) for path in paths: mailboxes: list[Path] = [] mailboxes_string = "mailboxes " if not path.exists(): exit(1) get_mailbox_from_dir(path=path, mailboxes=mailboxes) for mailbox in mailboxes: mailboxes_string += f" '+{(str(mailbox).replace(str(path.parent) + '/', ''))}'" file_string += mailboxes_string + "\n" with open(output, "w") as f: print(file_string, file=f) if __name__ == "__main__": parser = ArgumentParser( description=""" Create 'mailboxes' entries for one or more directories. Each provided directory is """ ) parser.add_argument( "-o", "--output", default=Path("mailboxes.rc"), help="a file to write the mailboxes to (mailboxes.rc by default)", type=Path, ) parser.add_argument( "paths", help="a directory to search in for mailbox directories", nargs="+", type=Path, ) namespace = parser.parse_args() main(output=namespace.output, paths=namespace.paths)