Subversion Repositories Koakuma

Rev

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