Subversion Repositories Koakuma

Rev

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