| OLD | NEW |
| (Empty) |
| 1 | |
| 2 import os | |
| 3 from twisted.python.failure import Failure | |
| 4 from twisted.internet import defer, reactor, protocol, error | |
| 5 from twisted.protocols.basic import LineOnlyReceiver | |
| 6 | |
| 7 class FakeTransport: | |
| 8 disconnecting = False | |
| 9 | |
| 10 class BuildmasterTimeoutError(Exception): | |
| 11 pass | |
| 12 class BuildslaveTimeoutError(Exception): | |
| 13 pass | |
| 14 class ReconfigError(Exception): | |
| 15 pass | |
| 16 class BuildSlaveDetectedError(Exception): | |
| 17 pass | |
| 18 | |
| 19 class TailProcess(protocol.ProcessProtocol): | |
| 20 def outReceived(self, data): | |
| 21 self.lw.dataReceived(data) | |
| 22 def errReceived(self, data): | |
| 23 print "ERR: '%s'" % (data,) | |
| 24 | |
| 25 | |
| 26 class LogWatcher(LineOnlyReceiver): | |
| 27 POLL_INTERVAL = 0.1 | |
| 28 TIMEOUT_DELAY = 10.0 | |
| 29 delimiter = os.linesep | |
| 30 | |
| 31 def __init__(self, logfile): | |
| 32 self.logfile = logfile | |
| 33 self.in_reconfig = False | |
| 34 self.transport = FakeTransport() | |
| 35 self.pp = TailProcess() | |
| 36 self.pp.lw = self | |
| 37 self.processtype = "buildmaster" | |
| 38 self.timer = None | |
| 39 | |
| 40 def start(self): | |
| 41 # If the log file doesn't exist, create it now. | |
| 42 if not os.path.exists(self.logfile): | |
| 43 open(self.logfile, 'a').close() | |
| 44 | |
| 45 # return a Deferred that fires when the reconfig process has | |
| 46 # finished. It errbacks with TimeoutError if the finish line has not | |
| 47 # been seen within 10 seconds, and with ReconfigError if the error | |
| 48 # line was seen. If the logfile could not be opened, it errbacks with | |
| 49 # an IOError. | |
| 50 self.p = reactor.spawnProcess(self.pp, "/usr/bin/tail", | |
| 51 ("tail", "-f", "-n", "0", self.logfile), | |
| 52 env=os.environ, | |
| 53 ) | |
| 54 self.running = True | |
| 55 d = defer.maybeDeferred(self._start) | |
| 56 return d | |
| 57 | |
| 58 def _start(self): | |
| 59 self.d = defer.Deferred() | |
| 60 self.timer = reactor.callLater(self.TIMEOUT_DELAY, self.timeout) | |
| 61 return self.d | |
| 62 | |
| 63 def timeout(self): | |
| 64 self.timer = None | |
| 65 if self.processtype == "buildmaster": | |
| 66 e = BuildmasterTimeoutError() | |
| 67 else: | |
| 68 e = BuildslaveTimeoutError() | |
| 69 self.finished(Failure(e)) | |
| 70 | |
| 71 def finished(self, results): | |
| 72 try: | |
| 73 self.p.signalProcess("KILL") | |
| 74 except error.ProcessExitedAlready: | |
| 75 pass | |
| 76 if self.timer: | |
| 77 self.timer.cancel() | |
| 78 self.timer = None | |
| 79 self.running = False | |
| 80 self.in_reconfig = False | |
| 81 self.d.callback(results) | |
| 82 | |
| 83 def lineReceived(self, line): | |
| 84 if not self.running: | |
| 85 return | |
| 86 if "Log opened." in line: | |
| 87 self.in_reconfig = True | |
| 88 if "loading configuration from" in line: | |
| 89 self.in_reconfig = True | |
| 90 if "Creating BuildSlave" in line: | |
| 91 self.processtype = "buildslave" | |
| 92 | |
| 93 if self.in_reconfig: | |
| 94 print line | |
| 95 | |
| 96 if "message from master: attached" in line: | |
| 97 return self.finished("buildslave") | |
| 98 if "I will keep using the previous config file" in line: | |
| 99 return self.finished(Failure(ReconfigError())) | |
| 100 if "configuration update complete" in line: | |
| 101 return self.finished("buildmaster") | |
| OLD | NEW |