Subversion Repositories Koakuma

Rev

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