Browse Source

add checking for matches in voixicron, update mail text

Helmut Pozimski 7 years ago
parent
commit
9f5a23113b
1 changed files with 31 additions and 14 deletions
  1. 31 14
      voixicron.py

+ 31 - 14
voixicron.py

@@ -44,7 +44,7 @@ try:
                                                   stderr=DEVNULL)
 except subprocess.CalledProcessError as error:
     LOGGER.error("Could not get list of updated packages, xbps-install error:"
-                 " %s" % error.output)
+                 " %s", error.output)
     sys.exit(1)
 else:
     XBPS_INSTALL_RESULT = XBPS_INSTALL_RESULT.decode("utf-8").split("\n")
@@ -52,10 +52,14 @@ else:
         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]
+                if match_object:
+                    package_name = match_object.groups()[0]
+                    new_package_version = match_object.groups()[1]
+                else:
+                    LOGGER.error("Not match for package %s", package)
+                    continue
             except IndexError:
                 continue
             else:
@@ -64,8 +68,8 @@ else:
                         ["xbps-query", "--show", package_name], stderr=DEVNULL)
                 except subprocess.CalledProcessError as error:
                     LOGGER.debug("Querying the installed version of package %s"
-                                 " failed with error message: %s"
-                                 %(package_name, error.output))
+                                 " failed with error message: %s",
+                                 package_name, error.output)
                 else:
                     xbps_query_result = xbps_query_result.decode(
                         "utf-8").split("\n")
@@ -74,13 +78,23 @@ else:
                             installed_package = line.split(
                                 ":")[1].strip()
                             match = re.match(package_regex,
-                                                       installed_package)
-                            installed_package_version = match.groups()[1]
-                            package_update = {
-                                "name": package_name,
-                                "new_version": new_package_version,
-                                "old_version": installed_package_version,
-                            }
+                                             installed_package)
+                            if match:
+                                installed_package_version = match.groups()[1]
+                                package_update = {
+                                    "name": package_name,
+                                    "new_version": new_package_version,
+                                    "old_version": installed_package_version,
+                                }
+                            else:
+                                LOGGER.error("Unable to detect installed "
+                                             "version of package %s",
+                                             package_name)
+                                package_update = {
+                                    "name": package_name,
+                                    "new_version": new_package_version,
+                                    "old_version": "unknown",
+                                }
                             AVAILABLE_UPDATES.append(package_update)
                             break
 if AVAILABLE_UPDATES:
@@ -97,6 +111,9 @@ if AVAILABLE_UPDATES:
                      % (update["name"],
                         update["old_version"],
                         update["new_version"])
+    MAIL_TEXT += "\n\nYou can install the updates by issuing the command:" \
+                 "\n\n\txbps-install -Su\n\nas root on %s\n\n--\nvoixicron"\
+                 % HOSTNAME
     MSG_TEXT = MIMEText(MAIL_TEXT.encode("utf-8"), _charset="utf-8")
     MSG.attach(MSG_TEXT)
     SERVER_CONNECTION = smtplib.SMTP(MAIL_SERVER, MAIL_SERVER_PORT)
@@ -115,7 +132,7 @@ if AVAILABLE_UPDATES:
                 SERVER_CONNECTION.login(MAIL_SERVER_USER, MAIL_SERVER_PASSWORD)
             except smtplib.SMTPAuthenticationError:
                 LOGGER.error("Authentication on the mail server with user %s "
-                             "failed." % MAIL_SERVER_USER)
+                             "failed.", MAIL_SERVER_USER)
             except smtplib.SMTPException:
                 LOGGER.error("Error during authentication on the mail server.")
         try: