1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., or visit: http://www.gnu.org/.
##
## Author(s): Ronaldo Maia <romaia@async.com.br>
##
##
import gtk
import gnomevfs
import pynotify
import gobject
import imp
import traceback
import time
import unittest
import sys
from kiwi.utils import gsignal
ROUND_TIME = 1 # minutes
class DojoMonitor(gobject.GObject):
(STATUS_PASS,
STATUS_FAIL,
STATUS_ERROR,
STATUS_LOAD_ERROR) = range(4)
gsignal('status-changed', int, object)
def __init__(self, name):
gobject.GObject.__init__(self)
uri = gnomevfs.make_uri_from_shell_arg(name)
gnomevfs.monitor_add(uri,
gnomevfs.MONITOR_FILE,
self._on_monitor)
self.name = name
def _load_module(self):
try:
self.module = imp.load_source('tests', self.name)
except:
self.traceback = traceback.format_exc()
self.module = None
def _on_monitor(self, dir, file, event):
if event == gnomevfs.MONITOR_EVENT_CHANGED:
self.run_tests()
def run_tests(self):
self._load_module()
if self.module is None:
self.emit('status-changed',
self.STATUS_LOAD_ERROR,
self.traceback)
return
loader = unittest.TestLoader()
suit = loader.loadTestsFromTestCase(self.module.StubTests)
results = unittest.TestResult()
suit.run(results)
if results.failures:
self.emit('status-changed', self.STATUS_FAIL, results)
else:
self.emit('status-changed', self.STATUS_PASS, results)
class DojoNotification(pynotify.Notification):
def __init__(self):
pynotify.init('DojoTools')
pynotify.Notification.__init__(self, 'DojoTools', 'Foo Bar')
self.connect('closed', self._on_closed)
#self.set_property('icon-name', gtk.STOCK_DIALOG_WARNING)
self._add_timeout()
self._waiting_next = False
def _on_closed(self, notification):
if self._waiting_next:
self._add_timeout()
self._waiting_next = False
def _add_timeout(self):
self.round_start = time.time()
self._timeout = gobject.timeout_add(ROUND_TIME * 60 * 1000,
self._on_timeout)
def _on_timeout(self):
self.notify_timeout()
def _clear(self):
self.close()
self.clear_actions()
self.clear_hints()
def notify_failure(self, results):
if self._waiting_next:
return
self._clear()
messages = []
for test, trace in results.failures:
message = ""
lines = trace.split('\n')
line_number = lines[-4].split(', ')[1]
message += "<b>%s</b>: %s \n%s" % (test, line_number, lines[-2])
messages.append(message)
message = '\n'.join(messages)
self.set_property('summary',
'%s Failures' % len(results.failures))
self.set_property('body',
message)
self.show()
def notify_error(self, message):
if self._waiting_next:
return
self._clear()
self.set_property('summary', 'Error')
self.set_property('body', message)
self.show()
def notify_load_error(self, message):
if self._waiting_next:
return
self._clear()
self.set_property('summary', 'Load Error')
self.set_property('body', message)
self.show()
def notify_timeout(self):
self._clear()
self.set_timeout(0)
self.add_action('continue', 'Continue', self._on_continue)
self.set_property('summary', 'Next, please')
self.set_property('body', 'Get your ass out of there!')
self.set_property('icon-name', gtk.STOCK_DIALOG_WARNING)
self.show()
self._waiting_next = True
def _on_continue(self, notification, action):
self._add_timeout()
self._waiting_next = False
class DojoTray(gtk.StatusIcon):
def __init__(self, monitor, notification):
gtk.StatusIcon.__init__(self)
self.notification = notification
notification.attach_to_status_icon(self)
self.set_status(monitor.STATUS_PASS)
monitor.connect('status-changed', self._on_status_changed)
gobject.timeout_add(1000, self._update_tooltip)
def _update_tooltip(self):
elapsed = int(time.time() - self.notification.round_start)
minutes = int(elapsed/60.0)
seconds = elapsed - minutes*60
self.set_tooltip("time elapsed: %d:%02d" % (minutes, seconds))
return True
def _on_status_changed(self, monitor, status, results):
self.set_status(status, results)
def set_status(self, status, results=None):
if status == DojoMonitor.STATUS_PASS:
self.set_from_stock(gtk.STOCK_YES)
self.notification.close()
elif status == DojoMonitor.STATUS_FAIL:
self.set_from_stock(gtk.STOCK_NO)
self.notification.notify_failure(results)
elif status == DojoMonitor.STATUS_ERROR:
self.set_from_stock(gtk.STOCK_DIALOG_WARNING)
self.notification.notify_error(results)
elif status == DojoMonitor.STATUS_LOAD_ERROR:
self.set_from_stock(gtk.STOCK_DIALOG_WARNING)
self.notification.notify_load_error(results)
class DojoTools:
def __init__(self, name):
self.monitor = DojoMonitor(name)
self.notification = DojoNotification()
self.tray = DojoTray(self.monitor, self.notification)
if __name__ == '__main__':
if len(sys.argv) < 2:
print 'usage:'
else:
name = sys.argv[1]
dojo = DojoTools(name)
gtk.main()
|