X-Git-Url: https://git.ctpug.org.za/?p=bzrirker.git;a=blobdiff_plain;f=irkerhook.py;h=e9aabc8477726d850f9779cb0f6844333f62c995;hp=0f694c8f15e910cfe87ecfead10ca58337a4b488;hb=HEAD;hpb=1ee038af54756c72553edc23a529091d94592bba diff --git a/irkerhook.py b/irkerhook.py index 0f694c8..e9aabc8 100644 --- a/irkerhook.py +++ b/irkerhook.py @@ -3,71 +3,58 @@ # Copyright 2012 Neil Muller # GPL 2+ - see COPYING for details -from bzrlib import ( - errors, - revision as _mod_revision, - ) -from bzrlib.config import ( - ListOption, - Option, - bool_from_store, - int_from_store, - ) +from bzrlib.config import Option import socket import sys import json +IRKER_PORT = 6659 + class IrkerSender(object): """An irker message sender.""" - def __init__(self, branch, revision_id, config, local_branch=None, - op='commit'): + def __init__(self, branch, revision_id, config): self.config = config self.branch = branch - self.repository = branch.repository - if (local_branch is not None and - local_branch.repository.has_revision(revision_id)): - self.repository = local_branch.repository self._revision_id = revision_id self.revision = None self.revno = None - self.op = op def _setup_revision_and_revno(self): - self.revision = self.repository.get_revision(self._revision_id) + self.revision = self.branch.repository.get_revision(self._revision_id) self.revno = self.branch.revision_id_to_revno(self._revision_id) def _format(self): + """Munge the commit info into an irc message""" + delta = self.branch.repository.get_revision_delta(self._revision_id) + files = [] + [files.append(f) for (f, _, _) in delta.added] + [files.append(f) for (f, _, _) in delta.removed] + [files.append(f) for (_, f, _, _, _, _) in delta.renamed] + [files.append(f) for (f, _, _, _, _) in delta.modified] + fields = { 'project': self.project(), 'committer': self.revision.committer, - 'repo': '', + 'repo': self.branch.nick, 'rev': '%d' % self.revno, - 'files': '', + 'files': ' '.join(files), 'logmsg': self.revision.get_summary(), - 'url': self.url(), } - for name, value in self.colours(): - fields[name] = value + if len(fields['files']) > 250: + # Dangerously long looking list of files, so truncate it + fields['files'] = fields['files'][:250] + fields.update(self.colours()) text = ('%(bold)s%(project)s:%(reset)s ' '%(green)s%(committer)s%(reset)s ' '%(repo)s * %(bold)s%(rev)s%(reset)s / ' - ' %(bold)s%(files)s%(reset)s: %(logmsg)s ' - '%(brown)s%(url)s%(reset)s' % fields) + ' %(bold)s%(files)s%(reset)s: %(logmsg)s ' % fields) return text - def url(self): - """What URL to display in the subject of the mail""" - url = self.config.get('irker_url') - if url is None: - url = self.config.get('public_branch') - if url is None: - url = self.branch.base - return url - def colours(self): - colour_style = self.config.get('irker_colors') + """Utility function to handle the colours""" + colour_style = self.config.get('irker_colours') colours = { 'bold': '', 'green': '', @@ -116,38 +103,46 @@ class IrkerSender(object): """Send the info to irkerd. """ self.branch.lock_read() - self.repository.lock_read() - # These should become configurable at some point - default_server = 'localhost' - IRKER_PORT = 6659 + self.branch.repository.lock_read() + server = self.config.get('irker_server') + if not server: + server = 'localhost' + port = int(self.config.get('irker_port')) + if not port: + port = IRKER_PORT try: # Do this after we have locked, to make things faster. self._setup_revision_and_revno() channels = self.config.get('irker_channels') - msg = unicode(self._format()) - # We rely on the server truncating, rather than trying to be - # too fancy here + if channels: + channels = channels.split(',') + # If we're too long, we might not get any notification, so + # we truncate the msg to max 450 chars, and hope that's + # going to be within the 510 irc limit + msg = unicode(self._format())[:450] message = json.dumps({"to": channels, "privmsg": msg}) if channels: # We assume tcp, since I'm lazy, so we just grab that bit # of irker's code try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.connect((default_server, IRKER_PORT)) + sock.connect((server, port)) sock.sendall(message + "\n") except socket.error, e: sys.stderr.write("%s\n" % e) finally: sock.close() finally: - self.repository.unlock() + self.branch.repository.unlock() self.branch.unlock() -opt_irker_url = Option('irker_url', - help='URL to mention for branch in messages.') opt_irker_channels = Option('irker_channels', help='Channel(s) to post commit messages to.') opt_irker_colours = Option('irker_colours', help='Colour option for irker.') opt_irker_project = Option('irker_project', help='Project name to use.') +opt_irker_server = Option('irker_server', + help='host for the irkerd server (default localhost).') +opt_irker_port = Option('irker_port', + help='port for the irkerd server (default %d)' % IRKER_PORT)