rev 3576 - scripts

Pierre Habouzit madcoder at costa.debian.org
Mon Apr 17 23:43:00 UTC 2006


Author: madcoder
Date: 2006-04-17 23:42:58 +0000 (Mon, 17 Apr 2006)
New Revision: 3576

Modified:
   scripts/bzlink.py
Log:
make bzlink more friendly (usage, ...)

make it able to deal with mboxes.



Modified: scripts/bzlink.py
===================================================================
--- scripts/bzlink.py	2006-04-17 23:00:40 UTC (rev 3575)
+++ scripts/bzlink.py	2006-04-17 23:42:58 UTC (rev 3576)
@@ -27,10 +27,29 @@
 # SUCH DAMAGE.
 ###############################################################################
 
-import sys, re, os, ldap
-import email, smtplib
+"""
+Usage: %s [-h] [-m] [file]
+
+    Processes Bugzilla notifications to update debbugs bz* usertags
+
+    -h            this help
+    --help
+
+    -m            file is a mailbox
+    --as-mailbox
+
+    file          name of the file, - means stdin and is default
+
+"""
+
+import sys, re, os, getopt
+import email, mailbox, smtplib, ldap
 from email.MIMEText import MIMEText
 
+def usage(exitCode = 0):
+    print >> sys.stderr, __doc__.lstrip() % (sys.argv[0])
+    sys.exit(exitCode)
+
 class InvalidBzMsg(Exception):
     def __init__(self, reason):
         self.args = (reason)
@@ -56,11 +75,14 @@
 class BzMsg:
     _blocksRegex = re.compile(r"(^------- [^\n]* -------)\n", re.MULTILINE)
     _modifRegex  = re.compile(r"^ *([^ |]*)\|([^| ]*) *\|(.*)$")
+    _emptyRegex  = re.compile(r"^From nobody[^\n]*\n\n\n$")
 
     def __init__(self, fp):
         self.msg = email.message_from_file(fp)
-        fp.close()
 
+        if not len(self.msg.keys()):
+            return None
+
         self.bug = -1
         self.blocks  = {}
         self.additional = None
@@ -140,7 +162,7 @@
                 if after == "REOPENED":
                     self.btscmds.append("reopen %s" % (id))
 
-            if k == "Resolution":
+            elif k == "Resolution":
                 if before != "":
                     btscmds.append("usertag %s - bzRes-%s" % (id, before.lower()))
                 if after != "":
@@ -149,10 +171,12 @@
                     btscmds.append("tag %s + wontfix" % (id))
                 if before == "WONTFIX":
                     btscmds.append("tag %s - wontfix" % (id))
+
         if len(btscmds):
             btscmds.append("thanks")
             return '\n'.join(btscmds)
-        return None
+        else:
+            return None
 
 def createMail(From, bzm, bug):
     cmds = bzm.createCmds(bug)
@@ -182,14 +206,11 @@
 
     return mail, to
 
-patterns = [ "http://bugs.kde.org/show_bug.cgi?id=%i",
-             "http://bugs.kde.org/%i",
-             "%i at bugs.kde.org" ]
+def processBzMsg(bts, bzm):
+    patterns = [ "http://bugs.kde.org/show_bug.cgi?id=%i",
+                 "http://bugs.kde.org/%i",
+                 "%i at bugs.kde.org" ]
 
-if __name__ == "__main__":
-    bzm = BzMsg(sys.stdin)
-
-    bts = BtsQuery()
     bug = bts.fromBzBug(patterns, bzm.bug)
 
     if bug is None:
@@ -198,11 +219,51 @@
     else:
         bug = bug['debbugsID'][0]
 
-    msg, to = createMail('debian-qt-kde at lists.debian.org', bzm, bug)
+    return createMail('debian-qt-kde at lists.debian.org', bzm, bug)
 
-    s = smtplib.SMTP()
-    s.connect()
-    s.sendmail(msg['From'], to, msg.as_string())
-    s.close()
+def parseOpts():
+    try:
+        opts, args = getopt.getopt(sys.argv[1:], 'hm', ["help", "as-mailbox"])
+    except getopt.GetoptError:
+        usage(2)
 
+    isMbox = False
+
+    for o, a in opts:
+        if   o in ["-h", "--help"]:
+            usage(0)
+
+        elif o in ["-m", "--as-mailbox"]:
+            isMbox = True
+
+    if len(args) > 1:
+        usage(2)
+
+    if len(args) == 0 or args[0] == '-':
+        return isMbox, sys.stdin
+    else:
+        return isMbox, file(args[0], "r")
+
+if __name__ == "__main__":
+    bts = BtsQuery()
+
+    isMbox, fp = parseOpts()
+    to_send = []
+    if isMbox:
+        mbox = mailbox.UnixMailbox(fp, BzMsg)
+        while 1:
+            bzm = mbox.next()
+            if bzm is None:
+                break
+            to_send.append(processBzMsg(bts, bzm))
+    else:
+        to_send.append(processBzMsg(bts, BzMsg(fp)))
+    fp.close()
+
+    smtp = smtplib.SMTP()
+    smtp.connect()
+    for msg, to in to_send:
+        smtp.sendmail(msg['From'], to, msg.as_string())
+    smtp.close()
+
 # vim:set foldmethod=indent foldnestmax=1:




More information about the pkg-kde-commits mailing list