Package buildslave :: Package scripts :: Module logwatcher
[frames] | no frames]

Source Code for Module buildslave.scripts.logwatcher

  1  # This file is part of Buildbot.  Buildbot is free software: you can 
  2  # redistribute it and/or modify it under the terms of the GNU General Public 
  3  # License as published by the Free Software Foundation, version 2. 
  4  # 
  5  # This program is distributed in the hope that it will be useful, but WITHOUT 
  6  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
  8  # details. 
  9  # 
 10  # You should have received a copy of the GNU General Public License along with 
 11  # this program; if not, write to the Free Software Foundation, Inc., 51 
 12  # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 13  # 
 14  # Copyright Buildbot Team Members 
 15   
 16   
 17  import os 
 18  from twisted.python.failure import Failure 
 19  from twisted.internet import defer, reactor, protocol, error 
 20  from twisted.protocols.basic import LineOnlyReceiver 
 21   
22 -class FakeTransport:
23 disconnecting = False
24
25 -class BuildmasterTimeoutError(Exception):
26 pass
27 -class BuildslaveTimeoutError(Exception):
28 pass
29 -class ReconfigError(Exception):
30 pass
31 -class BuildSlaveDetectedError(Exception):
32 pass
33
34 -class TailProcess(protocol.ProcessProtocol):
35 - def outReceived(self, data):
36 self.lw.dataReceived(data)
37 - def errReceived(self, data):
38 print "ERR: '%s'" % (data,)
39 40
41 -class LogWatcher(LineOnlyReceiver):
42 POLL_INTERVAL = 0.1 43 TIMEOUT_DELAY = 10.0 44 delimiter = os.linesep 45
46 - def __init__(self, logfile):
47 self.logfile = logfile 48 self.in_reconfig = False 49 self.transport = FakeTransport() 50 self.pp = TailProcess() 51 self.pp.lw = self 52 self.processtype = "buildmaster" 53 self.timer = None
54
55 - def start(self):
56 # If the log file doesn't exist, create it now. 57 if not os.path.exists(self.logfile): 58 open(self.logfile, 'a').close() 59 60 # return a Deferred that fires when the reconfig process has 61 # finished. It errbacks with TimeoutError if the finish line has not 62 # been seen within 10 seconds, and with ReconfigError if the error 63 # line was seen. If the logfile could not be opened, it errbacks with 64 # an IOError. 65 self.p = reactor.spawnProcess(self.pp, "/usr/bin/tail", 66 ("tail", "-f", "-n", "0", self.logfile), 67 env=os.environ, 68 ) 69 self.running = True 70 d = defer.maybeDeferred(self._start) 71 return d
72
73 - def _start(self):
74 self.d = defer.Deferred() 75 self.timer = reactor.callLater(self.TIMEOUT_DELAY, self.timeout) 76 return self.d
77
78 - def timeout(self):
79 self.timer = None 80 if self.processtype == "buildmaster": 81 e = BuildmasterTimeoutError() 82 else: 83 e = BuildslaveTimeoutError() 84 self.finished(Failure(e))
85
86 - def finished(self, results):
87 try: 88 self.p.signalProcess("KILL") 89 except error.ProcessExitedAlready: 90 pass 91 if self.timer: 92 self.timer.cancel() 93 self.timer = None 94 self.running = False 95 self.in_reconfig = False 96 self.d.callback(results)
97
98 - def lineReceived(self, line):
99 if not self.running: 100 return 101 if "Log opened." in line: 102 self.in_reconfig = True 103 if "loading configuration from" in line: 104 self.in_reconfig = True 105 if "Creating BuildSlave" in line: 106 self.processtype = "buildslave" 107 108 if self.in_reconfig: 109 print line 110 111 if "message from master: attached" in line: 112 return self.finished("buildslave") 113 if "I will keep using the previous config file" in line: 114 return self.finished(Failure(ReconfigError())) 115 if "configuration update complete" in line: 116 return self.finished("buildmaster")
117