stream.rb 3.05 KB
Newer Older
1 2 3 4 5 6
module Gitlab
  module Ci
    class Trace
      # This was inspired from: http://stackoverflow.com/a/10219411/1520132
      class Stream
        BUFFER_SIZE = 4096
7
        LIMIT_SIZE = 500.kilobytes
8 9 10 11 12 13 14 15 16

        attr_reader :stream

        delegate :close, :tell, :seek, :size, :path, :truncate, to: :stream, allow_nil: true

        delegate :valid?, to: :stream, as: :present?, allow_nil: true

        def initialize
          @stream = yield
17
          @stream&.binmode
18 19 20 21 22 23 24 25 26 27 28
        end

        def valid?
          self.stream.present?
        end

        def file?
          self.path.present?
        end

        def limit(last_bytes = LIMIT_SIZE)
29 30 31
          if last_bytes < size
            stream.seek(-last_bytes, IO::SEEK_END)
            stream.readline
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
          end
        end

        def append(data, offset)
          stream.truncate(offset)
          stream.seek(0, IO::SEEK_END)
          stream.write(data)
          stream.flush()
        end

        def set(data)
          truncate(0)
          stream.write(data)
          stream.flush()
        end

        def raw(last_lines: nil)
          return unless valid?

          if last_lines.to_i > 0
            read_last_lines(last_lines)
          else
            stream.read
55
          end.force_encoding(Encoding.default_external)
56 57 58 59 60 61 62 63
        end

        def html_with_state(state = nil)
          ::Ci::Ansi2html.convert(stream, state)
        end

        def html(last_lines: nil)
          text = raw(last_lines: last_lines)
64 65
          buffer = StringIO.new(text)
          ::Ci::Ansi2html.convert(buffer).html
66 67 68 69 70 71 72 73 74 75
        end

        def extract_coverage(regex)
          return unless valid?
          return unless regex

          regex = Regexp.new(regex)

          match = ""

Shinya Maeda's avatar
Shinya Maeda committed
76
          reverse_line do |line|
77
            matches = line.scan(regex)
78
            next unless matches.is_a?(Array)
79
            next if matches.empty?
80 81 82

            match = matches.flatten.last
            coverage = match.gsub(/\d+(\.\d+)?/).first
83
            return coverage if coverage.present?
84
          end
85 86

          nil
87 88
        rescue
          # if bad regex or something goes wrong we dont want to interrupt transition
89
          # so we just silently ignore error for now
90 91 92 93
        end

        private

94
        def read_last_lines(limit)
Shinya Maeda's avatar
Shinya Maeda committed
95
          to_enum(:reverse_line).first(limit).reverse.join
96
        end
Shinya Maeda's avatar
Shinya Maeda committed
97 98

        def reverse_line
Shinya Maeda's avatar
Shinya Maeda committed
99
          stream.seek(0, IO::SEEK_END)
100
          debris = ''
Shinya Maeda's avatar
Shinya Maeda committed
101

102
          until (buf = read_backward(BUFFER_SIZE)).empty?
Shinya Maeda's avatar
Shinya Maeda committed
103
            buf += debris
104 105
            debris, *lines = buf.each_line.to_a
            lines.reverse_each do |line|
106
              yield(line.force_encoding('UTF-8'))
107
            end
Shinya Maeda's avatar
Shinya Maeda committed
108
          end
Shinya Maeda's avatar
Shinya Maeda committed
109

110
          yield(debris.force_encoding('UTF-8')) unless debris.empty?
Shinya Maeda's avatar
Shinya Maeda committed
111 112
        end

Shinya Maeda's avatar
Shinya Maeda committed
113 114 115 116 117 118 119 120 121
        def read_backward(length)
          cur_offset = stream.tell
          start = cur_offset - length
          start = 0 if start < 0

          stream.seek(start, IO::SEEK_SET)
          stream.read(cur_offset - start).tap do
            stream.seek(start, IO::SEEK_SET)
          end
Shinya Maeda's avatar
Shinya Maeda committed
122
        end
123 124 125 126
      end
    end
  end
end