cookutils view web/cooker.cgi @ rev 806

cooker.cgi: fix package search
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Jul 03 11:44:55 2016 +0200 (2016-07-03)
parents eb765a8c3872
children f6f8a4d07d48
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} ] && cat <<EOT
163 <div style="float: right;">
164 <a class="button" href="cooker.cgi?file=$1">$2</a>
165 </div>
166 EOT
167 }
170 # Show the running command and its progression
172 running_command()
173 {
174 local state="Not running"
175 if [ -s "$command" ]; then
176 state="$(cat $command)"
177 if grep -q "^$state" $cooktime ; then
178 set -- $(cat $cooktime)
179 state="$state $((($(date +%s)-$3)*100/$2))%"
180 [ $2 -gt 300 ] && state="$state (should end $(date -u -d @$(($2+$3))))"
181 fi
182 fi
183 echo $state
184 }
187 # xHTML header. Pages can be customized with a separated html.header file.
189 if [ -f "header.html" ]; then
190 cat header.html
191 else
192 cat <<EOT
193 <!DOCTYPE html>
194 <html lang="en">
195 <head>
196 <meta charset="utf-8"/>
197 <title>SliTaz Cooker</title>
198 <link rel="shortcut icon" href="favicon.ico"/>
199 <link rel="stylesheet" type="text/css" href="style.css"/>
200 <meta name="robots" content="nofollow">
201 </head>
202 <body>
204 <div id="header">
205 <div id="logo"></div>
206 <h1><a href="cooker.cgi">SliTaz Cooker</a></h1>
207 </div>
209 <!-- Content -->
210 <div id="content">
211 EOT
212 fi
215 #
216 # Load requested page
217 #
219 case "${QUERY_STRING}" in
220 pkg=*)
221 pkg=${QUERY_STRING#pkg=}
222 log=$LOGS/$pkg.log
223 echo "<h2>Package: $pkg</h2>"
225 # Define cook variables for syntax highlighter
226 if [ -s "$WOK/$pkg/receipt" ]; then
227 . "$WOK/$pkg/receipt"
228 _wok='/home/slitaz/wok'
229 _src="$_wok/$pkg/source/$PACKAGE-$VERSION"
230 _install="$_wok/$pkg/install"
231 _fs="$_wok/$pkg/taz/$PACKAGE-$VERSION/fs"
232 _stuff="$_wok/$pkg/stuff"
233 fi
235 # Package info.
236 echo '<div id="info">'
237 if [ -f "$wok/$pkg/receipt" ]; then
238 echo "<a href='cooker.cgi?receipt=$pkg'>receipt</a>"
239 unset WEB_SITE
240 . $wok/$pkg/receipt
242 [ -n "$WEB_SITE" ] && # busybox wget -s $WEB_SITE &&
243 echo "<a href='$WEB_SITE'>home</a>"
245 if [ -f "$wok/$pkg/taz/$PACKAGE-$VERSION/receipt" ]; then
246 echo "<a href='cooker.cgi?files=$pkg'>files</a>"
247 unset EXTRAVERSION
248 . $wok/$pkg/taz/$PACKAGE-$VERSION/receipt
249 if [ -f $wok/$pkg/taz/$PACKAGE-$VERSION/description.txt ]; then
250 echo "<a href='cooker.cgi?description=$pkg'>description</a>"
251 fi
252 if [ -f $PKGS/$PACKAGE-$VERSION$EXTRAVERSION.tazpkg ]; then
253 echo "<a href='cooker.cgi?download=$PACKAGE-$VERSION$EXTRAVERSION.tazpkg'>download</a>"
254 fi
255 if [ -f $PKGS/$PACKAGE-$VERSION$EXTRAVERSION-$ARCH.tazpkg ]; then
256 echo "<a href='cooker.cgi?download=$PACKAGE-$VERSION$EXTRAVERSION-$ARCH.tazpkg'>download</a>"
257 fi
258 fi
259 echo "<a href='ftp://${HTTP_HOST%:*}/$pkg/'>browse</a>"
260 else
261 if [ $(ls $wok/*$pkg*/receipt 2> /dev/null | wc -l) -eq 0 ]; then
262 echo "No package named: $pkg"
263 else
264 ls $wok/$pkg/receipt >/dev/null 2>&1 || pkg="*$pkg*"
265 echo '<table style="width:100%">'
266 for i in $(cd $wok ; ls $pkg/receipt); do
267 pkg=$(dirname $i)
268 unset SHORT_DESC CATEGORY
269 . $wok/$pkg/receipt
270 cat <<EOT
271 <tr>
272 <td><a href="cooker.cgi?pkg=$pkg">$pkg</a></td>
273 <td>$SHORT_DESC</td>
274 <td>$CATEGORY</td>
275 </tr>
276 EOT
277 done
278 echo '</table>'
279 unset pkg
280 fi
281 fi
282 echo '</div>'
284 # Check for a log file and display summary if it exists.
285 if [ -f "$log" ]; then
286 if grep -q "cook:$pkg$" $command; then
287 echo "<pre>The Cooker is currently building: $pkg</pre>"
288 fi
289 if fgrep -q "Summary for:" $LOGS/$pkg.log; then
290 echo '<h3>Cook summary</h3>'
291 echo '<pre>'
292 grep -A 12 "^Summary for:" $LOGS/$pkg.log | sed /^$/d | \
293 syntax_highlighter log
294 echo '</pre>'
295 fi
296 if fgrep -q "Debug information" $LOGS/$pkg.log; then
297 echo '<h3>Cook failed</h3>'
298 echo '<pre>'
299 grep -A 8 "^Debug information" $LOGS/$pkg.log | sed /^$/d | \
300 syntax_highlighter log
301 echo '</pre>'
302 fi
303 echo '<h3>Cook log</h3>'
304 echo '<pre>'
305 cat $log | syntax_highlighter log
306 echo '</pre>'
307 case "$HTTP_USER_AGENT" in
308 *SliTaz*)
309 [ -f $CACHE/cooker-request ] && [ -n "$HTTP_REFERER" ] &&
310 echo "<a class=\"button\" href=\"cooker.cgi?recook=$pkg\">Recook $pkg</a>"
311 esac
312 else
313 [ "$pkg" ] && echo "<pre>No log: $pkg</pre>"
314 fi ;;
316 file=*)
317 # Don't allow all files on the system for security reasons.
318 file=${QUERY_STRING#file=}
319 case "$file" in
320 activity|cooknotes|cooklist)
321 [ "$file" == "cooklist" ] && \
322 nb="- Packages: $(cat $cooklist | wc -l)"
323 echo "<h2>DB: $file $nb</h2>"
324 echo '<pre>'
325 tac $CACHE/$file | syntax_highlighter activity
326 echo '</pre>' ;;
328 broken)
329 nb=$(cat $broken | wc -l)
330 echo "<h2>DB: broken - Packages: $nb</h2>"
331 echo '<pre>'
332 cat $CACHE/$file | sort | \
333 sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g
334 echo '</pre>' ;;
336 *.diff)
337 diff=$CACHE/$file
338 echo "<h2>Diff for: ${file%.diff}</h2>"
339 [ "$file" == "installed.diff" ] && echo \
340 "<p>This is the latest diff between installed packages \
341 and installed build dependencies to cook.</p>"
342 echo '<pre>'
343 cat $diff | syntax_highlighter diff
344 echo '</pre>' ;;
346 *.log)
347 log=$LOGS/$file
348 name=$(basename $log)
349 echo "<h2>Log for: ${name%.log}</h2>"
350 if [ -f "$log" ]; then
351 if fgrep -q "Summary" $log; then
352 echo '<pre>'
353 grep -A 20 "^Summary" $log | sed /^$/d | \
354 syntax_highlighter log
355 echo '</pre>'
356 fi
357 echo '<pre>'
358 cat $log | syntax_highlighter log
359 echo '</pre>'
360 else
361 echo "<pre>No log file: $log</pre>"
362 fi ;;
363 esac ;;
365 stuff=*)
366 file=${QUERY_STRING#stuff=}
367 echo "<h2>$file</h2>"
368 echo '<pre>'
369 cat $wok/$file | sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
370 echo '</pre>' ;;
372 receipt=*)
373 pkg=${QUERY_STRING#receipt=}
374 echo "<h2>Receipt for: $pkg</h2>"
375 if [ -f "$wok/$pkg/receipt" ]; then
376 ( cd $wok/$pkg ; find stuff -type f 2> /dev/null ) | \
377 while read file ; do
378 echo "<a href=\"?stuff=$pkg/$file\">$file</a>"
379 done
380 echo '<pre>'
381 cat $wok/$pkg/receipt | \
382 syntax_highlighter receipt
383 echo '</pre>'
384 else
385 echo "<pre>No receipt for: $pkg</pre>"
386 fi ;;
388 files=*)
389 pkg=${QUERY_STRING#files=}
390 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
391 if [ -d "$dir/fs" ]; then
392 echo "<h2>Installed files by: $pkg ($(du -hs $dir/fs | awk '{ print $1 }'))</h2>"
393 echo '<pre>'
394 find $dir/fs -not -type d -print0 | xargs -0 ls -ld | \
395 sed "s|\(.*\) /.*\(${dir#*wok}/fs\)\(.*\)|\1 <a href=\"?download=../wok\2\3\">\3</a>|;s|^\([^-].*\)\(<a.*\)\">\(.*\)</a>|\1\3|"
396 echo '</pre>'
397 else
398 echo "<pre>No files list for: $pkg</pre>"
399 fi ;;
401 description=*)
402 pkg=${QUERY_STRING#description=}
403 echo "<h2>Description of $pkg</h2>"
404 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
405 if [ -s "$dir/description.txt" ]; then
406 echo '<pre>'
407 cat $dir/description.txt | \
408 sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
409 echo '</pre>'
410 else
411 echo "<pre>No description for: $pkg</pre>"
412 fi ;;
414 *)
415 # We may have a toolchain.cgi script for cross cooker's
416 if [ -f "toolchain.cgi" ]; then
417 toolchain='toolchain.cgi'
418 else
419 toolchain='cooker.cgi?pkg=slitaz-toolchain'
420 fi
421 # Main page with summary. Count only package include in ARCH,
422 # use 'cooker arch-db' to manually create arch.$ARCH files.
423 inwok=$(ls $WOK/*/arch.$ARCH | wc -l)
424 cooked=$(ls $PKGS/*.tazpkg | wc -l)
425 unbuilt=$(($inwok - $cooked))
426 pct=0
427 [ $inwok -gt 0 ] && pct=$(( ($cooked * 100) / $inwok ))
428 cat <<EOT
429 <div style="float: right;">
430 <form method="get" action="$SCRIPT_NAME">
431 Package:
432 <input type="text" name="pkg" />
433 </form>
434 </div>
436 <h2>Summary</h2>
438 <pre>
439 Running command : $(running_command)
440 Wok revision : <a href="$WOK_URL">$(cat $wokrev)</a>
441 Commits to cook : $(cat $commits | wc -l)
442 Current cooklist : $(cat $cooklist | wc -l)
443 Broken packages : $(cat $broken | wc -l)
444 Blocked packages : $(cat $blocked | wc -l)
445 </pre>
446 EOT
447 [ -e $CACHE/cooker-request ] &&
448 [ $CACHE/activity -nt $CACHE/cooker-request ] && cat <<EOT
449 <div style="float: right;">
450 <a class="button" href="cooker.cgi?poke">Poke cooker</a>
451 </div>
452 EOT
453 cat <<EOT
454 <p class="info">
455 Packages: $inwok in the wok | $cooked cooked | $unbuilt unbuilt |
456 Server date: $(date -u '+%F %R %Z')
457 </p>
458 <div class="pctbar">
459 <div class="pct" style="width: ${pct}%;">${pct}%</div>
460 </div>
462 <p>
463 Latest:
464 <a href="cooker.cgi?file=cookorder.log">cookorder.log</a>
465 <a href="cooker.cgi?file=commits.log">commits.log</a>
466 <a href="cooker.cgi?file=pkgdb.log">pkgdb.log</a>
467 <a href="cooker.cgi?file=installed.diff">installed.diff</a>
468 - Architecture $ARCH:
469 <a href="$toolchain">toolchain</a>
470 </p>
472 $(more_button activity "More activity" $CACHE/activity 12)
473 <h2 id="activity">Activity</h2>
474 <pre>
475 $(tac $CACHE/activity | head -n 12 | syntax_highlighter activity)
476 </pre>
477 EOT
479 [ -s $cooknotes ] && cat <<EOT
480 $(more_button cooknotes "More notes" $cooknotes 12)
481 <h2 id="cooknotes">Cooknotes</h2>
482 <pre>
483 $(tac $cooknotes | head -n 12 | syntax_highlighter activity)
484 </pre>
485 EOT
487 [ -s $commits ] && cat <<EOT
488 <h2 id="commits">Commits</h2>
489 <pre>
490 $(cat $commits)
491 </pre>
492 EOT
494 [ -s $cooklist ] && cat <<EOT
495 $(more_button cooklist "Full cooklist" $cooklist 20)
496 <h2 id="cooklist">Cooklist</h2>
497 <pre>
498 $(cat $cooklist | head -n 20)
499 </pre>
500 EOT
502 [ -s $broken ] && cat <<EOT
503 $(more_button broken "All broken packages" $broken 20)
504 <h2 id="broken">Broken</h2>
505 <pre>
506 $(cat $broken | head -n 20 | sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g)
507 </pre>
508 EOT
510 [ -s $blocked ] && cat <<EOT
511 <h2 id="blocked">Blocked</h2>
512 <pre>
513 $(cat $blocked | sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g)
514 </pre>
515 EOT
517 cat <<EOT
518 <h2 id="lastcook">Latest cook</h2>
519 <pre>
520 $(list_packages | sed s"#^\([^']*\).* : #<span class='log-date'>\0</span>#"g)
521 </pre>
522 EOT
523 ;;
524 esac
527 # Close xHTML page
529 cat <<EOT
530 </div>
532 <div id="footer">
533 <a href="http://www.slitaz.org/">SliTaz Website</a>
534 <a href="cooker.cgi">Cooker</a>
535 <a href="http://hg.slitaz.org/cookutils/raw-file/tip/doc/cookutils.en.html">
536 Documentation</a>
537 </div>
539 </body>
540 </html>
541 EOT
543 exit 0