Subversion Repositories Koakuma

Rev

Rev 36 | Rev 38 | 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 37 2024-10-02 09:44:50Z 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
36 nishi 485
 
486
								puts "Build trigger description: $builddesc"
13 nishi 487
								puts "===== Checkout"
488
								puts "Using VCS: $use_vcs"
489
								if { [llength [info procs "${use_vcs}_repository"]] == 0 } {
490
									puts "Component internal failure"
491
									set fail 1
492
								} else {
493
									cd "@@PREFIX@@/lib/koakuma/db/data/$projname"
494
									if { [${use_vcs}_repository "$use_vcs_url" "workspace"] } {
495
										puts "Checkout failure"
496
										set fail 1
497
									}
498
								}
499
								if { $fail == 0 } {
500
									puts "===== Build"
501
									cd "@@PREFIX@@/lib/koakuma/db/data/$projname/workspace"
502
									if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/workspace/Koakumafile"] } {
503
										if { [catch {
504
											namespace eval koakumafile {
505
												source "@@PREFIX@@/lib/koakuma/db/data/$projname/workspace/Koakumafile"
506
											}
507
											koakumafile::run "$projname"
508
										}] } {
509
											puts "Failed to run Koakumafile"
510
											set fail 1
511
										}
512
									} else {
513
										puts "Nothing to do"
514
									}
515
								}
516
								if { $fail == 0 } {
517
									puts "Build successful"
518
									set fidsuc [open "@@PREFIX@@/lib/koakuma/db/data/$projname/lastsuccessfulrun" "w"]
519
									puts $fidsuc "[clock seconds]"
520
									close $fidsuc
521
 
522
									set fidsuc [open "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild" "r"]
523
									set sucbul [gets $fidsuc]
524
									close $fidsuc
525
 
526
									set fidsuc [open "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild" "w"]
527
									puts $fidsuc "[expr $sucbul + 1]"
528
									close $fidsuc
529
								}
530
 
531
								close $fid
532
 
533
								file delete "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock"
534
								exit 0
535
							}
536
						}
12 nishi 537
					}
538
				}
539
			}
540
			rputs ""
541
			rputs "[$doc asJSON]"
5 nishi 542
		} elseif { "$api" == "/create-project" } {
543
			if { [catch {dom parse -json "$data" clidoc}] } {
544
				rputs "Status: 400 Bad Request"
545
				$doc appendFromScript {
546
					keyError {valueString "Bad JSON"}
547
				}
548
			} else {
18 nishi 549
				set projname "[regsub -all { } "[$clidoc selectNodes "string(/name)"]" "-"]"
5 nishi 550
				set projdescription "[$clidoc selectNodes "string(/description)"]"
6 nishi 551
				set projvcs "[$clidoc selectNodes "string(/vcs)"]"
5 nishi 552
				set url "[$clidoc selectNodes "string(/url)"]"
6 nishi 553
				if { "$projname" == "" || "$projdescription" == "" || "$projvcs" == "" || "$url" == "" } {
5 nishi 554
					rputs "Status: 400 Bad Request"
555
					$doc appendFromScript {
556
						keyError {valueString "Required field missing"}
557
					}
558
				} else {
559
					set has_vcs 0
6 nishi 560
					set has_name 0
5 nishi 561
					loop_components {
562
						upvar 1 has_vcs has_vcs
6 nishi 563
						upvar 1 projvcs projvcs
564
						if { "$name" == "$projvcs" && "$genre" == "VCS" } {
5 nishi 565
							set has_vcs 1
566
							break
567
						}
568
					}
6 nishi 569
					open_projects
570
					scan_projects {
571
						upvar 1 has_name has_name
572
						upvar 1 projname projname
573
						if { "$name" == "$projname" } {
574
							set has_name 1
575
							break
576
						}
577
					}
578
					close_projects
5 nishi 579
					if { $has_vcs == 0 } {
580
						rputs "Status: 400 Bad Request"
581
						$doc appendFromScript {
582
							keyError {valueString "Not a valid VCS"}
583
						}
6 nishi 584
					} elseif { $has_name == 1 } {
585
						rputs "Status: 400 Bad Request"
586
						$doc appendFromScript {
587
							keyError {valueString "Project already exists"}
588
						}
5 nishi 589
					} else {
590
						open_projects
6 nishi 591
						set xml "[readall_db]"
592
						set xmldoc [dom parse "$xml"]
593
						set root [$xmldoc documentElement]
594
						$root appendFromScript {
595
							keyProject {
596
								keyName {valueString "$projname"}
597
								keyDescription {valueString "$projdescription"}
598
								keyVCS {valueString "$projvcs"}
599
								keyURL {valueString "$url"}
600
							}
601
						}
602
						write_db "[$xmldoc asXML]"
11 nishi 603
						file mkdir "@@PREFIX@@/lib/koakuma/db/data/$projname"
13 nishi 604
						set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/buildcount" "w"]
605
						puts $fid "0"
606
						close $fid
607
						set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild" "w"]
608
						puts $fid "0"
609
						close $fid
5 nishi 610
						close_projects
611
					}
612
				}
613
			}
614
			rputs ""
615
			rputs "[$doc asJSON]"
616
		} else {
4 nishi 617
			$root appendFromScript {
5 nishi 618
				keyError {valueString "No such endpoint"}
4 nishi 619
			}
5 nishi 620
			rputs "Status: 404 Not Found"
621
			rputs ""
4 nishi 622
			rputs "[$doc asJSON]"
623
		}
