gitlab-org--gitlab-foss/lib/gitlab/ci/trace/stream.rb

148 lines
3.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module Gitlab
module Ci
class Trace
# This was inspired from: http://stackoverflow.com/a/10219411/1520132
class Stream
BUFFER_SIZE = 4096
LIMIT_SIZE = 500.kilobytes
attr_reader :stream
2018-04-05 19:41:45 +00:00
delegate :close, :tell, :seek, :size, :url, :truncate, to: :stream, allow_nil: true
delegate :valid?, to: :stream, allow_nil: true
alias_method :present?, :valid?
def initialize
@stream = yield
@stream&.binmode
end
def valid?
self.stream.present?
end
def file?
self.path.present?
end
2018-04-03 12:52:23 +00:00
def path
self.stream.path if self.stream.respond_to?(:path)
2018-04-05 19:41:45 +00:00
end
def limit(last_bytes = LIMIT_SIZE)
if last_bytes < size
stream.seek(-last_bytes, IO::SEEK_END)
stream.readline
end
end
def append(data, offset)
data = data.force_encoding(Encoding::BINARY)
2018-11-23 16:25:11 +00:00
stream.seek(offset, IO::SEEK_SET)
stream.write(data)
2018-11-23 16:25:11 +00:00
stream.truncate(offset + data.bytesize)
stream.flush
end
def set(data)
2018-11-23 16:25:11 +00:00
append(data, 0)
end
def raw(last_lines: nil)
return unless valid?
if last_lines.to_i > 0
read_last_lines(last_lines)
else
stream.read
end.force_encoding(Encoding.default_external)
end
def html(last_lines: nil)
text = raw(last_lines: last_lines)
buffer = StringIO.new(text)
::Gitlab::Ci::Ansi2html.convert(buffer).html
end
def extract_coverage(regex)
return unless valid?
return unless regex.present?
regex = Gitlab::UntrustedRegexp.new(regex)
match = ""
2017-05-05 18:16:39 +00:00
reverse_line do |line|
line.chomp!
matches = regex.scan(line)
next unless matches.is_a?(Array)
next if matches.empty?
match = matches.flatten.last
coverage = match.gsub(/\d+(\.\d+)?/).first
return coverage if coverage.present? # rubocop:disable Cop/AvoidReturnFromBlocks
end
nil
rescue
# if bad regex or something goes wrong we dont want to interrupt transition
2017-05-17 08:53:31 +00:00
# so we just silently ignore error for now
end
2017-09-25 16:54:08 +00:00
def extract_sections
return [] unless valid?
lines = to_enum(:each_line_with_pos)
parser = SectionParser.new(lines)
parser.parse!
parser.sections
end
private
2017-09-25 16:54:08 +00:00
def each_line_with_pos
stream.seek(0, IO::SEEK_SET)
stream.each_line do |line|
yield [line, stream.pos - line.bytesize]
end
end
def read_last_lines(limit)
2017-05-17 09:26:38 +00:00
to_enum(:reverse_line).first(limit).reverse.join
end
2017-05-05 18:16:39 +00:00
def reverse_line
2017-05-24 11:20:20 +00:00
stream.seek(0, IO::SEEK_END)
2017-05-09 17:40:34 +00:00
debris = ''
2017-05-05 18:16:39 +00:00
until (buf = read_backward(BUFFER_SIZE)).empty?
debris, *lines = (buf + debris).each_line.to_a
2017-05-17 16:44:15 +00:00
lines.reverse_each do |line|
yield(line.force_encoding(Encoding.default_external))
2017-05-09 17:40:34 +00:00
end
2017-05-05 18:16:39 +00:00
end
2017-05-06 08:38:56 +00:00
yield(debris.force_encoding(Encoding.default_external)) unless debris.empty?
2017-05-17 11:21:13 +00:00
end
2017-05-24 11:20:20 +00:00
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
2017-05-05 18:16:39 +00:00
end
end
end
end
end