Browse Source

extend the regex in voixicron and add error messages to the script

Helmut Pozimski 7 years ago
parent
commit
857386f283
1 changed files with 24 additions and 10 deletions
  1. 24 10
      voixicron.py

+ 24 - 10
voixicron.py

@@ -10,6 +10,8 @@ import subprocess
 import smtplib
 import socket
 import re
+import logging
+import sys
 
 from email.mime.multipart import MIMEMultipart
 from email.mime.text import MIMEText
@@ -25,23 +27,32 @@ MAIL_SERVER_PORT = 25
 MAIL_SERVER_USER = ""
 # Password to log in to the mail server
 MAIL_SERVER_PASSWORD = ""
+# Log level for the stderr/stdout output
+LOG_LEVEL = logging.ERROR
 
 AVAILABLE_UPDATES = []
 
 DEVNULL = open("/dev/null", "w")
 
+LOGGER = logging.getLogger("voixicron")
+LOGGER.setLevel(LOG_LEVEL)
+CONSOLE_HANDLER = logging.StreamHandler()
+LOGGER.addHandler(CONSOLE_HANDLER)
+
 try:
     XBPS_INSTALL_RESULT = subprocess.check_output(["xbps-install", "-Sun"],
                                                   stderr=DEVNULL)
-except subprocess.CalledProcessError:
-    pass
+except subprocess.CalledProcessError as error:
+    LOGGER.error("Could not get list of updated packages, xbps-install error:"
+                 " %s" % error.output)
+    sys.exit(1)
 else:
     XBPS_INSTALL_RESULT = XBPS_INSTALL_RESULT.decode("utf-8").split("\n")
     for line in XBPS_INSTALL_RESULT:
         if "update" in line:
             try:
                 package = line.split(" ")[0]
-                package_regex = "^([A-Za-z0-9-]*)-([0-9].*_[0-9])$"
+                package_regex = "^([A-Za-z0-9-._]*)-([0-9].*_[0-9])$"
                 match_object = re.match(package_regex, package)
                 package_name = match_object.groups()[0]
                 new_package_version = match_object.groups()[1]
@@ -51,8 +62,10 @@ else:
                 try:
                     xbps_query_result = subprocess.check_output(
                         ["xbps-query", "--show", package_name], stderr=DEVNULL)
-                except subprocess.CalledProcessError:
-                    pass
+                except subprocess.CalledProcessError as error:
+                    LOGGER.debug("Querying the installed version of package %s"
+                                 " failed with error message: %s"
+                                 %(package_name, error.output))
                 else:
                     xbps_query_result = xbps_query_result.decode(
                         "utf-8").split("\n")
@@ -91,7 +104,7 @@ if AVAILABLE_UPDATES:
         SERVER_CONNECTION.connect()
     except (smtplib.SMTPConnectError, smtplib.SMTPServerDisconnected,
             socket.error):
-        pass
+        LOGGER.error("Failed to establish a connection to the SMTP server")
     else:
         try:
             SERVER_CONNECTION.starttls()
@@ -101,16 +114,17 @@ if AVAILABLE_UPDATES:
             try:
                 SERVER_CONNECTION.login(MAIL_SERVER_USER, MAIL_SERVER_PASSWORD)
             except smtplib.SMTPAuthenticationError:
-                pass
+                LOGGER.error("Authentication on the mail server with user %s "
+                             "failed." % MAIL_SERVER_USER)
             except smtplib.SMTPException:
-                pass
+                LOGGER.error("Error during authentication on the mail server.")
         try:
             SERVER_CONNECTION.sendmail("voixicron@%s" % HOSTNAME, ADMIN_EMAIL,
                                        MSG.as_string())
         except smtplib.SMTPRecipientsRefused:
-            pass
+            LOGGER.error("The mail server refused the recipient.")
         except smtplib.SMTPSenderRefused:
-            pass
+            LOGGER.error("The mail server refused the sender.")
         else:
             SERVER_CONNECTION.quit()
 DEVNULL.close()