cookutils view web/cooker.cgi @ rev 815
cooker.cgi: add info support
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Wed Jul 06 19:05:12 2016 +0200 (2016-07-06) |
parents | c660416ff9f2 |
children | 6c2d5b07b30b |
line source
1 #!/bin/sh
2 #
3 # SliTaz Cooker CGI/web interface.
4 #
6 . /usr/lib/slitaz/httphelper.sh
8 [ -f "/etc/slitaz/cook.conf" ] && . /etc/slitaz/cook.conf
9 [ -f "cook.conf" ] && . ./cook.conf
11 # The same wok as cook.
12 wok="$WOK"
14 # Cooker DB files.
15 activity="$CACHE/activity"
16 commits="$CACHE/commits"
17 cooklist="$CACHE/cooklist"
18 cookorder="$CACHE/cookorder"
19 command="$CACHE/command"
20 blocked="$CACHE/blocked"
21 broken="$CACHE/broken"
22 cooknotes="$CACHE/cooknotes"
23 cooktime="$CACHE/cooktime"
24 wokrev="$CACHE/wokrev"
26 # We're not logged and want time zone to display correct server date.
27 export TZ=$(cat /etc/TZ)
29 case "$QUERY_STRING" in
30 recook=*)
31 case "$HTTP_USER_AGENT" in
32 *SliTaz*)
33 grep -qs "^${QUERY_STRING#recook=}$" $CACHE/recook-packages ||
34 echo ${QUERY_STRING#recook=} >> $CACHE/recook-packages
35 esac
36 cat <<EOT
37 Location: ${HTTP_REFERER:-${REQUEST_URI%\?*}}
39 EOT
40 exit ;;
41 poke)
42 touch $CACHE/cooker-request
43 cat <<EOT
44 Location: ${HTTP_REFERER:-${REQUEST_URI%\?*}}
46 EOT
47 exit ;;
48 download*)
49 file=$(busybox httpd -d "$PKGS/${QUERY_STRING#*=}")
50 cat <<EOT
51 Content-Type: application/octet-stream
52 Content-Length: $(stat -c %s "$file")
53 Content-Disposition: attachment; filename="$(basename "$file")"
55 EOT
56 cat "$file"
57 exit ;;
58 rss)
59 cat <<EOT
60 Content-Type: application/rss+xml
62 EOT
63 ;;
64 *)
65 cat <<EOT
66 Content-Type: text/html; charset=utf-8
68 EOT
69 ;;
70 esac
73 # RSS feed generator
74 if [ "$QUERY_STRING" == 'rss' ]; then
75 pubdate=$(date -R)
76 cat <<EOT
77 <?xml version="1.0" encoding="utf-8" ?>
78 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
79 <channel>
80 <title>SliTaz Cooker</title>
81 <description>The SliTaz packages cooker feed</description>
82 <link>$COOKER_URL</link>
83 <lastBuildDate>$pubdate</lastBuildDate>
84 <pubDate>$pubdate</pubDate>
85 <atom:link href="http://cook.slitaz.org/?rss" rel="self" type="application/rss+xml" />
86 EOT
87 for rss in $(ls -lt $FEEDS/*.xml | head -n 12); do
88 cat $rss | sed 's|<guid|& isPermaLink="false"|g;s|</pubDate| GMT&|g'
89 done
90 cat <<EOT
91 </channel>
92 </rss>
93 EOT
94 exit 0
95 fi
98 #
99 # Functions
100 #
103 # Unpack to stdout
105 docat() {
106 case "$1" in
107 *gz) zcat ;;
108 *bz2) bzcat ;;
109 *xz) xzcat ;;
110 *) cat
111 esac < $1
112 }
115 # Tiny texinfo browser
117 info2html() {
118 sed -e 's|^\* \(.*\)::$|* <a href="#\1">\1</a>|' \
119 -e '/^File: /s|(dir)|Top|g' \
120 -e '/^File: /s|Node: \([^,]*\)|Node: <a name="\1"></a>\1|' \
121 -e '/^File: /s|Next: \([^,]*\)|Next: <a href="#\1">\1</a>|' \
122 -e '/^File: /s|Prev: \([^,]*\)|Prev: <a href="#\1">\1</a>|' \
123 -e '/^File: /s|Up: \([^,]*\)|Up: <a href="#\1">\1</a>|' \
124 -e '/^File: /s|^.*$|<i>&</i>|' \
125 -e '/^Tag Table:$/,/^End Tag Table$/d'
126 }
129 # Put some colors in log and DB files.
131 syntax_highlighter() {
132 case $1 in
133 log)
134 # If variables not defined - define them with some rare values
135 : ${_src=#_#_#}
136 : ${_install=#_#_#}
137 : ${_fs=#_#_#}
138 : ${_stuff=#_#_#}
139 sed -e 's/&/\&/g;s/</\</g;s/>/\>/g' \
140 -e 's#OK$#<span class="span-ok">OK</span>#g' \
141 -e 's#Done$#<span class="span-ok">Done</span>#g' \
142 -e 's#yes$#<span class="span-ok">yes</span>#g' \
143 -e 's#no$#<span class="span-no">no</span>#g' \
144 -e 's#error$#<span class="span-red">error</span>#g' \
145 -e 's#ERROR:#<span class="span-red">ERROR:</span>#g' \
146 -e 's#WARNING:#<span class="span-red">WARNING:</span>#g' \
147 -e s"#^Executing:\([^']*\).#<span class='sh-val'>\0</span>#"g \
148 -e s"#^====\([^']*\).#<span class='span-line'>\0</span>#"g \
149 -e s"#^[a-zA-Z0-9]\([^']*\) :: #<span class='span-sky'>\0</span>#"g \
150 -e s"#ftp://[^ '\"]*#<a href='\0'>\0</a>#"g \
151 -e s"#http://[^ '\"]*#<a href='\0'>\0</a>#"g | \
152 sed "s|$_src|<span class='var'>\${src}</span>|g;
153 s|$_install|<span class='var'>\${install}</span>|g;
154 s|$_fs|<span class='var'>\${fs}</span>|g;
155 s|$_stuff|<span class='var'>\${stuff}</span>|g"
156 ;;
158 receipt)
159 sed -e s'|&|\&|g' -e 's|<|\<|g' -e 's|>|\>|'g \
160 -e s"#^\#\([^']*\)#<span class='sh-comment'>\0</span>#"g \
161 -e s"#\"\([^']*\)\"#<span class='sh-val'>\0</span>#"g ;;
163 diff)
164 sed -e 's|&|\&|g' -e 's|<|\<|g' -e 's|>|\>|g' \
165 -e s"#^-\([^']*\).#<span class='span-red'>\0</span>#"g \
166 -e s"#^+\([^']*\).#<span class='span-ok'>\0</span>#"g \
167 -e s"#@@\([^']*\)@@#<span class='span-sky'>@@\1@@</span>#"g ;;
169 activity)
170 sed s"#^\([^']* : \)#<span class='log-date'>\0</span>#"g ;;
171 esac
172 }
175 # Latest build pkgs.
177 list_packages() {
178 cd $PKGS
179 ls -1t *.tazpkg | head -20 | \
180 while read file; do
181 echo -n $(TZ=UTC stat -c '%y' $PKGS/$file | cut -d . -f 1 | sed s/:[0-9]*$//)
182 echo " : $file"
183 done
184 }
187 # Optional full list button
189 more_button() {
190 [ $(wc -l < ${3:-$CACHE/$1}) -gt ${4:-12} ] && cat <<EOT
191 <div style="float: right;">
192 <a class="button" href="?file=$1">$2</a>
193 </div>
194 EOT
195 }
198 # Show the running command and its progression
200 running_command()
201 {
202 local state="Not running"
203 if [ -s "$command" ]; then
204 state="$(cat $command)"
205 if grep -q "^$state" $cooktime ; then
206 set -- $(cat $cooktime)
207 state="$state $((($(date +%s)-$3)*100/$2))%"
208 [ $2 -gt 300 ] && state="$state (should end $(date -u -d @$(($2+$3))))"
209 fi
210 fi
211 echo $state
212 }
215 # xHTML header. Pages can be customized with a separated html.header file.
217 if [ -f "header.html" ]; then
218 cat header.html
219 else
220 cat <<EOT
221 <!DOCTYPE html>
222 <html lang="en">
223 <head>
224 <meta charset="utf-8"/>
225 <title>SliTaz Cooker</title>
226 <link rel="shortcut icon" href="favicon.ico"/>
227 <link rel="stylesheet" type="text/css" href="style.css"/>
228 <meta name="robots" content="nofollow">
229 </head>
230 <body>
232 <div id="header">
233 <div id="logo"></div>
234 <h1><a href="cooker.cgi">SliTaz Cooker</a></h1>
235 </div>
237 <!-- Content -->
238 <div id="content">
239 EOT
240 fi
243 #
244 # Load requested page
245 #
247 case "${QUERY_STRING}" in
248 pkg=*)
249 pkg=${QUERY_STRING#pkg=}
250 log=$LOGS/$pkg.log
251 echo "<h2>Package: $pkg</h2>"
253 # Define cook variables for syntax highlighter
254 if [ -s "$WOK/$pkg/receipt" ]; then
255 . "$WOK/$pkg/receipt"
256 _wok='/home/slitaz/wok'
257 _src="$_wok/$pkg/source/$PACKAGE-$VERSION"
258 _install="$_wok/$pkg/install"
259 _fs="$_wok/$pkg/taz/$PACKAGE-$VERSION/fs"
260 _stuff="$_wok/$pkg/stuff"
261 fi
263 # Package info.
264 echo '<div id="info">'
265 if [ -f "$wok/$pkg/receipt" ]; then
266 echo "<a href='?receipt=$pkg'>receipt</a>"
267 unset WEB_SITE
268 . $wok/$pkg/receipt
270 [ -n "$WEB_SITE" ] && # busybox wget -s $WEB_SITE &&
271 echo "<a href='$WEB_SITE'>home</a>"
273 if [ -f "$wok/$pkg/taz/$PACKAGE-$VERSION/receipt" ]; then
274 echo "<a href='?files=$pkg'>files</a>"
275 unset EXTRAVERSION
276 . $wok/$pkg/taz/$PACKAGE-$VERSION/receipt
277 if [ -f $wok/$pkg/taz/$PACKAGE-$VERSION/description.txt ]; then
278 echo "<a href='?description=$pkg'>description</a>"
279 fi
280 if [ -f $PKGS/$PACKAGE-$VERSION$EXTRAVERSION.tazpkg ]; then
281 echo "<a href='?download=$PACKAGE-$VERSION$EXTRAVERSION.tazpkg'>download</a>"
282 fi
283 if [ -f $PKGS/$PACKAGE-$VERSION$EXTRAVERSION-$ARCH.tazpkg ]; then
284 echo "<a href='?download=$PACKAGE-$VERSION$EXTRAVERSION-$ARCH.tazpkg'>download</a>"
285 fi
286 fi
287 [ -x ./man2html ] &&
288 if [ -d $wok/$pkg/install/usr/man -o -d $wok/$pkg/install/usr/share/man ]; then
289 echo "<a href='?man=$PACKAGE'>man</a>"
290 fi
291 if [ -d $wok/$pkg/install/usr/doc -o -d $wok/$pkg/install/usr/share/doc ]; then
292 echo "<a href='?doc=$PACKAGE'>doc</a>"
293 fi
294 if [ -d $wok/$pkg/install/usr/info -o -d $wok/$pkg/install/usr/share/info ]; then
295 echo "<a href='?info=$PACKAGE'>info</a>"
296 fi
297 echo "<a href='ftp://${HTTP_HOST%:*}/$pkg/'>browse</a>"
298 else
299 if [ $(ls $wok/*$pkg*/receipt 2> /dev/null | wc -l) -eq 0 ]; then
300 echo "No package named: $pkg"
301 else
302 ls $wok/$pkg/receipt >/dev/null 2>&1 || pkg="*$pkg*"
303 echo '<table style="width:100%">'
304 for i in $(cd $wok ; ls $pkg/receipt); do
305 pkg=$(dirname $i)
306 unset SHORT_DESC CATEGORY
307 . $wok/$pkg/receipt
308 cat <<EOT
309 <tr>
310 <td><a href="?pkg=$pkg">$pkg</a></td>
311 <td>$SHORT_DESC</td>
312 <td>$CATEGORY</td>
313 </tr>
314 EOT
315 done
316 echo '</table>'
317 unset pkg
318 fi
319 fi
320 echo '</div>'
322 # Check for a log file and display summary if it exists.
323 if [ -f "$log" ]; then
324 if grep -q "cook:$pkg$" $command; then
325 echo "<pre>The Cooker is currently building: $pkg</pre>"
326 fi
327 if fgrep -q "Summary for:" $LOGS/$pkg.log; then
328 echo '<h3>Cook summary</h3>'
329 echo '<pre>'
330 grep -A 12 "^Summary for:" $LOGS/$pkg.log | sed /^$/d | \
331 syntax_highlighter log
332 echo '</pre>'
333 fi
334 if fgrep -q "Debug information" $LOGS/$pkg.log; then
335 echo '<h3>Cook failed</h3>'
336 echo '<pre>'
337 grep -A 8 "^Debug information" $LOGS/$pkg.log | sed /^$/d | \
338 syntax_highlighter log
339 echo '</pre>'
340 fi
341 echo '<h3>Cook log</h3>'
342 echo '<pre>'
343 cat $log | syntax_highlighter log
344 echo '</pre>'
345 case "$HTTP_USER_AGENT" in
346 *SliTaz*)
347 [ -f $CACHE/cooker-request ] && [ -n "$HTTP_REFERER" ] &&
348 echo "<a class=\"button\" href=\"?recook=$pkg\">Recook $pkg</a>"
349 esac
350 else
351 [ "$pkg" ] && echo "<pre>No log: $pkg</pre>"
352 fi ;;
354 file=*)
355 # Don't allow all files on the system for security reasons.
356 file=${QUERY_STRING#file=}
357 case "$file" in
358 activity|cooknotes|cooklist)
359 [ "$file" == "cooklist" ] && \
360 nb="- Packages: $(cat $cooklist | wc -l)"
361 echo "<h2>DB: $file $nb</h2>"
362 echo '<pre>'
363 tac $CACHE/$file | syntax_highlighter activity
364 echo '</pre>' ;;
366 broken)
367 nb=$(cat $broken | wc -l)
368 echo "<h2>DB: broken - Packages: $nb</h2>"
369 echo '<pre>'
370 cat $CACHE/$file | sort | \
371 sed s"#^[^']*#<a href='?pkg=\0'>\0</a>#"g
372 echo '</pre>' ;;
374 *.diff)
375 diff=$CACHE/$file
376 echo "<h2>Diff for: ${file%.diff}</h2>"
377 [ "$file" == "installed.diff" ] && echo \
378 "<p>This is the latest diff between installed packages \
379 and installed build dependencies to cook.</p>"
380 echo '<pre>'
381 cat $diff | syntax_highlighter diff
382 echo '</pre>' ;;
384 *.log)
385 log=$LOGS/$file
386 name=$(basename $log)
387 echo "<h2>Log for: ${name%.log}</h2>"
388 if [ -f "$log" ]; then
389 if fgrep -q "Summary" $log; then
390 echo '<pre>'
391 grep -A 20 "^Summary" $log | sed /^$/d | \
392 syntax_highlighter log
393 echo '</pre>'
394 fi
395 echo '<pre>'
396 cat $log | syntax_highlighter log
397 echo '</pre>'
398 else
399 echo "<pre>No log file: $log</pre>"
400 fi ;;
401 esac ;;
403 stuff=*)
404 file=${QUERY_STRING#stuff=}
405 echo "<h2>$file</h2>"
406 echo '<pre>'
407 cat $wok/$file | sed 's/&/\&/g;s/</\</g;s/>/\>/g'
408 echo '</pre>' ;;
410 receipt=*)
411 pkg=${QUERY_STRING#receipt=}
412 echo "<h2>Receipt for: $pkg</h2>"
413 if [ -f "$wok/$pkg/receipt" ]; then
414 ( cd $wok/$pkg ; find stuff -type f 2> /dev/null ) | \
415 while read file ; do
416 echo "<a href=\"?stuff=$pkg/$file\">$file</a>"
417 done
418 echo '<pre>'
419 cat $wok/$pkg/receipt | \
420 syntax_highlighter receipt
421 echo '</pre>'
422 else
423 echo "<pre>No receipt for: $pkg</pre>"
424 fi ;;
426 files=*)
427 pkg=${QUERY_STRING#files=}
428 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
429 if [ -d "$dir/fs" ]; then
430 echo "<h2>Installed files by: $pkg ($(du -hs $dir/fs | awk '{ print $1 }'))</h2>"
431 echo '<pre>'
432 find $dir/fs -not -type d -print0 | xargs -0 ls -ld | \
433 sed "s|\(.*\) /.*\(${dir#*wok}/fs\)\(.*\)|\1 <a href=\"?download=../wok\2\3\">\3</a>|;s|^\([^-].*\)\(<a.*\)\">\(.*\)</a>|\1\3|"
434 echo '</pre>'
435 else
436 echo "<pre>No files list for: $pkg</pre>"
437 fi ;;
439 description=*)
440 pkg=${QUERY_STRING#description=}
441 echo "<h2>Description of $pkg</h2>"
442 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
443 if [ -s "$dir/description.txt" ]; then
444 echo '<pre>'
445 cat $dir/description.txt | \
446 sed 's/&/\&/g;s/</\</g;s/>/\>/g'
447 echo '</pre>'
448 else
449 echo "<pre>No description for: $pkg</pre>"
450 fi ;;
452 man=*|doc=*|info=*)
453 type=${QUERY_STRING%%=*}
454 pkg=$(GET $type)
455 dir=$WOK/$pkg/install/usr/share/$type
456 [ -d $dir ] || dir=$WOK/$pkg/install/usr/$type
457 page=$(GET file)
458 if [ -z "$page" ]; then
459 page=$(find $dir -type f | sed q)
460 page=${page#$dir/}
461 fi
462 find $dir -type f | while read file ; do
463 [ -s $file ] || continue
464 case "$file" in
465 *.jp*g|*.png|*.gif|*.svg) continue
466 esac
467 file=${file#$dir/}
468 echo "<a href='?$type=$pkg&file=$file'>$(basename $file)</a>"
469 done
470 echo "<h2>$(basename $page)</h2>"
471 tmp="$(mktemp)"
472 docat "$dir/$page" > $tmp
473 [ -s "$tmp" ] && case "$type" in
474 info)
475 echo '<pre>'
476 info2html < "$tmp"
477 echo '</pre>' ;;
478 doc)
479 echo '<pre>'
480 case "$page" in
481 *.htm*) cat ;;
482 *) sed 's/&/\&/g;s/</\</g;s/>/\>/g'
483 esac < "$tmp"
484 echo '</pre>' ;;
485 man)
486 export TEXTDOMAIN='man2html'
487 ./man2html "$tmp" | sed -e '1,/<header>/d' \
488 -e 's|<a href="file:///[^>]*>\([^<]*\)</a>|\1|g' \
489 -e 's|<a href="?[1-9]\+[^>]*>\([^<]*\)</a>|\1|g' ;;
490 esac
491 rm -f $tmp
492 ;;
493 *)
494 # We may have a toolchain.cgi script for cross cooker's
495 if [ -f "toolchain.cgi" ]; then
496 toolchain='toolchain.cgi'
497 else
498 toolchain='?pkg=slitaz-toolchain'
499 fi
500 # Main page with summary. Count only package include in ARCH,
501 # use 'cooker arch-db' to manually create arch.$ARCH files.
502 inwok=$(ls $WOK/*/arch.$ARCH | wc -l)
503 cooked=$(ls $PKGS/*.tazpkg | wc -l)
504 unbuilt=$(($inwok - $cooked))
505 pct=0
506 [ $inwok -gt 0 ] && pct=$(( ($cooked * 100) / $inwok ))
507 cat <<EOT
508 <div style="float: right;">
509 <form method="get" action="$SCRIPT_NAME">
510 Package:
511 <input type="text" name="pkg" />
512 </form>
513 </div>
515 <h2>Summary</h2>
517 <pre>
518 Running command : $(running_command)
519 Wok revision : <a href="$WOK_URL">$(cat $wokrev)</a>
520 Commits to cook : $(cat $commits | wc -l)
521 Current cooklist : $(cat $cooklist | wc -l)
522 Broken packages : $(cat $broken | wc -l)
523 Blocked packages : $(cat $blocked | wc -l)
524 </pre>
525 EOT
526 [ -e $CACHE/cooker-request ] &&
527 [ $CACHE/activity -nt $CACHE/cooker-request ] && cat <<EOT
528 <div style="float: right;">
529 <a class="button" href="?poke">Poke cooker</a>
530 </div>
531 EOT
532 cat <<EOT
533 <p class="info">
534 Packages: $inwok in the wok | $cooked cooked | $unbuilt unbuilt |
535 Server date: $(date -u '+%F %R %Z')
536 </p>
537 <div class="pctbar">
538 <div class="pct" style="width: ${pct}%;">${pct}%</div>
539 </div>
541 <p>
542 Latest:
543 <a href="?file=cookorder.log">cookorder.log</a>
544 <a href="?file=commits.log">commits.log</a>
545 <a href="?file=pkgdb.log">pkgdb.log</a>
546 <a href="?file=installed.diff">installed.diff</a>
547 - Architecture $ARCH:
548 <a href="$toolchain">toolchain</a>
549 </p>
551 $(more_button activity "More activity" $CACHE/activity 12)
552 <h2 id="activity">Activity</h2>
553 <pre>
554 $(tac $CACHE/activity | head -n 12 | syntax_highlighter activity)
555 </pre>
556 EOT
558 [ -s $cooknotes ] && cat <<EOT
559 $(more_button cooknotes "More notes" $cooknotes 12)
560 <h2 id="cooknotes">Cooknotes</h2>
561 <pre>
562 $(tac $cooknotes | head -n 12 | syntax_highlighter activity)
563 </pre>
564 EOT
566 [ -s $commits ] && cat <<EOT
567 <h2 id="commits">Commits</h2>
568 <pre>
569 $(cat $commits)
570 </pre>
571 EOT
573 [ -s $cooklist ] && cat <<EOT
574 $(more_button cooklist "Full cooklist" $cooklist 20)
575 <h2 id="cooklist">Cooklist</h2>
576 <pre>
577 $(cat $cooklist | head -n 20)
578 </pre>
579 EOT
581 [ -s $broken ] && cat <<EOT
582 $(more_button broken "All broken packages" $broken 20)
583 <h2 id="broken">Broken</h2>
584 <pre>
585 $(cat $broken | head -n 20 | sed s"#^[^']*#<a href='?pkg=\0'>\0</a>#"g)
586 </pre>
587 EOT
589 [ -s $blocked ] && cat <<EOT
590 <h2 id="blocked">Blocked</h2>
591 <pre>
592 $(cat $blocked | sed s"#^[^']*#<a href='?pkg=\0'>\0</a>#"g)
593 </pre>
594 EOT
596 cat <<EOT
597 <h2 id="lastcook">Latest cook</h2>
598 <pre>
599 $(list_packages | sed s"#^\([^']*\).* : #<span class='log-date'>\0</span>#"g)
600 </pre>
601 EOT
602 ;;
603 esac
606 # Close xHTML page
608 cat <<EOT
609 </div>
611 <div id="footer">
612 <a href="http://www.slitaz.org/">SliTaz Website</a>
613 <a href="cooker.cgi">Cooker</a>
614 <a href="http://hg.slitaz.org/cookutils/raw-file/tip/doc/cookutils.en.html">
615 Documentation</a>
616 </div>
618 </body>
619 </html>
620 EOT
622 exit 0