Fix #739120 (Migrate calibre bzr commit plugin to launchpad)

This commit is contained in:
Kovid Goyal 2011-03-20 21:09:18 -06:00
parent 99cd38f8d9
commit 32703cb261

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python2
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
@ -8,114 +8,71 @@ __docformat__ = 'restructuredtext en'
Plugin to make the commit command automatically close bugs when the commit
message contains `Fix #number` or `Implement #number`. Also updates the commit
message with the summary of the closed bug. It also set the `--fixes` metadata
appropriately. Currently only works with a Trac bug repository with the XMLRPC
plugin enabled.
To use copy this file into `~/.bazaar/plugins` and add the following to branch.conf
in the working tree you want to use it with::
trac_reponame_url = <url>
trac_reponame_username = <username>
trac_reponame_password = <password>
appropriately.
'''
import os, re, xmlrpclib, subprocess
from bzrlib.builtins import cmd_commit as _cmd_commit, tree_files
from bzrlib import branch
import re, urllib, importlib, sys
from bzrlib.builtins import cmd_commit as _cmd_commit
import bzrlib
from lxml import html
SENDMAIL = ('/home/kovid/work/kde', 'pgp_mail')
class cmd_commit(_cmd_commit):
@classmethod
def trac_url(self, username, password, url):
return url.replace('//', '//%s:%s@'%(username, password))+'/login/xmlrpc'
def get_trac_summary(self, bug, url):
print 'Getting bug summary for bug #%s'%bug,
server = xmlrpclib.ServerProxy(url)
attributes = server.ticket.get(int(bug))[-1]
print attributes['summary']
return attributes['summary']
def expand_bug(self, msg, nick, config, bug_tracker, type='trac'):
prefix = '%s_%s_'%(type, nick)
username = config.get_user_option(prefix+'username')
password = config.get_user_option(prefix+'password')
close_bug = config.get_user_option(prefix+'pattern')
if close_bug is None:
close_bug = r'(Fix|Implement|Fixes|Fixed|Implemented)\s+#(\d+)'
def expand_bug(self, msg):
close_bug = r'(Fix|Implement|Fixes|Fixed|Implemented)\s+#(\d+)'
close_bug_pat = re.compile(close_bug, re.IGNORECASE)
match = close_bug_pat.search(msg)
if not match:
return msg, None, None, None
action, bug = match.group(1), match.group(2)
summary = ''
if type == 'trac':
url = self.trac_url(username, password, bug_tracker)
summary = self.get_trac_summary(bug, url)
raw = urllib.urlopen('https://bugs.launchpad.net/calibre/+bug/' +
bug).read()
h1 = html.fromstring(raw).xpath('//h1[@id="edit-title"]')[0]
summary = html.tostring(h1, method='text', encoding=unicode).strip()
print 'Working on bug:', summary
if summary:
msg = msg.replace('#%s'%bug, '#%s (%s)'%(bug, summary))
msg = msg.replace('Fixesed', 'Fixed')
return msg, bug, url, action
def get_bugtracker(self, basedir, type='trac'):
config = os.path.join(basedir, '.bzr', 'branch', 'branch.conf')
bugtracker, nick = None, None
if os.access(config, os.R_OK):
for line in open(config).readlines():
match = re.search(r'%s_(\S+)_url\s*=\s*(\S+)'%type, line)
if match:
nick, bugtracker = match.group(1), match.group(2)
break
return nick, bugtracker
def expand_message(self, msg, tree):
nick, bugtracker = self.get_bugtracker(tree.basedir, type='trac')
if not bugtracker:
return msg
config = branch.Branch.open(tree.basedir).get_config()
msg, bug, url, action = self.expand_bug(msg, nick, config, bugtracker)
return msg, bug, url, action, nick, config
return msg, bug, action
def run(self, message=None, file=None, verbose=False, selected_list=None,
unchanged=False, strict=False, local=False, fixes=None,
author=None, show_diff=False, exclude=None):
nick = config = bug = action = None
bug = action = None
if message:
try:
message, bug, url, action, nick, config = \
self.expand_message(message, tree_files(selected_list)[0])
except ValueError:
pass
message, bug, action = self.expand_bug(message)
if nick and bug and not fixes:
fixes = [nick+':'+bug]
if bug and not fixes:
fixes = ['lp:'+bug]
ret = _cmd_commit.run(self, message=message, file=file, verbose=verbose,
selected_list=selected_list, unchanged=unchanged,
strict=strict, local=local, fixes=fixes,
author=author, show_diff=show_diff, exclude=exclude)
if message and bug and action and nick and config:
self.close_bug(bug, action, url, config)
if message and bug and action:
self.close_bug(bug, action)
return ret
def close_bug(self, bug, action, url, config):
def close_bug(self, bug, action):
print 'Closing bug #%s'% bug
#nick = config.get_nickname()
suffix = config.get_user_option('bug_close_comment')
if suffix is None:
suffix = 'The fix will be in the next release.'
suffix = ('The fix will be in the next release.'
'calibre is usually released every Friday.')
action = action+'ed'
msg = '%s in branch %s. %s'%(action, 'lp:calibre', suffix)
msg = msg.replace('Fixesed', 'Fixed')
server = xmlrpclib.ServerProxy(url)
server.ticket.update(int(bug), msg,
{'status':'closed', 'resolution':'fixed'},
True)
subprocess.Popen('/home/kovid/work/kde/mail.py -f --delay 10'.split())
msg += '\n\n status fixreleased'
sys.path.insert(0, SENDMAIL[0])
sendmail = importlib.import_module(SENDMAIL[1])
to = bug+'@bugs.launchpad.net'
sendmail.sendmail(msg, to, 'Re: calibre bug '+bug)
bzrlib.commands.register_command(cmd_commit)