We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
FakeTransport error while starting buildbot.
I'm trying to understand why I'm getting the following error, even though the master seems to start and run normally. Googling for this message was fruitless. I'm using buildbot 2.9.3 and Twisted 20.3.0 (both latest). Do I need to downgrade Twisted?

```
(venv) alex@solitude:~/build_bot$ buildbot start master
Following twistd.log until startup finished..
Unhandled Error
Traceback (most recent call last):
  File "/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/python/log.py", line 103, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/python/log.py", line 86, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/python/context.py", line 122, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/python/context.py", line 85, in callWithContext
    return func(*args,**kw)
--- <exception caught here> ---
  File "/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/internet/posixbase.py", line 614, in _doReadOrWrite
    why = selectable.doRead()
  File "/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/internet/process.py", line 252, in doRead
    return fdesc.readFromFD(self.fd, self.dataReceived)
  File "/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/internet/fdesc.py", line 94, in readFromFD
    callback(output)
  File "/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/internet/process.py", line 256, in dataReceived
    self.proc.childDataReceived(self.name, data)
  File "/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/internet/process.py", line 922, in childDataReceived
    self.proto.childDataReceived(name, data)
  File "/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/internet/protocol.py", line 640, in childDataReceived
    self.outReceived(data)
  File "/home/alex/build_bot/venv/lib/python3.7/site-packages/buildbot/scripts/logwatcher.py", line 49, in outReceived
    self.lw.dataReceived(data)
  File "/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/protocols/basic.py", line 452, in dataReceived
    return self.lineLengthExceeded(line)
  File "/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/protocols/basic.py", line 484, in lineLengthExceeded
    return self.transport.loseConnection()
builtins.AttributeError: 'FakeTransport' object has no attribute 'loseConnection'

The buildmaster took more than 10.0 seconds to start, so we were unable to
confirm that it started correctly. Please 'tail twistd.log' and look for a line
that says 'BuildMaster is running' to verify correct startup.
```

