| 1 |
madcoder |
3563 |
#! /usr/bin/env python
|
| 2 |
|
|
|
| 3 |
madcoder |
3566 |
import sys, re, os, ldap
|
| 4 |
|
|
import email, smtplib
|
| 5 |
|
|
from email.MIMEText import MIMEText
|
| 6 |
madcoder |
3563 |
|
| 7 |
|
|
class InvalidBzMsg(Exception):
|
| 8 |
|
|
def __init__(self, reason):
|
| 9 |
|
|
self.args = (reason)
|
| 10 |
|
|
|
| 11 |
madcoder |
3565 |
class BtsQuery:
|
| 12 |
|
|
dn = "dc=current,dc=bugs,dc=debian,dc=org"
|
| 13 |
|
|
|
| 14 |
|
|
def __init__(self):
|
| 15 |
|
|
self.l = ldap.initialize("ldap://bts2ldap.debian.net:10101")
|
| 16 |
|
|
self.l.simple_bind_s()
|
| 17 |
|
|
|
| 18 |
|
|
def search(self, filter):
|
| 19 |
|
|
return [i[1] for i in self.l.search_s(BtsQuery.dn, ldap.SCOPE_SUBTREE, filter)]
|
| 20 |
|
|
|
| 21 |
|
|
def fromBzBug(self, patterns, nb):
|
| 22 |
|
|
filter = ''.join(["(debbugsForwardedTo=" + (p % (nb)) + ")" for p in patterns])
|
| 23 |
|
|
l = self.search("(|%s)" % filter)
|
| 24 |
|
|
if len(l) is 0:
|
| 25 |
|
|
return None
|
| 26 |
|
|
else:
|
| 27 |
|
|
return l[0]
|
| 28 |
|
|
|
| 29 |
madcoder |
3563 |
class BzMsg:
|
| 30 |
madcoder |
3564 |
_blocksRegex = re.compile(r"(^------- [^\n]* -------)\n", re.MULTILINE)
|
| 31 |
|
|
_modifRegex = re.compile(r"^ *([^ |]*)\|([^| ]*) *\|(.*)$")
|
| 32 |
madcoder |
3563 |
|
| 33 |
|
|
def __init__(self, fp):
|
| 34 |
|
|
self.msg = email.message_from_file(fp)
|
| 35 |
|
|
fp.close()
|
| 36 |
|
|
|
| 37 |
|
|
self.bug = -1
|
| 38 |
madcoder |
3566 |
self.blocks = {}
|
| 39 |
madcoder |
3563 |
self.additional = None
|
| 40 |
madcoder |
3566 |
self.modifs = {}
|
| 41 |
madcoder |
3563 |
|
| 42 |
|
|
self._parse()
|
| 43 |
|
|
|
| 44 |
|
|
def _parse(self):
|
| 45 |
|
|
if 'x-bugzilla-url' not in self.msg:
|
| 46 |
|
|
raise InvalidBzMsg("Missing Bugzilla URL")
|
| 47 |
|
|
|
| 48 |
|
|
self.bug = self._findBugNumber()
|
| 49 |
|
|
|
| 50 |
|
|
if self.msg.is_multipart():
|
| 51 |
|
|
raise InvalidBzMsg("Cannot deal with multipart messages")
|
| 52 |
|
|
|
| 53 |
|
|
self._parsePayload()
|
| 54 |
|
|
|
| 55 |
|
|
def _findBugNumber(self):
|
| 56 |
|
|
if 'reply-to' in self.msg:
|
| 57 |
|
|
nb = self.msg['reply-to'].split('@')[0]
|
| 58 |
|
|
try:
|
| 59 |
|
|
return int(nb)
|
| 60 |
|
|
except:
|
| 61 |
|
|
pass
|
| 62 |
|
|
|
| 63 |
|
|
if 'subject' in self.msg:
|
| 64 |
|
|
m = re.compile(r"^\[Bug (\d*)\] ").match(self.msg['subject'])
|
| 65 |
|
|
if m:
|
| 66 |
|
|
return int(m.group(1))
|
| 67 |
|
|
|
| 68 |
|
|
raise InvalidBzMsg("Cannot guess bug number")
|
| 69 |
|
|
|
| 70 |
|
|
def _parsePayload(self, stripsig = True):
|
| 71 |
|
|
payload = self.msg.get_payload()
|
| 72 |
|
|
|
| 73 |
|
|
if stripsig:
|
| 74 |
|
|
where = payload.rfind("\n-- \n")
|
| 75 |
|
|
if where >= 0:
|
| 76 |
|
|
payload = payload[:where+1]
|
| 77 |
|
|
|
| 78 |
|
|
tmp = BzMsg._blocksRegex.split(payload)[1:]
|
| 79 |
|
|
for i in xrange(0, len(tmp) / 2):
|
| 80 |
|
|
self.blocks[tmp[2*i]] = tmp[2*i+1]
|
| 81 |
|
|
|
| 82 |
|
|
for k, v in self.blocks.iteritems():
|
| 83 |
|
|
if k == "------- You are receiving this mail because: -------":
|
| 84 |
|
|
self._parseBzChanges(v)
|
| 85 |
|
|
if k.startswith("------- Additional Comments From"):
|
| 86 |
|
|
self.additional = "%s\n\n%s" % (k, v)
|
| 87 |
|
|
|
| 88 |
|
|
def _parseBzChanges(self, s):
|
| 89 |
madcoder |
3564 |
modifs = []
|
| 90 |
|
|
for l in s.splitlines():
|
| 91 |
|
|
m = BzMsg._modifRegex.match(l)
|
| 92 |
|
|
if not m:
|
| 93 |
|
|
continue
|
| 94 |
|
|
what = m.group(1)
|
| 95 |
|
|
removed = m.group(2)
|
| 96 |
|
|
added = m.group(3)
|
| 97 |
|
|
if len(what) is 0:
|
| 98 |
|
|
w, r, a = modifs[-1]
|
| 99 |
|
|
modifs[-1] = w, r+removed, a+added
|
| 100 |
|
|
else:
|
| 101 |
|
|
modifs.append((what, removed, added))
|
| 102 |
madcoder |
3563 |
|
| 103 |
madcoder |
3564 |
for (w, r, a) in modifs:
|
| 104 |
|
|
self.modifs[w] = r, a
|
| 105 |
madcoder |
3563 |
|
| 106 |
madcoder |
3566 |
def createCmds(self, id):
|
| 107 |
|
|
btscmds = []
|
| 108 |
|
|
for k, (before, after) in self.modifs.iteritems():
|
| 109 |
madcoder |
3565 |
|
| 110 |
madcoder |
3566 |
if k == "Status":
|
| 111 |
|
|
btscmds.append("usertag %s - bzState-%s" % (id, before.lower()))
|
| 112 |
|
|
btscmds.append("usertag %s + bzState-%s" % (id, after.lower()))
|
| 113 |
|
|
if after == "REOPENED":
|
| 114 |
|
|
self.btscmds.append("reopen %s" % (id))
|
| 115 |
|
|
|
| 116 |
|
|
if k == "Resolution":
|
| 117 |
|
|
if before != "":
|
| 118 |
|
|
btscmds.append("usertag %s - bzRes-%s" % (id, before.lower()))
|
| 119 |
|
|
if after != "":
|
| 120 |
|
|
btscmds.append("usertag %s + bzRes-%s" % (id, after.lower()))
|
| 121 |
|
|
if after == "WONTFIX":
|
| 122 |
|
|
btscmds.append("tag %s + wontfix" % (id))
|
| 123 |
|
|
if before == "WONTFIX":
|
| 124 |
|
|
btscmds.append("tag %s - wontfix" % (id))
|
| 125 |
|
|
if len(btscmds):
|
| 126 |
|
|
btscmds.append("thanks")
|
| 127 |
|
|
return '\n'.join(btscmds)
|
| 128 |
|
|
return None
|
| 129 |
|
|
|
| 130 |
|
|
class BtsMail(MIMEText):
|
| 131 |
|
|
def __init__(self, bzm, btsbug, From):
|
| 132 |
|
|
pass
|
| 133 |
|
|
|
| 134 |
|
|
patterns = [ "http://bugs.kde.org/show_bug.cgi?id=%i",
|
| 135 |
|
|
"http://bugs.kde.org/%i",
|
| 136 |
|
|
"%i@bugs.kde.org" ]
|
| 137 |
|
|
|
| 138 |
madcoder |
3565 |
if __name__ == "__main__":
|
| 139 |
|
|
bzm = BzMsg(sys.stdin)
|
| 140 |
|
|
|
| 141 |
|
|
bts = BtsQuery()
|
| 142 |
madcoder |
3566 |
bug = bts.fromBzBug(patterns, bzm.bug)['debbugsID'][0]
|
| 143 |
madcoder |
3565 |
|
| 144 |
madcoder |
3566 |
cmds = bzm.createCmds(bug)
|
| 145 |
|
|
|
| 146 |
|
|
to = []
|
| 147 |
|
|
body = []
|
| 148 |
|
|
if cmds is not None:
|
| 149 |
|
|
body.append(cmds)
|
| 150 |
|
|
to.append('control@bugs.debian.org')
|
| 151 |
|
|
if bzm.additional is not None:
|
| 152 |
|
|
body.append("------- From bugzilla: <URL:%s> -------\n%s" % (bzm.msg['x-bugzilla-url'].strip(), bzm.additional))
|
| 153 |
|
|
to.append(("%s-quiet@bugs.debian.org" % bug))
|
| 154 |
|
|
|
| 155 |
|
|
if len(body) is 0:
|
| 156 |
|
|
sys.exit(0)
|
| 157 |
|
|
|
| 158 |
|
|
body = '\n\n'.join(body)
|
| 159 |
|
|
|
| 160 |
|
|
mail = MIMEText(body)
|
| 161 |
|
|
mail['From'] = 'debian-qt-kde@lists.debian.org'
|
| 162 |
|
|
mail['To'] = ', '.join(to)
|
| 163 |
|
|
mail['X-Debbugs-No-Ack'] = 'no-acks' # actual value is not used, http://www.debian.org/Bugs/Reporting
|
| 164 |
|
|
try:
|
| 165 |
|
|
mail['Content-Type'] = bzm.msg['Content-Type']
|
| 166 |
|
|
except:
|
| 167 |
|
|
mail['Content-Type'] = 'text/plain; charset="utf-8"'
|
| 168 |
|
|
|
| 169 |
|
|
print mail
|
| 170 |
|
|
|
| 171 |
|
|
|
| 172 |
|
|
# vim:set foldmethod=indent foldnestmax=1:
|