Subversion Repositories Koakuma

Rev

Rev 22 | Rev 24 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 nishi 1
#!/usr/bin/env tclsh
2
# $Id: koakuma.cgi.in 23 2024-10-02 08:02:38Z nishi $
3
 
4
set KOAKUMA_VERSION "1.00"
5
set components ""
6
 
13 nishi 7
chan configure stdout -buffering none
8
 
2 nishi 9
proc exiting {code} {
10
	exit $code
11
}
12
 
3 nishi 13
proc loop_components {run} {
14
	global components
6 nishi 15
	foreach {name description version genre} $components {
3 nishi 16
		eval $run
17
	}
18
}
19
 
2 nishi 20
proc crash {reason} {
21
	global components KOAKUMA_VERSION
22
	puts stderr "----- Start Koakuma Crash dump log -----"
23
	puts stderr "Included components:"
3 nishi 24
	loop_components {
2 nishi 25
		puts stderr "	$name: $description, version $version"
26
	}
27
	puts stderr "Reason: $reason"
13 nishi 28
	puts stderr "Code: $::errorCode"
29
	puts stderr "Info: $::errorInfo"
2 nishi 30
	puts stderr "----- End Koakuma Crash dump log -----"
31
	puts	"Content-Type: text/html"
32
	puts	"Status: 500 Internal Server Error"
33
	puts	""
34
	puts	"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">"
35
	puts	"<html>"
36
	puts	"	<head>"
37
	puts	"		<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">"
38
	puts	"		<title>Oops</title>"
39
	puts	"	</head>"
40
	puts	"	<body>"
41
	puts	"		<h1>Oops</h1>"
42
	puts	"		<hr>"
43
	puts	"		Koakuma version $KOAKUMA_VERSION crashed, reason: <code>$reason</code><br>"
44
	puts	"		See the server error log for details."
45
	puts	"	</body>"
46
	puts	"</html>"
47
	exiting 1
48
}
49
 
3 nishi 50
if { ![info exists env(PATH_INFO)] } {
51
	puts "Status: 301 Moved Permanently"
52
	puts "Location: $env(SCRIPT_NAME)/"
53
	puts ""
54
	exiting 0
2 nishi 55
}
56
 
13 nishi 57
if { [file exists "@@PREFIX@@/etc/koakuma/cgi.conf"] } {
58
	if { [catch {
59
		source "@@PREFIX@@/etc/koakuma/cgi.conf"
60
	}] } {
61
		crash "Config failure"
62
	}
63
}
64
 
3 nishi 65
if { [catch {
13 nishi 66
	set tdom_version "[package require tdom]"
4 nishi 67
	dom createNodeCmd -tagName "rpc" elementNode rootXML
6 nishi 68
	dom createNodeCmd -tagName "project" elementNode keyProject
4 nishi 69
	dom createNodeCmd -tagName "version" -jsonType NONE elementNode keyVersion
5 nishi 70
	dom createNodeCmd -tagName "error" -jsonType NONE elementNode keyError
6 nishi 71
	dom createNodeCmd -tagName "name" -jsonType NONE elementNode keyName
72
	dom createNodeCmd -tagName "description" -jsonType NONE elementNode keyDescription
73
	dom createNodeCmd -tagName "vcs" -jsonType NONE elementNode keyVCS
74
	dom createNodeCmd -tagName "url" -jsonType NONE elementNode keyURL
4 nishi 75
	dom createNodeCmd -jsonType STRING textNode valueString
3 nishi 76
}] } {
77
	crash "Failed to load tDOM"
2 nishi 78
}
3 nishi 79
 
80
if { [catch {
13 nishi 81
	set tclx_version "[package require Tclx]"
82
}] } {
83
	crash "Failed to load TclX"
84
}
85
 
86
proc Get_KV {lst key} {
87
	foreach {k v} $lst {
88
		if { "$k" == "$key" } {
89
			return "$v"
90
		}
91
	}
92
	return ""
93
}
94
 
