Subversion Repositories Koakuma

Rev

Rev 28 | Rev 30 | 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 29 2024-10-02 08:56:47Z 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\">"
24 nishi 243
	set msie " src=\"$koakuma_png\""
29 nishi 244
	set msie2 ""
24 nishi 245
	if { [info exists "env(HTTP_USER_AGENT)"] } {
246
		if { [regexp {MSIE 6} "$env(HTTP_USER_AGENT)"] } {
247
			set msie " style=\"filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='$koakuma_png', sizingMethod='scale');\" src=\"/static/transparent.gif\""
29 nishi 248
			set msie2 " style=\"margin-top: 20px;\""
24 nishi 249
		}
250
	}
3 nishi 251
	rputs	"	</head>"
252
	rputs	"	<body>"
253
	rputs	"		<a href=\"/koakuma\" id=\"gomain\">"
24 nishi 254
	rputs	"			<img alt=\"Koakuma by Kasuya Baian\" height=\"128px\"$msie>"
3 nishi 255
	rputs	"		</a>"
256
	rputs	"		<div id=\"space\"></div>"
257
	rputs	"		<div id=\"title\">"
258
	rputs	"			Koakuma"
259
	rputs	"		</div>"
12 nishi 260
	rputs	"		<a href=\"$env(SCRIPT_NAME)\">Root</a>"
3 nishi 261
	rputs	"		<div id=\"clearfix\"></div>"
262
	if { "$has_toc" == "1" } {
29 nishi 263
		rputs	"		<div id=\"doc\">"
264
	} else {
265
		rputs	"		<div id=\"doc-notoc\">"
266
	}
267
	if { "$has_toc" == "1" } {
268
		rputs	"		<div id=\"toc\"$msie2>"
3 nishi 269
		rputs	"			<div id=\"toctitle\">TOC</div>"
270
		foreach sect $toc {
271
			if { "[string range "[regsub -all { } "$sect" "-"]" 0 0]" == "-" } {
272
				rputs "<a class=\"shiftlink\" href=\"#TOC-[regsub {^-} "[regsub -all { } "$sect" "-"]" ""]\">[regsub {^-} "$sect" ""]</a><br>"
273
			} else {
274
				rputs "<a href=\"#TOC-[regsub -all { } "$sect" "-"]\">$sect</a><br>"
275
			}
276
		}
277
		rputs	"		</div>"
278
	}
29 nishi 279
	rputs	"			<div id=\"docinside\">"
280
	rputs	"				<h1>$title</h1>"
3 nishi 281
}
282
proc end_html {has_toc} {
28 nishi 283
	global KOAKUMA_VERSION toc
29 nishi 284
	rputs	"			</div>"
3 nishi 285
	rputs	"		</div>"
28 nishi 286
	rputs	"		<div id=\"clearfix\"></div>"
3 nishi 287
	rputs	"		<hr>"
288
	rputs	"		<i>Powered by <a href=\"http://nishi.boats/koakuma\">Koakuma</a> $KOAKUMA_VERSION</i>"
289
	rputs	"	</body>"
290
	rputs	"</html>"
291
}
292
 
293
proc add_toc {data} {
294
	global toc
295
	tputs	"<h2 id=\"TOC-[regsub -all { } "$data" "-"]\"><a href=\"#TOC-[regsub -all { } "$data" "-"]\">#</a> $data</h2>"
296
	lappend toc "$data"
297
}
298
 
299
proc add_toc2 {data} {
300
	global toc
301
	tputs	"<h3 id=\"TOC-[regsub -all { } "$data" "-"]\"><a href=\"#TOC-[regsub -all { } "$data" "-"]\">#</a> $data</h3>"
302
	lappend toc "-$data"
303
}
304
 
