cookutils view web/cooker.cgi @ rev 811

cooker.cgi: typo
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Jul 05 20:58:27 2016 +0200 (2016-07-05)
parents f6f8a4d07d48
children c502237e2fe6
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 # Put some colors in log and DB files.
117 syntax_highlighter() {
118 case $1 in
119 log)
120 # If variables not defined - define them with some rare values
121 : ${_src=#_#_#}
122 : ${_install=#_#_#}
123 : ${_fs=#_#_#}
124 : ${_stuff=#_#_#}
125 sed -e 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g' \
126 -e 's#OK$#<span class="span-ok">OK</span>#g' \
127 -e 's#Done$#<span class="span-ok">Done</span>#g' \
128 -e 's#yes$#<span class="span-ok">yes</span>#g' \
129 -e 's#no$#<span class="span-no">no</span>#g' \
130 -e 's#error$#<span class="span-red">error</span>#g' \
131 -e 's#ERROR:#<span class="span-red">ERROR:</span>#g' \
132 -e 's#WARNING:#<span class="span-red">WARNING:</span>#g' \
133 -e s"#^Executing:\([^']*\).#<span class='sh-val'>\0</span>#"g \
134 -e s"#^====\([^']*\).#<span class='span-line'>\0</span>#"g \
135 -e s"#^[a-zA-Z0-9]\([^']*\) :: #<span class='span-sky'>\0</span>#"g \
136 -e s"#ftp://[^ '\"]*#<a href='\0'>\0</a>#"g \
137 -e s"#http://[^ '\"]*#<a href='\0'>\0</a>#"g | \
138 sed "s|$_src|<span class='var'>\${src}</span>|g;
139 s|$_install|<span class='var'>\${install}</span>|g;
140 s|$_fs|<span class='var'>\${fs}</span>|g;
141 s|$_stuff|<span class='var'>\${stuff}</span>|g"
142 ;;
144 receipt)
145 sed -e s'|&|\&amp;|g' -e 's|<|\&lt;|g' -e 's|>|\&gt;|'g \
146 -e s"#^\#\([^']*\)#<span class='sh-comment'>\0</span>#"g \
147 -e s"#\"\([^']*\)\"#<span class='sh-val'>\0</span>#"g ;;
149 diff)
150 sed -e 's|&|\&amp;|g' -e 's|<|\&lt;|g' -e 's|>|\&gt;|g' \
151 -e s"#^-\([^']*\).#<span class='span-red'>\0</span>#"g \
152 -e s"#^+\([^']*\).#<span class='span-ok'>\0</span>#"g \
153 -e s"#@@\([^']*\)@@#<span class='span-sky'>@@\1@@</span>#"g ;;
155 activity)
156 sed s"#^\([^']* : \)#<span class='log-date'>\0</span>#"g ;;
157 esac
158 }
161 # Latest build pkgs.
163 list_packages() {
164 cd $PKGS
165 ls -1t *.tazpkg | head -20 | \
166 while read file; do
167 echo -n $(TZ=UTC stat -c '%y' $PKGS/$file | cut -d . -f 1 | sed s/:[0-9]*$//)
168 echo " : $file"
169 done
170 }
173 # Optional full list button
175 more_button() {
176 [ $(wc -l < ${3:-$CACHE/$1}) -gt ${4:-12} ] && cat <<EOT
177 <div style="float: right;">
178 <a class="button" href="?file=$1">$2</a>
179 </div>
180 EOT
181 }
184 # Show the running command and its progression
186 running_command()
187 {
188 local state="Not running"
189 if [ -s "$command" ]; then
190 state="$(cat $command)"
191 if grep -q "^$state" $cooktime ; then
192 set -- $(cat $cooktime)
193 state="$state $((($(date +%s)-$3)*100/$2))%"
194 [ $2 -gt 300 ] && state="$state (should end $(date -u -d @$(($2+$3))))"
195 fi
196 fi
197 echo $state
198 }
201 # xHTML header. Pages can be customized with a separated html.header file.
203 if [ -f "header.html" ]; then
204 cat header.html
205 else
206 cat <<EOT
207 <!DOCTYPE html>
208 <html lang="en">
209 <head>
210 <meta charset="utf-8"/>
211 <title>SliTaz Cooker</title>
212 <link rel="shortcut icon" href="favicon.ico"/>
213 <link rel="stylesheet" type="text/css" href="style.css"/>
214 <meta name="robots" content="nofollow">
215 </head>
216 <body>
218 <div id="header">
219 <div id="logo"></div>
220 <h1><a href="cooker.cgi">SliTaz Cooker</a></h1>
221 </div>
223 <!-- Content -->
224 <div id="content">
225 EOT
226 fi
229 #
230 # Load requested page
231 #
233 case "${QUERY_STRING}" in
234 pkg=*)
235 pkg=${QUERY_STRING#pkg=}
236 log=$LOGS/$pkg.log
237 echo "<h2>Package: $pkg</h2>"
239 # Define cook variables for syntax highlighter
240 if [ -s "$WOK/$pkg/receipt" ]; then
241 . "$WOK/$pkg/receipt"
242 _wok='/home/slitaz/wok'
243 _src="$_wok/$pkg/source/$PACKAGE-$VERSION"
244 _install="$_wok/$pkg/install"
245 _fs="$_wok/$pkg/taz/$PACKAGE-$VERSION/fs"
246 _stuff="$_wok/$pkg/stuff"
247 fi
249 # Package info.
250 echo '<div id="info">'
251 if [ -f "$wok/$pkg/receipt" ]; then
252 echo "<a href='?receipt=$pkg'>receipt</a>"
253 unset WEB_SITE
254 . $wok/$pkg/receipt
256 [ -n "$WEB_SITE" ] && # busybox wget -s $WEB_SITE &&
257 echo "<a href='$WEB_SITE'>home</a>"
259 if [ -f "$wok/$pkg/taz/$PACKAGE-$VERSION/receipt" ]; then
260 echo "<a href='?files=$pkg'>files</a>"
261 unset EXTRAVERSION
262 . $wok/$pkg/taz/$PACKAGE-$VERSION/receipt
263 if [ -f $wok/$pkg/taz/$PACKAGE-$VERSION/description.txt ]; then
264 echo "<a href='?description=$pkg'>description</a>"
265 fi
266 if [ -f $PKGS/$PACKAGE-$VERSION$EXTRAVERSION.tazpkg ]; then
267 echo "<a href='?download=$PACKAGE-$VERSION$EXTRAVERSION.tazpkg'>download</a>"
268 fi
269 if [ -f $PKGS/$PACKAGE-$VERSION$EXTRAVERSION-$ARCH.tazpkg ]; then
270 echo "<a href='?download=$PACKAGE-$VERSION$EXTRAVERSION-$ARCH.tazpkg'>download</a>"
271 fi
272 fi
273 if [ -x ./man2html -a -d $wok/$pkg/install/usr/share/man ]; then
274 echo "<a href='?man=$PACKAGE'>man</a>"
275 fi
276 if [ -d $wok/$pkg/install/usr/share/doc ]; then
277 echo "<a href='?doc=$PACKAGE'>doc</a>"
278 fi
279 echo "<a href='ftp://${HTTP_HOST%:*}/$pkg/'>browse</a>"
280 else
281 if [ $(ls $wok/*$pkg*/receipt 2> /dev/null | wc -l) -eq 0 ]; then
282 echo "No package named: $pkg"
283 else
284 ls $wok/$pkg/receipt >/dev/null 2>&1 || pkg="*$pkg*"
285 echo '<table style="width:100%">'
286 for i in $(cd $wok ; ls $pkg/receipt); do
287 pkg=$(dirname $i)
288 unset SHORT_DESC CATEGORY
289 . $wok/$pkg/receipt
290 cat <<EOT
291 <tr>
292 <td><a href="?pkg=$pkg">$pkg</a></td>
293 <td>$SHORT_DESC</td>
294 <td>$CATEGORY</td>
295 </tr>
296 EOT
297 done
298 echo '</table>'
299 unset pkg
300 fi
301 fi
302 echo '</div>'
304 # Check for a log file and display summary if it exists.
305 if [ -f "$log" ]; then
306 if grep -q "cook:$pkg$" $command; then
307 echo "<pre>The Cooker is currently building: $pkg</pre>"
308 fi
309 if fgrep -q "Summary for:" $LOGS/$pkg.log; then
310 echo '<h3>Cook summary</h3>'
311 echo '<pre>'
312 grep -A 12 "^Summary for:" $LOGS/$pkg.log | sed /^$/d | \
313 syntax_highlighter log
314 echo '</pre>'
315 fi
316 if fgrep -q "Debug information" $LOGS/$pkg.log; then
317 echo '<h3>Cook failed</h3>'
318 echo '<pre>'
319 grep -A 8 "^Debug information" $LOGS/$pkg.log | sed /^$/d | \
320 syntax_highlighter log
321 echo '</pre>'
322 fi
323 echo '<h3>Cook log</h3>'
324 echo '<pre>'
325 cat $log | syntax_highlighter log
326 echo '</pre>'
327 case "$HTTP_USER_AGENT" in
328 *SliTaz*)
329 [ -f $CACHE/cooker-request ] && [ -n "$HTTP_REFERER" ] &&
330 echo "<a class=\"button\" href=\"?recook=$pkg\">Recook $pkg</a>"
331 esac
332 else
333 [ "$pkg" ] && echo "<pre>No log: $pkg</pre>"
334 fi ;;
336 file=*)
337 # Don't allow all files on the system for security reasons.
338 file=${QUERY_STRING#file=}
339 case "$file" in
340 activity|cooknotes|cooklist)
341 [ "$file" == "cooklist" ] && \
342 nb="- Packages: $(cat $cooklist | wc -l)"
343 echo "<h2>DB: $file $nb</h2>"
344 echo '<pre>'
345 tac $CACHE/$file | syntax_highlighter activity
346 echo '</pre>' ;;
348 broken)
349 nb=$(cat $broken | wc -l)
350 echo "<h2>DB: broken - Packages: $nb</h2>"
351 echo '<pre>'
352 cat $CACHE/$file | sort | \
353 sed s"#^[^']*#<a href='?pkg=\0'>\0</a>#"g
354 echo '</pre>' ;;
356 *.diff)
357 diff=$CACHE/$file
358 echo "<h2>Diff for: ${file%.diff}</h2>"
359 [ "$file" == "installed.diff" ] && echo \
360 "<p>This is the latest diff between installed packages \
361 and installed build dependencies to cook.</p>"
362 echo '<pre>'
363 cat $diff | syntax_highlighter diff
364 echo '</pre>' ;;
366 *.log)
367 log=$LOGS/$file
368 name=$(basename $log)
369 echo "<h2>Log for: ${name%.log}</h2>"
370 if [ -f "$log" ]; then
371 if fgrep -q "Summary" $log; then
372 echo '<pre>'
373 grep -A 20 "^Summary" $log | sed /^$/d | \
374 syntax_highlighter log
375 echo '</pre>'
376 fi
377 echo '<pre>'
378 cat $log | syntax_highlighter log
379 echo '</pre>'
380 else
381 echo "<pre>No log file: $log</pre>"
382 fi ;;
383 esac ;;
385 stuff=*)
386 file=${QUERY_STRING#stuff=}
387 echo "<h2>$file</h2>"
388 echo '<pre>'
389 cat $wok/$file | sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
390 echo '</pre>' ;;
392 receipt=*)
393 pkg=${QUERY_STRING#receipt=}
394 echo "<h2>Receipt for: $pkg</h2>"
395 if [ -f "$wok/$pkg/receipt" ]; then
396 ( cd $wok/$pkg ; find stuff -type f 2> /dev/null ) | \
397 while read file ; do
398 echo "<a href=\"?stuff=$pkg/$file\">$file</a>"
399 done
400 echo '<pre>'
401 cat $wok/$pkg/receipt | \
402 syntax_highlighter receipt
403 echo '</pre>'
404 else
405 echo "<pre>No receipt for: $pkg</pre>"
406 fi ;;
408 files=*)
409 pkg=${QUERY_STRING#files=}
410 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
411 if [ -d "$dir/fs" ]; then
412 echo "<h2>Installed files by: $pkg ($(du -hs $dir/fs | awk '{ print $1 }'))</h2>"
413 echo '<pre>'
414 find $dir/fs -not -type d -print0 | xargs -0 ls -ld | \
415 sed "s|\(.*\) /.*\(${dir#*wok}/fs\)\(.*\)|\1 <a href=\"?download=../wok\2\3\">\3</a>|;s|^\([^-].*\)\(<a.*\)\">\(.*\)</a>|\1\3|"
416 echo '</pre>'
417 else
418 echo "<pre>No files list for: $pkg</pre>"
419 fi ;;
421 description=*)
422 pkg=${QUERY_STRING#description=}
423 echo "<h2>Description of $pkg</h2>"
424 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
425 if [ -s "$dir/description.txt" ]; then
426 echo '<pre>'
427 cat $dir/description.txt | \
428 sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
429 echo '</pre>'
430 else
431 echo "<pre>No description for: $pkg</pre>"
432 fi ;;
434 man=*|doc=*)
435 type=${QUERY_STRING%%=*}
436 pkg=$(GET $type)
437 dir=$WOK/$pkg/install/usr/share/$type
438 page=$(GET file)
439 if [ -z "$page" ]; then
440 page=$(find $dir -type f | sed q)
441 page=${page#$dir/}
442 fi
443 find $dir -type f | while read file ; do
444 file=${file#$dir/}
445 echo "<a href='?$type=$pkg&amp;file=$file'>$(basename $file)</a>"
446 done
447 echo "<h2>$(basename $page)</h2>"
448 tmp="$(mktemp)"
449 docat "$dir/$page" > $tmp
450 case "$type" in
451 doc)
452 echo '<pre>'
453 sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g' < "$tmp"
454 echo '</pre>' ;;
455 man)
456 export TEXTDOMAIN='man2html'
457 ./man2html "$tmp" | sed -e '1,/<header>/d' \
458 -e 's|<a href="file:///[^>]*>\([^<]*\)</a>|\1|g' \
459 -e 's|<a href="?[1-9]\+[^>]*>\([^<]*\)</a>|\1|g' ;;
460 esac
461 rm -f $tmp
462 ;;
463 *)
464 # We may have a toolchain.cgi script for cross cooker's
465 if [ -f "toolchain.cgi" ]; then
466 toolchain='toolchain.cgi'
467 else
468 toolchain='?pkg=slitaz-toolchain'
469 fi
470 # Main page with summary. Count only package include in ARCH,
471 # use 'cooker arch-db' to manually create arch.$ARCH files.
472 inwok=$(ls $WOK/*/arch.$ARCH | wc -l)
473 cooked=$(ls $PKGS/*.tazpkg | wc -l)
474 unbuilt=$(($inwok - $cooked))
475 pct=0
476 [ $inwok -gt 0 ] && pct=$(( ($cooked * 100) / $inwok ))
477 cat <<EOT
478 <div style="float: right;">
479 <form method="get" action="$SCRIPT_NAME">
480 Package:
481 <input type="text" name="pkg" />
482 </form>
483 </div>
485 <h2>Summary</h2>
487 <pre>
488 Running command : $(running_command)
489 Wok revision : <a href="$WOK_URL">$(cat $wokrev)</a>
490 Commits to cook : $(cat $commits | wc -l)
491 Current cooklist : $(cat $cooklist | wc -l)
492 Broken packages : $(cat $broken | wc -l)
493 Blocked packages : $(cat $blocked | wc -l)
494 </pre>
495 EOT
496 [ -e $CACHE/cooker-request ] &&
497 [ $CACHE/activity -nt $CACHE/cooker-request ] && cat <<EOT
498 <div style="float: right;">
499 <a class="button" href="?poke">Poke cooker</a>
500 </div>
501 EOT
502 cat <<EOT
503 <p class="info">
504 Packages: $inwok in the wok | $cooked cooked | $unbuilt unbuilt |
505 Server date: $(date -u '+%F %R %Z')
506 </p>
507 <div class="pctbar">
508 <div class="pct" style="width: ${pct}%;">${pct}%</div>
509 </div>
511 <p>
512 Latest:
513 <a href="?file=cookorder.log">cookorder.log</a>
514 <a href="?file=commits.log">commits.log</a>
515 <a href="?file=pkgdb.log">pkgdb.log</a>
516 <a href="?file=installed.diff">installed.diff</a>
517 - Architecture $ARCH:
518 <a href="$toolchain">toolchain</a>
519 </p>
521 $(more_button activity "More activity" $CACHE/activity 12)
522 <h2 id="activity">Activity</h2>
523 <pre>
524 $(tac $CACHE/activity | head -n 12 | syntax_highlighter activity)
525 </pre>
526 EOT
528 [ -s $cooknotes ] && cat <<EOT
529 $(more_button cooknotes "More notes" $cooknotes 12)
530 <h2 id="cooknotes">Cooknotes</h2>
531 <pre>
532 $(tac $cooknotes | head -n 12 | syntax_highlighter activity)
533 </pre>
534 EOT
536 [ -s $commits ] && cat <<EOT
537 <h2 id="commits">Commits</h2>
538 <pre>
539 $(cat $commits)
540 </pre>
541 EOT
543 [ -s $cooklist ] && cat <<EOT
544 $(more_button cooklist "Full cooklist" $cooklist 20)
545 <h2 id="cooklist">Cooklist</h2>
546 <pre>
547 $(cat $cooklist | head -n 20)
548 </pre>
549 EOT
551 [ -s $broken ] && cat <<EOT
552 $(more_button broken "All broken packages" $broken 20)
553 <h2 id="broken">Broken</h2>
554 <pre>
555 $(cat $broken | head -n 20 | sed s"#^[^']*#<a href='?pkg=\0'>\0</a>#"g)
556 </pre>
557 EOT
559 [ -s $blocked ] && cat <<EOT
560 <h2 id="blocked">Blocked</h2>
561 <pre>
562 $(cat $blocked | sed s"#^[^']*#<a href='?pkg=\0'>\0</a>#"g)
563 </pre>
564 EOT
566 cat <<EOT
567 <h2 id="lastcook">Latest cook</h2>
568 <pre>
569 $(list_packages | sed s"#^\([^']*\).* : #<span class='log-date'>\0</span>#"g)
570 </pre>
571 EOT
572 ;;
573 esac
576 # Close xHTML page
578 cat <<EOT
579 </div>
581 <div id="footer">
582 <a href="http://www.slitaz.org/">SliTaz Website</a>
583 <a href="cooker.cgi">Cooker</a>
584 <a href="http://hg.slitaz.org/cookutils/raw-file/tip/doc/cookutils.en.html">
585 Documentation</a>
586 </div>
588 </body>
589 </html>
590 EOT
592 exit 0