2017-04-06 16:20:27 +00:00
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class Trace
|
|
|
|
# This was inspired from: http://stackoverflow.com/a/10219411/1520132
|
|
|
|
class Stream
|
|
|
|
BUFFER_SIZE = 4096
|
2017-04-18 12:27:10 +00:00
|
|
|
LIMIT_SIZE = 500.kilobytes
|
2017-04-06 16:20:27 +00:00
|
|
|
|
|
|
|
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
|
2017-04-18 09:33:17 +00:00
|
|
|
@stream&.binmode
|
2017-04-06 16:20:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid?
|
|
|
|
self.stream.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def file?
|
|
|
|
self.path.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def limit(last_bytes = LIMIT_SIZE)
|
2017-04-13 14:07:37 +00:00
|
|
|
if last_bytes < size
|
|
|
|
stream.seek(-last_bytes, IO::SEEK_END)
|
|
|
|
stream.readline
|
2017-04-06 16:20:27 +00:00
|
|
|
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
|
2017-04-17 06:30:42 +00:00
|
|
|
end.force_encoding(Encoding.default_external)
|
2017-04-06 16:20:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def html_with_state(state = nil)
|
|
|
|
::Ci::Ansi2html.convert(stream, state)
|
|
|
|
end
|
|
|
|
|
|
|
|
def html(last_lines: nil)
|
|
|
|
text = raw(last_lines: last_lines)
|
2017-04-18 09:03:02 +00:00
|
|
|
buffer = StringIO.new(text)
|
|
|
|
::Ci::Ansi2html.convert(buffer).html
|
2017-04-06 16:20:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def extract_coverage(regex)
|
|
|
|
return unless valid?
|
|
|
|
return unless regex
|
|
|
|
|
|
|
|
regex = Regexp.new(regex)
|
|
|
|
|
|
|
|
match = ""
|
|
|
|
|
2017-05-05 18:16:39 +00:00
|
|
|
reverse_line do |line|
|
2017-05-22 06:17:08 +00:00
|
|
|
matches = line.scan(regex)
|
2017-04-06 16:20:27 +00:00
|
|
|
next unless matches.is_a?(Array)
|
2017-04-11 19:10:12 +00:00
|
|
|
next if matches.empty?
|
2017-04-06 16:20:27 +00:00
|
|
|
|
|
|
|
match = matches.flatten.last
|
|
|
|
coverage = match.gsub(/\d+(\.\d+)?/).first
|
2017-04-11 19:10:12 +00:00
|
|
|
return coverage if coverage.present?
|
2017-04-06 16:20:27 +00:00
|
|
|
end
|
2017-04-11 19:10:12 +00:00
|
|
|
|
|
|
|
nil
|
2017-04-06 16:20:27 +00:00
|
|
|
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
|
2017-04-06 16:20:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-05-17 09:18:16 +00:00
|
|
|
def read_last_lines(limit)
|
2017-05-17 09:26:38 +00:00
|
|
|
to_enum(:reverse_line).first(limit).reverse.join
|
2017-04-06 16:20:27 +00:00
|
|
|
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
|
|
|
|
2017-05-24 16:35:40 +00:00
|
|
|
until (buf = read_backward(BUFFER_SIZE)).empty?
|
2017-05-24 11:20:20 +00:00
|
|
|
buf += debris
|
2017-05-17 16:44:15 +00:00
|
|
|
debris, *lines = buf.each_line.to_a
|
|
|
|
lines.reverse_each do |line|
|
2017-05-22 06:17:08 +00:00
|
|
|
yield(line.force_encoding('UTF-8'))
|
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
|
|
|
|
2017-05-24 16:35:40 +00:00
|
|
|
yield(debris.force_encoding('UTF-8')) 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
|
2017-04-06 16:20:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|