]> git.decadent.org.uk Git - dak.git/blobdiff - dak/bts_categorize.py
Merge remote-tracking branch 'nthykier/auto-decruft'
[dak.git] / dak / bts_categorize.py
index 11fc18c61b60ca6a3a899c222db6c973da953508..c8739af65618fa16ff2c569306f61a45e94dc975 100755 (executable)
@@ -1,9 +1,14 @@
 #!/usr/bin/python
 
-#  bts -- manage bugs filed against ftp.debian.org
-#
-#  Copyright 2009 Mike O'Connor <stew@vireo.org>
-#
+"""
+bts -- manage bugs filed against ftp.debian.org
+
+@contact: Debian FTP Master <ftpmaster@debian.org>
+@copyright: 2009 Mike O'Connor <stew@vireo.org>
+@copyright: 2010 Alexander Reichle-Schmehl <tolimar@debian.org>
+@license: GNU General Public License version 2 or later
+"""
+
 #  This program is free software; you can redistribute it and/or modify it
 #  under the terms of the GNU General Public License as published by the
 #  Free Software Foundation; either version 2, or (at your option) any
 ################################################################################
 ################################################################################
 
+import sys
+import re
+import logging
+log = logging.getLogger()
+
+import apt_pkg
+from daklib import utils
+import debianbts as bts
+
 def usage():
     print """
 SYNOPSIS
-    dak bts-categorize [options] command
-
-COMMANDS
-    list-categories
-        List the currently defind categorizations for bugs
-
-    categorize
-        Find the bugs filed against ftp.debian.org which have no usertag
-        and see if we can categorize the bug by adding a usertag by matching
-        the subject against a list of regexps.
+    dak bts-categorize [options]
 
 OPTIONS
     -s
@@ -60,15 +65,6 @@ arguments = [('s','simulate','BtsCategorize::Options::Simulate'),
              ('q', 'quiet', 'BtsCategorize::Options::Quiet'),
              ('h', 'help', 'BtsCategorize::Options::Help')]
 
-import sys
-import re
-import logging
-log = logging.getLogger()
-
-import apt_pkg
-from daklib import utils
-from btsutils.debbugs import debbugs
-
 class BugClassifier(object):
     """
     classify bugs using usertags based on the bug subject lines
@@ -83,23 +79,26 @@ class BugClassifier(object):
     rm_re = re.compile( "^RM" )
     dak_re = re.compile( "^\[dak\]" )
     arch_re = re.compile( "^\[Architectures\]" )
+    override_re = re.compile( "^override" )
 
     classifiers = { rm_re: 'remove',
                     dak_re: 'dak',
-                    arch_re: 'archs'}
-
-    def __init__( self ):
-        self.bts = debbugs()
-        self.bts.setUsers(['ftp.debian.org@packages.debian.org'])
-
+                    arch_re: 'archs',
+                    override_re: 'override'}
 
     def unclassified_bugs(self):
         """
         Returns a list of open bugs which have not yet been classified
         by one of our usertags.
         """
-        return [ bug for bug in self.bts.query("pkg:ftp.debian.org") \
-                     if bug.status=='pending' and not bug.usertags ]
+
+       tagged_bugs = bts.get_usertag('ftp.debian.org@packages.debian.org')
+       tagged_bugs_ftp = []
+       for tags in tagged_bugs.keys():
+               tagged_bugs_ftp += tagged_bugs[tags]
+
+        return [ bug for bug in bts.get_status( bts.get_bugs("package", "ftp.debian.org" ) ) \
+                     if bug.pending=='pending' and not bug.bug_num in tagged_bugs_ftp ]
 
 
     def classify_bug(self, bug):
@@ -111,15 +110,15 @@ class BugClassifier(object):
         retval = ""
 
         for classifier in self.classifiers.keys():
-            if classifier.match(bug.summary):
-                retval = "usertag %s %s\n" % (bug.bug,
+            if classifier.match(bug.subject):
+                retval = "usertag %s %s\n" % (bug.bug_num,
                                             self.classifiers[classifier])
                 break
 
         if retval:
             log.info(retval)
         else:
-            log.debug("Unmatched: [%s] %s" % (bug.bug, bug.summary))
+            log.debug("Unmatched: [%s] %s" % (bug.bug_num, bug.subject))
 
         return retval
 
@@ -127,25 +126,28 @@ class BugClassifier(object):
         controls = ""
 
         bc = BugClassifier()
-        for bug in bc.unclassified_bugs():
-            controls += bc.classify_bug(bug)
-
-        if controls:
-            return 'user ftp.debian.org@packages.debian.org\n' + controls
-
-import smtplib
-import email.Message
-
-def send_email(body):
-    to = 'control@bugs.debian.org'
-    sender = 'ak@ries.debian.org'
-    message = email.Message.Message()
-    message["To"] = to
-    message["From"] = sender
-    message.set_payload(body)
-    mailServer = smtplib.SMTP('localhost')
-    mailServer.sendmail(sender, to, message.as_string())
-    mailServer.quit()
+        try:
+            for bug in bc.unclassified_bugs():
+                controls += bc.classify_bug(bug)
+
+            return controls
+        except:
+            log.error("couldn't retrieve bugs from soap interface: %s" % sys.exc_info()[0])
+            return None
+
+def send_email(commands, simulate=False):
+    global Cnf
+
+    Subst = {'__COMMANDS__' : commands,
+             "__DAK_ADDRESS__": Cnf["Dinstall::MyAdminAddress"]}
+
+    bts_mail_message = utils.TemplateSubst(
+        Subst,Cnf["Dir::Templates"]+"/bts-categorize")
+
+    if simulate:
+        print bts_mail_message
+    else:
+        utils.send_mail( bts_mail_message )
 
 def main():
     """
@@ -160,8 +162,8 @@ def main():
         if not Cnf.has_key(opt):
             Cnf[opt] = ""
 
-    packages = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
-    Options = Cnf.SubTree('BtsCategorize::Options')
+    packages = apt_pkg.parse_commandline(Cnf, arguments, sys.argv)
+    Options = Cnf.subtree('BtsCategorize::Options')
 
     if Options["Help"]:
         usage()
@@ -183,10 +185,7 @@ def main():
     body = BugClassifier().email_text()
 
     if body:
-        if Options["Simulate"]:
-            print body
-        else:
-            send_email(body)
+        send_email(body, Options["Simulate"])
 
     else:
         log.info( "nothing to do" )