95
proc URL_parse {url} {
96
	if { [regexp {^([^:]+)://(([^:]+:[^@]+|[^:]+:|[^:]+)@)?([^/]+)(.+)?$} "$url" -> scheme userpass_at userpass host path] } {
97
		lappend result "scheme" "$scheme"
98
		lappend result "userpass" "$userpass"
99
		lappend result "host" "$host"
100
		lappend result "path" "$path"
101
		return $result
102
	} elseif { [regexp {^/.+$} "$url" path] } {
103
		lappend result "scheme" "file"
104
		lappend result "userpass" ""
105
		lappend result "host" ""
106
		lappend result "path" "$path"
107
		return $result
108
	}
109
}
110
 
111
if { [catch {
3 nishi 112
	foreach path [glob @@PREFIX@@/lib/koakuma/component/*.tcl] {
113
		source "$path"
114
	}
115
}] } {
116
	crash "Could not load components"
2 nishi 117
}
3 nishi 118
 
5 nishi 119
set data ""
120
 
121
while { [gets stdin line] >= 0 } {
122
	if { "$data" == "" } {
123
		set data "$line"
124
	} else {
125
		set data "$data\n$line"
126
	}
127
}
13 nishi 128
chan close stdin
5 nishi 129
 
3 nishi 130
set toc ""
131
set result ""
132
set content ""
133
 
6 nishi 134
proc write_db {data} {
135
	set fid [open "@@PREFIX@@/lib/koakuma/db/projects.db" "w"]
136
	puts $fid "$data"
137
	close $fid
138
}
139
 
140
proc readall_db {} {
141
	set data ""
142
	set fid [open "@@PREFIX@@/lib/koakuma/db/projects.db" "r"]
143
	while { [gets $fid line] >= 0 } {
144
		if { "$data" == "" } {
145
			set data "$line"
146
		} else {
147
			set data "$data\n$line"
148
		}
149
	}
150
	close $fid
151
	return "$data"
152
}
153
 
3 nishi 154
proc rputs {data} {
155
	global result
156
	if { "$result" == "" } {
157
		set result "$data"
158
	} else {
159
		set result "$result\n$data"
160
	}
161
}
162
 
163
proc tputs {data} {
164
	global content
165
	if { "$content" == "" } {
166
		set content "$data"
167
	} else {
168
		set content "$content\n$data"
169
	}
170
}
171
 
172
proc html_escape {data} {
22 nishi 173
	set tmp "[regsub -all {<} "[regsub -all {>} "[string trim "$data"]" {\&gt;}]" {\&lt;}]"
174
	set link "[regsub -all {[^: ]+://[^ \n]+} "$tmp" {<a href="\0">\0</a>}]"
175
	return "[regsub -all {\n} "$link" {<br>}]"
3 nishi 176
}
177
 
178
proc open_projects {} {
179
	while 1 {
180
		if { ![info exists "@@PREFIX@@/lib/koakuma/db/projects.lock"] } {
181
			break
182
		}
183
		set fid [open "@@PREFIX@@/lib/koakuma/db/projects.lock" "w"]
184
		if { ![info exists "/proc/[gets $fid line]"] } {
185
			close $fid
186
			break
187
		}
188
		after 10
189
		close $fid
190
	}
191
	set fid [open "@@PREFIX@@/lib/koakuma/db/projects.lock" "w"]
192
	puts $fid "[pid]"
193
	close $fid
194
}
195
 
196
proc scan_projects {run} {
197
	set fid [open "@@PREFIX@@/lib/koakuma/db/projects.db" "r"]
198
	set content ""
199
	while { [gets $fid line] >= 0 } {
200
		if { "$content" == "" } {
201
			set content "$line"
202
		} else {
203
			set content "$content\n$line"
204
		}
205
	}
206
	close $fid
207
	set dom [dom parse "$content"]
208
	set doc [$dom documentElement]
209
	foreach elem [$doc selectNodes "/projects/project"] {
210
		set name "[$elem selectNodes "string(name)"]"
211
		set description "[$elem selectNodes "string(description)"]"
13 nishi 212
		set vcs "[$elem selectNodes "string(vcs)"]"
213
		set vcs_url "[$elem selectNodes "string(url)"]"
3 nishi 214
		eval $run
215
	}
216
}
217
 
218
proc project_exists {projname} {
219
	set desc ""
220
	scan_projects {
221
		upvar 1 desc desc
222
		upvar 1 projname projname
223
		if { "$name" == "$projname" } {
224
			set desc "$description"
225
			break
226
		}
227
	}
228
	return "$desc"
229
}
230
 
231
proc close_projects {} {
232
	file delete "@@PREFIX@@/lib/koakuma/db/projects.lock"
233
}
234
 
235
proc start_html {title has_toc} {
13 nishi 236
	global toc env koakuma_png css
3 nishi 237
	rputs	"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">"
238
	rputs	"<html>"
239
	rputs	"	<head>"
240
	rputs	"		<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">"
241
	rputs	"		<title>$title - Koakuma</title>"
13 nishi 242
	rputs	"		<link rel=\"stylesheet\" href=\"$css\">"
3 nishi 243
	rputs	"	</head>"
244
	rputs	"	<body>"
245
	rputs	"		<a href=\"/koakuma\" id=\"gomain\">"
23 nishi 246
	rputs	"			<img src=\"$koakuma_png\" alt=\"Koakuma by Kasuya Baian\" height=\"128px\">"
3 nishi 247
	rputs	"		</a>"
248
	rputs	"		<div id=\"space\"></div>"
249
	rputs	"		<div id=\"title\">"
250
	rputs	"			Koakuma"
251
	rputs	"		</div>"
12 nishi 252
	rputs	"		<a href=\"$env(SCRIPT_NAME)\">Root</a>"
3 nishi 253
	rputs	"		<div id=\"clearfix\"></div>"
254
	if { "$has_toc" == "1" } {
255
		rputs	"		<div id=\"toc\">"
256
		rputs	"			<div id=\"toctitle\">TOC</div>"
257
		foreach sect $toc {
258
			if { "[string range "[regsub -all { } "$sect" "-"]" 0 0]" == "-" } {
259
				rputs "<a class=\"shiftlink\" href=\"#TOC-[regsub {^-} "[regsub -all { } "$sect" "-"]" ""]\">[regsub {^-} "$sect" ""]</a><br>"
260
			} else {
261
				rputs "<a href=\"#TOC-[regsub -all { } "$sect" "-"]\">$sect</a><br>"
262
			}
263
		}
264
		rputs	"		</div>"
265
	}
266
	if { "$has_toc" == "1" } {
267
		rputs	"		<div id=\"doc\">"
268
	} else {
269
		rputs	"		<div id=\"doc-notoc\">"
270
	}
271
	rputs	"			<h1>$title</h1>"
272
}
273
proc end_html {has_toc} {
274
	global KOAKUMA_VERSION
275
	rputs	"		</div>"
276
	rputs	"		<hr>"
277
	rputs	"		<i>Powered by <a href=\"http://nishi.boats/koakuma\">Koakuma</a> $KOAKUMA_VERSION</i>"
278
	rputs	"	</body>"
279
	rputs	"</html>"
280
}
281
 
282
proc add_toc {data} {
283
	global toc
284
	tputs	"<h2 id=\"TOC-[regsub -all { } "$data" "-"]\"><a href=\"#TOC-[regsub -all { } "$data" "-"]\">#</a> $data</h2>"
285
	lappend toc "$data"
286
}
287
 
288
proc add_toc2 {data} {
289
	global toc
290
	tputs	"<h3 id=\"TOC-[regsub -all { } "$data" "-"]\"><a href=\"#TOC-[regsub -all { } "$data" "-"]\">#</a> $data</h3>"
291
	lappend toc "-$data"
292
}
293
 
294
if { [catch {
295
	set path "[regsub -all {/+} "$env(PATH_INFO)" "/"]"
4 nishi 296
	if { [regexp {^/rpc(/.*)?$} "$path"] } {
297
		rputs "Content-Type: application/json"
298
	} else {
12 nishi 299
		if { ![regexp {/$} "$env(PATH_INFO)"] } {
300
			puts "Status: 301 Moved Permanently"
301
			puts "Location: $env(SCRIPT_NAME)$env(PATH_INFO)/"
302
			puts ""
303
			exiting 0
304
		}
4 nishi 305
		rputs "Content-Type: text/html"
306
	}
3 nishi 307
	if { "$path" == "/" } {
308
		add_toc "Tcl Information"
309
		tputs	"<table border=\"0\">"
310
		tputs	"	<tr>"
311
		tputs	"		<th>"
312
		tputs	"			Version"
313
		tputs	"		</th>"
314
		tputs	"		<td>"
315
		tputs	"			$tcl_version"
316
		tputs	"		</td>"
317
		tputs	"	</tr>"
318
		tputs	"	<tr>"
319
		tputs	"		<th>"
320
		tputs	"			Platform"
321
		tputs	"		</th>"
322
		tputs	"		<td>"
323
		tputs	"			$tcl_platform(os)/$tcl_platform(machine) $tcl_platform(osVersion)"
324
		tputs	"		</td>"
325
		tputs	"	</tr>"
13 nishi 326
		tputs	"	<tr>"
327
		tputs	"		<th>"
328
		tputs	"			tDOM version"
329
		tputs	"		</th>"
330
		tputs	"		<td>"
331
		tputs	"			$tdom_version"
332
		tputs	"		</td>"
333
		tputs	"	</tr>"
334
		tputs	"	<tr>"
335
		tputs	"		<th>"
336
		tputs	"			TclX version"
337
		tputs	"		</th>"
338
		tputs	"		<td>"
339
		tputs	"			$tclx_version"
340
		tputs	"		</td>"
341
		tputs	"	</tr>"
3 nishi 342
		tputs	"</table>"
343
		add_toc "Components"
344
		loop_components {
345
			if { [llength [info procs "${name}_info"]] > 0 } {
346
				${name}_info
347
			}
348
		}
349
		set has_projects 0
350
		add_toc "Projects"
351
		open_projects
352
		scan_projects {
353
			upvar 1 has_projects has_projects
354
			if { "$has_projects" == "0" } {
355
				set has_projects 1
356
				tputs	"<table border=\"0\">"
357
			}
358
			tputs	"<tr>"
359
			tputs	"	<th><a href=\"/koakuma/project/$name\">$name</a></th>"
13 nishi 360
			tputs	"	<td>[html_escape "$description"]</td>"
3 nishi 361
			tputs	"</tr>"
362
		}
363
		close_projects
364
		if { "$has_projects" == "1" } {
365
			tputs	"</table>"
366
		} else {
367
			tputs	"No projects have been added, yet."
368
		}
369
 
370
		rputs ""
371
		start_html "Main" 1
372
		rputs "$content"
373
		end_html 1
4 nishi 374
	} elseif { [regexp {^/rpc(/.*)?$} "$path"] } {
375
		regexp {^/rpc(/.*)?$} "$path" -> api
376
		set doc [dom createDocumentNode]
5 nishi 377
		$doc appendFromScript {
378
			keyVersion {valueString "$KOAKUMA_VERSION"}
379
		}
4 nishi 380
		if { "$api" == "" || "$api" == "/" } {
5 nishi 381
			rputs ""
382
			rputs "[$doc asJSON]"
12 nishi 383
		} elseif { "$api" == "/launch-job" } {
384
			if { [catch {dom parse -json "$data" clidoc}] } {
385
				rputs "Status: 400 Bad Request"
386
				$doc appendFromScript {
387
					keyError {valueString "Bad JSON"}
388
				}
389
			} else {
18 nishi 390
				set projname "[regsub -all { } "[$clidoc selectNodes "string(/name)"]" "-"]"
12 nishi 391
				set builddesc "[$clidoc selectNodes "string(/description)"]"
392
				if { "$projname" == "" || "$builddesc" == "" } {
393
					rputs "Status: 400 Bad Request"
394
					$doc appendFromScript {
395
						keyError {valueString "Required field missing"}
396
					}
397
				} else {
398
					set has_name 0
13 nishi 399
					set use_vcs ""
400
					set use_vcs_url ""
12 nishi 401
					open_projects
402
					scan_projects {
403
						upvar 1 has_name has_name
404
						upvar 1 projname projname
13 nishi 405
						upvar 1 use_vcs use_vcs
406
						upvar 1 use_vcs_url use_vcs_url
12 nishi 407
						if { "$name" == "$projname" } {
408
							set has_name 1
13 nishi 409
							set use_vcs "$vcs"
410
							set use_vcs_url "$vcs_url"
12 nishi 411
							break
412
						}
413
					}
414
					close_projects
415
					if { $has_name == 0 } {
416
						rputs "Status: 400 Bad Request"
417
						$doc appendFromScript {
418
							keyError {valueString "Project does not exist"}
419
						}
420
					} else {
13 nishi 421
						set cont 1
422
						if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock"] } {
423
							set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock" "r"]
424
							set readpid "[gets $fid]"
425
							close $fid
426
							if { [file exists "/proc/$readpid"] } {
427
								set cont 0
428
								rputs "Status: 403 Forbidden"
429
								$doc appendFromScript {
430
									keyError {valueString "Other building process has been running"}
431
								}
432
							}
433
						}
434
						if { $cont == 1 } {
435
							set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/buildcount" "r"]
436
							set count [expr [gets $fid] + 1]
437
							close $fid
438
 
439
							set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/buildcount" "w"]
440
							puts $fid "$count"
441
							close $fid
442
 
443
							set count "[format %08s "$count"]"
444
 
445
							set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/lastrun" "w"]
446
							puts $fid "[clock seconds]"
447
							close $fid
448
 
449
							file mkdir "@@PREFIX@@/lib/koakuma/db/data/$projname/build-$count"
450
 
451
							set pid [fork]
452
							if { $pid } {
453
								set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock" "w"]
454
								puts $fid "$pid"
455
								close $fid
456
							} else {
457
								set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/build-$count/log" "w"]
458
								set fail 0
459
 
460
								dup $fid stdout
461
								dup $fid stderr
462
 
463
								puts "===== Checkout"
464
								puts "Using VCS: $use_vcs"
465
								if { [llength [info procs "${use_vcs}_repository"]] == 0 } {
466
									puts "Component internal failure"
467
									set fail 1
468
								} else {
469
									cd "@@PREFIX@@/lib/koakuma/db/data/$projname"
470
									if { [${use_vcs}_repository "$use_vcs_url" "workspace"] } {
471
										puts "Checkout failure"
472
										set fail 1
473
									}
474
								}
475
								if { $fail == 0 } {
476
									puts "===== Build"
477
									cd "@@PREFIX@@/lib/koakuma/db/data/$projname/workspace"
478
									if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/workspace/Koakumafile"] } {
479
										if { [catch {
480
											namespace eval koakumafile {
481
												source "@@PREFIX@@/lib/koakuma/db/data/$projname/workspace/Koakumafile"
482
											}
483
											koakumafile::run "$projname"
484
										}] } {
485
											puts "Failed to run Koakumafile"
486
											set fail 1
487
										}
488
									} else {
489
										puts "Nothing to do"
490
									}
491
								}
492
								if { $fail == 0 } {
493
									puts "Build successful"
494
									set fidsuc [open "@@PREFIX@@/lib/koakuma/db/data/$projname/lastsuccessfulrun" "w"]
495
									puts $fidsuc "[clock seconds]"
496
									close $fidsuc
497
 
498
									set fidsuc [open "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild" "r"]
499
									set sucbul [gets $fidsuc]
500
									close $fidsuc
501
 
502
									set fidsuc [open "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild" "w"]
503
									puts $fidsuc "[expr $sucbul + 1]"
504
									close $fidsuc
505
								}
506
 
507
								close $fid
508
 
509
								file delete "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock"
510
								exit 0
511
							}
512
						}
12 nishi 513
					}
514
				}
515
			}
516
			rputs ""
517
			rputs "[$doc asJSON]"
5 nishi 518
		} elseif { "$api" == "/create-project" } {
519
			if { [catch {dom parse -json "$data" clidoc}] } {
520
				rputs "Status: 400 Bad Request"
521
				$doc appendFromScript {
522
					keyError {valueString "Bad JSON"}
523
				}
524
			} else {
18 nishi 525
				set projname "[regsub -all { } "[$clidoc selectNodes "string(/name)"]" "-"]"
5 nishi 526
				set projdescription "[$clidoc selectNodes "string(/description)"]"
6 nishi 527
				set projvcs "[$clidoc selectNodes "string(/vcs)"]"
5 nishi 528
				set url "[$clidoc selectNodes "string(/url)"]"
6 nishi 529
				if { "$projname" == "" || "$projdescription" == "" || "$projvcs" == "" || "$url" == "" } {
5 nishi 530
					rputs "Status: 400 Bad Request"
531
					$doc appendFromScript {
532
						keyError {valueString "Required field missing"}
533
					}
534
				} else {
535
					set has_vcs 0
6 nishi 536
					set has_name 0
5 nishi 537
					loop_components {
538
						upvar 1 has_vcs has_vcs
6 nishi 539
						upvar 1 projvcs projvcs
540
						if { "$name" == "$projvcs" && "$genre" == "VCS" } {
5 nishi 541
							set has_vcs 1
542
							break
543
						}
544
					}
6 nishi 545
					open_projects
546
					scan_projects {
547
						upvar 1 has_name has_name
548
						upvar 1 projname projname
549
						if { "$name" == "$projname" } {
550
							set has_name 1
551
							break
552
						}
553
					}
554
					close_projects
5 nishi 555
					if { $has_vcs == 0 } {
556
						rputs "Status: 400 Bad Request"
557
						$doc appendFromScript {
558
							keyError {valueString "Not a valid VCS"}
559
						}
6 nishi 560
					} elseif { $has_name == 1 } {
561
						rputs "Status: 400 Bad Request"
562
						$doc appendFromScript {
563
							keyError {valueString "Project already exists"}
564
						}
5 nishi 565
					} else {
566
						open_projects
6 nishi 567
						set xml "[readall_db]"
568
						set xmldoc [dom parse "$xml"]
569
						set root [$xmldoc documentElement]
570
						$root appendFromScript {
571
							keyProject {
572
								keyName {valueString "$projname"}
573
								keyDescription {valueString "$projdescription"}
574
								keyVCS {valueString "$projvcs"}
575
								keyURL {valueString "$url"}
576
							}
577
						}
578
						write_db "[$xmldoc asXML]"
11 nishi 579
						file mkdir "@@PREFIX@@/lib/koakuma/db/data/$projname"
13 nishi 580
						set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/buildcount" "w"]
581
						puts $fid "0"
582
						close $fid
583
						set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild" "w"]
584
						puts $fid "0"
585
						close $fid
5 nishi 586
						close_projects
587
					}
588
				}
589
			}
590
			rputs ""
591
			rputs "[$doc asJSON]"
592
		} else {
4 nishi 593
			$root appendFromScript {
5 nishi 594
				keyError {valueString "No such endpoint"}
4 nishi 595
			}
5 nishi 596
			rputs "Status: 404 Not Found"
597
			rputs ""
4 nishi 598
			rputs "[$doc asJSON]"
599
		}
3 nishi 600
	} elseif { [regexp {^/project/[^/]+.*$} "$path"] } {
12 nishi 601
		regexp {^/project/([^/]+)(.*)$} "$path" -> projname projpath
3 nishi 602
		open_projects
603
		set has_project [project_exists "$projname"]
604
		close_projects
605
 
606
		if { "$has_project" != "" } {
12 nishi 607
			if { "$projpath" == "" || "$projpath" == "/" } {
608
				add_toc "Description"
609
				tputs "[html_escape "$has_project"]"
610
				add_toc "Details"
611
				tputs	"<table border=\"0\">"
612
				tputs	"	<tr>"
613
				tputs	"		<th>"
15 nishi 614
				tputs	"			Status"
12 nishi 615
				tputs	"		</th>"
616
				tputs	"		<td>"
15 nishi 617
				if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock"] } {
18 nishi 618
					set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock" "r"]
15 nishi 619
					if { [file exists "/proc/[gets $fid]"] } {
620
						tputs "Running"
621
					} else {
622
						tputs "Idle"
623
					}
12 nishi 624
					close $fid
625
				} else {
15 nishi 626
					tputs "Idle"
12 nishi 627
				}
628
				tputs	"			"
629
				tputs	"		</td>"
630
				tputs	"	</tr>"
631
				tputs	"	<tr>"
632
				tputs	"		<th>"
15 nishi 633
				tputs	"			Last run"
14 nishi 634
				tputs	"		</th>"
635
				tputs	"		<td>"
15 nishi 636
				if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/lastrun"] } {
637
					set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/lastrun" "r"]
638
					set date "[clock format "[gets $fid]" -format "%a %b %d %H:%M:%S %Z %Y"]"
14 nishi 639
					close $fid
15 nishi 640
					tputs "$date"
14 nishi 641
				} else {
15 nishi 642
					tputs "No builds yet"
14 nishi 643
				}
644
				tputs	"			"
645
				tputs	"		</td>"
646
				tputs	"	</tr>"
647
				tputs	"	<tr>"
648
				tputs	"		<th>"
12 nishi 649
				tputs	"			Last successful run"
650
				tputs	"		</th>"
651
				tputs	"		<td>"
652
				if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/lastsuccessfulrun"] } {
653
					set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/lastsuccessfulrun" "r"]
654
					set date "[clock format "[gets $fid]" -format "%a %b %d %H:%M:%S %Z %Y"]"
655
					close $fid
656
					tputs "$date"
657
				} else {
658
					tputs "No successful builds yet"
659
				}
660
				tputs	"			"
661
				tputs	"		</td>"
662
				tputs	"	</tr>"
13 nishi 663
				set builds [lsort -ascii [glob -nocomplain "@@PREFIX@@/lib/koakuma/db/data/$projname/build-*"]]
664
				if { [llength $builds] > 0 } {
665
					tputs	"	<tr>"
666
					tputs	"		<th>"
667
					tputs	"			Successful builds"
668
					tputs	"		</th>"
669
					tputs	"		<td>"
670
					if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild"] } {
671
						set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild" "r"]
672
						tputs "[format %.2f [expr [gets $fid] / [llength $builds] * 100]]%"
673
						close $fid
674
					}
675
					tputs	"			"
676
					tputs	"		</td>"
677
					tputs	"	</tr>"
678
				}
12 nishi 679
				tputs	"</table>"
13 nishi 680
 
681
				set builds [lsort -ascii [glob -nocomplain "@@PREFIX@@/lib/koakuma/db/data/$projname/build-*"]]
682
				if { [llength $builds] > 0 } {
683
					add_toc "Last build log"
684
					set lastbuild "[lindex $builds [expr [llength $builds] - 1]]"
685
					set fid [open "$lastbuild/log" "r"]
686
					tputs "<pre>"
687
					while { [gets $fid line] >= 0 } {
688
						tputs "[html_escape "$line"]"
689
					}
690
					tputs "</pre>"
691
					close $fid
692
				}
12 nishi 693
 
694
				rputs ""
695
				start_html "Project: $projname" 1
696
				rputs "$content"
697
				end_html 1
11 nishi 698
			} else {
12 nishi 699
				tputs "I could not find the endpoint you were finding."
700
 
701
				rputs "Status: 404 Not Found"
702
				rputs ""
703
				start_html "Project: $projname" 1
704
				rputs "$content"
705
				end_html 1
11 nishi 706
			}
3 nishi 707
		} else {
708
			tputs "I could not find the project you were finding."
709
 
710
			rputs "Status: 404 Not Found"
711
			rputs ""
712
			start_html "Not Found" 0
713
			rputs "$content"
714
			end_html 0
715
		}
716
	} else {
717
		tputs "I could not find the content you were finding."
718
 
719
		rputs "Status: 404 Not Found"
720
		rputs ""
721
		start_html "Not Found" 0
722
		rputs "$content"
723
		end_html 0
724
	}
725
}] } {
726
	crash "Could not render the HTML"
727
} else {
728
	puts "$result"
729
}
730
exiting 0