cookutils view web/cooker.cgi @ rev 804

Fix previous commit (`syntax_highlighter log` used not only for cooking logs)
author Aleksej Bobylev <al.bobylev@gmail.com>
date Sun Jul 03 11:17:49 2016 +0300 (2016-07-03)
parents 166a91b87ac8
children eb765a8c3872
line source
1 #!/bin/sh
2 #
3 # SliTaz Cooker CGI/web interface.
4 #
6 [ -f "/etc/slitaz/cook.conf" ] && . /etc/slitaz/cook.conf
7 [ -f "cook.conf" ] && . ./cook.conf
9 # The same wok as cook.
10 wok="$WOK"
12 # Cooker DB files.
13 activity="$CACHE/activity"
14 commits="$CACHE/commits"
15 cooklist="$CACHE/cooklist"
16 cookorder="$CACHE/cookorder"
17 command="$CACHE/command"
18 blocked="$CACHE/blocked"
19 broken="$CACHE/broken"
20 cooknotes="$CACHE/cooknotes"
21 cooktime="$CACHE/cooktime"
22 wokrev="$CACHE/wokrev"
24 # We're not logged and want time zone to display correct server date.
25 export TZ=$(cat /etc/TZ)
27 case "$QUERY_STRING" in
28 recook=*)
29 case "$HTTP_USER_AGENT" in
30 *SliTaz*)
31 grep -qs "^${QUERY_STRING#recook=}$" $CACHE/recook-packages ||
32 echo ${QUERY_STRING#recook=} >> $CACHE/recook-packages
33 esac
34 cat <<EOT
35 Location: ${HTTP_REFERER:-${REQUEST_URI%\?*}}
37 EOT
38 exit ;;
39 poke)
40 touch $CACHE/cooker-request
41 cat <<EOT
42 Location: ${HTTP_REFERER:-${REQUEST_URI%\?*}}
44 EOT
45 exit ;;
46 download*)
47 file=$(busybox httpd -d "$PKGS/${QUERY_STRING#*=}")
48 cat <<EOT
49 Content-Type: application/octet-stream
50 Content-Length: $(stat -c %s "$file")
51 Content-Disposition: attachment; filename="$(basename "$file")"
53 EOT
54 cat "$file"
55 exit ;;
56 rss)
57 cat <<EOT
58 Content-Type: application/rss+xml
60 EOT
61 ;;
62 *)
63 cat <<EOT
64 Content-Type: text/html; charset=utf-8
66 EOT
67 ;;
68 esac
71 # RSS feed generator
72 if [ "$QUERY_STRING" == 'rss' ]; then
73 pubdate=$(date -R)
74 cat <<EOT
75 <?xml version="1.0" encoding="utf-8" ?>
76 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
77 <channel>
78 <title>SliTaz Cooker</title>
79 <description>The SliTaz packages cooker feed</description>
80 <link>$COOKER_URL</link>
81 <lastBuildDate>$pubdate</lastBuildDate>
82 <pubDate>$pubdate</pubDate>
83 <atom:link href="http://cook.slitaz.org/cooker.cgi?rss" rel="self" type="application/rss+xml" />
84 EOT
85 for rss in $(ls -lt $FEEDS/*.xml | head -n 12); do
86 cat $rss | sed 's|<guid|& isPermaLink="false"|g;s|</pubDate| GMT&|g'
87 done
88 cat <<EOT
89 </channel>
90 </rss>
91 EOT
92 exit 0
93 fi
96 #
97 # Functions
98 #
101 # Put some colors in log and DB files.
103 syntax_highlighter() {
104 case $1 in
105 log)
106 # If variables not defined - define them with some rare values
107 : ${_src=#_#_#}
108 : ${_install=#_#_#}
109 : ${_fs=#_#_#}
110 : ${_stuff=#_#_#}
111 sed -e 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g' \
112 -e 's#OK$#<span class="span-ok">OK</span>#g' \
113 -e 's#Done$#<span class="span-ok">Done</span>#g' \
114 -e 's#yes$#<span class="span-ok">yes</span>#g' \
115 -e 's#no$#<span class="span-no">no</span>#g' \
116 -e 's#error$#<span class="span-red">error</span>#g' \
117 -e 's#ERROR:#<span class="span-red">ERROR:</span>#g' \
118 -e 's#WARNING:#<span class="span-red">WARNING:</span>#g' \
119 -e s"#^Executing:\([^']*\).#<span class='sh-val'>\0</span>#"g \
120 -e s"#^====\([^']*\).#<span class='span-line'>\0</span>#"g \
121 -e s"#^[a-zA-Z0-9]\([^']*\) :: #<span class='span-sky'>\0</span>#"g \
122 -e s"#ftp://[^ '\"]*#<a href='\0'>\0</a>#"g \
123 -e s"#http://[^ '\"]*#<a href='\0'>\0</a>#"g | \
124 sed "s|$_src|<span class='var'>\${src}</span>|g;
125 s|$_install|<span class='var'>\${install}</span>|g;
126 s|$_fs|<span class='var'>\${fs}</span>|g;
127 s|$_stuff|<span class='var'>\${stuff}</span>|g"
128 ;;
130 receipt)
131 sed -e s'|&|\&amp;|g' -e 's|<|\&lt;|g' -e 's|>|\&gt;|'g \
132 -e s"#^\#\([^']*\)#<span class='sh-comment'>\0</span>#"g \
133 -e s"#\"\([^']*\)\"#<span class='sh-val'>\0</span>#"g ;;
135 diff)
136 sed -e 's|&|\&amp;|g' -e 's|<|\&lt;|g' -e 's|>|\&gt;|g' \
137 -e s"#^-\([^']*\).#<span class='span-red'>\0</span>#"g \
138 -e s"#^+\([^']*\).#<span class='span-ok'>\0</span>#"g \
139 -e s"#@@\([^']*\)@@#<span class='span-sky'>@@\1@@</span>#"g ;;
141 activity)
142 sed s"#^\([^']* : \)#<span class='log-date'>\0</span>#"g ;;
143 esac
144 }
147 # Latest build pkgs.
149 list_packages() {
150 cd $PKGS
151 ls -1t *.tazpkg | head -20 | \
152 while read file; do
153 echo -n $(TZ=UTC stat -c '%y' $PKGS/$file | cut -d . -f 1 | sed s/:[0-9]*$//)
154 echo " : $file"
155 done
156 }
159 # Optional full list button
161 more_button() {
162 [ $(wc -l < ${3:-$CACHE/$1}) -gt ${4:-12} ] &&
163 echo "<a class=\"button\" href=\"cooker.cgi?file=$1\">$2</a>"
164 }
167 # Show the running command and its progression
169 running_command()
170 {
171 local state="Not running"
172 if [ -s "$command" ]; then
173 state="$(cat $command)"
174 if grep -q "^$state" $cooktime ; then
175 set -- $(cat $cooktime)
176 state="$state $((($(date +%s)-$3)*100/$2))%"
177 [ $2 -gt 300 ] && state="$state (should end $(date -u -d @$(($2+$3))))"
178 fi
179 fi
180 echo $state
181 }
184 # xHTML header. Pages can be customized with a separated html.header file.
186 if [ -f "header.html" ]; then
187 cat header.html
188 else
189 cat <<EOT
190 <!DOCTYPE html>
191 <html lang="en">
192 <head>
193 <meta charset="utf-8"/>
194 <title>SliTaz Cooker</title>
195 <link rel="shortcut icon" href="favicon.ico"/>
196 <link rel="stylesheet" type="text/css" href="style.css"/>
197 <meta name="robots" content="nofollow">
198 </head>
199 <body>
201 <div id="header">
202 <div id="logo"></div>
203 <h1><a href="cooker.cgi">SliTaz Cooker</a></h1>
204 </div>
206 <!-- Content -->
207 <div id="content">
208 EOT
209 fi
212 #
213 # Load requested page
214 #
216 case "${QUERY_STRING}" in
217 pkg=*)
218 pkg=${QUERY_STRING#pkg=}
219 log=$LOGS/$pkg.log
220 echo "<h2>Package: $pkg</h2>"
222 # Define cook variables for syntax highlighter
223 . "$WOK/$pkg/receipt"
224 _wok='/home/slitaz/wok'
225 _src="$_wok/$pkg/source/$PACKAGE-$VERSION"
226 _install="$_wok/$pkg/install"
227 _fs="$_wok/$pkg/taz/$PACKAGE-$VERSION/fs"
228 _stuff="$_wok/$pkg/stuff"
230 # Package info.
231 echo '<div id="info">'
232 if [ -f "$wok/$pkg/receipt" ]; then
233 echo "<a href='cooker.cgi?receipt=$pkg'>receipt</a>"
234 unset WEB_SITE
235 . $wok/$pkg/receipt
237 [ -n "$WEB_SITE" ] && # busybox wget -s $WEB_SITE &&
238 echo "<a href='$WEB_SITE'>home</a>"
240 if [ -f "$wok/$pkg/taz/$PACKAGE-$VERSION/receipt" ]; then
241 echo "<a href='cooker.cgi?files=$pkg'>files</a>"
242 unset EXTRAVERSION
243 . $wok/$pkg/taz/$PACKAGE-$VERSION/receipt
244 if [ -f $wok/$pkg/taz/$PACKAGE-$VERSION/description.txt ]; then
245 echo "<a href='cooker.cgi?description=$pkg'>description</a>"
246 fi
247 if [ -f $PKGS/$PACKAGE-$VERSION$EXTRAVERSION.tazpkg ]; then
248 echo "<a href='cooker.cgi?download=$PACKAGE-$VERSION$EXTRAVERSION.tazpkg'>download</a>"
249 fi
250 if [ -f $PKGS/$PACKAGE-$VERSION$EXTRAVERSION-$ARCH.tazpkg ]; then
251 echo "<a href='cooker.cgi?download=$PACKAGE-$VERSION$EXTRAVERSION-$ARCH.tazpkg'>download</a>"
252 fi
253 fi
254 echo "<a href='ftp://${HTTP_HOST%:*}/$pkg/'>browse</a>"
255 else
256 if [ $(ls $wok/*$pkg*/receipt 2> /dev/null | wc -l) -eq 0 ]; then
257 echo "No package named: $pkg"
258 else
259 ls $wok/$pkg/receipt >/dev/null 2>&1 || pkg="*$pkg*"
260 echo '<table style="width:100%">'
261 for i in $(cd $wok ; ls $pkg/receipt); do
262 pkg=$(dirname $i)
263 unset SHORT_DESC CATEGORY
264 . $wok/$pkg/receipt
265 cat <<EOT
266 <tr>
267 <td><a href="cooker.cgi?pkg=$pkg">$pkg</a></td>
268 <td>$SHORT_DESC</td>
269 <td>$CATEGORY</td>
270 </tr>
271 EOT
272 done
273 echo '</table>'
274 unset pkg
275 fi
276 fi
277 echo '</div>'
279 # Check for a log file and display summary if it exists.
280 if [ -f "$log" ]; then
281 if grep -q "cook:$pkg$" $command; then
282 echo "<pre>The Cooker is currently building: $pkg</pre>"
283 fi
284 if fgrep -q "Summary for:" $LOGS/$pkg.log; then
285 echo '<h3>Cook summary</h3>'
286 echo '<pre>'
287 grep -A 12 "^Summary for:" $LOGS/$pkg.log | sed /^$/d | \
288 syntax_highlighter log
289 echo '</pre>'
290 fi
291 if fgrep -q "Debug information" $LOGS/$pkg.log; then
292 echo '<h3>Cook failed</h3>'
293 echo '<pre>'
294 grep -A 8 "^Debug information" $LOGS/$pkg.log | sed /^$/d | \
295 syntax_highlighter log
296 echo '</pre>'
297 fi
298 echo '<h3>Cook log</h3>'
299 echo '<pre>'
300 cat $log | syntax_highlighter log
301 echo '</pre>'
302 case "$HTTP_USER_AGENT" in
303 *SliTaz*)
304 [ -f $CACHE/cooker-request ] && [ -n "$HTTP_REFERER" ] &&
305 echo "<a class=\"button\" href=\"cooker.cgi?recook=$pkg\">Recook $pkg</a>"
306 esac
307 else
308 [ "$pkg" ] && echo "<pre>No log: $pkg</pre>"
309 fi ;;
311 file=*)
312 # Don't allow all files on the system for security reasons.
313 file=${QUERY_STRING#file=}
314 case "$file" in
315 activity|cooknotes|cooklist)
316 [ "$file" == "cooklist" ] && \
317 nb="- Packages: $(cat $cooklist | wc -l)"
318 echo "<h2>DB: $file $nb</h2>"
319 echo '<pre>'
320 tac $CACHE/$file | syntax_highlighter activity
321 echo '</pre>' ;;
323 broken)
324 nb=$(cat $broken | wc -l)
325 echo "<h2>DB: broken - Packages: $nb</h2>"
326 echo '<pre>'
327 cat $CACHE/$file | sort | \
328 sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g
329 echo '</pre>' ;;
331 *.diff)
332 diff=$CACHE/$file
333 echo "<h2>Diff for: ${file%.diff}</h2>"
334 [ "$file" == "installed.diff" ] && echo \
335 "<p>This is the latest diff between installed packages \
336 and installed build dependencies to cook.</p>"
337 echo '<pre>'
338 cat $diff | syntax_highlighter diff
339 echo '</pre>' ;;
341 *.log)
342 log=$LOGS/$file
343 name=$(basename $log)
344 echo "<h2>Log for: ${name%.log}</h2>"
345 if [ -f "$log" ]; then
346 if fgrep -q "Summary" $log; then
347 echo '<pre>'
348 grep -A 20 "^Summary" $log | sed /^$/d | \
349 syntax_highlighter log
350 echo '</pre>'
351 fi
352 echo '<pre>'
353 cat $log | syntax_highlighter log
354 echo '</pre>'
355 else
356 echo "<pre>No log file: $log</pre>"
357 fi ;;
358 esac ;;
360 stuff=*)
361 file=${QUERY_STRING#stuff=}
362 echo "<h2>$file</h2>"
363 echo '<pre>'
364 cat $wok/$file | sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
365 echo '</pre>' ;;
367 receipt=*)
368 pkg=${QUERY_STRING#receipt=}
369 echo "<h2>Receipt for: $pkg</h2>"
370 if [ -f "$wok/$pkg/receipt" ]; then
371 ( cd $wok/$pkg ; find stuff -type f 2> /dev/null ) | \
372 while read file ; do
373 echo "<a href=\"?stuff=$pkg/$file\">$file</a>"
374 done
375 echo '<pre>'
376 cat $wok/$pkg/receipt | \
377 syntax_highlighter receipt
378 echo '</pre>'
379 else
380 echo "<pre>No receipt for: $pkg</pre>"
381 fi ;;
383 files=*)
384 pkg=${QUERY_STRING#files=}
385 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
386 if [ -d "$dir/fs" ]; then
387 echo "<h2>Installed files by: $pkg ($(du -hs $dir/fs | awk '{ print $1 }'))</h2>"
388 echo '<pre>'
389 find $dir/fs -not -type d -print0 | xargs -0 ls -ld | \
390 sed "s|\(.*\) /.*\(${dir#*wok}/fs\)\(.*\)|\1 <a href=\"?download=../wok\2\3\">\3</a>|;s|^\([^-].*\)\(<a.*\)\">\(.*\)</a>|\1\3|"
391 echo '</pre>'
392 else
393 echo "<pre>No files list for: $pkg</pre>"
394 fi ;;
396 description=*)
397 pkg=${QUERY_STRING#description=}
398 echo "<h2>Description of $pkg</h2>"
399 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
400 if [ -s "$dir/description.txt" ]; then
401 echo '<pre>'
402 cat $dir/description.txt | \
403 sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
404 echo '</pre>'
405 else
406 echo "<pre>No description for: $pkg</pre>"
407 fi ;;
409 *)
410 # We may have a toolchain.cgi script for cross cooker's
411 if [ -f "toolchain.cgi" ]; then
412 toolchain='toolchain.cgi'
413 else
414 toolchain='cooker.cgi?pkg=slitaz-toolchain'
415 fi
416 # Main page with summary. Count only package include in ARCH,
417 # use 'cooker arch-db' to manually create arch.$ARCH files.
418 inwok=$(ls $WOK/*/arch.$ARCH | wc -l)
419 cooked=$(ls $PKGS/*.tazpkg | wc -l)
420 unbuilt=$(($inwok - $cooked))
421 pct=0
422 [ $inwok -gt 0 ] && pct=$(( ($cooked * 100) / $inwok ))
423 cat <<EOT
424 <div style="float: right;">
425 <form method="get" action="$SCRIPT_NAME">
426 Package:
427 <input type="text" name="pkg" />
428 </form>
429 </div>
431 <h2>Summary</h2>
433 <pre>
434 Running command : $(running_command)
435 Wok revision : <a href="$WOK_URL">$(cat $wokrev)</a>
436 Commits to cook : $(cat $commits | wc -l)
437 Current cooklist : $(cat $cooklist | wc -l)
438 Broken packages : $(cat $broken | wc -l)
439 Blocked packages : $(cat $blocked | wc -l)
440 </pre>
442 <p class="info">
443 Packages: $inwok in the wok | $cooked cooked | $unbuilt unbuilt |
444 Server date: $(date -u '+%F %R %Z')
445 </p>
446 <div class="pctbar">
447 <div class="pct" style="width: ${pct}%;">${pct}%</div>
448 </div>
450 <p>
451 Latest:
452 <a href="cooker.cgi?file=cookorder.log">cookorder.log</a>
453 <a href="cooker.cgi?file=commits.log">commits.log</a>
454 <a href="cooker.cgi?file=pkgdb.log">pkgdb.log</a>
455 <a href="cooker.cgi?file=installed.diff">installed.diff</a>
456 - Architecture $ARCH:
457 <a href="$toolchain">toolchain</a>
458 </p>
459 EOT
460 [ -e $CACHE/cooker-request ] &&
461 [ $CACHE/activity -nt $CACHE/cooker-request ] && cat <<EOT
462 <a class="button" href="cooker.cgi?poke">Poke cooker</a>
463 EOT
465 cat <<EOT
466 <h2 id="activity">Activity</h2>
467 <pre>
468 $(tac $CACHE/activity | head -n 12 | syntax_highlighter activity)
469 </pre>
470 $(more_button activity "More activity" $CACHE/activity 12)
471 EOT
473 [ -s $cooknotes ] && cat <<EOT
474 <h2 id="cooknotes">Cooknotes</h2>
475 <pre>
476 $(tac $cooknotes | head -n 12 | syntax_highlighter activity)
477 </pre>
478 $(more_button cooknotes "More notes" $cooknotes 12)
479 EOT
481 [ -s $commits ] && cat <<EOT
482 <h2 id="commits">Commits</h2>
483 <pre>
484 $(cat $commits)
485 </pre>
486 EOT
488 [ -s $cooklist ] && cat <<EOT
489 <h2 id="cooklist">Cooklist</h2>
490 <pre>
491 $(cat $cooklist | head -n 20)
492 </pre>
493 $(more_button cooklist "Full cooklist" $cooklist 20)
494 EOT
496 [ -s $broken ] && cat <<EOT
497 <h2 id="broken">Broken</h2>
498 <pre>
499 $(cat $broken | head -n 20 | sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g)
500 </pre>
501 $(more_button broken "All broken packages" $broken 20)
502 EOT
504 [ -s $blocked ] && cat <<EOT
505 <h2 id="blocked">Blocked</h2>
506 <pre>
507 $(cat $blocked | sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g)
508 </pre>
509 EOT
511 cat <<EOT
512 <h2 id="lastcook">Latest cook</h2>
513 <pre>
514 $(list_packages | sed s"#^\([^']*\).* : #<span class='log-date'>\0</span>#"g)
515 </pre>
516 EOT
517 ;;
518 esac
521 # Close xHTML page
523 cat <<EOT
524 </div>
526 <div id="footer">
527 <a href="http://www.slitaz.org/">SliTaz Website</a>
528 <a href="cooker.cgi">Cooker</a>
529 <a href="http://hg.slitaz.org/cookutils/raw-file/tip/doc/cookutils.en.html">
530 Documentation</a>
531 </div>
533 </body>
534 </html>
535 EOT
537 exit 0