305
if { [catch {
306
	set path "[regsub -all {/+} "$env(PATH_INFO)" "/"]"
4 nishi 307
	if { [regexp {^/rpc(/.*)?$} "$path"] } {
308
		rputs "Content-Type: application/json"
309
	} else {
12 nishi 310
		if { ![regexp {/$} "$env(PATH_INFO)"] } {
311
			puts "Status: 301 Moved Permanently"
312
			puts "Location: $env(SCRIPT_NAME)$env(PATH_INFO)/"
313
			puts ""
314
			exiting 0
315
		}
4 nishi 316
		rputs "Content-Type: text/html"
317
	}
3 nishi 318
	if { "$path" == "/" } {
28 nishi 319
		set has_projects 0
320
		add_toc "Projects"
321
		open_projects
322
		scan_projects {
323
			upvar 1 has_projects has_projects
324
			if { "$has_projects" == "0" } {
325
				set has_projects 1
326
				tputs	"<table border=\"0\">"
327
			}
328
			tputs	"<tr>"
329
			tputs	"	<th><a href=\"/koakuma/project/$name\">$name</a></th>"
330
			tputs	"	<td>[html_escape "$description"]</td>"
331
			tputs	"</tr>"
332
		}
333
		close_projects
334
		if { "$has_projects" == "1" } {
335
			tputs	"</table>"
336
		} else {
337
			tputs	"No projects have been added, yet."
338
		}
3 nishi 339
		add_toc "Tcl Information"
340
		tputs	"<table border=\"0\">"
341
		tputs	"	<tr>"
342
		tputs	"		<th>"
343
		tputs	"			Version"
344
		tputs	"		</th>"
345
		tputs	"		<td>"
346
		tputs	"			$tcl_version"
347
		tputs	"		</td>"
348
		tputs	"	</tr>"
349
		tputs	"	<tr>"
350
		tputs	"		<th>"
351
		tputs	"			Platform"
352
		tputs	"		</th>"
353
		tputs	"		<td>"
354
		tputs	"			$tcl_platform(os)/$tcl_platform(machine) $tcl_platform(osVersion)"
355
		tputs	"		</td>"
356
		tputs	"	</tr>"
13 nishi 357
		tputs	"	<tr>"
358
		tputs	"		<th>"
359
		tputs	"			tDOM version"
360
		tputs	"		</th>"
361
		tputs	"		<td>"
362
		tputs	"			$tdom_version"
363
		tputs	"		</td>"
364
		tputs	"	</tr>"
365
		tputs	"	<tr>"
366
		tputs	"		<th>"
367
		tputs	"			TclX version"
368
		tputs	"		</th>"
369
		tputs	"		<td>"
370
		tputs	"			$tclx_version"
371
		tputs	"		</td>"
372
		tputs	"	</tr>"
3 nishi 373
		tputs	"</table>"
374
		add_toc "Components"
375
		loop_components {
28 nishi 376
			add_toc2 "${name} (${genre})"
3 nishi 377
			if { [llength [info procs "${name}_info"]] > 0 } {
378
				${name}_info
379
			}
380
		}
381
 
382
		rputs ""
383
		start_html "Main" 1
384
		rputs "$content"
385
		end_html 1
4 nishi 386
	} elseif { [regexp {^/rpc(/.*)?$} "$path"] } {
387
		regexp {^/rpc(/.*)?$} "$path" -> api
388
		set doc [dom createDocumentNode]
5 nishi 389
		$doc appendFromScript {
390
			keyVersion {valueString "$KOAKUMA_VERSION"}
391
		}
4 nishi 392
		if { "$api" == "" || "$api" == "/" } {
5 nishi 393
			rputs ""
394
			rputs "[$doc asJSON]"
12 nishi 395
		} elseif { "$api" == "/launch-job" } {
396
			if { [catch {dom parse -json "$data" clidoc}] } {
397
				rputs "Status: 400 Bad Request"
398
				$doc appendFromScript {
399
					keyError {valueString "Bad JSON"}
400
				}
401
			} else {
18 nishi 402
				set projname "[regsub -all { } "[$clidoc selectNodes "string(/name)"]" "-"]"
12 nishi 403
				set builddesc "[$clidoc selectNodes "string(/description)"]"
404
				if { "$projname" == "" || "$builddesc" == "" } {
405
					rputs "Status: 400 Bad Request"
406
					$doc appendFromScript {
407
						keyError {valueString "Required field missing"}
408
					}
409
				} else {
410
					set has_name 0
13 nishi 411
					set use_vcs ""
412
					set use_vcs_url ""
12 nishi 413
					open_projects
414
					scan_projects {
415
						upvar 1 has_name has_name
416
						upvar 1 projname projname
13 nishi 417
						upvar 1 use_vcs use_vcs
418
						upvar 1 use_vcs_url use_vcs_url
12 nishi 419
						if { "$name" == "$projname" } {
420
							set has_name 1
13 nishi 421
							set use_vcs "$vcs"
422
							set use_vcs_url "$vcs_url"
12 nishi 423
							break
424
						}
425
					}
426
					close_projects
427
					if { $has_name == 0 } {
428
						rputs "Status: 400 Bad Request"
429
						$doc appendFromScript {
430
							keyError {valueString "Project does not exist"}
431
						}
432
					} else {
13 nishi 433
						set cont 1
434
						if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock"] } {
435
							set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock" "r"]
436
							set readpid "[gets $fid]"
437
							close $fid
438
							if { [file exists "/proc/$readpid"] } {
439
								set cont 0
440
								rputs "Status: 403 Forbidden"
441
								$doc appendFromScript {
442
									keyError {valueString "Other building process has been running"}
443
								}
444
							}
445
						}
446
						if { $cont == 1 } {
447
							set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/buildcount" "r"]
448
							set count [expr [gets $fid] + 1]
449
							close $fid
450
 
451
							set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/buildcount" "w"]
452
							puts $fid "$count"
453
							close $fid
454
 
455
							set count "[format %08s "$count"]"
456
 
457
							set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/lastrun" "w"]
458
							puts $fid "[clock seconds]"
459
							close $fid
460
 
461
							file mkdir "@@PREFIX@@/lib/koakuma/db/data/$projname/build-$count"
462
 
463
							set pid [fork]
464
							if { $pid } {
465
								set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock" "w"]
466
								puts $fid "$pid"
467
								close $fid
468
							} else {
469
								set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/build-$count/log" "w"]
470
								set fail 0
471
 
472
								dup $fid stdout
473
								dup $fid stderr
474
 
475
								puts "===== Checkout"
476
								puts "Using VCS: $use_vcs"
477
								if { [llength [info procs "${use_vcs}_repository"]] == 0 } {
478
									puts "Component internal failure"
479
									set fail 1
480
								} else {
481
									cd "@@PREFIX@@/lib/koakuma/db/data/$projname"
482
									if { [${use_vcs}_repository "$use_vcs_url" "workspace"] } {
483
										puts "Checkout failure"
484
										set fail 1
485
									}
486
								}
487
								if { $fail == 0 } {
488
									puts "===== Build"
489
									cd "@@PREFIX@@/lib/koakuma/db/data/$projname/workspace"
490
									if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/workspace/Koakumafile"] } {
491
										if { [catch {
492
											namespace eval koakumafile {
493
												source "@@PREFIX@@/lib/koakuma/db/data/$projname/workspace/Koakumafile"
494
											}
495
											koakumafile::run "$projname"
496
										}] } {
497
											puts "Failed to run Koakumafile"
498
											set fail 1
499
										}
500
									} else {
501
										puts "Nothing to do"
502
									}
503
								}
504
								if { $fail == 0 } {
505
									puts "Build successful"
506
									set fidsuc [open "@@PREFIX@@/lib/koakuma/db/data/$projname/lastsuccessfulrun" "w"]
507
									puts $fidsuc "[clock seconds]"
508
									close $fidsuc
509
 
510
									set fidsuc [open "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild" "r"]
511
									set sucbul [gets $fidsuc]
512
									close $fidsuc
513
 
514
									set fidsuc [open "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild" "w"]
515
									puts $fidsuc "[expr $sucbul + 1]"
516
									close $fidsuc
517
								}
518
 
519
								close $fid
520
 
521
								file delete "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock"
522
								exit 0
523
							}
524
						}
12 nishi 525
					}
526
				}
527
			}
528
			rputs ""
529
			rputs "[$doc asJSON]"
5 nishi 530
		} elseif { "$api" == "/create-project" } {
531
			if { [catch {dom parse -json "$data" clidoc}] } {
532
				rputs "Status: 400 Bad Request"
533
				$doc appendFromScript {
534
					keyError {valueString "Bad JSON"}
535
				}
536
			} else {
18 nishi 537
				set projname "[regsub -all { } "[$clidoc selectNodes "string(/name)"]" "-"]"
5 nishi 538
				set projdescription "[$clidoc selectNodes "string(/description)"]"
6 nishi 539
				set projvcs "[$clidoc selectNodes "string(/vcs)"]"
5 nishi 540
				set url "[$clidoc selectNodes "string(/url)"]"
6 nishi 541
				if { "$projname" == "" || "$projdescription" == "" || "$projvcs" == "" || "$url" == "" } {
5 nishi 542
					rputs "Status: 400 Bad Request"
543
					$doc appendFromScript {
544
						keyError {valueString "Required field missing"}
545
					}
546
				} else {
547
					set has_vcs 0
6 nishi 548
					set has_name 0
5 nishi 549
					loop_components {
550
						upvar 1 has_vcs has_vcs
6 nishi 551
						upvar 1 projvcs projvcs
552
						if { "$name" == "$projvcs" && "$genre" == "VCS" } {
5 nishi 553
							set has_vcs 1
554
							break
555
						}
556
					}
6 nishi 557
					open_projects
558
					scan_projects {
559
						upvar 1 has_name has_name
560
						upvar 1 projname projname
561
						if { "$name" == "$projname" } {
562
							set has_name 1
563
							break
564
						}
565
					}
566
					close_projects
5 nishi 567
					if { $has_vcs == 0 } {
568
						rputs "Status: 400 Bad Request"
569
						$doc appendFromScript {
570
							keyError {valueString "Not a valid VCS"}
571
						}
6 nishi 572
					} elseif { $has_name == 1 } {
573
						rputs "Status: 400 Bad Request"
574
						$doc appendFromScript {
575
							keyError {valueString "Project already exists"}
576
						}
5 nishi 577
					} else {
578
						open_projects
6 nishi 579
						set xml "[readall_db]"
580
						set xmldoc [dom parse "$xml"]
581
						set root [$xmldoc documentElement]
582
						$root appendFromScript {
583
							keyProject {
584
								keyName {valueString "$projname"}
585
								keyDescription {valueString "$projdescription"}
586
								keyVCS {valueString "$projvcs"}
587
								keyURL {valueString "$url"}
588
							}
589
						}
590
						write_db "[$xmldoc asXML]"
11 nishi 591
						file mkdir "@@PREFIX@@/lib/koakuma/db/data/$projname"
13 nishi 592
						set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/buildcount" "w"]
593
						puts $fid "0"
594
						close $fid
595
						set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild" "w"]
596
						puts $fid "0"
597
						close $fid
5 nishi 598
						close_projects
599
					}
600
				}
601
			}
602
			rputs ""
603
			rputs "[$doc asJSON]"
604
		} else {
4 nishi 605
			$root appendFromScript {
5 nishi 606
				keyError {valueString "No such endpoint"}
4 nishi 607
			}
5 nishi 608
			rputs "Status: 404 Not Found"
609
			rputs ""
4 nishi 610
			rputs "[$doc asJSON]"
611
		}
3 nishi 612
	} elseif { [regexp {^/project/[^/]+.*$} "$path"] } {
12 nishi 613
		regexp {^/project/([^/]+)(.*)$} "$path" -> projname projpath
3 nishi 614
		open_projects
615
		set has_project [project_exists "$projname"]
616
		close_projects
617
 
618
		if { "$has_project" != "" } {
12 nishi 619
			if { "$projpath" == "" || "$projpath" == "/" } {
620
				add_toc "Description"
621
				tputs "[html_escape "$has_project"]"
622
				add_toc "Details"
623
				tputs	"<table border=\"0\">"
624
				tputs	"	<tr>"
625
				tputs	"		<th>"
15 nishi 626
				tputs	"			Status"
12 nishi 627
				tputs	"		</th>"
628
				tputs	"		<td>"
15 nishi 629
				if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock"] } {
18 nishi 630
					set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock" "r"]
15 nishi 631
					if { [file exists "/proc/[gets $fid]"] } {
632
						tputs "Running"
633
					} else {
634
						tputs "Idle"
635
					}
12 nishi 636
					close $fid
637
				} else {
15 nishi 638
					tputs "Idle"
12 nishi 639
				}
640
				tputs	"			"
641
				tputs	"		</td>"
642
				tputs	"	</tr>"
643
				tputs	"	<tr>"
644
				tputs	"		<th>"
15 nishi 645
				tputs	"			Last run"
14 nishi 646
				tputs	"		</th>"
647
				tputs	"		<td>"
15 nishi 648
				if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/lastrun"] } {
649
					set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/lastrun" "r"]
650
					set date "[clock format "[gets $fid]" -format "%a %b %d %H:%M:%S %Z %Y"]"
14 nishi 651
					close $fid
15 nishi 652
					tputs "$date"
14 nishi 653
				} else {
15 nishi 654
					tputs "No builds yet"
14 nishi 655
				}
656
				tputs	"			"
657
				tputs	"		</td>"
658
				tputs	"	</tr>"
659
				tputs	"	<tr>"
660
				tputs	"		<th>"
12 nishi 661
				tputs	"			Last successful run"
662
				tputs	"		</th>"
663
				tputs	"		<td>"
664
				if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/lastsuccessfulrun"] } {
665
					set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/lastsuccessfulrun" "r"]
666
					set date "[clock format "[gets $fid]" -format "%a %b %d %H:%M:%S %Z %Y"]"
667
					close $fid
668
					tputs "$date"
669
				} else {
670
					tputs "No successful builds yet"
671
				}
672
				tputs	"			"
673
				tputs	"		</td>"
674
				tputs	"	</tr>"
13 nishi 675
				set builds [lsort -ascii [glob -nocomplain "@@PREFIX@@/lib/koakuma/db/data/$projname/build-*"]]
676
				if { [llength $builds] > 0 } {
677
					tputs	"	<tr>"
678
					tputs	"		<th>"
679
					tputs	"			Successful builds"
680
					tputs	"		</th>"
681
					tputs	"		<td>"
682
					if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild"] } {
683
						set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild" "r"]
684
						tputs "[format %.2f [expr [gets $fid] / [llength $builds] * 100]]%"
685
						close $fid
686
					}
687
					tputs	"			"
688
					tputs	"		</td>"
689
					tputs	"	</tr>"
690
				}
