SF.net SVN: gar:[23339] csw/mgar/gar/v2

guengel at users.sourceforge.net guengel at users.sourceforge.net
Sun Apr 6 09:41:27 CEST 2014


Revision: 23339
          http://sourceforge.net/p/gar/code/23339
Author:   guengel
Date:     2014-04-06 07:41:26 +0000 (Sun, 06 Apr 2014)
Log Message:
-----------
Does now send mails to maintainers which might have caused db catalog corruption.

Modified Paths:
--------------
    csw/mgar/gar/v2/bin/chkdbcat
    csw/mgar/gar/v2/lib/python/chkdbcat.py
    csw/mgar/gar/v2/lib/python/chkdbcat_test.py

Modified: csw/mgar/gar/v2/bin/chkdbcat
===================================================================
--- csw/mgar/gar/v2/bin/chkdbcat	2014-04-05 22:09:11 UTC (rev 23338)
+++ csw/mgar/gar/v2/bin/chkdbcat	2014-04-06 07:41:26 UTC (rev 23339)
@@ -4,6 +4,7 @@
 import argparse
 import logging
 from lib.python import chkdbcat
+import smtplib
 
 class MyCheckDBCatalog(chkdbcat.CheckDBCatalog):
     """Class overriding CheckDBCatalog.notify()"""
@@ -12,9 +13,9 @@
         self.__verbose = verbose
 
     def notify(self, date, addr, pkginfo):
-        # TODO: Do actual notification. To be discussed.
-        if self.__verbose: print('Notify %s' % addr)
-        print(date, add, pkginfo)
+        notifier = chkdbcat.InformMaintainer((self._catrel, self._osrel, self._arch),
+                                             date, addr, pkginfo))
+        notfier.send_mail()
 
 
 def argparser():

Modified: csw/mgar/gar/v2/lib/python/chkdbcat.py
===================================================================
--- csw/mgar/gar/v2/lib/python/chkdbcat.py	2014-04-05 22:09:11 UTC (rev 23338)
+++ csw/mgar/gar/v2/lib/python/chkdbcat.py	2014-04-06 07:41:26 UTC (rev 23339)
@@ -7,6 +7,7 @@
 Notifications for invalid catalogs can be customized by overriding
 CheckDBCat.notify().
 """
+from email.mime.text import MIMEText
 import cjson
 import datetime
 import dateutil.parser
@@ -14,6 +15,7 @@
 import os
 import os.path
 import shutil
+import smtplib
 import subprocess
 import sys
 import tempfile
@@ -330,9 +332,9 @@
             self.__chkcat = chkcat
 
             # store for later use
-            self.__catrel = catrel
-            self.__arch = arch
-            self.__osrel = osrel
+            self._catrel = catrel
+            self._arch = arch
+            self._osrel = osrel
 
             # will be set by __enter__ and unset by __exit__
             self.tmpdir = None
@@ -415,9 +417,9 @@
                   # Only record successful checks.
                   if retval:
                         with self.__timestamp_record:
-                              self.__timestamp_record.set(self.__catrel,
-                                                          self.__arch,
-                                                          self.__osrel,
+                              self.__timestamp_record.set(self._catrel,
+                                                          self._arch,
+                                                          self._osrel,
                                                           datetime.datetime.now())
 
                   # Compose list of packages uploaded since last successful
@@ -425,18 +427,18 @@
                   notifications = {}
                   if not retval:
                         lastsuccessful = self.__timestamp_record.get(
-                              self.__catrel,
-                              self.__arch,
-                              self.__osrel)
+                              self._catrel,
+                              self._arch,
+                              self._osrel)
 
                         if lastsuccessful is None:
                               logging.warn("No successful catalog check recorded for %s,%s,%s" %
-                                           (self.__catrel, self.__arch, self.__osrel))
+                                           (self._catrel, self._arch, self._osrel))
                               return retval;
 
-                        newpkgs = self.__cattiming_class(self.__catrel,
-                                                         self.__arch,
-                                                         self.__osrel).upload_newer_than(lastsuccessful)
+                        newpkgs = self.__cattiming_class(self._catrel,
+                                                         self._arch,
+                                                         self._osrel).upload_newer_than(lastsuccessful)
 
                         # compose notifications list in a manner so that
                         # each email address is notified exactly once, even
@@ -451,3 +453,57 @@
                               self.notify(notifications[n]['lastsuccessful'], n, notifications[n]['newpkgs'])
 
             return retval
+
+
+class InformMaintainer(object):
+      MAIL_CAT_BROKEN_HEADER = u"""Hi
+
+You uploaded following packages
+
+"""
+
+      MAIL_CAT_BROKEN_FOOTER = u"""
+which may (or may not) have broken the catalog for
+
+ %s %s %s
+
+since the last successful check on %s.
+
+Please check the package(s) and re-upload if necessary.
+
+Your Check Database Catalog script
+
+"""
+
+      def __init__(self, cat_tuple, date, addr, pkginfo):
+            self._cat_tuple = cat_tuple
+            self._date = date
+            self._addr = addr
+            self._pkginfo = pkginfo
+
+      def _compose_mail(self, from_address):
+            """Compose Mail"""
+
+            msg = InformMaintainer.MAIL_CAT_BROKEN_HEADER
+            for p in self._pkginfo:
+                  msg = msg + p['fullname'] + "\n"
+            msg = msg + InformMaintainer.MAIL_CAT_BROKEN_FOOTER
+
+            mail = MIMEText(msg  % (self._cat_tuple + (str(self._date),)))
+            mail['From'] = from_address
+            mail['To'] = self._addr
+
+            return mail
+
+      def send_mail(self):
+            from_address = "Check Database Catalog <noreply at opencsw.org>"
+            s = smtplib.SMTP('mail.opencsw.org')
+            try:
+                  s.sendmail(from_address, [self._addr],
+                             self._compose_mail(from_address).as_string())
+                  logging.debug("E-mail sending finished.")
+            except smtplib.SMTPRecipientsRefused, e:
+                  logging.error(
+                        "Sending email to %s failed, recipient refused.",
+                        repr(self._addr))
+            

Modified: csw/mgar/gar/v2/lib/python/chkdbcat_test.py
===================================================================
--- csw/mgar/gar/v2/lib/python/chkdbcat_test.py	2014-04-05 22:09:11 UTC (rev 23338)
+++ csw/mgar/gar/v2/lib/python/chkdbcat_test.py	2014-04-06 07:41:26 UTC (rev 23339)
@@ -7,7 +7,7 @@
 import datetime
 import logging
 import unittest
-from lib.python.chkdbcat import TimestampRecord, CatalogTiming, CheckDBCatalog
+from lib.python.chkdbcat import TimestampRecord, CatalogTiming, CheckDBCatalog, InformMaintainer
 
 ##
 ## Unit tests
@@ -254,7 +254,11 @@
                   for p in pkginfo:
                         assert p['fullname'] in self.expected_notification_on[addr]['newpkgs']
 
+                  mail = InformMaintainer((self._catrel, self._osrel, self._arch),
+                                          date, addr, pkginfo)
+                  print mail._compose_mail('TestScript')
 
+
       def setUp(self):
             self.__timestamp_file = '/tmp/TestCheckDBCatalog.ts'
 

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the devel mailing list