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