12 nishi 691
				tputs	"</table>"
13 nishi 692
 
693
				set builds [lsort -ascii [glob -nocomplain "@@PREFIX@@/lib/koakuma/db/data/$projname/build-*"]]
694
				if { [llength $builds] > 0 } {
695
					add_toc "Last build log"
696
					set lastbuild "[lindex $builds [expr [llength $builds] - 1]]"
697
					set fid [open "$lastbuild/log" "r"]
698
					tputs "<pre>"
699
					while { [gets $fid line] >= 0 } {
700
						tputs "[html_escape "$line"]"
701
					}
702
					tputs "</pre>"
703
					close $fid
704
				}
12 nishi 705
 
706
				rputs ""
707
				start_html "Project: $projname" 1
708
				rputs "$content"
709
				end_html 1
11 nishi 710
			} else {
12 nishi 711
				tputs "I could not find the endpoint you were finding."
712
 
713
				rputs "Status: 404 Not Found"
714
				rputs ""
715
				start_html "Project: $projname" 1
716
				rputs "$content"
717
				end_html 1
11 nishi 718
			}
3 nishi 719
		} else {
720
			tputs "I could not find the project you were finding."
721
 
722
			rputs "Status: 404 Not Found"
723
			rputs ""
724
			start_html "Not Found" 0
725
			rputs "$content"
726
			end_html 0
727
		}
728
	} else {
729
		tputs "I could not find the content you were finding."
730
 
731
		rputs "Status: 404 Not Found"
732
		rputs ""
733
		start_html "Not Found" 0
734
		rputs "$content"
735
		end_html 0
736
	}
737
}] } {
738
	crash "Could not render the HTML"
739
} else {
740
	puts "$result"
741
}
742
exiting 0