Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add line-boundary finder for processing logs
- Loading branch information
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# 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 twisted.trial import unittest | ||
from twisted.internet import defer, reactor | ||
from buildbot.util import lineboundaries | ||
|
||
class LBF(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.callbacks = [] | ||
self.lbf = lineboundaries.LineBoundaryFinder(self._callback) | ||
|
||
def _callback(self, wholeLines): | ||
self.assertEqual(wholeLines[-1], '\n', 'got %r' % (wholeLines)) | ||
self.callbacks.append(wholeLines) | ||
d = defer.Deferred() | ||
reactor.callLater(0, d.callback, None) | ||
return d | ||
|
||
def assertCallbacks(self, callbacks): | ||
self.assertEqual(self.callbacks, callbacks) | ||
self.callbacks = [] | ||
|
||
# tests | ||
|
||
@defer.inlineCallbacks | ||
def test_already_terminated(self): | ||
yield self.lbf.append('abcd\ndefg\n') | ||
self.assertCallbacks(['abcd\ndefg\n']) | ||
yield self.lbf.append('xyz\n') | ||
self.assertCallbacks(['xyz\n']) | ||
yield self.lbf.flush() | ||
self.assertCallbacks([]) | ||
|
||
@defer.inlineCallbacks | ||
def test_partial_line(self): | ||
for c in "hello\nworld": | ||
yield self.lbf.append(c) | ||
self.assertCallbacks(['hello\n']) | ||
yield self.lbf.flush() | ||
self.assertCallbacks(['world\n']) | ||
|
||
@defer.inlineCallbacks | ||
def test_embedded_newlines(self): | ||
yield self.lbf.append('hello, ') | ||
self.assertCallbacks([]) | ||
yield self.lbf.append('cruel\nworld') | ||
self.assertCallbacks(['hello, cruel\n']) | ||
yield self.lbf.flush() | ||
self.assertCallbacks(['world\n']) | ||
|
||
def test_empty_flush(self): | ||
d = self.lbf.flush() | ||
@d.addCallback | ||
def check(_): | ||
self.assertEqual(self.callbacks, []) | ||
return d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# 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 twisted.internet import defer | ||
|
||
class LineBoundaryFinder(object): | ||
|
||
__slots_ = ['partialLine', 'callback'] | ||
|
||
def __init__(self, callback): | ||
self.partialLine = None | ||
self.callback = callback | ||
|
||
def append(self, text): | ||
if self.partialLine: | ||
text = self.partialLine + text | ||
self.partialLine = None | ||
if text[-1] != '\n': | ||
i = text.rfind('\n') | ||
if i >= 0: | ||
i = i + 1 | ||
text, self.partialLine = text[:i], text[i:] | ||
else: | ||
self.partialLine = text | ||
return defer.succeed(None) | ||
return self.callback(text) | ||
|
||
def flush(self): | ||
if self.partialLine: | ||
return self.append('\n') | ||
else: | ||
return defer.succeed(None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters