]> sipb.mit.edu Git - snippets/.git/blob - TracZephyrPlugin/ZephyrPlugin.py
barnowl/zcrypt.pl: Pass the unencrypted message to zcrypt
[snippets/.git] / TracZephyrPlugin / ZephyrPlugin.py
1 # -*- coding: utf-8 -*-
2
3 from trac.core import *
4 from trac.ticket import ITicketChangeListener
5 import subprocess
6 import re
7 import textwrap
8 import shlex
9
10 quoted_re = re.compile('^(?:> ?\n)*> .+\n(?:>(?: .*)?\n)*', re.MULTILINE)
11
12 class ZephyrPlugin(Component):
13     implements(ITicketChangeListener)
14     
15     def zwrite(self, id, message):
16         zclass = self.config.get('ZephyrPlugin', 'class')
17         if zclass == '':
18             return
19         command = shlex.split(self.config.get('ZephyrPlugin', 'command').encode('utf-8'))
20         if not command:
21             command = ['zwrite', '-q', '-l', '-d']
22         p = subprocess.Popen(command +
23                              ['-c', zclass,
24                               '-i', 'trac-#%s' % id],
25                              stdin=subprocess.PIPE)
26         p.stdin.write(message.replace('@', '@@').encode('utf-8', 'replace'))
27         p.stdin.close()
28         p.wait()
29
30     def format_text(self, text):
31         text = re.sub(quoted_re, u'> […]\n', text)
32         lines = textwrap.fill(text).split('\n')
33         if len(lines) > 5:
34             lines = lines[:5] + [u'[…]']
35         return '\n'.join(lines)
36     
37     def ticket_created(self, ticket):
38         message = "%s filed a new ticket:\n%s\n\n%s" % (ticket['reporter'],
39                                                         ticket['summary'],
40                                                         self.format_text(ticket['description']))
41         self.zwrite(ticket.id, message)
42     
43     def ticket_changed(self, ticket, comment, author, old_values):
44         message = "(%s)\n" % ticket['summary']
45         for field in ticket.fields:
46             name = field['name']
47             if name not in old_values:
48                 pass
49             elif field['type'] == 'textarea':
50                 message += "%s changed %s to:\n%s\n" % (author, name, self.format_text(ticket[name]))
51             elif ticket[name] and old_values[name]:
52                 message += "%s changed %s from %s to %s.\n" % (author, name, old_values[name], ticket[name])
53             elif ticket[name]:
54                 message += "%s set %s to %s.\n" % (author, name, ticket[name])
55             elif old_values[name]:
56                 message += "%s deleted %s.\n" % (author, name)
57             else:
58                 message += "%s changed %s.\n" % (author, name)
59         if comment:
60             message += "%s commented:\n%s\n" % (author, self.format_text(comment))
61         self.zwrite(ticket.id, message)
62     
63     def ticket_deleted(self, ticket):
64         message = "%s deleted ticket %d" % (author, ticket.id)
65         self.zwrite(ticket.id, message)