3 nishi 624
	} elseif { [regexp {^/project/[^/]+.*$} "$path"] } {
12 nishi 625
		regexp {^/project/([^/]+)(.*)$} "$path" -> projname projpath
3 nishi 626
		open_projects
627
		set has_project [project_exists "$projname"]
628
		close_projects
629
 
630
		if { "$has_project" != "" } {
12 nishi 631
			if { "$projpath" == "" || "$projpath" == "/" } {
33 nishi 632
				set what "This is the project page."
12 nishi 633
				add_toc "Description"
634
				tputs "[html_escape "$has_project"]"
635
				add_toc "Details"
636
				tputs	"<table border=\"0\">"
637
				tputs	"	<tr>"
638
				tputs	"		<th>"
15 nishi 639
				tputs	"			Status"
12 nishi 640
				tputs	"		</th>"
641
				tputs	"		<td>"
15 nishi 642
				if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock"] } {
18 nishi 643
					set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/build.lock" "r"]
15 nishi 644
					if { [file exists "/proc/[gets $fid]"] } {
645
						tputs "Running"
646
					} else {
647
						tputs "Idle"
648
					}
12 nishi 649
					close $fid
650
				} else {
15 nishi 651
					tputs "Idle"
12 nishi 652
				}
653
				tputs	"			"
654
				tputs	"		</td>"
655
				tputs	"	</tr>"
656
				tputs	"	<tr>"
657
				tputs	"		<th>"
15 nishi 658
				tputs	"			Last run"
14 nishi 659
				tputs	"		</th>"
660
				tputs	"		<td>"
15 nishi 661
				if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/lastrun"] } {
662
					set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/lastrun" "r"]
663
					set date "[clock format "[gets $fid]" -format "%a %b %d %H:%M:%S %Z %Y"]"
14 nishi 664
					close $fid
15 nishi 665
					tputs "$date"
14 nishi 666
				} else {
15 nishi 667
					tputs "No builds yet"
14 nishi 668
				}
669
				tputs	"			"
670
				tputs	"		</td>"
671
				tputs	"	</tr>"
672
				tputs	"	<tr>"
673
				tputs	"		<th>"
12 nishi 674
				tputs	"			Last successful run"
675
				tputs	"		</th>"
676
				tputs	"		<td>"
677
				if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/lastsuccessfulrun"] } {
678
					set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/lastsuccessfulrun" "r"]
679
					set date "[clock format "[gets $fid]" -format "%a %b %d %H:%M:%S %Z %Y"]"
680
					close $fid
681
					tputs "$date"
682
				} else {
683
					tputs "No successful builds yet"
684
				}
685
				tputs	"			"
686
				tputs	"		</td>"
687
				tputs	"	</tr>"
13 nishi 688
				set builds [lsort -ascii [glob -nocomplain "@@PREFIX@@/lib/koakuma/db/data/$projname/build-*"]]
689
				if { [llength $builds] > 0 } {
690
					tputs	"	<tr>"
691
					tputs	"		<th>"
692
					tputs	"			Successful builds"
693
					tputs	"		</th>"
694
					tputs	"		<td>"
695
					if { [file exists "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild"] } {
696
						set fid [open "@@PREFIX@@/lib/koakuma/db/data/$projname/successbuild" "r"]
37 nishi 697
						set sucbui "[gets $fid]"
698
						tputs "[format %.2f [expr $sucbui / [llength $builds] * 100]]% ($sucbui/[llength $builds])"
13 nishi 699
						close $fid
700
					}
701
					tputs	"			"
702
					tputs	"		</td>"
703
					tputs	"	</tr>"
704
				}
12 nishi 705
				tputs	"</table>"
13 nishi 706
 
707
				set builds [lsort -ascii [glob -nocomplain "@@PREFIX@@/lib/koakuma/db/data/$projname/build-*"]]
708
				if { [llength $builds] > 0 } {
709
					add_toc "Last build log"
710
					set lastbuild "[lindex $builds [expr [llength $builds] - 1]]"
711
					set fid [open "$lastbuild/log" "r"]
712
					tputs "<pre>"
713
					while { [gets $fid line] >= 0 } {
714
						tputs "[html_escape "$line"]"
715
					}
716
					tputs "</pre>"
717
					close $fid
718
				}
12 nishi 719
 
720
				rputs ""
721
				start_html "Project: $projname" 1
722
				rputs "$content"
723
				end_html 1
11 nishi 724
			} else {
12 nishi 725
				tputs "I could not find the endpoint you were finding."
726
 
727
				rputs "Status: 404 Not Found"
728
				rputs ""
729
				start_html "Project: $projname" 1
730
				rputs "$content"
731
				end_html 1
11 nishi 732
			}
3 nishi 733
		} else {
734
			tputs "I could not find the project you were finding."
735
 
736
			rputs "Status: 404 Not Found"
737
			rputs ""
738
			start_html "Not Found" 0
739
			rputs "$content"
740
			end_html 0
741
		}
742
	} else {
743
		tputs "I could not find the content you were finding."
744
 
745
		rputs "Status: 404 Not Found"
746
		rputs ""
747
		start_html "Not Found" 0
748
		rputs "$content"
749
		end_html 0
750
	}
751
}] } {
752
	crash "Could not render the HTML"
753
} else {
754
	puts "$result"
755
}
756
exiting 0