forked from chyh1990/autotester_v2
-
Notifications
You must be signed in to change notification settings - Fork 3
/
frontend.rb
executable file
·251 lines (222 loc) · 6.79 KB
/
frontend.rb
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env ruby
# encoding: utf-8
require 'sinatra'
require 'grit'
require 'yaml'
require 'pp'
require 'socket'
require 'timeout'
require 'logger'
def md5sum fn
md5 = `md5sum #{fn}`
fail if $?.exitstatus != 0
md5
end
YAML::ENGINE.yamler='syck'
ROOT= File.dirname(File.expand_path __FILE__)
CONFIG_FILE= File.join ROOT, "config.yaml"
$CONFIG=YAML.load File.read(CONFIG_FILE)
config_md5 = md5sum CONFIG_FILE
pp $CONFIG
REQUEST=File.join ROOT, "request_mgmt", "request"
LOGGER = Logger.new STDERR
set :public_folder, File.dirname(__FILE__) + '/views'
set :bind, $CONFIG[:bind] || '0.0.0.0'
set :port, $CONFIG[:port] || 4567
$formats = {
/(\[ *PASS *\])/ => '<span class="line-ok">\1</span>',
/(\[ *! *PASS *! *\])/ => '<span class="line-warning">\1</span>',
/(\[ *FAIL *\])/ => '<span class="line-warning">\1</span>',
/(\[ *! *FAIL *! *\])/ => '<span class="line-error">\1</span>',
/(\[ *!? *BROKEN *!? *\])/ => '<span class="line-error">\1</span>',
}
helpers do
def h(text)
Rack::Utils.escape_html(text)
end
def tr(text, len)
len = 3 if len<3
return "nil" unless text
return text if text.length < len
text[0..len-3]+"..."
end
def ol(line)
t = h line.chomp
$formats.each do |p, s|
return t.gsub(p, s) if t =~ p
end
t
end
def format(section, repo, tid, result)
text = ""
formatter_output = ""
cwd = Dir.getwd
repo_abspath = $CONFIG[:repo_abspath]
result_abspath = $CONFIG[:result_abspath]
Dir.chdir File.join(repo_abspath, repo)
formatter = ""
if File.executable_real? "formatter.py"
formatter = "./formatter.py"
elsif File.executable_real? "labcodes/formatter.py"
formatter = "./labcodes/formatter.py"
end
if formatter != ""
command = formatter + " " + section + " " + result_abspath + " " + repo + " " + tid
result.each do |line|
text << line + "\n"
end
begin
pipe = IO.popen("#{command}", mode="r+")
rescue Exception => e
return text
end
pipe.write text
pipe.close_write
formatter_output = pipe.read
Process.waitpid2(pipe.pid)
else
result.each do |line|
formatter_output << ol(line) + "<br>"
end
end
Dir.chdir cwd
return formatter_output
end
end
class ReportCache
@@cache = Hash.new
class << self
def check_repo(repo)
return false unless repo =~ /[a-zA-A_]+/
$CONFIG[:repos].any? {|e| e[:name] == repo }
end
def [](repo)
@@cache[repo]
end
def check_and_update(repo)
return nil unless check_repo repo
unless File.directory? File.join($CONFIG[:result_abspath], repo)
@@cache.delete repo
return nil
end
@@cache[repo] = Hash.new
fs = Hash.new
dir = File.join($CONFIG[:result_abspath], repo)
Dir.foreach dir do |f|
fs[f[0..-6]] = :f if f =~ /^[a-z0-9]{40}-\d+-[A-Z]+-\d+\.yaml$/
end
@@cache[repo].delete_if { |k,v| fs[k].nil? }
fs.each do |k,v|
next if @@cache[repo][k]
file = File.join($CONFIG[:result_abspath], repo, k+".yaml")
report = YAML.load File.read(file) rescue nil
next unless report
report[:ok] ||= k.include?("OK") ? "OK" : "FAIL"
report[:timestamp] ||= k.split('-')[1].to_i
report[:ref] ||= ["UNKNOWN", ""]
report[:result] ||= []
report[:filter_commits] ||= []
report[:tid] = k
report[:head] ||= k.split('-')[0]
@@cache[repo][k] = report
end
@@cache[repo]
end
end
private
def initialize
end
end
get '/repo/:repo/' do
repo = params[:repo]
halt 404 unless ReportCache.check_and_update repo
cache = ReportCache[repo]
halt 404 if cache.nil?
@list = cache.sort_by {|k,v| v[:timestamp]}.reverse.map{|e| e[1]}
@repo = repo
erb :testlist
end
get '/repo/:repo/:tid' do
repo = params[:repo]
tid = params[:tid]
halt 404 unless ReportCache.check_and_update repo
cache = ReportCache[repo]
halt 404 if cache.nil?
@report = cache[tid]
halt 404 if @report.nil?
@repo = repo
@tid = tid
erb :result
end
get '/repo/:repo/:commit/:arch_or_lab/:testcase' do
repo = params[:repo]
commit = params[:commit]
arch_or_lab = params[:arch_or_lab]
testcase = params[:testcase]
filepath = File.join($CONFIG[:result_abspath], repo, commit, arch_or_lab, testcase + ".error")
@error = File.read(filepath).gsub(/\n/, '<br>') rescue "Error logs not found!"
filepath = File.join($CONFIG[:result_abspath], repo, commit, arch_or_lab, testcase + ".log")
@log = File.read(filepath).gsub(/\n/, '<br>') rescue "Error logs not found!"
erb :error
end
get '/register' do
if not $CONFIG[:registration][:frontend_enable]
erb :no_register
else
repo = params[:repo]
email = params[:email]
is_public = params[:is_public] == "on" ? "true" : "false"
if repo != nil and email != nil
`#{REQUEST} append #{$CONFIG[:registration][:queue]} "#{repo}|#{email}|#{is_public}|0"`
erb :register_done
else
erb :register
end
end
end
get '/mooc_marking' do
if not $CONFIG[:marking][:enable]
halt 403, "marking is disabled"
else
id = params[:id]
lab = params[:lab]
if id != nil and lab != nil
`#{REQUEST} append #{$CONFIG[:marking][:queue]} "#{id}|#{lab}"`
halt 200, "request queued"
else
halt 400, "require id and lab"
end
end
end
get '/about' do
erb :about
end
get '/' do
@status_line = []
begin
Timeout.timeout(2) do
s = TCPSocket.open($CONFIG[:ping][:frontend_addr], $CONFIG[:ping][:port])
while l = s.gets
@status_line << l.chomp
end
s.close
end
rescue Timeout::Error
@status_line << "ERROR: Timeout to connect to backend!"
rescue StandardError => e
@status_line << "ERROR: Failed to connect to backend!"
end
@env = File.read(File.join(ROOT, "env.txt")) rescue "Unknown"
new_config_md5 = md5sum CONFIG_FILE
if config_md5 != new_config_md5
$CONFIG = YAML.load File.read(CONFIG_FILE)
config_md5 = new_config_md5
end
@repos = $CONFIG[:repos]
erb :index, :locals => {}
end
##
# Local variables:
# ruby-indent-level: 4
# End:
##