Subversion Repositories Koakuma

Rev

Rev 33 | Rev 36 | 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 35 2024-10-02 09:40: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
 
35 nishi 20
proc RunCommand {command} {
21
	puts "* $command"
22
	eval exec $command >@stdout 2>@1
23
}
24
 
2 nishi 25
proc crash {reason} {
26
	global components KOAKUMA_VERSION
27
	puts stderr "----- Start Koakuma Crash dump log -----"
28
	puts stderr "Included components:"
3 nishi 29
	loop_components {
2 nishi 30
		puts stderr "	$name: $description, version $version"
31
	}
32
	puts stderr "Reason: $reason"
13 nishi 33
	puts stderr "Code: $::errorCode"
34
	puts stderr "Info: $::errorInfo"
2 nishi 35
	puts stderr "----- End Koakuma Crash dump log -----"
36
	puts	"Content-Type: text/html"
37
	puts	"Status: 500 Internal Server Error"
38
	puts	""
39
	puts	"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">"
40
	puts	"<html>"
41
	puts	"	<head>"
42
	puts	"		<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">"
43
	puts	"		<title>Oops</title>"
44
	puts	"	</head>"
45
	puts	"	<body>"
46
	puts	"		<h1>Oops</h1>"
47
	puts	"		<hr>"
48
	puts	"		Koakuma version $KOAKUMA_VERSION crashed, reason: <code>$reason</code><br>"
49
	puts	"		See the server error log for details."
50
	puts	"	</body>"
51
	puts	"</html>"
52
	exiting 1
53
}
54
 
3 nishi 55
if { ![info exists env(PATH_INFO)] } {
56
	puts "Status: 301 Moved Permanently"
57
	puts "Location: $env(SCRIPT_NAME)/"
58
	puts ""
59
	exiting 0
2 nishi 60
}
61
 
13 nishi 62
if { [file exists "@@PREFIX@@/etc/koakuma/cgi.conf"] } {
63
	if { [catch {
64
		source "@@PREFIX@@/etc/koakuma/cgi.conf"
65
	}] } {
66
		crash "Config failure"
67
	}
68
}
69
 
3 nishi 70
if { [catch {
13 nishi 71
	set tdom_version "[package require tdom]"
4 nishi 72
	dom createNodeCmd -tagName "rpc" elementNode rootXML
6 nishi 73
	dom createNodeCmd -tagName "project" elementNode keyProject
4 nishi 74
	dom createNodeCmd -tagName "version" -jsonType NONE elementNode keyVersion
5 nishi 75
	dom createNodeCmd -tagName "error" -jsonType NONE elementNode keyError
6 nishi 76
	dom createNodeCmd -tagName "name" -jsonType NONE elementNode keyName
77
	dom createNodeCmd -tagName "description" -jsonType NONE elementNode keyDescription
78
	dom createNodeCmd -tagName "vcs" -jsonType NONE elementNode keyVCS
79
	dom createNodeCmd -tagName "url" -jsonType NONE elementNode keyURL
4 nishi 80
	dom createNodeCmd -jsonType STRING textNode valueString
3 nishi 81
}] } {
82
	crash "Failed to load tDOM"
2 nishi 83
}
3 nishi 84
 
85
if { [catch {
13 nishi 86
	set tclx_version "[package require Tclx]"
87
}] } {
88
	crash "Failed to load TclX"
89
}
90
 
91
proc Get_KV {lst key} {
92
	foreach {k v} $lst {
93
		if { "$k" == "$key" } {
94
			return "$v"
95
		}
96
	}
97
	return ""
98
}
99
 
