Subversion Repositories Koakuma

Rev

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