cookutils view web/cooker.cgi @ rev 814
cooker.cgi: look for /usr/share/man and /usr/man
| author | Pascal Bellard <pascal.bellard@slitaz.org> |
|---|---|
| date | Wed Jul 06 16:57:55 2016 +0200 (2016-07-06) |
| parents | d3b274c35d2d |
| children | 48ebce528e77 |
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/&/\&/g;s/</\</g;s/>/\>/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'|&|\&|g' -e 's|<|\<|g' -e 's|>|\>|'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|&|\&|g' -e 's|<|\<|g' -e 's|>|\>|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 [ -x ./man2html ] &&
274 if [ -d $wok/$pkg/install/usr/man -o -d $wok/$pkg/install/usr/share/man ]; then
275 echo "<a href='?man=$PACKAGE'>man</a>"
276 fi
277 if [ -d $wok/$pkg/install/usr/doc -o -d $wok/$pkg/install/usr/share/doc ]; then
278 echo "<a href='?doc=$PACKAGE'>doc</a>"
279 fi
280 echo "<a href='ftp://${HTTP_HOST%:*}/$pkg/'>browse</a>"
281 else
282 if [ $(ls $wok/*$pkg*/receipt 2> /dev/null | wc -l) -eq 0 ]; then
283 echo "No package named: $pkg"
284 else
285 ls $wok/$pkg/receipt >/dev/null 2>&1 || pkg="*$pkg*"
286 echo '<table style="width:100%">'
287 for i in $(cd $wok ; ls $pkg/receipt); do
288 pkg=$(dirname $i)
289 unset SHORT_DESC CATEGORY
290 . $wok/$pkg/receipt
291 cat <<EOT
292 <tr>
293 <td><a href="?pkg=$pkg">$pkg</a></td>
294 <td>$SHORT_DESC</td>
295 <td>$CATEGORY</td>
296 </tr>
297 EOT
298 done
299 echo '</table>'
300 unset pkg
301 fi
302 fi
303 echo '</div>'
305 # Check for a log file and display summary if it exists.
306 if [ -f "$log" ]; then
307 if grep -q "cook:$pkg$" $command; then
308 echo "<pre>The Cooker is currently building: $pkg</pre>"
309 fi
310 if fgrep -q "Summary for:" $LOGS/$pkg.log; then
311 echo '<h3>Cook summary</h3>'
312 echo '<pre>'
313 grep -A 12 "^Summary for:" $LOGS/$pkg.log | sed /^$/d | \
314 syntax_highlighter log
315 echo '</pre>'
316 fi
317 if fgrep -q "Debug information" $LOGS/$pkg.log; then
318 echo '<h3>Cook failed</h3>'
319 echo '<pre>'
320 grep -A 8 "^Debug information" $LOGS/$pkg.log | sed /^$/d | \
321 syntax_highlighter log
322 echo '</pre>'
323 fi
324 echo '<h3>Cook log</h3>'
325 echo '<pre>'
326 cat $log | syntax_highlighter log
327 echo '</pre>'
328 case "$HTTP_USER_AGENT" in
329 *SliTaz*)
330 [ -f $CACHE/cooker-request ] && [ -n "$HTTP_REFERER" ] &&
331 echo "<a class=\"button\" href=\"?recook=$pkg\">Recook $pkg</a>"
332 esac
333 else
334 [ "$pkg" ] && echo "<pre>No log: $pkg</pre>"
335 fi ;;
337 file=*)
338 # Don't allow all files on the system for security reasons.
339 file=${QUERY_STRING#file=}
340 case "$file" in
341 activity|cooknotes|cooklist)
342 [ "$file" == "cooklist" ] && \
343 nb="- Packages: $(cat $cooklist | wc -l)"
344 echo "<h2>DB: $file $nb</h2>"
345 echo '<pre>'
346 tac $CACHE/$file | syntax_highlighter activity
347 echo '</pre>' ;;
349 broken)
350 nb=$(cat $broken | wc -l)
351 echo "<h2>DB: broken - Packages: $nb</h2>"
352 echo '<pre>'
353 cat $CACHE/$file | sort | \
354 sed s"#^[^']*#<a href='?pkg=\0'>\0</a>#"g
355 echo '</pre>' ;;
357 *.diff)
358 diff=$CACHE/$file
359 echo "<h2>Diff for: ${file%.diff}</h2>"
360 [ "$file" == "installed.diff" ] && echo \
361 "<p>This is the latest diff between installed packages \
362 and installed build dependencies to cook.</p>"
363 echo '<pre>'
364 cat $diff | syntax_highlighter diff
365 echo '</pre>' ;;
367 *.log)
368 log=$LOGS/$file
369 name=$(basename $log)
370 echo "<h2>Log for: ${name%.log}</h2>"
371 if [ -f "$log" ]; then
372 if fgrep -q "Summary" $log; then
373 echo '<pre>'
374 grep -A 20 "^Summary" $log | sed /^$/d | \
375 syntax_highlighter log
376 echo '</pre>'
377 fi
378 echo '<pre>'
379 cat $log | syntax_highlighter log
380 echo '</pre>'
381 else
382 echo "<pre>No log file: $log</pre>"
383 fi ;;
384 esac ;;
386 stuff=*)
387 file=${QUERY_STRING#stuff=}
388 echo "<h2>$file</h2>"
389 echo '<pre>'
390 cat $wok/$file | sed 's/&/\&/g;s/</\</g;s/>/\>/g'
391 echo '</pre>' ;;
393 receipt=*)
394 pkg=${QUERY_STRING#receipt=}
395 echo "<h2>Receipt for: $pkg</h2>"
396 if [ -f "$wok/$pkg/receipt" ]; then
397 ( cd $wok/$pkg ; find stuff -type f 2> /dev/null ) | \
398 while read file ; do
399 echo "<a href=\"?stuff=$pkg/$file\">$file</a>"
400 done
401 echo '<pre>'
402 cat $wok/$pkg/receipt | \
403 syntax_highlighter receipt
404 echo '</pre>'
405 else
406 echo "<pre>No receipt for: $pkg</pre>"
407 fi ;;
409 files=*)
410 pkg=${QUERY_STRING#files=}
411 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
412 if [ -d "$dir/fs" ]; then
413 echo "<h2>Installed files by: $pkg ($(du -hs $dir/fs | awk '{ print $1 }'))</h2>"
414 echo '<pre>'
415 find $dir/fs -not -type d -print0 | xargs -0 ls -ld | \
416 sed "s|\(.*\) /.*\(${dir#*wok}/fs\)\(.*\)|\1 <a href=\"?download=../wok\2\3\">\3</a>|;s|^\([^-].*\)\(<a.*\)\">\(.*\)</a>|\1\3|"
417 echo '</pre>'
418 else
419 echo "<pre>No files list for: $pkg</pre>"
420 fi ;;
422 description=*)
423 pkg=${QUERY_STRING#description=}
424 echo "<h2>Description of $pkg</h2>"
425 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
426 if [ -s "$dir/description.txt" ]; then
427 echo '<pre>'
428 cat $dir/description.txt | \
429 sed 's/&/\&/g;s/</\</g;s/>/\>/g'
430 echo '</pre>'
431 else
432 echo "<pre>No description for: $pkg</pre>"
433 fi ;;
435 man=*|doc=*)
436 type=${QUERY_STRING%%=*}
437 pkg=$(GET $type)
438 dir=$WOK/$pkg/install/usr/share/$type
439 [ -d $dir ] || dir=$WOK/$pkg/install/usr/$type
440 page=$(GET file)
441 if [ -z "$page" ]; then
442 page=$(find $dir -type f | sed q)
443 page=${page#$dir/}
444 fi
445 find $dir -type f | while read file ; do
446 [ -s $file ] || continue
447 case "$file" in
448 *.jp*g|*.png|*.gif|*.svg) continue
449 esac
450 file=${file#$dir/}
451 echo "<a href='?$type=$pkg&file=$file'>$(basename $file)</a>"
452 done
453 echo "<h2>$(basename $page)</h2>"
454 tmp="$(mktemp)"
455 docat "$dir/$page" > $tmp
456 [ -s "$tmp" ] && case "$type" in
457 doc)
458 echo '<pre>'
459 case "$page" in
460 *.htm*) cat ;;
461 *) sed 's/&/\&/g;s/</\</g;s/>/\>/g'
462 esac < "$tmp"
463 echo '</pre>' ;;
464 man)
465 export TEXTDOMAIN='man2html'
466 ./man2html "$tmp" | sed -e '1,/<header>/d' \
467 -e 's|<a href="file:///[^>]*>\([^<]*\)</a>|\1|g' \
468 -e 's|<a href="?[1-9]\+[^>]*>\([^<]*\)</a>|\1|g' ;;
469 esac
470 rm -f $tmp
471 ;;
472 *)
473 # We may have a toolchain.cgi script for cross cooker's
474 if [ -f "toolchain.cgi" ]; then
475 toolchain='toolchain.cgi'
476 else
477 toolchain='?pkg=slitaz-toolchain'
478 fi
479 # Main page with summary. Count only package include in ARCH,
480 # use 'cooker arch-db' to manually create arch.$ARCH files.
481 inwok=$(ls $WOK/*/arch.$ARCH | wc -l)
482 cooked=$(ls $PKGS/*.tazpkg | wc -l)
483 unbuilt=$(($inwok - $cooked))
484 pct=0
485 [ $inwok -gt 0 ] && pct=$(( ($cooked * 100) / $inwok ))
486 cat <<EOT
487 <div style="float: right;">
488 <form method="get" action="$SCRIPT_NAME">
489 Package:
490 <input type="text" name="pkg" />
491 </form>
492 </div>
494 <h2>Summary</h2>
496 <pre>
497 Running command : $(running_command)
498 Wok revision : <a href="$WOK_URL">$(cat $wokrev)</a>
499 Commits to cook : $(cat $commits | wc -l)
500 Current cooklist : $(cat $cooklist | wc -l)
501 Broken packages : $(cat $broken | wc -l)
502 Blocked packages : $(cat $blocked | wc -l)
503 </pre>
504 EOT
505 [ -e $CACHE/cooker-request ] &&
506 [ $CACHE/activity -nt $CACHE/cooker-request ] && cat <<EOT
507 <div style="float: right;">
508 <a class="button" href="?poke">Poke cooker</a>
509 </div>
510 EOT
511 cat <<EOT
512 <p class="info">
513 Packages: $inwok in the wok | $cooked cooked | $unbuilt unbuilt |
514 Server date: $(date -u '+%F %R %Z')
515 </p>
516 <div class="pctbar">
517 <div class="pct" style="width: ${pct}%;">${pct}%</div>
518 </div>
520 <p>
521 Latest:
522 <a href="?file=cookorder.log">cookorder.log</a>
523 <a href="?file=commits.log">commits.log</a>
524 <a href="?file=pkgdb.log">pkgdb.log</a>
525 <a href="?file=installed.diff">installed.diff</a>
526 - Architecture $ARCH:
527 <a href="$toolchain">toolchain</a>
528 </p>
530 $(more_button activity "More activity" $CACHE/activity 12)
531 <h2 id="activity">Activity</h2>
532 <pre>
533 $(tac $CACHE/activity | head -n 12 | syntax_highlighter activity)
534 </pre>
535 EOT
537 [ -s $cooknotes ] && cat <<EOT
538 $(more_button cooknotes "More notes" $cooknotes 12)
539 <h2 id="cooknotes">Cooknotes</h2>
540 <pre>
541 $(tac $cooknotes | head -n 12 | syntax_highlighter activity)
542 </pre>
543 EOT
545 [ -s $commits ] && cat <<EOT
546 <h2 id="commits">Commits</h2>
547 <pre>
548 $(cat $commits)
549 </pre>
550 EOT
552 [ -s $cooklist ] && cat <<EOT
553 $(more_button cooklist "Full cooklist" $cooklist 20)
554 <h2 id="cooklist">Cooklist</h2>
555 <pre>
556 $(cat $cooklist | head -n 20)
557 </pre>
558 EOT
560 [ -s $broken ] && cat <<EOT
561 $(more_button broken "All broken packages" $broken 20)
562 <h2 id="broken">Broken</h2>
563 <pre>
564 $(cat $broken | head -n 20 | sed s"#^[^']*#<a href='?pkg=\0'>\0</a>#"g)
565 </pre>
566 EOT
568 [ -s $blocked ] && cat <<EOT
569 <h2 id="blocked">Blocked</h2>
570 <pre>
571 $(cat $blocked | sed s"#^[^']*#<a href='?pkg=\0'>\0</a>#"g)
572 </pre>
573 EOT
575 cat <<EOT
576 <h2 id="lastcook">Latest cook</h2>
577 <pre>
578 $(list_packages | sed s"#^\([^']*\).* : #<span class='log-date'>\0</span>#"g)
579 </pre>
580 EOT
581 ;;
582 esac
585 # Close xHTML page
587 cat <<EOT
588 </div>
590 <div id="footer">
591 <a href="http://www.slitaz.org/">SliTaz Website</a>
592 <a href="cooker.cgi">Cooker</a>
593 <a href="http://hg.slitaz.org/cookutils/raw-file/tip/doc/cookutils.en.html">
594 Documentation</a>
595 </div>
597 </body>
598 </html>
599 EOT
601 exit 0