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

Source Code for Module buildbot.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 ReconfigError(Exception):
28 pass
29
30 -class TailProcess(protocol.ProcessProtocol):
31 - def outReceived(self, data):
32 self.lw.dataReceived(data)
33 - def errReceived(self, data):
34 print "ERR: '%s'" % (data,)
35 36
37 -class LogWatcher(LineOnlyReceiver):
38 POLL_INTERVAL = 0.1 39 TIMEOUT_DELAY = 10.0 40 delimiter = os.linesep 41
42 - def __init__(self, logfile):
43 self.logfile = logfile 44 self.in_reconfig = False 45 self.transport = FakeTransport() 46 self.pp = TailProcess() 47 self.pp.lw = self 48 self.timer = None
49
50 - def start(self):
51 # If the log file doesn't exist, create it now. 52 if not os.path.exists(self.logfile): 53 open(self.logfile, 'a').close() 54 55 # return a Deferred that fires when the reconfig process has 56 # finished. It errbacks with TimeoutError if the finish line has not 57 # been seen within 10 seconds, and with ReconfigError if the error 58 # line was seen. If the logfile could not be opened, it errbacks with 59 # an IOError. 60 self.p = reactor.spawnProcess(self.pp, "/usr/bin/tail", 61 ("tail", "-f", "-n", "0", self.logfile), 62 env=os.environ, 63 ) 64 self.running = True 65 d = defer.maybeDeferred(self._start) 66 return d
67
68 - def _start(self):
69 self.d = defer.Deferred() 70 self.timer = reactor.callLater(self.TIMEOUT_DELAY, self.timeout) 71 return self.d
72
73 - def timeout(self):
74 self.timer = None 75 e = BuildmasterTimeoutError() 76 self.finished(Failure(e))
77
78 - def finished(self, results):
79 try: 80 self.p.signalProcess("KILL") 81 except error.ProcessExitedAlready: 82 pass 83 if self.timer: 84 self.timer.cancel() 85 self.timer = None 86 self.running = False 87 self.in_reconfig = False 88 self.d.callback(results)
89
90 - def lineReceived(self, line):
91 if not self.running: 92 return 93 if "Log opened." in line: 94 self.in_reconfig = True 95 if "beginning configuration update" in line: 96 self.in_reconfig = True 97 98 if self.in_reconfig: 99 print line 100 101 if "message from master: attached" in line: 102 return self.finished("buildslave") 103 if "reconfig aborted" in line or 'reconfig partially applied' in line: 104 return self.finished(Failure(ReconfigError())) 105 if "Server Shut Down" in line: 106 return self.finished(Failure(ReconfigError())) 107 if "configuration update complete" in line: 108 return self.finished("buildmaster") 109 if "BuildMaster is running" in line: 110 return self.finished("buildmaster")
111