_Originally posted by @alexreinking in https://github.com/buildbot/buildbot/discussions/5702_

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `master/buildbot/scripts/logwatcher.py`
Content:
```
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 import platform
19 
20 from twisted.internet import defer
21 from twisted.internet import error
22 from twisted.internet import protocol
23 from twisted.internet import reactor
24 from twisted.protocols.basic import LineOnlyReceiver
25 from twisted.python.failure import Failure
26 
27 from buildbot.util import unicode2bytes
28 
29 
30 class FakeTransport:
31     disconnecting = False
32 
33 
34 class BuildmasterTimeoutError(Exception):
35     pass
36 
37 
38 class BuildmasterStartupError(Exception):
39     pass
40 
41 
42 class ReconfigError(Exception):
43     pass
44 
45 
46 class TailProcess(protocol.ProcessProtocol):
47 
48     def outReceived(self, data):
49         self.lw.dataReceived(data)
50 
51     def errReceived(self, data):
52         print("ERR: '{}'".format(data))
53 
54 
55 class LogWatcher(LineOnlyReceiver):
56     POLL_INTERVAL = 0.1
57     TIMEOUT_DELAY = 10.0
58     delimiter = unicode2bytes(os.linesep)
59 
60     def __init__(self, logfile, timeout=None, _reactor=reactor):
61         self.logfile = logfile
62         self.in_reconfig = False
63         self.transport = FakeTransport()
64         self.pp = TailProcess()
65         self.pp.lw = self
66         self.timer = None
67         self._reactor = _reactor
68         self._timeout_delay = timeout or self.TIMEOUT_DELAY
69 
70     def start(self):
71         # If the log file doesn't exist, create it now.
72         if not os.path.exists(self.logfile):
73             open(self.logfile, 'a').close()
74 
75         # return a Deferred that fires when the reconfig process has
76         # finished. It errbacks with TimeoutError if the startup has not
77         # progressed for 10 seconds, and with ReconfigError if the error
78         # line was seen. If the logfile could not be opened, it errbacks with
79         # an IOError.
80         if platform.system().lower() == 'sunos' and os.path.exists('/usr/xpg4/bin/tail'):
81             tailBin = "/usr/xpg4/bin/tail"
82         elif platform.system().lower() == 'haiku' and os.path.exists('/bin/tail'):
83             tailBin = "/bin/tail"
84         else:
85             tailBin = "/usr/bin/tail"
86 
87         args = ("tail", "-f", "-n", "0", self.logfile)
88         self.p = self._reactor.spawnProcess(self.pp, tailBin, args,
89                                             env=os.environ)
90         self.running = True
91         d = defer.maybeDeferred(self._start)
92         return d
93 
94     def _start(self):
95         self.d = defer.Deferred()
96         self.startTimer()
97         return self.d
98 
99     def startTimer(self):
100         self.timer = self._reactor.callLater(self._timeout_delay, self.timeout)
101 
102     def timeout(self):
103         # was the timeout set to be ignored? if so, restart it
104         if not self.timer:
105             self.startTimer()
106             return
107 
108         self.timer = None
109         e = BuildmasterTimeoutError()
110         self.finished(Failure(e))
111 
112     def finished(self, results):
113         try:
114             self.p.signalProcess("KILL")
115         except error.ProcessExitedAlready:
116             pass
117         if self.timer:
118             self.timer.cancel()
119             self.timer = None
120         self.running = False
121         self.in_reconfig = False
122         self.d.callback(results)
123 
124     def lineReceived(self, line):
125         if not self.running:
126             return None
127         if b"Log opened." in line:
128             self.in_reconfig = True
129         if b"beginning configuration update" in line:
130             self.in_reconfig = True
131 
132         if self.in_reconfig:
133             print(line.decode())
134 
135         # certain lines indicate progress, so we "cancel" the timeout
136         # and it will get re-added when it fires
137         PROGRESS_TEXT = [b'Starting BuildMaster', b'Loading configuration from', b'added builder',
138                          b'adding scheduler', b'Loading builder', b'Starting factory']
139         for progressText in PROGRESS_TEXT:
140             if progressText in line:
141                 self.timer = None
142                 break
143 
144         if b"message from master: attached" in line:
145             return self.finished("worker")
146         if b"reconfig aborted" in line or b'reconfig partially applied' in line:
147             return self.finished(Failure(ReconfigError()))
148         if b"Server Shut Down" in line:
149             return self.finished(Failure(ReconfigError()))
150         if b"configuration update complete" in line:
151             return self.finished("buildmaster")
152         if b"BuildMaster is running" in line:
153             return self.finished("buildmaster")
154         if b"BuildMaster startup failed" in line:
155             return self.finished(Failure(BuildmasterStartupError()))
156         return None
157
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/master/buildbot/scripts/logwatcher.py b/master/buildbot/scripts/logwatcher.py
--- a/master/buildbot/scripts/logwatcher.py
+++ b/master/buildbot/scripts/logwatcher.py
@@ -21,7 +21,6 @@
 from twisted.internet import error
 from twisted.internet import protocol
 from twisted.internet import reactor
-from twisted.protocols.basic import LineOnlyReceiver
 from twisted.python.failure import Failure
 
 from buildbot.util import unicode2bytes
@@ -49,10 +48,41 @@
         self.lw.dataReceived(data)
 
     def errReceived(self, data):
-        print("ERR: '{}'".format(data))
+        self.lw.print_output("ERR: '{}'".format(data))
+
+
+class LineOnlyLongLineReceiver(protocol.Protocol):
+    """
+    This is almost the same as Twisted's LineOnlyReceiver except that long lines are handled
+    appropriately.
+    """
+    _buffer = b''
+    delimiter = b'\r\n'
+    MAX_LENGTH = 16384
+
+    def dataReceived(self, data):
+        lines = (self._buffer + data).split(self.delimiter)
+        self._buffer = lines.pop(-1)
+        for line in lines:
+            if self.transport.disconnecting:
+                # this is necessary because the transport may be told to lose
+                # the connection by a line within a larger packet, and it is
+                # important to disregard all the lines in that packet following
+                # the one that told it to close.
+                return
+            if len(line) > self.MAX_LENGTH:
+                self.lineLengthExceeded(line)
+            else:
+                self.lineReceived(line)
+
+    def lineReceived(self, line):
+        raise NotImplementedError
+
+    def lineLengthExceeded(self, line):
+        raise NotImplementedError
 
 
-class LogWatcher(LineOnlyReceiver):
+class LogWatcher(LineOnlyLongLineReceiver):
     POLL_INTERVAL = 0.1
     TIMEOUT_DELAY = 10.0
     delimiter = unicode2bytes(os.linesep)
@@ -69,8 +99,7 @@
 
     def start(self):
         # If the log file doesn't exist, create it now.
-        if not os.path.exists(self.logfile):
-            open(self.logfile, 'a').close()
+        self.create_logfile(self.logfile)
 
         # return a Deferred that fires when the reconfig process has
         # finished. It errbacks with TimeoutError if the startup has not
@@ -121,6 +150,17 @@
         self.in_reconfig = False
         self.d.callback(results)
 
+    def create_logfile(self, path):  # pragma: no cover
+        if not os.path.exists(path):
+            open(path, 'a').close()
+
+    def print_output(self, output):  # pragma: no cover
+        print(output)
+
+    def lineLengthExceeded(self, line):
+        msg = 'Got an a very long line in the log (length {} bytes), ignoring'.format(len(line))
+        self.print_output(msg)
+
     def lineReceived(self, line):
         if not self.running:
             return None
@@ -130,7 +170,7 @@
             self.in_reconfig = True
 
         if self.in_reconfig:
-            print(line.decode())
+            self.print_output(line.decode())
 
         # certain lines indicate progress, so we "cancel" the timeout
         # and it will get re-added when it fires

{"golden_diff": "diff --git a/master/buildbot/scripts/logwatcher.py b/master/buildbot/scripts/logwatcher.py\n--- a/master/buildbot/scripts/logwatcher.py\n+++ b/master/buildbot/scripts/logwatcher.py\n@@ -21,7 +21,6 @@\n from twisted.internet import error\n from twisted.internet import protocol\n from twisted.internet import reactor\n-from twisted.protocols.basic import LineOnlyReceiver\n from twisted.python.failure import Failure\n \n from buildbot.util import unicode2bytes\n@@ -49,10 +48,41 @@\n         self.lw.dataReceived(data)\n \n     def errReceived(self, data):\n-        print(\"ERR: '{}'\".format(data))\n+        self.lw.print_output(\"ERR: '{}'\".format(data))\n+\n+\n+class LineOnlyLongLineReceiver(protocol.Protocol):\n+    \"\"\"\n+    This is almost the same as Twisted's LineOnlyReceiver except that long lines are handled\n+    appropriately.\n+    \"\"\"\n+    _buffer = b''\n+    delimiter = b'\\r\\n'\n+    MAX_LENGTH = 16384\n+\n+    def dataReceived(self, data):\n+        lines = (self._buffer + data).split(self.delimiter)\n+        self._buffer = lines.pop(-1)\n+        for line in lines:\n+            if self.transport.disconnecting:\n+                # this is necessary because the transport may be told to lose\n+                # the connection by a line within a larger packet, and it is\n+                # important to disregard all the lines in that packet following\n+                # the one that told it to close.\n+                return\n+            if len(line) > self.MAX_LENGTH:\n+                self.lineLengthExceeded(line)\n+            else:\n+                self.lineReceived(line)\n+\n+    def lineReceived(self, line):\n+        raise NotImplementedError\n+\n+    def lineLengthExceeded(self, line):\n+        raise NotImplementedError\n \n \n-class LogWatcher(LineOnlyReceiver):\n+class LogWatcher(LineOnlyLongLineReceiver):\n     POLL_INTERVAL = 0.1\n     TIMEOUT_DELAY = 10.0\n     delimiter = unicode2bytes(os.linesep)\n@@ -69,8 +99,7 @@\n \n     def start(self):\n         # If the log file doesn't exist, create it now.\n-        if not os.path.exists(self.logfile):\n-            open(self.logfile, 'a').close()\n+        self.create_logfile(self.logfile)\n \n         # return a Deferred that fires when the reconfig process has\n         # finished. It errbacks with TimeoutError if the startup has not\n@@ -121,6 +150,17 @@\n         self.in_reconfig = False\n         self.d.callback(results)\n \n+    def create_logfile(self, path):  # pragma: no cover\n+        if not os.path.exists(path):\n+            open(path, 'a').close()\n+\n+    def print_output(self, output):  # pragma: no cover\n+        print(output)\n+\n+    def lineLengthExceeded(self, line):\n+        msg = 'Got an a very long line in the log (length {} bytes), ignoring'.format(len(line))\n+        self.print_output(msg)\n+\n     def lineReceived(self, line):\n         if not self.running:\n             return None\n@@ -130,7 +170,7 @@\n             self.in_reconfig = True\n \n         if self.in_reconfig:\n-            print(line.decode())\n+            self.print_output(line.decode())\n \n         # certain lines indicate progress, so we \"cancel\" the timeout\n         # and it will get re-added when it fires\n", "issue": "FakeTransport error while starting buildbot.\nI'm trying to understand why I'm getting the following error, even though the master seems to start and run normally. Googling for this message was fruitless. I'm using buildbot 2.9.3 and Twisted 20.3.0 (both latest). Do I need to downgrade Twisted?\r\n\r\n```\r\n(venv) alex@solitude:~/build_bot$ buildbot start master\r\nFollowing twistd.log until startup finished..\r\nUnhandled Error\r\nTraceback (most recent call last):\r\n  File \"/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/python/log.py\", line 103, in callWithLogger\r\n    return callWithContext({\"system\": lp}, func, *args, **kw)\r\n  File \"/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/python/log.py\", line 86, in callWithContext\r\n    return context.call({ILogContext: newCtx}, func, *args, **kw)\r\n  File \"/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/python/context.py\", line 122, in callWithContext\r\n    return self.currentContext().callWithContext(ctx, func, *args, **kw)\r\n  File \"/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/python/context.py\", line 85, in callWithContext\r\n    return func(*args,**kw)\r\n--- <exception caught here> ---\r\n  File \"/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/internet/posixbase.py\", line 614, in _doReadOrWrite\r\n    why = selectable.doRead()\r\n  File \"/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/internet/process.py\", line 252, in doRead\r\n    return fdesc.readFromFD(self.fd, self.dataReceived)\r\n  File \"/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/internet/fdesc.py\", line 94, in readFromFD\r\n    callback(output)\r\n  File \"/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/internet/process.py\", line 256, in dataReceived\r\n    self.proc.childDataReceived(self.name, data)\r\n  File \"/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/internet/process.py\", line 922, in childDataReceived\r\n    self.proto.childDataReceived(name, data)\r\n  File \"/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/internet/protocol.py\", line 640, in childDataReceived\r\n    self.outReceived(data)\r\n  File \"/home/alex/build_bot/venv/lib/python3.7/site-packages/buildbot/scripts/logwatcher.py\", line 49, in outReceived\r\n    self.lw.dataReceived(data)\r\n  File \"/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/protocols/basic.py\", line 452, in dataReceived\r\n    return self.lineLengthExceeded(line)\r\n  File \"/home/alex/build_bot/venv/lib/python3.7/site-packages/twisted/protocols/basic.py\", line 484, in lineLengthExceeded\r\n    return self.transport.loseConnection()\r\nbuiltins.AttributeError: 'FakeTransport' object has no attribute 'loseConnection'\r\n\r\nThe buildmaster took more than 10.0 seconds to start, so we were unable to\r\nconfirm that it started correctly. Please 'tail twistd.log' and look for a line\r\nthat says 'BuildMaster is running' to verify correct startup.\r\n```\r\n\r\n_Originally posted by @alexreinking in https://github.com/buildbot/buildbot/discussions/5702_\n", "before_files": [{"content": "# This file is part of Buildbot.  Buildbot is free software: you can\n# redistribute it and/or modify it under the terms of the GNU General Public\n# License as published by the Free Software Foundation, version 2.\n#\n# This program is distributed in the hope that it will be useful, but WITHOUT\n# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\n# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\n# details.\n#\n# You should have received a copy of the GNU General Public License along with\n# this program; if not, write to the Free Software Foundation, Inc., 51\n# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\n#\n# Copyright Buildbot Team Members\n\n\nimport os\nimport platform\n\nfrom twisted.internet import defer\nfrom twisted.internet import error\nfrom twisted.internet import protocol\nfrom twisted.internet import reactor\nfrom twisted.protocols.basic import LineOnlyReceiver\nfrom twisted.python.failure import Failure\n\nfrom buildbot.util import unicode2bytes\n\n\nclass FakeTransport:\n    disconnecting = False\n\n\nclass BuildmasterTimeoutError(Exception):\n    pass\n\n\nclass BuildmasterStartupError(Exception):\n    pass\n\n\nclass ReconfigError(Exception):\n    pass\n\n\nclass TailProcess(protocol.ProcessProtocol):\n\n    def outReceived(self, data):\n        self.lw.dataReceived(data)\n\n    def errReceived(self, data):\n        print(\"ERR: '{}'\".format(data))\n\n\nclass LogWatcher(LineOnlyReceiver):\n    POLL_INTERVAL = 0.1\n    TIMEOUT_DELAY = 10.0\n    delimiter = unicode2bytes(os.linesep)\n\n    def __init__(self, logfile, timeout=None, _reactor=reactor):\n        self.logfile = logfile\n        self.in_reconfig = False\n        self.transport = FakeTransport()\n        self.pp = TailProcess()\n        self.pp.lw = self\n        self.timer = None\n        self._reactor = _reactor\n        self._timeout_delay = timeout or self.TIMEOUT_DELAY\n\n    def start(self):\n        # If the log file doesn't exist, create it now.\n        if not os.path.exists(self.logfile):\n            open(self.logfile, 'a').close()\n\n        # return a Deferred that fires when the reconfig process has\n        # finished. It errbacks with TimeoutError if the startup has not\n        # progressed for 10 seconds, and with ReconfigError if the error\n        # line was seen. If the logfile could not be opened, it errbacks with\n        # an IOError.\n        if platform.system().lower() == 'sunos' and os.path.exists('/usr/xpg4/bin/tail'):\n            tailBin = \"/usr/xpg4/bin/tail\"\n        elif platform.system().lower() == 'haiku' and os.path.exists('/bin/tail'):\n            tailBin = \"/bin/tail\"\n        else:\n            tailBin = \"/usr/bin/tail\"\n\n        args = (\"tail\", \"-f\", \"-n\", \"0\", self.logfile)\n        self.p = self._reactor.spawnProcess(self.pp, tailBin, args,\n                                            env=os.environ)\n        self.running = True\n        d = defer.maybeDeferred(self._start)\n        return d\n\n    def _start(self):\n        self.d = defer.Deferred()\n        self.startTimer()\n        return self.d\n\n    def startTimer(self):\n        self.timer = self._reactor.callLater(self._timeout_delay, self.timeout)\n\n    def timeout(self):\n        # was the timeout set to be ignored? if so, restart it\n        if not self.timer:\n            self.startTimer()\n            return\n\n        self.timer = None\n        e = BuildmasterTimeoutError()\n        self.finished(Failure(e))\n\n    def finished(self, results):\n        try:\n            self.p.signalProcess(\"KILL\")\n        except error.ProcessExitedAlready:\n            pass\n        if self.timer:\n            self.timer.cancel()\n            self.timer = None\n        self.running = False\n        self.in_reconfig = False\n        self.d.callback(results)\n\n    def lineReceived(self, line):\n        if not self.running:\n            return None\n        if b\"Log opened.\" in line:\n            self.in_reconfig = True\n        if b\"beginning configuration update\" in line:\n            self.in_reconfig = True\n\n        if self.in_reconfig:\n            print(line.decode())\n\n        # certain lines indicate progress, so we \"cancel\" the timeout\n        # and it will get re-added when it fires\n        PROGRESS_TEXT = [b'Starting BuildMaster', b'Loading configuration from', b'added builder',\n                         b'adding scheduler', b'Loading builder', b'Starting factory']\n        for progressText in PROGRESS_TEXT:\n            if progressText in line:\n                self.timer = None\n                break\n\n        if b\"message from master: attached\" in line:\n            return self.finished(\"worker\")\n        if b\"reconfig aborted\" in line or b'reconfig partially applied' in line:\n            return self.finished(Failure(ReconfigError()))\n        if b\"Server Shut Down\" in line:\n            return self.finished(Failure(ReconfigError()))\n        if b\"configuration update complete\" in line:\n            return self.finished(\"buildmaster\")\n        if b\"BuildMaster is running\" in line:\n            return self.finished(\"buildmaster\")\n        if b\"BuildMaster startup failed\" in line:\n            return self.finished(Failure(BuildmasterStartupError()))\n        return None\n", "path": "master/buildbot/scripts/logwatcher.py"}], "after_files": [{"content": "# This file is part of Buildbot.  Buildbot is free software: you can\n# redistribute it and/or modify it under the terms of the GNU General Public\n# License as published by the Free Software Foundation, version 2.\n#\n# This program is distributed in the hope that it will be useful, but WITHOUT\n# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\n# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\n# details.\n#\n# You should have received a copy of the GNU General Public License along with\n# this program; if not, write to the Free Software Foundation, Inc., 51\n# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\n#\n# Copyright Buildbot Team Members\n\n\nimport os\nimport platform\n\nfrom twisted.internet import defer\nfrom twisted.internet import error\nfrom twisted.internet import protocol\nfrom twisted.internet import reactor\nfrom twisted.python.failure import Failure\n\nfrom buildbot.util import unicode2bytes\n\n\nclass FakeTransport:\n    disconnecting = False\n\n\nclass BuildmasterTimeoutError(Exception):\n    pass\n\n\nclass BuildmasterStartupError(Exception):\n    pass\n\n\nclass ReconfigError(Exception):\n    pass\n\n\nclass TailProcess(protocol.ProcessProtocol):\n\n    def outReceived(self, data):\n        self.lw.dataReceived(data)\n\n    def errReceived(self, data):\n        self.lw.print_output(\"ERR: '{}'\".format(data))\n\n\nclass LineOnlyLongLineReceiver(protocol.Protocol):\n    \"\"\"\n    This is almost the same as Twisted's LineOnlyReceiver except that long lines are handled\n    appropriately.\n    \"\"\"\n    _buffer = b''\n    delimiter = b'\\r\\n'\n    MAX_LENGTH = 16384\n\n    def dataReceived(self, data):\n        lines = (self._buffer + data).split(self.delimiter)\n        self._buffer = lines.pop(-1)\n        for line in lines:\n            if self.transport.disconnecting:\n                # this is necessary because the transport may be told to lose\n                # the connection by a line within a larger packet, and it is\n                # important to disregard all the lines in that packet following\n                # the one that told it to close.\n                return\n            if len(line) > self.MAX_LENGTH:\n                self.lineLengthExceeded(line)\n            else:\n                self.lineReceived(line)\n\n    def lineReceived(self, line):\n        raise NotImplementedError\n\n    def lineLengthExceeded(self, line):\n        raise NotImplementedError\n\n\nclass LogWatcher(LineOnlyLongLineReceiver):\n    POLL_INTERVAL = 0.1\n    TIMEOUT_DELAY = 10.0\n    delimiter = unicode2bytes(os.linesep)\n\n    def __init__(self, logfile, timeout=None, _reactor=reactor):\n        self.logfile = logfile\n        self.in_reconfig = False\n        self.transport = FakeTransport()\n        self.pp = TailProcess()\n        self.pp.lw = self\n        self.timer = None\n        self._reactor = _reactor\n        self._timeout_delay = timeout or self.TIMEOUT_DELAY\n\n    def start(self):\n        # If the log file doesn't exist, create it now.\n        self.create_logfile(self.logfile)\n\n        # return a Deferred that fires when the reconfig process has\n        # finished. It errbacks with TimeoutError if the startup has not\n        # progressed for 10 seconds, and with ReconfigError if the error\n        # line was seen. If the logfile could not be opened, it errbacks with\n        # an IOError.\n        if platform.system().lower() == 'sunos' and os.path.exists('/usr/xpg4/bin/tail'):\n            tailBin = \"/usr/xpg4/bin/tail\"\n        elif platform.system().lower() == 'haiku' and os.path.exists('/bin/tail'):\n            tailBin = \"/bin/tail\"\n        else:\n            tailBin = \"/usr/bin/tail\"\n\n        args = (\"tail\", \"-f\", \"-n\", \"0\", self.logfile)\n        self.p = self._reactor.spawnProcess(self.pp, tailBin, args,\n                                            env=os.environ)\n        self.running = True\n        d = defer.maybeDeferred(self._start)\n        return d\n\n    def _start(self):\n        self.d = defer.Deferred()\n        self.startTimer()\n        return self.d\n\n    def startTimer(self):\n        self.timer = self._reactor.callLater(self._timeout_delay, self.timeout)\n\n    def timeout(self):\n        # was the timeout set to be ignored? if so, restart it\n        if not self.timer:\n            self.startTimer()\n            return\n\n        self.timer = None\n        e = BuildmasterTimeoutError()\n        self.finished(Failure(e))\n\n    def finished(self, results):\n        try:\n            self.p.signalProcess(\"KILL\")\n        except error.ProcessExitedAlready:\n            pass\n        if self.timer:\n            self.timer.cancel()\n            self.timer = None\n        self.running = False\n        self.in_reconfig = False\n        self.d.callback(results)\n\n    def create_logfile(self, path):  # pragma: no cover\n        if not os.path.exists(path):\n            open(path, 'a').close()\n\n    def print_output(self, output):  # pragma: no cover\n        print(output)\n\n    def lineLengthExceeded(self, line):\n        msg = 'Got an a very long line in the log (length {} bytes), ignoring'.format(len(line))\n        self.print_output(msg)\n\n    def lineReceived(self, line):\n        if not self.running:\n            return None\n        if b\"Log opened.\" in line:\n            self.in_reconfig = True\n        if b\"beginning configuration update\" in line:\n            self.in_reconfig = True\n\n        if self.in_reconfig:\n            self.print_output(line.decode())\n\n        # certain lines indicate progress, so we \"cancel\" the timeout\n        # and it will get re-added when it fires\n        PROGRESS_TEXT = [b'Starting BuildMaster', b'Loading configuration from', b'added builder',\n                         b'adding scheduler', b'Loading builder', b'Starting factory']\n        for progressText in PROGRESS_TEXT:\n            if progressText in line:\n                self.timer = None\n                break\n\n        if b\"message from master: attached\" in line:\n            return self.finished(\"worker\")\n        if b\"reconfig aborted\" in line or b'reconfig partially applied' in line:\n            return self.finished(Failure(ReconfigError()))\n        if b\"Server Shut Down\" in line:\n            return self.finished(Failure(ReconfigError()))\n        if b\"configuration update complete\" in line:\n            return self.finished(\"buildmaster\")\n        if b\"BuildMaster is running\" in line:\n            return self.finished(\"buildmaster\")\n        if b\"BuildMaster startup failed\" in line:\n            return self.finished(Failure(BuildmasterStartupError()))\n        return None\n", "path": "master/buildbot/scripts/logwatcher.py"}]}