Hash :
dd4230f7
Author :
Thomas de Grivel
Date :
2023-02-26T23:44:57
rbpkg_ci
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
require 'base64'
module Rbpkg
class Log
attr_reader :name, :path, :path_html, :status, :status_path
def initialize(name)
@name = name
@path = "#{Rbpkg::Log.log_dir}/#{name}.log"
@path_html = "#{path}.html"
@status_path = "#{Rbpkg::Log.status_dir}/#{name}.status"
raise "log file exists: #{@path.inspect}" if File.exist?(@path)
raise "log file exists: #{@path_html.inspect}" if File.exist?(@path_html)
status = :running
end
def img
Rbpkg.status_img(status)
end
def put_layout(log_path = path, html_path = path_html, s = status)
tmp = html_path + '.tmp'
log = File.read(log_path)
html = <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>#{path.to_html}</title>
<link rel="stylesheet" href="/_assets/app.css">
<script defer type="text/javascript" src="/_assets/app.js"></script>
<link rel="icon" type="image/png" sizes="64x64" href="${IMG}">
</head>
<body>
<div class="ci-header">
<a href="./"><i class="fas fa-asterisk"></i> All logs</a>
</div>
<h1>
<img src="#{img}" class="status-#{s.to_s.to_html}"/>
#{path.to_html}
</h1>
<pre class="wrap"><code>#{log.ansi_to_html}</code></pre>
</body>
</html>
EOF
File.write(tmp, html)
verbose(3, "Writing #{html_path}")
FileUtils.mv(tmp, html_path)
end
def puts(string)
dir = File.dirname(path)
cmd! "mkdir -p #{sh_quote(dir)}" unless File.directory?(dir)
File.open(path, "a") do |output|
output.puts string
end
end
def status=(sym)
status_dir = File.dirname(status_path)
cmd! "mkdir -p #{sh_quote(status_dir)}" unless File.directory?(status_dir)
File.write(status_path, "#{sym.to_s}\n")
@status = sym
end
def self.add(name)
raise "already logging to #{name.inspect}" if @@log[name]
log = new(name)
@@log[name] = log
verbose(3, "Logging to #{log.path}")
end
def self.init(log_dir, name, status_dir)
@@log_dir = log_dir
@@name = name
@@status_dir = status_dir
@@log = {}
add(name)
at_exit { ko_all }
end
def self.ko(name)
log = remove(name)
log.status = :ko
log.puts(status_message_ko)
log.put_layout
end
def self.ko_all
if @@log != {}
@@log.each do |name, log|
log.status = :ko
end
verbose(-1, status_message_ko)
remove_all.each do |name, log|
log.put_layout
end
end
end
def self.log_dir
@@log_dir
end
def self.ok(name)
log = remove(name)
log.status = :ok
log.puts(status_message_ok)
log.put_layout
end
def self.ok_all
if @@log != []
@@log.each do |name, log|
log.status = :ok
end
verbose(3, status_message_ok)
remove_all.each do |name, log|
log.put_layout
end
end
end
def self.puts(string)
@@log.each do |name, log|
log.puts(string)
end
end
def self.remove(name)
log = @@log[name]
raise "not logging to #{name.inspect}" unless log
@@log.delete(name)
log
end
def self.remove_all
logs = @@log
@@log = {}
logs
end
def self.status_dir
@@status_dir
end
def self.status_message(status)
"Status: #{@@name} -> #{status}"
end
def self.status_message_ko
status_message("KO")
end
def self.status_message_ok
status_message("OK")
end
end
end
class String
def ansi_to_html
substitution = {
"\33[0;31m" => "<span style=\"color: red;\">",
"\33[0;34m" => "<span style=\"color: blue;\">",
"\33[0;35m" => "<span style=\"color: purple;\">",
"\33[0m" => "</span>"
}
s = to_html
substitution.each do |k, v|
s.gsub!(k, v)
end
s
end
def to_html
substitution = {
"&" => "&",
"<" => "<",
">" => ">"
}
s = "#{self}"
substitution.each do |k, v|
s.gsub!(k, v)
end
s
end
end