"Fossies" - the Fresh Open Source Software Archive

Member "buildbot-2.3.1/buildbot/util/lineboundaries.py" (23 May 2019, 3093 Bytes) of package /linux/misc/buildbot-2.3.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "lineboundaries.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.0.1_vs_2.1.0.

    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 re
   18 
   19 from twisted.internet import defer
   20 
   21 from buildbot.util.logger import Logger
   22 
   23 log = Logger()
   24 
   25 
   26 class LineBoundaryFinder:
   27 
   28     __slots__ = ['partialLine', 'callback', 'warned']
   29     # split at reasonable line length.
   30     # too big lines will fill master's memory, and slow down the UI too much.
   31     MAX_LINELENGTH = 4096
   32     # the lookahead here (`(?=.)`) ensures that `\r` doesn't match at the end
   33     # of the buffer
   34     # we also convert cursor control sequence to newlines
   35     # and ugly \b+ (use of backspace to implement progress bar)
   36     newline_re = re.compile(r'(\r\n|\r(?=.)|\033\[u|\033\[[0-9]+;[0-9]+[Hf]|\033\[2J|\x08+)')
   37 
   38     def __init__(self, callback):
   39         self.partialLine = None
   40         self.callback = callback
   41         self.warned = False
   42 
   43     def append(self, text):
   44         if self.partialLine:
   45             if len(self.partialLine) > self.MAX_LINELENGTH:
   46                 if not self.warned:
   47                     # Unfortunately we cannot give more hint as per which log that is
   48                     log.warn("Splitting long line: {line_start} {length} (not warning anymore for this log)",
   49                              line_start=self.partialLine[:30], length=len(self.partialLine))
   50                     self.warned = True
   51                 # switch the variables, and return previous _partialLine_,
   52                 # split every MAX_LINELENGTH plus a trailing \n
   53                 self.partialLine, text = text, self.partialLine
   54                 ret = []
   55                 while len(text) > self.MAX_LINELENGTH:
   56                     ret.append(text[:self.MAX_LINELENGTH])
   57                     text = text[self.MAX_LINELENGTH:]
   58                 ret.append(text)
   59                 return self.callback("\n".join(ret) + "\n")
   60             text = self.partialLine + text
   61             self.partialLine = None
   62         text = self.newline_re.sub('\n', text)
   63         if text:
   64             if text[-1] != '\n':
   65                 i = text.rfind('\n')
   66                 if i >= 0:
   67                     i = i + 1
   68                     text, self.partialLine = text[:i], text[i:]
   69                 else:
   70                     self.partialLine = text
   71                     return defer.succeed(None)
   72             return self.callback(text)
   73         return defer.succeed(None)
   74 
   75     def flush(self):
   76         if self.partialLine:
   77             return self.append('\n')
   78         return defer.succeed(None)