[Zope-CVS] CVS: Packages/TestScripts - summarizer.py:1.2

Steve Alexander steve at cat-box.net
Mon Dec 22 12:02:50 EST 2003


Update of /cvs-repository/Packages/TestScripts
In directory cvs.zope.org:/tmp/cvs-serv6624

Modified Files:
	summarizer.py 
Log Message:
Added code to parse the subject lines of emails.

The summary's email subject now summarises how many tests passed and
failed, and also categorises tests in the body of the email.



=== Packages/TestScripts/summarizer.py 1.1 => 1.2 ===
--- Packages/TestScripts/summarizer.py:1.1	Thu Dec 11 11:14:26 2003
+++ Packages/TestScripts/summarizer.py	Mon Dec 22 12:02:49 2003
@@ -34,11 +34,57 @@
 archive_url_template = 'http://mail.zope.org/pipermail/zope-tests/%s-%s'
 mailfrom = 'Zope tests summarizer <zopetests at z3u.com>'
 mailto = 'Zope Coders <zope-coders at zope.org>'
+mailto = 'Zope Coders <steve at z3u.com>'
 smtpserver = 'mail.z3u.com'
 
+# used when debugging
+print_not_email = False
+
 months = ("January February March April May June July August September "
           "October November December").split()
 
+def create_subject_regex():
+    """Create a regex that parses subjects like these:
+
+    OK -- anything at all -- Description
+    FAIL -- anything -- Descrcription
+    FAIL (failures=1) -- anything -- description
+    FAIL (errors=10) -- anything -- description
+    FAIL (errors=23, failures=3) -- anything -- description
+    FAIL (failures=3, errors=2) -- anything -- description
+    FAIL (errors=23 failures=3) -- anything -- description
+    FAIL (failures=3 errors=2) -- anything -- description
+    FAIL (errors:23, failures:3) -- anything -- description
+    FAIL (failures:3, errors:2) -- anything -- description
+    FAILED (errors=1): Test ZODB MVCC / Python 2.3 / Linux
+    OK: Test Zope 2.7 / Python 2.3 / Linux
+
+    TODO: Write these examples as a DocTest.
+    """
+    ok_or_fail = r"(?P<success>OK|FAIL(ED)?)"
+    failures_errors = (r"failures[=:](?P<failures1>\d+)"
+                       r"(,?\s*errors[=:](?P<errors1>\d+))?")
+    errors_failures = (r"errors[=:](?P<errors2>\d+)"
+                       r"(,?\s*failures[=:](?P<failures2>\d+))?")
+    success = (r"%(ok_or_fail)s"
+               r"(\s*[(]"
+               r"(%(failures_errors)s|%(errors_failures)s)"
+               r"[)])?"
+               ) % vars()
+    anything = r"(?P<anything>.*?)"
+    description = r"(?P<description1>.*?)"
+
+    full_regex = (r"^%(success)s"
+                  r":?\s+(?P<description2>.*)|"
+                  r"\s+--\s+"
+                  r"%(anything)s"
+                  r"\s+--\s+"
+                  r"%(description)s$"
+                  ) % vars()
+    return re.compile(full_regex)
+
+subject_regex = create_subject_regex()
+
 
 def get_archive(year, month):
     """Returns a list of the URLs archived for the given year and month.
@@ -73,6 +119,23 @@
             )
         self.fromaddr = fromaddr
         self.subject = subject
+        subject_search = subject_regex.search(self.subject)
+        self.subjectparsed = bool(subject_search)
+        if subject_search:
+            groups = subject_search.groupdict()
+            self.failed = groups['success'] != 'OK'
+            self.description = (groups['description1'] or
+                                groups['description2'])
+            self.anything = groups['anything']
+            self.failures = int(groups['failures1'] or
+                                groups['failures2'] or 0)
+            self.errors = int(groups['errors1'] or groups['errors2'] or 0)
+
+    def printToStream(self, stream):
+        print >>stream, "Subject: %s" % self.subject
+        print >>stream, "From: %s" % self.fromaddr
+        print >>stream, "Date: %s" % self.datetext
+        print >>stream, "URL: %s" % self.url
 
 def get_message(url):
     """Returns a Message object from the message archived at the given URL."""
@@ -158,31 +221,67 @@
             ['%s from %s' % (msgcount[addr], addr) for addr in fromaddrs])+'.'
 
     print >>out
-    print >>out
 
     # We want the messages to be oldest first, so reverse them.
     messages.reverse()
 
+    fail_messages = []
+    ok_messages = []
+    unknown_messages = []
+
     for message in messages:
-        print >>out, "Subject: %s" % message.subject
-        print >>out, "From: %s" % message.fromaddr
-        print >>out, "Date: %s" % message.datetext
-        print >>out, "URL: %s" % message.url
-        print >>out
-
-    # Create a text/plain message.
-    msg = MIMEText(out.getvalue())
-
-    msg['Subject'] = 'summary of automated tests'
-    msg['From'] = mailfrom
-    msg['To'] = mailto
-
-    fromname, fromaddr = parseaddr(mailfrom)
-    toname, toaddr = parseaddr(mailto)
-
-    s = smtplib.SMTP(smtpserver)
-    s.sendmail(fromaddr, [toaddr], msg.as_string())
-    s.close()
+        if message.subjectparsed:
+            if message.failed:
+                fail_messages.append(message)
+            else:
+                ok_messages.append(message)
+        else:
+            unknown_messages.append(message)
+
+    def print_messages(title, message_list):
+        if message_list:
+            print >>out
+            print >>out, title
+            print >>out, '-' * len(title)
+            print >>out
+            for message in message_list:
+                message.printToStream(out)
+                print >>out
+
+    print_messages('Test failures', fail_messages)
+    print_messages('Unknown', unknown_messages)
+    print_messages('Tests passed OK', ok_messages)
+
+    subject_info = ['%s %s' % (val, txt)
+                    for val, txt in (len(ok_messages), 'OK'),
+                                    (len(fail_messages), 'Failed'),
+                                    (len(unknown_messages), 'Unknown')
+                    if val
+                    ]
+    subject = 'Zope tests: ' + ', '.join(subject_info)
+
+    if print_not_email:
+        print "Not sending this email."
+        print
+        print "Subject:", subject
+        print "From:", mailfrom
+        print "To:", mailto
+        print
+        print out.getvalue()
+    else:
+        # Create a text/plain message.
+        msg = MIMEText(out.getvalue())
+
+        msg['Subject'] = subject
+        msg['From'] = mailfrom
+        msg['To'] = mailto
+
+        fromname, fromaddr = parseaddr(mailfrom)
+        toname, toaddr = parseaddr(mailto)
+
+        s = smtplib.SMTP(smtpserver)
+        s.sendmail(fromaddr, [toaddr], msg.as_string())
+        s.close()
 
 if __name__ == '__main__':
     main()




More information about the Zope-CVS mailing list