100
proc URL_parse {url} {
101
	if { [regexp {^([^:]+)://(([^:]+:[^@]+|[^:]+:|[^:]+)@)?([^/]+)(.+)?$} "$url" -> scheme userpass_at userpass host path] } {
102
		lappend result "scheme" "$scheme"
103
		lappend result "userpass" "$userpass"
104
		lappend result "host" "$host"
105
		lappend result "path" "$path"
106
		return $result
107
	} elseif { [regexp {^/.+$} "$url" path] } {
108
		lappend result "scheme" "file"
109
		lappend result "userpass" ""
110
		lappend result "host" ""
111
		lappend result "path" "$path"
112
		return $result
113
	}
114
}
115
 
116
if { [catch {
3 nishi 117
	foreach path [glob @@PREFIX@@/lib/koakuma/component/*.tcl] {
118
		source "$path"
119
	}
120
}] } {
121
	crash "Could not load components"
2 nishi 122
}
3 nishi 123
 
5 nishi 124
set data ""
125
 
126
while { [gets stdin line] >= 0 } {
127
	if { "$data" == "" } {
128
		set data "$line"
129
	} else {
130
		set data "$data\n$line"
131
	}
132
}
13 nishi 133
chan close stdin
5 nishi 134
 
3 nishi 135
set toc ""
136
set result ""
137
set content ""
138
 
6 nishi 139
proc write_db {data} {
140
	set fid [open "@@PREFIX@@/lib/koakuma/db/projects.db" "w"]
141
	puts $fid "$data"
142
	close $fid
143
}
144
 
145
proc readall_db {} {
146
	set data ""
147
	set fid [open "@@PREFIX@@/lib/koakuma/db/projects.db" "r"]
148
	while { [gets $fid line] >= 0 } {
149
		if { "$data" == "" } {
150
			set data "$line"
151
		} else {
152
			set data "$data\n$line"
153
		}
154
	}
155
	close $fid
156
	return "$data"
157
}
158
 
3 nishi 159
proc rputs {data} {
160
	global result
161
	if { "$result" == "" } {
162
		set result "$data"
163
	} else {
164
		set result "$result\n$data"
165
	}
166
}
167
 
168
proc tputs {data} {
169
	global content
170
	if { "$content" == "" } {
171
		set content "$data"
172
	} else {
173
		set content "$content\n$data"
174
	}
175
}
176
 
177
proc html_escape {data} {
22 nishi 178
	set tmp "[regsub -all {<} "[regsub -all {>} "[string trim "$data"]" {\&gt;}]" {\&lt;}]"
179
	set link "[regsub -all {[^: ]+://[^ \n]+} "$tmp" {<a href="\0">\0</a>}]"
180
	return "[regsub -all {\n} "$link" {<br>}]"
3 nishi 181
}
182
 
183
proc open_projects {} {
184
	while 1 {
185
		if { ![info exists "@@PREFIX@@/lib/koakuma/db/projects.lock"] } {
186
			break
187
		}
188
		set fid [open "@@PREFIX@@/lib/koakuma/db/projects.lock" "w"]
189
		if { ![info exists "/proc/[gets $fid line]"] } {
190
			close $fid
191
			break
192
		}
193
		after 10
194
		close $fid
195
	}
196
	set fid [open "@@PREFIX@@/lib/koakuma/db/projects.lock" "w"]
197
	puts $fid "[pid]"
198
	close $fid
199
}
200
 
201
proc scan_projects {run} {
202
	set fid [open "@@PREFIX@@/lib/koakuma/db/projects.db" "r"]
203
	set content ""
204
	while { [gets $fid line] >= 0 } {
205
		if { "$content" == "" } {
206
			set content "$line"
207
		} else {
208
			set content "$content\n$line"
209
		}
210
	}
211
	close $fid
212
	set dom [dom parse "$content"]
213
	set doc [$dom documentElement]
214
	foreach elem [$doc selectNodes "/projects/project"] {
215
		set name "[$elem selectNodes "string(name)"]"
216
		set description "[$elem selectNodes "string(description)"]"
13 nishi 217
		set vcs "[$elem selectNodes "string(vcs)"]"
218
		set vcs_url "[$elem selectNodes "string(url)"]"
3 nishi 219
		eval $run
220
	}
221
}
222
 
223
proc project_exists {projname} {
224
	set desc ""
225
	scan_projects {
226
		upvar 1 desc desc
227
		upvar 1 projname projname
228
		if { "$name" == "$projname" } {
229
			set desc "$description"
230
			break
231
		}
232
	}
233
	return "$desc"
234
}
235
 
236
proc close_projects {} {
237
	file delete "@@PREFIX@@/lib/koakuma/db/projects.lock"
238
}
239
 
33 nishi 240
set what ""
241
 
3 nishi 242
proc start_html {title has_toc} {
33 nishi 243
	global toc env koakuma_png css what
3 nishi 244
	rputs	"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">"
245
	rputs	"<html>"
246
	rputs	"	<head>"
247
	rputs	"		<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">"
248
	rputs	"		<title>$title - Koakuma</title>"
13 nishi 249
	rputs	"		<link rel=\"stylesheet\" href=\"$css\">"
24 nishi 250
	set msie " src=\"$koakuma_png\""
251
	if { [info exists "env(HTTP_USER_AGENT)"] } {
252
		if { [regexp {MSIE 6} "$env(HTTP_USER_AGENT)"] } {
253
			set msie " style=\"filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='$koakuma_png', sizingMethod='scale');\" src=\"/static/transparent.gif\""
254
		}
255
	}
3 nishi 256
	rputs	"	</head>"
257
	rputs	"	<body>"
258
	rputs	"		<a href=\"/koakuma\" id=\"gomain\">"
24 nishi 259
	rputs	"			<img alt=\"Koakuma by Kasuya Baian\" height=\"128px\"$msie>"
3 nishi 260
	rputs	"		</a>"
261
	rputs	"		<div id=\"space\"></div>"
262
	rputs	"		<div id=\"title\">"
263
	rputs	"			Koakuma"
264
	rputs	"		</div>"
12 nishi 265
	rputs	"		<a href=\"$env(SCRIPT_NAME)\">Root</a>"
3 nishi 266
	if { "$has_toc" == "1" } {
29 nishi 267
		rputs	"		<div id=\"doc\">"
268
	} else {
269
		rputs	"		<div id=\"doc-notoc\">"
270
	}
30 nishi 271
	rputs	"		<div style=\"clear: both;\"></div>"
29 nishi 272
	if { "$has_toc" == "1" } {
30 nishi 273
		rputs	"		<div id=\"toc\">"
274
		rputs	"			<div id=\"tocinside\">"
31 nishi 275
		rputs	"				<span id=\"toctitle\">TOC</span><hr>"
3 nishi 276
		foreach sect $toc {
277
			if { "[string range "[regsub -all { } "$sect" "-"]" 0 0]" == "-" } {
278
				rputs "<a class=\"shiftlink\" href=\"#TOC-[regsub {^-} "[regsub -all { } "$sect" "-"]" ""]\">[regsub {^-} "$sect" ""]</a><br>"
279
			} else {
280
				rputs "<a href=\"#TOC-[regsub -all { } "$sect" "-"]\">$sect</a><br>"
281
			}
282
		}
30 nishi 283
		rputs	"			</div>"
3 nishi 284
		rputs	"		</div>"
285
	}
29 nishi 286
	rputs	"			<div id=\"docinside\">"
287
	rputs	"				<h1>$title</h1>"
33 nishi 288
	rputs	"				$what"
31 nishi 289
	rputs	"				<div id=\"shift\">"
3 nishi 290
}
291
proc end_html {has_toc} {
28 nishi 292
	global KOAKUMA_VERSION toc
31 nishi 293
	rputs	"				</div>"
29 nishi 294
	rputs	"			</div>"
3 nishi 295
	rputs	"		</div>"
28 nishi 296
	rputs	"		<div id=\"clearfix\"></div>"
3 nishi 297
	rputs	"		<hr>"
298
	rputs	"		<i>Powered by <a href=\"http://nishi.boats/koakuma\">Koakuma</a> $KOAKUMA_VERSION</i>"
299
	rputs	"	</body>"
300
	rputs	"</html>"
301
}
302
 
303
proc add_toc {data} {
304
	global toc
305
	tputs	"<h2 id=\"TOC-[regsub -all { } "$data" "-"]\"><a href=\"#TOC-[regsub -all { } "$data" "-"]\">#</a> $data</h2>"
306
	lappend toc "$data"
307
}
308
 
309
proc add_toc2 {data} {
310
	global toc
311
	tputs	"<h3 id=\"TOC-[regsub -all { } "$data" "-"]\"><a href=\"#TOC-[regsub -all { } "$data" "-"]\">#</a> $data</h3>"
312
	lappend toc "-$data"
313
}
314
 
315
if { [catch {
316
	set path "[regsub -all {/+} "$env(PATH_INFO)" "/"]"
4 nishi 317
	if { [regexp {^/rpc(/.*)?$} "$path"] } {
318
		rputs "Content-Type: application/json"
319
	} else {
12 nishi 320
		if { ![regexp {/$} "$env(PATH_INFO)"] } {
321
			puts "Status: 301 Moved Permanently"
322
			puts "Location: $env(SCRIPT_NAME)$env(PATH_INFO)/"
323
			puts ""
324
			exiting 0
325
		}
4 nishi 326
		rputs "Content-Type: text/html"
327
	}
3 nishi 328
	if { "$path" == "/" } {
33 nishi 329
		set what "This is the main page."
28 nishi 330
		set has_projects 0
331
		add_toc "Projects"
332
		open_projects
333
		scan_projects {
334
			upvar 1 has_projects has_projects
335
			if { "$has_projects" == "0" } {
336
				set has_projects 1
337
				tputs	"<table border=\"0\">"
338
			}
339
			tputs	"<tr>"
340
			tputs	"	<th><a href=\"/koakuma/project/$name\">$name</a></th>"
341
			tputs	"	<td>[html_escape "$description"]</td>"
342
			tputs	"</tr>"
343
		}
344
		close_projects
345
		if { "$has_projects" == "1" } {
346
			tputs	"</table>"
347
		} else {
348
			tputs	"No projects have been added, yet."
349
		}
3 nishi 350
		add_toc "Tcl Information"
351
		tputs	"<table border=\"0\">"
352
		tputs	"	<tr>"
353
		tputs	"		<th>"
354
		tputs	"			Version"
355
		tputs	"		</th>"
356
		tputs	"		<td>"
357
		tputs	"			$tcl_version"
358
		tputs	"		</td>"
359
		tputs	"	</tr>"
360
		tputs	"	<tr>"
361
		tputs	"		<th>"
362
		tputs	"			Platform"
363
		tputs	"		</th>"
364
		tputs	"		<td>"
365
		tputs	"			$tcl_platform(os)/$tcl_platform(machine) $tcl_platform(osVersion)"
366
		tputs	"		</td>"
367
		tputs	"	</tr>"
13 nishi 368
		tputs	"	<tr>"
369
		tputs	"		<th>"
370
		tputs	"			tDOM version"
371
		tputs	"		</th>"
372
		tputs	"		<td>"
373
		tputs	"			$tdom_version"
374
		tputs	"		</td>"
375
		tputs	"	</tr>"
376
		tputs	"	<tr>"
377
		tputs	"		<th>"
378
		tputs	"			TclX version"
379
		tputs	"		</th>"
380
		tputs	"		<td>"
381
		tputs	"			$tclx_version"
382
		tputs	"		</td>"
383
		tputs	"	</tr>"
3 nishi 384
		tputs	"</table>"
385
		add_toc "Components"
386
		loop_components {
28 nishi 387
			add_toc2 "${name} (${genre})"
3 nishi 388
			if { [llength [info procs "${name}_info"]] > 0 } {
389
				${name}_info
390
			}
391
		}
392
 
393
		rputs ""
394
		start_html "Main" 1
395
		rputs "$content"
396
		end_html 1
4 nishi 397
	} elseif { [regexp {^/rpc(/.*)?$} "$path"] } {
398
		regexp {^/rpc(/.*)?$} "$path" -> api
399
		set doc [dom createDocumentNode]
5 nishi 400
		$doc appendFromScript {
401
			keyVersion {valueString "$KOAKUMA_VERSION"}
402
		}
4 nishi 403
		if { "$api" == "" || "$api" == "/" } {
5 nishi 404
			rputs ""
405
			rputs "[$doc asJSON]"
12 nishi 406
		} elseif { "$api" == "/launch-job" } {
407
			if { [catch {dom parse -json "$data" clidoc}] } {
408
				rputs "Status: 400 Bad Request"
409
				$doc appendFromScript {
410
					keyError {valueString "Bad JSON"}
411
				}
412
			} else {
18 nishi 413
				set projname "[regsub -all { } "[$clidoc selectNodes "string(/name)"]" "-"]"
12 nishi 414
				set builddesc "[$clidoc selectNodes "string(/description)"]"
415
				if { "$projname" == "" || "$builddesc" == "" } {
416
					rputs "Status: 400 Bad Request"
417
					$doc appendFromScript {
418
						keyError {valueString "Required field missing"}
419
					}
420
				} else {
421
					set has_name 0
13 nishi 422
					set use_vcs ""
423
					set use_vcs_url ""
12 nishi 424
					open_projects
425
					scan_projects {
426
						upvar 1 has_name has_name
427
						upvar 1 projname projname
13 nishi 428
						upvar 1 use_vcs use_vcs
429
						upvar 1 use_vcs_url use_vcs_url
12 nishi 430
						if { "$name" == "$projname" } {
431
							set has_name 1
13 nishi 432
							set use_vcs "$vcs"
433
							set use_vcs_url "$vcs_url"
12 nishi 434
							break
435
						}
436
					}
437
					close_projects
438
					if { $has_name == 0 } {
439
						rputs "Status: 400 Bad Request"
440
						$doc appendFromScript {
441
							keyError {valueString "Project does not exist"}
442
						}
443
					} else {
13 nishi 444
						set cont 1
445
						if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock"] } {
446
							set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock" "r"]
447
							set readpid "[gets $fid]"
448
							close $fid
449
							if { [file exists "/proc/$readpid"] } {
450
								set cont 0
451
								rputs "Status: 403 Forbidden"
452
								$doc appendFromScript {
453
									keyError {valueString "Other building process has been running"}
454
								}
455
							}
456
						}
457
						if { $cont == 1 } {
458
							set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/buildcount" "r"]
459
							set count [expr [gets $fid] + 1]
460
							close $fid
461
 
462
							set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/buildcount" "w"]
463
							puts $fid "$count"
464
							close $fid
465
 
466
							set count "[format %08s "$count"]"
467
 
468
							set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/lastrun" "w"]
469
							puts $fid "[clock seconds]"
470
							close $fid
471
 
472
							file mkdir "@@PREFIX@@/lib/koakuma/db/data/$projname/build-$count"
473
 
474
							set pid [fork]
475
							if { $pid } {
476
								set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock" "w"]
477
								puts $fid "$pid"
478
								close $fid
479
							} else {
480
								set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/build-$count/log" "w"]
481
								set fail 0
482
 
483
								dup $fid stdout
484
								dup $fid stderr
485
 
486
								puts "===== Checkout"
487
								puts "Using VCS: $use_vcs"
488
								if { [llength [info procs "${use_vcs}_repository"]] == 0 } {
489
									puts "Component internal failure"
490
									set fail 1
491
								} else {
492
									cd "@@PREFIX@@/lib/koakuma/db/data/$projname"
493
									if { [${use_vcs}_repository "$use_vcs_url" "workspace"] } {
494
										puts "Checkout failure"
495
										set fail 1
496
									}
497
								}
498
								if { $fail == 0 } {
499
									puts "===== Build"
500
									cd "@@PREFIX@@/lib/koakuma/db/data/$projname/workspace"
501
									if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/workspace/Koakumafile"] } {
502
										if { [catch {
503
											namespace eval koakumafile {
504
												source "@@PREFIX@@/lib/koakuma/db/data/$projname/workspace/Koakumafile"
505
											}
506
											koakumafile::run "$projname"
507
										}] } {
508
											puts "Failed to run Koakumafile"
509
											set fail 1
510
										}
511
									} else {
512
										puts "Nothing to do"
513
									}
514
								}
515
								if { $fail == 0 } {
516
									puts "Build successful"
517
									set fidsuc [open "@@PREFIX@@/lib/koakuma/db/data/$projname/lastsuccessfulrun" "w"]
518
									puts $fidsuc "[clock seconds]"
519
									close $fidsuc
520
 
521
									set fidsuc [open "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild" "r"]
522
									set sucbul [gets $fidsuc]
523
									close $fidsuc
524
 
525
									set fidsuc [open "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild" "w"]
526
									puts $fidsuc "[expr $sucbul + 1]"
527
									close $fidsuc
528
								}
529
 
530
								close $fid
531
 
532
								file delete "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock"
533
								exit 0
534
							}
535
						}
12 nishi 536
					}
537
				}
538
			}
539
			rputs ""
540
			rputs "[$doc asJSON]"
5 nishi 541
		} elseif { "$api" == "/create-project" } {
542
			if { [catch {dom parse -json "$data" clidoc}] } {
543
				rputs "Status: 400 Bad Request"
544
				$doc appendFromScript {
545
					keyError {valueString "Bad JSON"}
546
				}
547
			} else {
18 nishi 548
				set projname "[regsub -all { } "[$clidoc selectNodes "string(/name)"]" "-"]"
5 nishi 549
				set projdescription "[$clidoc selectNodes "string(/description)"]"
6 nishi 550
				set projvcs "[$clidoc selectNodes "string(/vcs)"]"
5 nishi 551
				set url "[$clidoc selectNodes "string(/url)"]"
6 nishi 552
				if { "$projname" == "" || "$projdescription" == "" || "$projvcs" == "" || "$url" == "" } {
5 nishi 553
					rputs "Status: 400 Bad Request"
554
					$doc appendFromScript {
555
						keyError {valueString "Required field missing"}
556
					}
557
				} else {
558
					set has_vcs 0
6 nishi 559
					set has_name 0
5 nishi 560
					loop_components {
561
						upvar 1 has_vcs has_vcs
6 nishi 562
						upvar 1 projvcs projvcs
563
						if { "$name" == "$projvcs" && "$genre" == "VCS" } {
5 nishi 564
							set has_vcs 1
565
							break
566
						}
567
					}
6 nishi 568
					open_projects
569
					scan_projects {
570
						upvar 1 has_name has_name
571
						upvar 1 projname projname
572
						if { "$name" == "$projname" } {
573
							set has_name 1
574
							break
575
						}
576
					}
577
					close_projects
5 nishi 578
					if { $has_vcs == 0 } {
579
						rputs "Status: 400 Bad Request"
580
						$doc appendFromScript {
581
							keyError {valueString "Not a valid VCS"}
582
						}
6 nishi 583
					} elseif { $has_name == 1 } {
584
						rputs "Status: 400 Bad Request"
585
						$doc appendFromScript {
586
							keyError {valueString "Project already exists"}
587
						}
5 nishi 588
					} else {
589
						open_projects
6 nishi 590
						set xml "[readall_db]"
591
						set xmldoc [dom parse "$xml"]
592
						set root [$xmldoc documentElement]
593
						$root appendFromScript {
594
							keyProject {
595
								keyName {valueString "$projname"}
596
								keyDescription {valueString "$projdescription"}
597
								keyVCS {valueString "$projvcs"}
598
								keyURL {valueString "$url"}
599
							}
600
						}
601
						write_db "[$xmldoc asXML]"
11 nishi 602
						file mkdir "@@PREFIX@@/lib/koakuma/db/data/$projname"
13 nishi 603
						set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/buildcount" "w"]
604
						puts $fid "0"
605
						close $fid
606
						set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild" "w"]
607
						puts $fid "0"
608
						close $fid
5 nishi 609
						close_projects
610
					}
611
				}
612
			}
613
			rputs ""
614
			rputs "[$doc asJSON]"
615
		} else {
4 nishi 616
			$root appendFromScript {
5 nishi 617
				keyError {valueString "No such endpoint"}
4 nishi 618
			}
5 nishi 619
			rputs "Status: 404 Not Found"
620
			rputs ""
4 nishi 621
			rputs "[$doc asJSON]"
622
		}
3 nishi 623
	} elseif { [regexp {^/project/[^/]+.*$} "$path"] } {
12 nishi 624
		regexp {^/project/([^/]+)(.*)$} "$path" -> projname projpath
3 nishi 625
		open_projects
626
		set has_project [project_exists "$projname"]
627
		close_projects
628
 
629
		if { "$has_project" != "" } {
12 nishi 630
			if { "$projpath" == "" || "$projpath" == "/" } {
33 nishi 631
				set what "This is the project page."
12 nishi 632
				add_toc "Description"
633
				tputs "[html_escape "$has_project"]"
634
				add_toc "Details"
635
				tputs	"<table border=\"0\">"
636
				tputs	"	<tr>"
637
				tputs	"		<th>"
15 nishi 638
				tputs	"			Status"
12 nishi 639
				tputs	"		</th>"
640
				tputs	"		<td>"
15 nishi 641
				if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock"] } {
18 nishi 642
					set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock" "r"]
15 nishi 643
					if { [file exists "/proc/[gets $fid]"] } {
644
						tputs "Running"
645
					} else {
646
						tputs "Idle"
647
					}
12 nishi 648
					close $fid
649
				} else {
15 nishi 650
					tputs "Idle"
12 nishi 651
				}
652
				tputs	"			"
653
				tputs	"		</td>"
654
				tputs	"	</tr>"
655
				tputs	"	<tr>"
656
				tputs	"		<th>"
15 nishi 657
				tputs	"			Last run"
14 nishi 658
				tputs	"		</th>"
659
				tputs	"		<td>"
15 nishi 660
				if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/lastrun"] } {
661
					set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/lastrun" "r"]
662
					set date "[clock format "[gets $fid]" -format "%a %b %d %H:%M:%S %Z %Y"]"
14 nishi 663
					close $fid
15 nishi 664
					tputs "$date"
14 nishi 665
				} else {
15 nishi 666
					tputs "No builds yet"
14 nishi 667
				}
668
				tputs	"			"
669
				tputs	"		</td>"
670
				tputs	"	</tr>"
671
				tputs	"	<tr>"
672
				tputs	"		<th>"
12 nishi 673
				tputs	"			Last successful run"
674
				tputs	"		</th>"
675
				tputs	"		<td>"
676
				if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/lastsuccessfulrun"] } {
677
					set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/lastsuccessfulrun" "r"]
678
					set date "[clock format "[gets $fid]" -format "%a %b %d %H:%M:%S %Z %Y"]"
679
					close $fid
680
					tputs "$date"
681
				} else {
682
					tputs "No successful builds yet"
683
				}
684
				tputs	"			"
685
				tputs	"		</td>"
686
				tputs	"	</tr>"
13 nishi 687
				set builds [lsort -ascii [glob -nocomplain "@@PREFIX@@/lib/koakuma/db/data/$projname/build-*"]]
688
				if { [llength $builds] > 0 } {
689
					tputs	"	<tr>"
690
					tputs	"		<th>"
691
					tputs	"			Successful builds"
692
					tputs	"		</th>"
693
					tputs	"		<td>"
694
					if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild"] } {
695
						set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild" "r"]
696
						tputs "[format %.2f [expr [gets $fid] / [llength $builds] * 100]]%"
697
						close $fid
698
					}
699
					tputs	"			"
700
					tputs	"		</td>"
701
					tputs	"	</tr>"
702
				}
12 nishi 703
				tputs	"</table>"
13 nishi 704
 
705
				set builds [lsort -ascii [glob -nocomplain "@@PREFIX@@/lib/koakuma/db/data/$projname/build-*"]]
706
				if { [llength $builds] > 0 } {
707
					add_toc "Last build log"
708
					set lastbuild "[lindex $builds [expr [llength $builds] - 1]]"
709
					set fid [open "$lastbuild/log" "r"]
710
					tputs "<pre>"
711
					while { [gets $fid line] >= 0 } {
712
						tputs "[html_escape "$line"]"
713
					}
714
					tputs "</pre>"
715
					close $fid
716
				}
12 nishi 717
 
718
				rputs ""
719
				start_html "Project: $projname" 1
720
				rputs "$content"
721
				end_html 1
11 nishi 722
			} else {
12 nishi 723
				tputs "I could not find the endpoint you were finding."
724
 
725
				rputs "Status: 404 Not Found"
726
				rputs ""
727
				start_html "Project: $projname" 1
728
				rputs "$content"
729
				end_html 1
11 nishi 730
			}
3 nishi 731
		} else {
732
			tputs "I could not find the project you were finding."
733
 
734
			rputs "Status: 404 Not Found"
735
			rputs ""
736
			start_html "Not Found" 0
737
			rputs "$content"
738
			end_html 0
739
		}
740
	} else {
741
		tputs "I could not find the content you were finding."
742
 
743
		rputs "Status: 404 Not Found"
744
		rputs ""
745
		start_html "Not Found" 0
746
		rputs "$content"
747
		end_html 0
748
	}
749
}] } {
750
	crash "Could not render the HTML"
751
} else {
752
	puts "$result"
753
}
754
exiting 0