This repository has been archived by the owner on Jun 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
log_handlers.py
95 lines (75 loc) · 3.21 KB
/
log_handlers.py
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
import os
import subprocess
import tempfile
from twisted.python import log as twlog
from twisted.python import failure
from twisted.internet import defer, reactor
from buildbot.status import base
class ThreadedLogHandler(base.StatusReceiverMultiService):
compare_attrs = ['categories', 'builders']
def __init__(self, categories=None, builders=None):
base.StatusReceiverMultiService.__init__(self)
self.categories = categories
self.builders = builders
# you should either limit on builders or categories, not both
if self.builders != None and self.categories != None:
twlog.err("Please specify only builders to ignore or categories to include")
raise ValueError("Please specify only builders or categories")
self.watched = []
def setServiceParent(self, parent):
base.StatusReceiverMultiService.setServiceParent(self, parent)
self.setup()
def setup(self):
self.master_status = self.parent.getStatus()
self.master_status.subscribe(self)
def disownServiceParent(self):
self.master_status.unsubscribe(self)
for w in self.watched:
w.unsubscribe(self)
return base.StatusReceiverMultiService.disownServiceParent(self)
def stopService(self):
base.StatusReceiverMultiService.stopService(self)
def builderAdded(self, name, builder):
# only subscribe to builders we are interested in
if self.categories != None and builder.category not in self.categories:
return None
self.watched.append(builder)
return self # subscribe to this builder
def buildStarted(self, builderName, build):
pass
def buildFinished(self, builderName, build, results):
builder = build.getBuilder()
if self.builders is not None and builderName not in self.builders:
return # ignore this build
if self.categories is not None and \
builder.category not in self.categories:
return # ignore this build
reactor.callInThread(self.handleLogs, builder, build, results)
def handleLogs(self, builder, build, results):
pass
class SubprocessLogHandler(ThreadedLogHandler):
compare_attrs = ['command', 'categories', 'builders']
def __init__(self, command, categories=None, builders=None):
ThreadedLogHandler.__init__(self, categories, builders)
self.command = command
def handleLogs(self, builder, build, results):
if isinstance(self.command, str):
cmd = [self.command]
else:
cmd = self.command[:]
cmd.extend([
os.path.join(self.master_status.basedir, builder.basedir),
str(build.number)])
properties = build.getProperties()
cmd = properties.render(cmd)
output = tempfile.TemporaryFile()
try:
twlog.msg("Running %s" % cmd)
subprocess.check_call(cmd, stdout=output, stderr=subprocess.STDOUT)
output.seek(0)
twlog.msg("Log output: %s" % output.read())
except:
twlog.msg("Error running %s" % cmd)
output.seek(0)
twlog.msg("Log output: %s" % output.read())
twlog.err()