]> git.decadent.org.uk Git - dak.git/blob - dak/bts_categorize.py
Merge remote branch 'tolimar/tolimar-adopt-bts-categorize' into merge
[dak.git] / dak / bts_categorize.py
1 #!/usr/bin/python
2
3 """
4 bts -- manage bugs filed against ftp.debian.org
5
6 @contact: Debian FTP Master <ftpmaster@debian.org>
7 @copyright: 2009 Mike O'Connor <stew@vireo.org>
8 @copyright: 2010 Alexander Reichle-Schmehl <tolimar@debian.org>
9 @license: GNU General Public License version 2 or later
10 """
11
12 #  This program is free software; you can redistribute it and/or modify it
13 #  under the terms of the GNU General Public License as published by the
14 #  Free Software Foundation; either version 2, or (at your option) any
15 #  later version.
16 #
17 #  This program is distributed in the hope that it will be useful,
18 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
19 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 #  GNU General Public License for more details.
21 #
22 #  You should have received a copy of the GNU General Public License
23 #  along with this program; if not, write to the Free Software
24 #  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25 #  USA.
26
27 ################################################################################
28 ################################################################################
29
30 import sys
31 import re
32 import logging
33 log = logging.getLogger()
34
35 import apt_pkg
36 from daklib import utils
37 import debianbts as bts
38
39 def usage():
40     print """
41 SYNOPSIS
42     dak bts-categorize [options]
43
44 OPTIONS
45     -s
46     --simulate
47         Don't send email, instead output the lines that would be sent to
48         control@b.d.o.
49
50     -v
51     --verbose
52         Print more informational log messages
53
54     -q
55     --quiet
56         Suppress informational messages
57
58     -h
59     --help
60         Print this documentation.
61 """
62
63 arguments = [('s','simulate','BtsCategorize::Options::Simulate'),
64              ('v', 'verbose', 'BtsCategorize::Options::Verbose'),
65              ('q', 'quiet', 'BtsCategorize::Options::Quiet'),
66              ('h', 'help', 'BtsCategorize::Options::Help')]
67
68 class BugClassifier(object):
69     """
70     classify bugs using usertags based on the bug subject lines
71
72     >>> BugClassifier.rm_re.match( "RM: asdf" ) != None
73     True
74     >>> BugClassifier.rm_re.match( "[dak] Packages.diff/Index broken" ) != None
75     False
76     >>> BugClassifier.dak_re.match( "[dak] Packages.diff/Index broken" ) != None
77     True
78     """
79     rm_re = re.compile( "^RM" )
80     dak_re = re.compile( "^\[dak\]" )
81     arch_re = re.compile( "^\[Architectures\]" )
82
83     classifiers = { rm_re: 'remove',
84                     dak_re: 'dak',
85                     arch_re: 'archs'}
86
87     def unclassified_bugs(self):
88         """
89         Returns a list of open bugs which have not yet been classified
90         by one of our usertags.
91         """
92
93         tagged_bugs = bts.get_usertag('ftp.debian.org@packages.debian.org')
94         tagged_bugs_ftp = []
95         for tags in tagged_bugs.keys():
96                 tagged_bugs_ftp += tagged_bugs[tags]
97
98         return [ bug for bug in bts.get_status( bts.get_bugs("package", "ftp.debian.org" ) ) \
99                      if bug.pending=='pending' and not bug.bug_num in tagged_bugs_ftp ]
100
101
102     def classify_bug(self, bug):
103         """
104         if any of our classifiers match, return a newline terminated
105         command to set an appropriate usertag, otherwise return an
106         empty string
107         """
108         retval = ""
109
110         for classifier in self.classifiers.keys():
111             if classifier.match(bug.subject):
112                 retval = "usertag %s %s\n" % (bug.bug_num,
113                                             self.classifiers[classifier])
114                 break
115
116         if retval:
117             log.info(retval)
118         else:
119             log.debug("Unmatched: [%s] %s" % (bug.bug_num, bug.summary))
120
121         return retval
122
123     def email_text(self):
124         controls = ""
125
126         bc = BugClassifier()
127         try:
128             for bug in bc.unclassified_bugs():
129                 controls += bc.classify_bug(bug)
130
131             return controls
132         except:
133             log.error("couldn't retrieve bugs from soap interface: %s" % sys.exc_info()[0])
134             return None
135
136 def send_email(commands, simulate=False):
137     global Cnf
138
139     Subst = {'__COMMANDS__' : commands,
140              "__DAK_ADDRESS__": Cnf["Dinstall::MyAdminAddress"]}
141
142     bts_mail_message = utils.TemplateSubst(
143         Subst,Cnf["Dir::Templates"]+"/bts-categorize")
144
145     if simulate:
146         print bts_mail_message
147     else:
148         utils.send_mail( bts_mail_message )
149
150 def main():
151     """
152     for now, we just dump a list of commands that could be sent for
153     control@b.d.o
154     """
155     global Cnf
156     Cnf = utils.get_conf()
157
158     for arg in arguments:
159         opt = "BtsCategorize::Options::%s" % arg[1]
160         if not Cnf.has_key(opt):
161             Cnf[opt] = ""
162
163     packages = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
164     Options = Cnf.SubTree('BtsCategorize::Options')
165
166     if Options["Help"]:
167         usage()
168         sys.exit( 0 )
169
170     if Options["Quiet"]:
171         level=logging.ERROR
172
173     elif Options["Verbose"]:
174         level=logging.DEBUG
175
176     else:
177         level=logging.INFO
178
179     logging.basicConfig( level=level,
180                          format='%(asctime)s %(levelname)s %(message)s',
181                          stream = sys.stderr )
182
183     body = BugClassifier().email_text()
184
185     if body:
186         send_email(body, Options["Simulate"])
187
188     else:
189         log.info( "nothing to do" )
190
191
192 if __name__ == '__main__':
193 #    import doctest
194 #    doctest.testmod()
195     main()