File: logwatcher.py

package info (click to toggle)
buildbot 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,080 kB
  • sloc: python: 174,183; sh: 1,204; makefile: 332; javascript: 119; xml: 16
file content (152 lines) | stat: -rw-r--r-- 4,884 bytes parent folder | download
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
# This file is part of Buildbot.  Buildbot 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, version 2.
#
# 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 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., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members
from __future__ import annotations

import os
import platform
from typing import TYPE_CHECKING
from typing import cast

from twisted.internet import defer
from twisted.internet import error
from twisted.internet import protocol
from twisted.internet import reactor
from twisted.internet.interfaces import ITransport
from twisted.protocols.basic import LineOnlyReceiver
from twisted.python.failure import Failure
from zope.interface import implementer

from buildbot_worker.compat import unicode2bytes

if TYPE_CHECKING:
    from typing import Iterable

    from twisted.internet.interfaces import IAddress
    from twisted.internet.interfaces import IDelayedCall
    from twisted.internet.interfaces import IReactorProcess
    from twisted.internet.interfaces import IReactorTime


@implementer(ITransport)
class FakeTransport:
    disconnecting = False

    def write(self, data: bytes) -> None:
        raise NotImplementedError()

    def writeSequence(self, data: Iterable[bytes]) -> None:
        raise NotImplementedError()

    def loseConnection(self) -> None:
        raise NotImplementedError()

    def getPeer(self) -> IAddress:
        raise NotImplementedError()

    def getHost(self) -> IAddress:
        raise NotImplementedError()


class WorkerTimeoutError(Exception):
    pass


class TailProcess(protocol.ProcessProtocol):
    lw: LogWatcher

    def outReceived(self, data: bytes) -> None:
        self.lw.dataReceived(unicode2bytes(data))

    def errReceived(self, data: bytes) -> None:
        print(f"ERR: '{data!r}'")


class LogWatcher(LineOnlyReceiver):
    POLL_INTERVAL = 0.1
    TIMEOUT_DELAY = 10.0
    delimiter = unicode2bytes(os.linesep)

    def __init__(self, logfile: str) -> None:
        self.logfile = logfile
        self.in_reconfig = False
        self.transport = FakeTransport()
        self.pp = TailProcess()
        self.pp.lw = self
        self.timer: IDelayedCall | None = None

        self.d: defer.Deferred[str]

    def start(self) -> defer.Deferred[str]:
        # If the log file doesn't exist, create it now.
        if not os.path.exists(self.logfile):
            open(self.logfile, 'a').close()

        # return a Deferred that fires when the start process has
        # finished. It errbacks with TimeoutError if the finish line has not
        # been seen within 10 seconds, and with ReconfigError if the error
        # line was seen. If the logfile could not be opened, it errbacks with
        # an IOError.
        if platform.system().lower() == 'sunos' and os.path.exists('/usr/xpg4/bin/tail'):
            tailBin = "/usr/xpg4/bin/tail"
        elif platform.system().lower() == 'haiku' and os.path.exists('/bin/tail'):
            tailBin = "/bin/tail"
        else:
            tailBin = "/usr/bin/tail"
        self.p = cast("IReactorProcess", reactor).spawnProcess(
            self.pp,
            tailBin,
            ("tail", "-f", "-n", "0", self.logfile),
            env=os.environ,
        )
        self.running = True
        d = defer.maybeDeferred(self._start)
        return d

    def _start(self) -> defer.Deferred[str]:
        self.d = defer.Deferred()
        self.timer = cast("IReactorTime", reactor).callLater(self.TIMEOUT_DELAY, self.timeout)
        return self.d

    def timeout(self) -> None:
        self.timer = None
        e = WorkerTimeoutError()
        self.finished(Failure(e))

    def finished(self, results: str | Failure) -> None:
        try:
            self.p.signalProcess("KILL")
        except error.ProcessExitedAlready:
            pass
        if self.timer:
            self.timer.cancel()
            self.timer = None
        self.running = False
        self.in_reconfig = False
        self.d.callback(results)

    def lineReceived(self, line: bytes) -> None:
        if not self.running:
            return None
        if b"Log opened." in line:
            self.in_reconfig = True
        if b"loading configuration from" in line:
            self.in_reconfig = True

        if self.in_reconfig:
            print(line)

        if b"message from master: attached" in line:
            return self.finished("buildbot-worker")
        return None