From 2a716bef60c307587f25c223ac98c56ac694c0f3 Mon Sep 17 00:00:00 2001 From: Jerry Date: Fri, 2 Jul 2021 18:47:20 +0800 Subject: [PATCH] add test-smtp command --- src/pacroller/config.py | 7 ++++++- src/pacroller/mailer.py | 6 ++++-- src/pacroller/main.py | 13 +++++++++++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/pacroller/config.py b/src/pacroller/config.py index 7ef1ea4..1f3290c 100644 --- a/src/pacroller/config.py +++ b/src/pacroller/config.py @@ -24,7 +24,12 @@ else: _config = dict() if (smtp_cfg := (CONFIG_DIR / CONFIG_FILE_SMTP)).exists(): - _smtp_config: dict = json.loads(smtp_cfg.read_text()) + try: + _smtp_cfg_text = smtp_cfg.read_text() + except PermissionError: + _smtp_config = dict() + else: + _smtp_config: dict = json.loads(_smtp_cfg_text) else: _smtp_config = dict() diff --git a/src/pacroller/mailer.py b/src/pacroller/mailer.py index 02f5fe8..1b074a3 100644 --- a/src/pacroller/mailer.py +++ b/src/pacroller/mailer.py @@ -17,9 +17,9 @@ class MailSender: self.mailfrom = SMTP_FROM self.mailto = SMTP_TO.split() self.smtp_cls = smtplib.SMTP_SSL if self.ssl else smtplib.SMTP - def send_text_plain(self, text: str, subject: str = f"pacroller from {hostname}", mailto: List[str] = list()) -> None: + def send_text_plain(self, text: str, subject: str = f"pacroller on {hostname}", mailto: List[str] = list()) -> bool: if not SMTP_ENABLED: - return + return None for _ in range(NETWORK_RETRY): try: server = self.smtp_cls(self.host, self.port) @@ -40,6 +40,8 @@ class MailSender: break else: logger.error(f"unable to send email after {NETWORK_RETRY} attempts {text=}") + return False + return True if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(module)s - %(funcName)s - %(levelname)s - %(message)s') diff --git a/src/pacroller/main.py b/src/pacroller/main.py index eef70fe..c6a6981 100644 --- a/src/pacroller/main.py +++ b/src/pacroller/main.py @@ -264,8 +264,8 @@ def main() -> None: logger.debug(f'needrestart {p.stdout=}') import argparse parser = argparse.ArgumentParser(description='Unattended Upgrades for Arch Linux') - parser.add_argument('action', choices=['run', 'status', 'reset', 'fail-reset', 'reset-failed'], - help="what to do", metavar="run / status / reset ") + parser.add_argument('action', choices=['run', 'status', 'reset', 'fail-reset', 'reset-failed', 'test-smtp'], + help="what to do", metavar="run / status / reset / test-smtp") parser.add_argument('-d', '--debug', action='store_true', help='enable debug mode') parser.add_argument('-v', '--verbose', action='store_true', help='show verbose report') parser.add_argument('-m', '--max', type=int, default=1, help='Number of upgrades to show') @@ -321,6 +321,15 @@ def main() -> None: if PACMAN_SCC: clear_pkg_cache() + elif args.action == 'test-smtp': + logger.info('sending test mail...') + if _smtp_result := MailSender().send_text_plain("This is a test mail\nIf you see this email, your smtp config is working."): + logger.info("success") + elif _smtp_result is None: + logger.warning("smtp is disabled") + else: + logger.error("fail") + elif args.action == 'status': count = 0 failed = False