From f0bcfc2f4dde73657e8835a8f55f050f8d09e8e9 Mon Sep 17 00:00:00 2001 From: boyska Date: Tue, 26 Nov 2024 00:44:47 +0100 Subject: [PATCH] send full log --- carichello.py | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/carichello.py b/carichello.py index f707d86..7a11a13 100644 --- a/carichello.py +++ b/carichello.py @@ -17,7 +17,7 @@ import contextlib import requests -def setup_logging(): +def setup_logging() -> Path: logdir = Path("~/.local/share/carichello/").expanduser() logdir.mkdir(exist_ok=True, parents=True) today = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M") @@ -32,9 +32,10 @@ def setup_logging(): level=logging.DEBUG, ) logfile.close() + return Path(logfile.name) -setup_logging() +LOGFILE: Path = setup_logging() LOG = logging.getLogger() @@ -272,7 +273,38 @@ class Carichello: def set_clipboard(self, text: str): subprocess.run(["xsel", "-bi"], input=text.encode("utf8")) + def run(self) -> int: + try: + ret = self._run() + except Exception: + self._send_log(-1) + raise + else: + if ret != 0: + self._send_log(ret) + return ret + + def send_mail(self, subject: str, text: str): + mail_to = self.config.get("mail", {}).get("to", None) + hostname = subprocess.check_output(["hostname", "-f"], encoding="utf8").strip() + if mail_to: + try: + subprocess.run( + ["mail", "-s", subject, mail_to], + input=f"{hostname}\n\n{text}", + encoding="utf8", + ) + return True + except subprocess.CalledProcessError: + LOG.error("error sending email") + return False + + def _send_log(self, ret: int): + contents = LOGFILE.read_text() + self.send_mail("Carichello log ERROR", contents) + + def _run(self) -> int: LOG.info("start") self.args = self.parser.parse_args() with self.args.config.open() as buf: @@ -357,17 +389,7 @@ class Carichello: return self.error_exception(exc) LOG.info("ready: %s", url) self.set_clipboard(url) - mail_to = self.config.get('mail', {}).get('to', None) - if mail_to: - hostname = subprocess.check_output(['hostname', '-f'], encoding='utf8').strip() - try: - subprocess.run( - ['mail', '-s', 'Nuovo file caricato', mail_to], - input=f"{hostname}\n\n{url}", - encoding='utf8', - ) - except subprocess.CalledProcessError: - LOG.error("error sending email") + self.send_mail("Nuovo file caricato", url) subprocess.run( [ "zenity",