cookutils view lighttpd/index.cgi @ rev 900

Tiny edits
author Paul Issott <paul@slitaz.org>
date Thu May 11 20:04:44 2017 +0100 (2017-05-11)
parents 2aaf73fe2cd4
children b03ac1e1213d
line source
1 #!/bin/sh
2 #
3 # SliTaz Cooker CGI + Lighttpd web interface.
4 #
6 # Make request URI relative to the script name
7 base="$(dirname "$SCRIPT_NAME")"; [ "$base" == '/' ] && base=''
8 REQUEST_URI=$(echo "$REQUEST_URI" | sed "s|^$base/*|/|; s|\?.*||")
10 # Split the URI request to /pkg/cmd/arg
11 export pkg=$(echo "$REQUEST_URI" | cut -d/ -f2)
12 export cmd=$(echo "$REQUEST_URI" | cut -d/ -f3)
13 export arg=$(echo "$REQUEST_URI" | sed 's|^/[^/]*/[^/]*/||')
16 . /usr/lib/slitaz/httphelper.sh
18 [ -f "/etc/slitaz/cook.conf" ] && . /etc/slitaz/cook.conf
19 [ -f "./cook.conf" ] && . ./cook.conf
20 wok="$WOK"
21 title=${title:-SliTaz Cooker}
22 # Cooker DB files.
23 activity="$CACHE/activity"
24 commits="$CACHE/commits"
25 cooklist="$CACHE/cooklist"
26 cookorder="$CACHE/cookorder"
27 command="$CACHE/command"; touch $command
28 blocked="$CACHE/blocked"
29 broken="$CACHE/broken"
30 cooknotes="$CACHE/cooknotes"
31 cooktime="$CACHE/cooktime"
32 wokrev="$CACHE/wokrev"
34 # Path to markdown to html convertor
35 cmark_opts='--smart -e table -e strikethrough -e autolink -e tagfilter'
36 if [ -n "$(which cmark 2>/dev/null)" ]; then
37 md2html="$(which cmark) $cmark_opts"
38 elif [ -x "./cmark" ]; then
39 md2html="./cmark $cmark_opts"
40 elif [ -n "$(which sundown 2>/dev/null)" ]; then
41 md2html=$(which sundown)
42 elif [ -x "./sundown" ]; then
43 md2html="./sundown"
44 fi
49 # Search form redirection
50 if [ -n "$(GET search)" ]; then
51 echo -e "HTTP/1.1 301 Moved Permanently\nLocation: $base/$(GET q)\n\n"
52 exit 0
53 fi
56 # Show the running command and it's progression
58 running_command() {
59 state="$(cat $command)"
60 local pct
61 if [ -n "$state" ];then
62 echo -n "$state</td></tr><tr><td>Completion</td>"
63 set -- $(grep "^$state" $cooktime)
64 [ -n "$1" -a $2 -ne 0 ] && pct=$((($(date +%s)-$3)*100/$2))
65 [ -n "$pct" ] && max="max='100'"
66 echo -n "<td><progress id='gauge' $max value='$pct' title='Click to stop updating' onclick='stopUpdating()'>"
67 echo -n "</progress> <span id='pct'>${pct:-?}%</span>"
68 [ "$2" -gt 60 ] &&
69 echo -n "</td></tr><tr><td>Estimated end time</td><td>$(date +%H:%M -ud @$(($2+$3)))"
70 else
71 echo 'not running'
72 fi
73 }
76 # HTML page header
78 page_header() {
79 local theme t='' css
80 theme=$(COOKIE theme)
81 [ "$theme" == 'default' ] && theme=''
82 [ -n "$theme" ] && theme="-$theme"
83 css="cooker$theme.css"
85 echo -e 'Content-Type: text/html; charset=UTF-8\n'
87 cat <<EOT
88 <!DOCTYPE html>
89 <html lang="en">
90 <head>
91 <title>$([ -n "$pkg" ] && echo "$pkg - ")$title</title>
92 <link rel="stylesheet" href="/$css">
93 <link rel="icon" type="image/png" href="/slitaz-cooker.png">
94 <!-- mobile -->
95 <meta name="viewport" content="width=device-width, initial-scale=1.0">
96 <meta name="theme-color" content="#222">
97 <!-- rss -->
98 <link rel="alternate" type="application/rss+xml" title="$title Feed" href="?rss">
99 </head>
100 <body>
101 <div id="container">
102 <header>
103 <h1><a href="$base/">$title</a></h1>
104 <div class="network">
105 <a href="http://www.slitaz.org/">Home</a>
106 <a href="http://bugs.slitaz.org/">Bugs</a>
107 <a href="http://hg.slitaz.org/wok-next/">Hg</a>
108 <a href="http://roadmap.slitaz.org/">Roadmap</a>
109 <a href="http://pizza.slitaz.me/">Pizza</a>
110 <a href="http://tank.slitaz.org/">Tank</a>
111 |
112 <a href="$base/cross/">Cross</a>
113 <a href="$base/i486.cgi">i486</a>
114 <a href="$base/cookiso.cgi">ISO</a>
115 <select onChange="window.location.href=this.value" style="display: none">
116 <option value=".">Go to…</option>
117 <option value="http://www.slitaz.org/">Home</option>
118 <option value="http://bugs.slitaz.org/">Bug tracker</option>
119 <option value="http://hg.slitaz.org/wok/">Hg wok</option>
120 <option value="http://roadmap.slitaz.org/">Roadmap</option>
121 <option value="http://pizza.slitaz.me/">Pizza</option>
122 <option value="http://tank.slitaz.org/">Tank</option>
123 <option disabled>---------</option>
124 <option value="cross/">Cross</option>
125 <option value="i486.cgi">i486</option>
126 <option value="cookiso.cgi">ISO</option>
127 </select>
128 </div>
129 </header>
131 <main>
132 EOT
134 [ -n "$(GET debug)" ] && echo "<pre><code class='language-ini'>$(env | sort)</code></pre>"
135 }
138 # HTML page footer
140 page_footer() {
141 date_now=$(date +%s)
142 sec_now=$(date +%S); sec_now=${sec_now#0} # remove one leading zero
143 wait_sec=$(( 60 - $sec_now ))
144 cat <<EOT
145 </main>
147 <footer>
148 <a href="http://www.slitaz.org/">SliTaz Website</a>
149 <a href="$base/">Cooker</a>
150 <a href="$base/doc/cookutils/cookutils.html">Documentation</a>
151 <a href="$base/?theme">Theme</a>
152 </footer>
153 </div>
154 <script src="/cooker.js"></script>
155 <script>refreshDate(${wait_sec}000, ${date_now}000)</script>
156 </body>
157 </html>
158 EOT
159 }
162 show_note() {
163 echo "<div class='bigicon-$1'>$2</div>"
164 }
167 not_found() {
168 local file="${1#$PKGS/}"; file="${file#$LOGS/}"; file="${file#$WOK/}"
169 echo "HTTP/1.1 404 Not Found"
170 page_header
171 echo "<h2>Not Found</h2>"
172 case $2 in
173 pkg)
174 show_note e "The requested package “$(basename "$(dirname "$file")")” was not found." ;;
175 *)
176 show_note e "The requested file “$file” was not found." ;;
177 esac
178 page_footer
179 }
182 manage_modified() {
183 local file="$1" option="$2" nul day mon year time hh mm ss date_s
184 if [ ! -f "$file" ]; then
185 if [ "$option" == 'silently-absent' ]; then
186 echo "HTTP/1.1 404 Not Found"
187 return
188 else
189 not_found "$file" "$2"
190 exit
191 fi
192 fi
193 [ "$option" == 'no-last-modified' ] && return
194 if [ -n "$HTTP_IF_MODIFIED_SINCE" ]; then
195 echo "$HTTP_IF_MODIFIED_SINCE" | \
196 while read nul day mon year time nul; do
197 case $mon in
198 Jan) mon='01';; Feb) mon='02';; Mar) mon='03';; Apr) mon='04';;
199 May) mon='05';; Jun) mon='06';; Jul) mon='07';; Aug) mon='08';;
200 Sep) mon='09';; Oct) mon='10';; Nov) mon='11';; Dec) mon='12';;
201 esac
202 hh=$(echo $time | cut -d: -f1)
203 mm=$(echo $time | cut -d: -f2)
204 ss=$(echo $time | cut -d: -f3)
205 date_s=$(date -ud "$year$mon$day$hh$mm.$ss" +%s)
206 # if [ "$date_s" -ge "$(date -ur "$file" +%s)" ]; then
207 # echo -e 'HTTP/1.1 304 Not Modified\n'
208 # exit
209 # fi
210 # TODO: improve caching control
211 done
212 fi
213 echo "Last-Modified: $(date -Rur "$file" | sed 's|UTC|GMT|')"
214 echo "Cache-Control: public, max-age=3600"
215 }
218 # Query '?pct=<package>': update percentage
220 if [ -n "$(GET pct)" ]; then
221 pkg="$(GET pct)"
222 state="$(cat $command)"
223 if [ "$state" == "cook:$pkg" ]; then
224 set -- $(grep "^$state" $cooktime)
225 [ -n "$1" ] && pct=$(( ($(date +%s) - $3) * 100 / $2 ))
226 echo "${pct:-?}"
227 else
228 echo 'reload'
229 fi
230 exit 0
231 fi
234 # Query '?poke': poke cooker
236 if [ -n "$(GET poke)" ]; then
237 touch $CACHE/cooker-request
238 echo -e "Location: ${HTTP_REFERER:-${REQUEST_URI%\?*}}\n"
239 exit
240 fi
243 # Query '?recook=<package>': query to recook package
245 if [ -n "$(GET recook)" ]; then
246 pkg="$(GET recook)"
247 case "$HTTP_USER_AGENT" in
248 *SliTaz*)
249 grep -qs "^$pkg$" $CACHE/recook-packages ||
250 echo "$pkg" >> $CACHE/recook-packages
251 esac
252 echo -e "Location: ${HTTP_REFERER:-${REQUEST_URI%\?*}}\n"
253 exit
254 fi
257 # Query '/i/<log>/<pkg>': show indicator icon
258 # Can't use ?query - not able to change '+' to '%2B' in the sed rules (see log handler)
260 if [ "$pkg" == 'i' ]; then
261 echo -en "Content-Type: image/svg+xml\n\n<svg xmlns='http://www.w3.org/2000/svg' height='12' width='8'><path d='"
262 if [ $LOGS/$cmd -nt $PKGS/$arg.tazpkg ]; then
263 echo "m1 2-1 1v8l1 1h6l1-1v-8l-1-1z' fill='#090'/></svg>"
264 else
265 echo "m0 3v8l1 1h6l1-1v-8l-1-1h-6zm3 0h2v5h-2zm0 6h2v2h-2z' fill='#d00'/></svg>"
266 fi
267 exit
268 fi
271 # Query '?theme[=<theme>]': change UI theme
273 if [ -n "$(GET theme)" ]; then
274 theme="$(GET theme)"
275 case $theme in
276 theme)
277 current=$(COOKIE theme)
278 page_header
279 cat <<EOT
280 <section>
281 <h2>Change theme</h2>
282 <p>Current theme: “${current:-default}”. Select other:</p>
283 <ul>
284 $(
285 for i in default emerald sky goldenrod like2016 terminal; do
286 [ "$i" == "${current:-default}" ] || echo "<li><a href="$base/?theme=$i">$i</a></li>"
287 done
288 )
289 </ul>
290 </section>
291 EOT
292 page_footer
293 exit 0
294 ;;
295 default|emerald|sky|goldenrod|like2016|terminal)
296 # Expires in a year
297 expires=$(date -uRd @$(($(date +%s)+31536000)) | sed 's|UTC|GMT|')
298 echo -e "HTTP/1.1 302 Found\nLocation: $base/\nCache-Control: no-cache\nSet-Cookie: theme=$theme; expires=$expires\n\n"
299 exit 0
300 ;;
301 esac
302 fi
305 #case "$QUERY_STRING" in
306 # stuff*)
307 # file="$wok/$(GET stuff)"
308 # manage_modified "$file"
309 # ;;
310 #
311 # pkg=*|receipt=*|description=*|files=*|log=*|man=*|doc=*|info=*)
312 # type=${QUERY_STRING%%=*}
313 # pkg=$(GET $type)
314 # case "$type" in
315 # description)
316 # manage_modified "$wok/$pkg/receipt" 'no-last-modified'
317 # manage_modified "$wok/$pkg/description.txt" 'silently-absent'
318 # ;;
319 # log)
320 # manage_modified "$wok/${pkg%%.log*}/receipt" 'no-last-modified'
321 # manage_modified "$LOGS/$pkg"
322 # ;;
323 # *)
324 # manage_modified "$wok/$pkg/receipt" pkg
325 # ;;
326 # esac
327 # ;;
328 #esac
331 # RSS feed generator
332 # URI: ?rss[&limit={1..100}]
334 if [ -n "$(GET rss)" ]; then
335 limit=$(GET limit); limit="${limit:-12}"; [ "$limit" -gt 100 ] && limit='100'
336 pubdate=$(date -Rur$(ls -t $FEEDS/*.xml | head -n1) | sed 's|UTC|GMT|')
337 cooker_url="http://$HTTP_HOST$base/"
338 cat <<EOT
339 Content-Type: application/rss+xml
341 <?xml version="1.0" encoding="utf-8" ?>
342 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
343 <channel>
344 <title>$title</title>
345 <description>The SliTaz packages cooker feed</description>
346 <link>$cooker_url</link>
347 <lastBuildDate>$pubdate</lastBuildDate>
348 <pubDate>$pubdate</pubDate>
349 <atom:link href="$cooker_url?rss" rel="self" type="application/rss+xml" />
350 EOT
351 for rss in $(ls -t $FEEDS/*.xml | head -n$limit); do
352 sed "s|http[^=]*=|$cooker_url|; s|<guid|& isPermaLink=\"false\"|g; s|</pubDate| GMT&|g" $rss
353 done
354 cat <<EOT
355 </channel>
356 </rss>
357 EOT
358 exit 0
359 fi
364 #
365 # Functions
366 #
369 # Unpack to stdout
371 docat() {
372 case "$1" in
373 *gz) zcat ;;
374 *bz2) bzcat ;;
375 *xz) xzcat ;;
376 *) cat
377 esac < $1
378 }
381 # Tiny texinfo converter
383 info2html() {
384 sed \
385 -e 's|&|\&amp;|g; s|<|\&lt;|g; s|>|\&gt;|g' \
386 -e 's|^\* \(.*\)::|* <a href="#\1">\1</a> |' \
387 -e 's|\*note \(.*\)::|<a href="#\1">\1</a>|' \
388 -e '/^File: / s|(dir)|Top|g' \
389 -e '/^File: / s|Next: \([^,]*\)|<a class="button" href="#\1">Next: \1</a>|' \
390 -e '/^File: / s|Prev: \([^,]*\)|<a class="button" href="#\1">Prev: \1</a>|' \
391 -e '/^File: / s|Up: \([^,]*\)|<a class="button" href="#\1">Up: \1</a>|' \
392 -e '/^File: / s|^.* Node: \([^,]*\), *\(.*\)$|<pre id="\1">\2|' \
393 -e '/^<pre id=/ s|^\([^>]*>\)\(<a[^>]*>Next: [^,]*\), *\(<a[^>]*>Prev: [^,]*\), *\(<a[^>]*>Up: .*\)|\1 \3 \4 \2|' \
394 -e '/^Tag Table:$/,/^End Tag Table$/d' \
395 -e '/INFO-DIR/,/^END-INFO-DIR/d' \
396 -e "s|https*://[^>),'\"\`’ ]*|<a href=\"&\">&</a>|g" \
397 -e "s|ftp://[^>),\"\` ]*|<a href=\"&\">&</a>|g" \
398 -e 's|^\* Menu:|<b>Menu:</b>|' \
399 -e "s|^|</pre>|"
400 }
403 # Put some colors into log and DB files.
405 syntax_highlighter() {
406 case $1 in
407 log)
408 # If variables not defined - define them with some rare values
409 : ${_src=#_#_#}
410 : ${_install=#_#_#}
411 : ${_fs=#_#_#}
412 : ${_stuff=#_#_#}
413 # Use one-letter html tags to save some bytes :)
414 # <b>is error (red)</b> <u>is warning (orange)</u> <i>is informal (green)</i>
415 sed -e 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g' \
416 -e 's#OK$#<i>OK</i>#' \
417 -e 's#\([Dd]one\)$#<i>\1</i>#' \
418 -e 's#Success$#<i>Success</i>#' \
419 -e 's#\([^a-z]\)ok$#\1<i>ok</i>#' \
420 -e 's#\([^a-z]\)yes$#\1<i>yes</i>#' \
421 -e 's#\([^a-z]\)no$#\1<u>no</u>#' \
422 -e 's#\([^a-z]\)none$#\1<u>none</u>#' \
423 -e 's#\([^a-z]\)false$#\1<u>false</u>#' \
424 -e 's#\(^checking .*\.\.\. \)\(.*\)$#\1<i>\2</i>#' \
425 \
426 -e 's#\( \[Y[nm/]\?\] n\)$# <u>\1</u>#' \
427 -e 's#\( \[N[ym/]\?\] y\)$# <i>\1</i>#' \
428 -e 's# y$# <i>y</i>#' \
429 -e 's# n$# <u>n</u>#' \
430 -e 's#(NEW) *$#<b>(NEW)</b>#' \
431 \
432 -e 's#.*(pkg/local).*#<i>\0</i>#' \
433 -e 's#.*(web/cache).*#<u>\0</u>#' \
434 \
435 -e 's#\([^a-zA-Z]\)\([Ee]rror\)$#\1<b>\2</b>#' \
436 -e 's#ERROR:#<b>ERROR:</b>#g' \
437 \
438 -e 's#^.*[Ff]ailed.*#<b>\0</b>#' \
439 -e 's#^.*[Ff]atal.*#<b>\0</b>#' \
440 -e 's#^.*[Nn]ot found.*#<b>\0</b>#' \
441 -e 's#^.*[Nn]o such file.*#<b>\0</b>#' \
442 -e 's#^.*No package .* found.*#<b>\0</b>#' \
443 -e 's#^.*Unable to find.*#<b>\0</b>#' \
444 -e 's#^.*[Ii]nvalid.*#<b>\0</b>#' \
445 -e 's#\([Nn][Oo][Tt] found\)$#<b>\1</b>#' \
446 -e 's#\(found\)$#<i>\1</i>#' \
447 \
448 -e 's#^.*WARNING:.*#<u>\0</u>#' \
449 -e 's#^.*warning:.*#<u>\0</u>#' \
450 -e 's#^.* [Ee]rror:* .*#<b>\0</b>#' \
451 -e 's#^.*terminated.*#<b>\0</b>#' \
452 -e 's#\(missing\)#<b>\1</b>#g' \
453 -e 's#^.*[Cc]annot find.*#<b>\0</b>#' \
454 -e 's#^.*unrecognized options.*#<u>\0</u>#' \
455 -e 's#^.*does not.*#<u>\0</u>#' \
456 -e 's#^.*[Ii]gnoring.*#<u>\0</u>#' \
457 -e 's#^.*note:.*#<u>\0</u>#' \
458 \
459 -e 's#^.* will not .*#<u>\0</u>#' \
460 -e 's!^Hunk .* succeeded at .*!<u>\0</u>!' \
461 -e 's#^.* Warning: .*#<u>\0</u>#' \
462 \
463 -e "s#^Executing:\([^']*\).#<em>\0</em>#" \
464 -e "s#^Making.*#<em>\0</em>#" \
465 -e "s#^Scanning dependencies of target .*#<em>\0</em>#" \
466 -e "s#^====\([^']*\).#<span class='span-line'>\0</span>#g" \
467 -e "s#^[a-zA-Z0-9]\([^']*\) :: #<span class='span-sky'>\0</span>#g" \
468 -e "s#[fh]tt*ps*://[^ '\"]*#<a href='\0'>\0</a>#g" \
469 \
470 -e "s|$_src|<span class='var'>\${src}</span>|g;
471 s|$_install|<span class='var'>\${install}</span>|g;
472 s|$_fs|<span class='var'>\${fs}</span>|g;
473 s|$_stuff|<span class='var'>\${stuff}</span>|g" \
474 -e "s|\[9\([1-6]\)m|<span class='c\1'>|;
475 s|\[39m|</span>|;"
476 ;;
478 files)
479 # Highlight the Busybox's `ls` output
480 awk '{
481 part1 = substr($0, 0, 16);
482 part2 = substr($0, 17, 9);
483 part3 = substr($0, 26, 9);
484 part4 = substr($0, 35);
485 if (part2 != "root ") part2 = "<span class=\"c11\">" part2 "</span>";
486 if (part3 != "root ") part3 = "<span class=\"c11\">" part3 "</span>";
487 print part1 part2 part3 part4;
488 }' | \
489 sed "s|\[\([01]\);3\([1-7]\)m|<a class='c\2\1'>|g;
490 s|\[\([01]\);0m|<a class='c0\1'>|g;
491 s|\[0m|</a>|g;
492 s|^\(lrwxrwxrwx\)|<span class='c61'>\1</span>|;
493 s|^\(-rwxr-xr-x\)|<span class='c21'>\1</span>|;
494 s|^\(-rw-r--r--\)|<span class='c31'>\1</span>|;
495 s|^\([lrwx-]*\)|<span class='c11'>\1</span>|;
496 "
497 ;;
498 esac
499 }
502 show_code() {
503 echo -n "<pre><code class=\"language-$1\">"
504 sed 's|&|\&amp;|g; s|<|\&lt;|g; s|>|\&gt;|g'
505 echo '</code></pre>'
506 }
509 datalist() {
510 (
511 cd $wok
513 ls | awk '
514 BEGIN{printf("<datalist id=\"packages\">")}
515 {printf("<option>%s</option>",$1)}
516 END {printf("</datalist>")}
517 '
518 )
519 }
522 mklog() {
523 awk '
524 BEGIN { printf("<pre class=\"log dog\">\n") }
525 { print }
526 END { print "</pre>" }'
527 }
530 summary() {
531 log="$1"
532 pkg="$(basename ${log%%.log*})"
534 if [ -f "$log" ]; then
535 if grep -q "cook:$pkg$" $command; then
536 show_note i "The Cooker is currently building $pkg"
537 elif fgrep -q "Summary for:" $log; then
538 sed '/^Summary for:/,$!d' $log | head -n13 | awk '
539 BEGIN { print "<section>" }
540 {
541 if (NR==1) {
542 printf("<h3>%s</h3>\n<table>\n", $0)
543 } else if ($0 !~ "===") {
544 split($0, s, " : ");
545 printf("<tr><td>%s</td><td>%s</td></tr>", s[1], s[2]);
546 }
547 }
548 END { print "</table></section>" }
549 '
550 elif fgrep -q "Debug information" $log; then
551 echo -e '<section>\n<h3>Debug information</h3>'
552 sed -e '/^Debug information/,$!d; /^===/d; /^$/d' $log | sed -n '1!p' | \
553 if [ -n "$2" ]; then
554 syntax_highlighter log | sed 's|\([0-9][0-9]*\):|<a href="#l\1">\1</a>:|'
555 else
556 sed 's|^[0-9][0-9]*:||' | syntax_highlighter log
557 fi | mklog
558 echo '</section>'
559 fi
560 else
561 [ -n "$pkg" -a -d "$wok/$pkg" ] && show_note e "No log for $pkg"
562 fi
563 }
566 active() {
567 [ "$cmd" == "$1" -o "$cmd" == "${2:-$1}" ] && echo -n ' active'
568 }
571 pkg_info() {
572 local log active bpkg
573 log="$LOGS/$pkg.log"
575 echo "<h2>Package “$pkg”</h2>"
576 echo '<div id="info">'
577 echo "<a class='button icon receipt$(active receipt stuff)' href='$base/$pkg/receipt'>receipt &amp; stuff</a>"
579 unset WEB_SITE WANTED
580 . $wok/$pkg/receipt
582 [ -n "$WEB_SITE" ] &&
583 echo "<a class='button icon website' href='$WEB_SITE' target='_blank' rel='noopener noreferrer'>web site</a>"
585 if [ -f "$wok/$pkg/taz/$PACKAGE-$VERSION/receipt" ]; then
586 echo "<a class='button icon files$(active files)' href='$base/$pkg/files'>files</a>"
588 [ -f "$wok/$pkg/description.txt" ] &&
589 echo "<a class='button icon desc$(active description)' href='$base/$pkg/description'>description</a>"
591 unset EXTRAVERSION
592 . $wok/$pkg/taz/$PACKAGE-$VERSION/receipt
593 for filename in "$PACKAGE-$VERSION$EXTRAVERSION.tazpkg" "$PACKAGE-$VERSION$EXTRAVERSION-$ARCH.tazpkg"; do
594 [ -f "$PKGS/$filename" ] &&
595 echo "<a class='button icon download' href='$base/get/$filename'>download</a>"
596 done
598 fi
600 [ -n "$TARBALL" -a -s "$SRC/$TARBALL" ] &&
601 echo "<a class='button icon source' href='$base/src/$TARBALL'>source</a>"
603 echo "<a class='button icon browse' href='$base/$pkg/browse/'>browse</a>"
605 [ -x ./man2html -a -d "$wok/$pkg/install/usr/share/man" ] &&
606 echo "<a class='button icon doc$(active man)' href='$base/$pkg/man/'>man</a>"
608 [ -d "$wok/$pkg/install/usr/share/doc" -o -d "$wok/$pkg/install/usr/share/gtk-doc" ] &&
609 echo "<a class='button icon doc$(active doc)' href='$base/$pkg/doc/'>doc</a>"
611 [ -d "$wok/$pkg/install/usr/share/info" ] &&
612 echo "<a class='button icon doc$(active info)' href='$base/$pkg/info/#Top'>info</a>"
614 [ -s "$log" ] &&
615 echo "<a class='button icon log$(active log)' href='$base/$pkg/log/'>logs</a>"
617 echo '</div>'
618 }
621 mktable() {
622 sed 's# : #|#' | awk -vc="$1" '
623 BEGIN { printf("<table class=\"%s\">\n", c); FS="|" }
624 { printf("<tr><td>%s</td>", $1);
625 if (NF == 2) printf("<td>%s</td>", $2);
626 printf("</tr>\n", $2) }
627 END { print "</table>" }'
628 }
631 section() {
632 local i=$(basename "$1")
633 echo -e '\n\n<section>'
634 [ $(wc -l < $1) -gt $2 ] && echo "<a class='button icon more r' href='?$i'>${3#*|}</a>"
635 echo "<h2>${3%|*}</h2>"
636 mktable "$i"
637 echo '</section>'
638 }
643 #
644 # Load requested page
645 #
647 if [ -z "$pkg" ]; then
649 page_header
650 if [ -n "$QUERY_STRING" -a "$QUERY_STRING" != 'debug' ]; then
652 for list in activity cooknotes cooklist; do
653 [ -n "$(GET $list)" ] || continue
654 [ "$list" == 'cooklist' ] && nb="- Packages: $(wc -l < $cooklist)"
655 echo '<section id="content2">'
656 echo "<h2>DB: $list $nb</h2>"
657 tac $CACHE/$list | sed 's|cooker.cgi?pkg=||;
658 s|\[ Done|<span class="r c20">Done|;
659 s|\[ Failed|<span class="r c10">Failed|;
660 s| \]|</span>|' | mktable $list
661 echo '</section>'
662 done
664 if [ -n "$(GET broken)" ]; then
665 echo '<div id="content2">'
666 echo "<h2>DB: broken - Packages: $(wc -l < $broken)</h2>"
667 sort $CACHE/broken | sed "s|^[^']*|<a href='$base/\0'>\0</a>|g" | mktable
668 echo '</div>'
669 fi
671 case "$QUERY_STRING" in
672 *.log)
673 log=$LOGS/$QUERY_STRING
674 name=$(basename $log)
675 if [ -f "$log" ]; then
676 echo "<h2>Log for: ${name%.log}</h2>"
677 if fgrep -q "Summary" $log; then
678 echo '<pre class="log">'
679 grep -A 20 '^Summary' $log | syntax_highlighter log
680 echo '</pre>'
681 fi
682 echo '<pre class="log">'
683 syntax_highlighter log < $log
684 echo '</pre>'
685 else
686 show_note e "No log file: $log"
687 fi
688 ;;
689 esac
690 page_footer
691 exit 0
692 fi
695 # We may have a toolchain.cgi script for cross cooker's
696 if [ -f "toolchain.cgi" ]; then
697 toolchain="toolchain.cgi"
698 else
699 toolchain="slitaz-toolchain/"
700 fi
701 # Main page with summary. Count only packages included in ARCH,
702 # use 'cooker arch-db' to manually create arch.$ARCH files.
703 inwok=$(ls $WOK/*/arch.$ARCH | wc -l)
704 cooked=$(ls $PKGS/*.tazpkg | wc -l)
705 unbuilt=$(($inwok - $cooked))
706 pct=0; [ $inwok -gt 0 ] && pct=$(( ($cooked * 100) / $inwok ))
707 cat <<EOT
708 <div id="content2">
710 <section>
711 <form method="get" action="" class="search r">
712 <input type="hidden" name="search" value="pkg"/>
713 <button type="submit" title="Search">Search</button>
714 <input type="search" name="q" placeholder="Package" list="packages" autocorrect="off" autocapitalize="off"/>
715 </form>
717 <h2>Summary</h2>
718 EOT
720 mktable <<EOT
721 Cooker state : $(running_command)
722 Wok revision : <a href='$WOK_URL' target='_blank' rel='noopener noreferrer'>$(cat $wokrev)</a>
723 Commits to cook : $(wc -l < $commits)
724 Current cooklist : $(wc -l < $cooklist)
725 Broken packages : $(wc -l < $broken)
726 Blocked packages : $(wc -l < $blocked)
727 Architecture : $ARCH, <a href="$toolchain">toolchain</a>
728 Server date : <span id='date'>$(date -u '+%F %R %Z')</span>
729 EOT
731 # If command is "cook:*", update gauge and percentage periodically.
732 # If different package is cooking, reload the page (with new settings)
733 cmd="$(cat $command)"
734 case "$cmd" in
735 cook:*)
736 pkg=${cmd#*:}
737 echo "<script>updatePkg = '${pkg//+/%2B}';</script>"
738 ;;
739 esac
741 if [ -e "$CACHE/cooker-request" -a ! -s $command ]; then
742 if [ "$activity" -nt "$CACHE/cooker-request" ]; then
743 echo '<a class="button icon bell r" href="?poke">Wake up</a>'
744 else
745 show_note i 'Cooker will be launched in the next 5 minutes.'
746 fi
747 fi
749 cat <<EOT
750 <p>Packages: $inwok in the wok · $cooked cooked · $unbuilt unbuilt</p>
752 <div class="meter"><progress max="100" value="$pct">${pct}%</progress><span>${pct}%</span></div>
754 <p>
755 Service logs:
756 <a href="?cookorder.log">cookorder</a> ·
757 <a href="?commits.log">commits</a> ·
758 <a href="?pkgdb.log">pkgdb</a>
759 </p>
760 </section>
761 EOT
763 tac $activity | head -n12 | sed 's|cooker.cgi?pkg=||;
764 s|\[ Done|<span class="r c20">Done|;
765 s|\[ Failed|<span class="r c10">Failed|;
766 s| \]|</span>|;
767 s|%2B|\+|g' | \
768 section $activity 12 "Activity|More activity"
770 [ -s "$cooknotes" ] && tac $cooknotes | head -n12 | \
771 section $cooknotes 12 "Cooknotes|More notes"
773 [ -s "$commits" ] &&
774 section $commits 20 "Commits|More commits" < $commits
776 [ -s "$cooklist" ] && head -n 20 $cooklist | \
777 section $cooklist 20 "Cooklist|Full cooklist"
779 [ -s "$broken" ] && head -n20 $broken | sed "s|^[^']*|<a href='\0'>\0</a>|g" | \
780 section $broken 20 "Broken|All broken packages"
782 [ -s "$blocked" ] && sed "s|^[^']*|<a href='\0'>\0</a>|g" $blocked | \
783 section $blocked 12 "Blocked|All blocked packages"
785 cd $PKGS
786 ls -let *.tazpkg | awk '
787 (NR<=20){
788 sub(/:[0-9][0-9]$/, "", $9);
789 mon = index(" JanFebMarAprMayJunJulAugSepOctNovDec", $7) / 3;
790 printf("%d-%02d-%02d %s : <a href=\"get/%s\">%s</a>\n", $10, mon, $8, $9, $11, $11);
791 }' | \
792 section $activity 1000 "Latest cook"
794 echo '</div>'
795 datalist
796 page_footer
797 exit 0
798 fi
801 case "$cmd" in
802 '')
803 page_header
804 log=$LOGS/$pkg.log
806 # Package info.
807 if [ -f "$wok/$pkg/receipt" ]; then
808 pkg_info
809 else
810 if [ $(ls $wok/*$pkg*/receipt 2>/dev/null | wc -l) -eq 0 ]; then
811 echo "<h2>Not Found</h2>"
812 show_note e "The requested package <b>$pkg</b> was not found on this server."
813 else
814 # Search page
815 echo "<section><h2>Package names matching “$pkg”</h2>"
816 echo "<table><thead><tr><th>Name</th><th>Description</th><th>Category</th></tr></thead><tbody>"
817 for i in $(cd $wok; ls *$pkg*/receipt); do
818 pkg=$(dirname $i)
819 unset SHORT_DESC CATEGORY
820 . $wok/$pkg/receipt
821 echo -n "<tr><td><a href="$base/$pkg">$pkg</a></td>"
822 echo -n "<td>$SHORT_DESC</td><td>$CATEGORY</td></tr>"
823 done
824 echo '</tbody></table></section>'
825 unset pkg
826 fi
827 page_footer
828 exit 0
829 fi
831 # Check for a log file and display summary if it exists.
832 summary "$log"
834 # Display <Recook> button only for SliTaz web browser
835 if [ -f "$log" ]; then
836 case "$HTTP_USER_AGENT" in
837 *SliTaz*)
838 if [ -f $CACHE/cooker-request -a -n "$HTTP_REFERER" ]; then
839 if grep -qs "^$pkg$" $CACHE/recook-packages; then
840 show_note i "The package “$pkg” has been requested for recook"
841 else
842 echo "<a class='button' href='$base/?recook=${pkg//+/%2B}'>Recook $pkg</a>"
843 fi
844 fi
845 ;;
846 esac
847 fi
848 ;;
850 receipt)
851 page_header
852 pkg_info
853 echo "<a class='button receipt' href='$base/$pkg/receipt'>receipt</a>"
854 ( cd $wok/$pkg; find stuff -type f 2>/dev/null ) | sort | \
855 awk -vb="$base/$pkg" '{printf("<a class=\"button\" href=\"%s/%s\">%s</a>\n", b, $0, $0)}'
857 show_code bash < $wok/$pkg/receipt
858 ;;
860 stuff)
861 page_header
862 pkg_info
863 file="$pkg/stuff/$arg"
864 echo "<a class='button' href='$base/$pkg/receipt'>receipt</a>"
865 ( cd $wok/$pkg; find stuff -type f 2>/dev/null ) | sort | \
866 awk -vb="$base/$pkg" -va="stuff/$arg" '{
867 printf("<a class=\"button%s\" href=\"%s/%s\">%s</a>\n", a==$0 ? " receipt" : "", b, $0, $0)
868 }'
870 if [ -f "$wok/$file" ]; then
871 case $file in
872 *.desktop|*.theme) class="ini" ;;
873 *.patch|*.diff|*.u) class="diff" ;;
874 *.sh) class="bash" ;;
875 *.conf*|*.ini)
876 class="bash"
877 [ -n "$(cut -c1 < $wok/$file | fgrep '[')" ] && class="ini"
878 ;;
879 *.pl) class="perl" ;;
880 *.c|*.h|*.awk) class="clike" ;;
881 *.svg) class="svg" ;;
882 *Makefile*) class="makefile" ;;
883 *.po|*.pot) class="bash" ;;
884 *.css) class="css" ;;
885 *.htm|*.html) class="html" ;;
886 *.js) class="js" ;;
887 *.txt) class="asciidoc" ;;
888 *)
889 case $(head -n1 $wok/$file) in
890 *!/bin/sh*|*!/bin/bash*) class="bash" ;;
891 esac
892 if [ -z "$class" -a "$(head -n1 $wok/$file | cut -b1)" == '#' ]; then
893 class="bash"
894 fi
895 if [ -z "$class" ]; then
896 # Follow Busybox restrictions. Search for non-printable chars
897 if [ $(tr -d '[:alnum:][:punct:][:blank:][:cntrl:]' < "$wok/$file" | wc -c) -gt 0 ]; then
898 raw="true"
899 fi
900 fi
901 ;;
902 esac
904 # Display image
905 case $file in
906 *.png|*.svg|*.jpg|*.jpeg|*.ico)
907 echo "<img src='$base/$pkg/browse/stuff/$arg' style='display: block; max-width: 100%; margin: auto'/>"
908 ;;
909 esac
911 # Display colored listing for all text-based documents (also for *.svg)
912 case $file in
913 *.png|*.jpg|*.jpeg|*.ico) ;;
914 *)
915 if [ -z "$raw" ]; then
916 cat $wok/$file | show_code $class
917 fi
918 ;;
919 esac
921 # Display hex dump for binary files
922 if [ -n "$raw" ]; then
923 hexdump -C $wok/$file | show_code #| sed 's|^\([0-9a-f][0-9a-f]*\)|<span class="c2">\1</span>|'
924 fi
925 else
926 show_note e "File “$file” absent!"
927 fi
928 ;;
930 files)
931 page_header
932 pkg_info
934 packaged=$(mktemp)
936 # find main package
937 wanted=$(. $wok/$pkg/receipt; echo $WANTED)
938 main=${wanted:-$pkg}
939 # identify split packages
940 split="$main $(. $wok/$main/receipt; echo $SPLIT)"
941 [ -d "$wok/$main-dev" ] && split="$split $main-dev"
942 split="$(echo $split | tr ' ' '\n' | sort -u)"
943 # finally we need the version
944 ver=$(. $wok/$main/receipt; echo $VERSION$EXTRAVERSION)
946 for p in $split; do
947 namever="$p-$ver"
948 if [ -d "$wok/$p/taz/$p-$ver" ]; then
949 indir=$p
950 elif [ -d "$wok/$main/taz/$p-$ver" ]; then
951 indir=$main
952 fi
953 dir="$wok/$indir/taz/$p-$ver/fs"
955 size=$(du -hs $dir | awk '{ sub(/\.0/, ""); print $1 }')
957 echo "<section><h3>Files of package “$namever” ($size):</h3>"
958 echo -en '<pre class="files">\n<span class="underline">permissions·lnk·user ·'
959 echo -en 'group · size·date &amp; time ·file name\n</span>'
960 find $dir -not -type d -print0 | sort -z | xargs -0 ls -ld --color=always | \
961 syntax_highlighter files | \
962 sed "s|\([^>]*\)>/.*/fs\([^<]*\)\(<.*\)$|\1 href='$base/$indir/browse/taz/$p-$ver/fs\2'>\2\3|" | \
963 awk 'BEGIN { FS="\""; }
964 { gsub("+", "%2B", $2); print; }'
965 echo '</pre></section>'
966 cat $wok/$indir/taz/$p-$ver/files.list >> $packaged
967 done
969 # find repeatedly packaged files
970 repeats="$(sort $packaged | uniq -d)"
971 if [ -n "$repeats" ]; then
972 echo -n '<section><h3>Repeatedly packaged files:</h3><pre class="files">'
973 echo "$repeats" | sed 's|^|<span class="c11">!!!</span> |'
974 echo "</pre></section>"
975 fi
977 # find unpackaged files
978 all_files=$(mktemp)
979 cd $wok/$main/install; find ! -type d | sed 's|\.||' > $all_files
980 orphans="$(sort $all_files $packaged | uniq -u)"
981 if [ -n "$orphans" ]; then
982 echo -n '<section><h3>Unpackaged files:</h3><pre class="files">'
983 echo "$orphans" | awk '
984 function tag(text, color) { printf("<span class=\"c%s1\">%s</span> %s\n", color, text, $0); }
985 /\/perllocal.pod$/ || /\/\.packlist$/ || /\/share\/bash-completion\// { tag("---", 0); next }
986 /\.pod$/ { tag("pod", 5); next }
987 /\/share\/man\// { tag("man", 5); next }
988 /\/share\/doc\// || /\/share\/gtk-doc\// || /\/share\/info\// || /\/share\/devhelp\// { tag("doc", 5); next }
989 /\/share\/icons\// { tag("ico", 2); next }
990 /\/share\/locale\// { tag("loc", 4); next }
991 /\.h$/ || /\.a$/ || /\.la$/ || /\.pc$/ || /\/bin\/.*-config$/ { tag("dev", 3); next }
992 { tag("???", 1) }
993 '
994 echo '</pre></section>'
995 fi
996 ;;
998 description)
999 page_header
1000 pkg_info
1001 desc="$WOK$REQUEST_URI.txt"
1002 if [ -s "$desc" ]; then
1003 echo '<div id="content2">'
1004 if [ -n "$md2html" ]; then
1005 $md2html $desc
1006 else
1007 show_code markdown < $desc
1008 fi
1009 echo '</div>'
1010 else
1011 show_note w "No description of $pkg"
1012 fi
1013 ;;
1015 log)
1016 page_header
1017 pkg_info
1018 [ -z "$arg" ] && arg=$(stat -c %Y $LOGS/$pkg.log)
1020 echo '<div class="btnList">'
1021 acc='l'
1022 while read log; do
1023 timestamp=$(stat -c %Y $log)
1024 class=''
1025 if [ "$arg" == "$timestamp" ]; then
1026 class=' log'
1027 logfile="$log"
1028 fi
1029 echo -n "<a class='button$class' data-acc='$acc' accesskey='$acc' href='$base/$pkg/log/$timestamp'>"
1030 echo "$(stat -c %y $log | cut -d: -f1,2)</a>"
1031 case $acc in
1032 l) acc=0;;
1033 *) acc=$((acc+1));;
1034 esac
1035 done <<EOT
1036 $(find $LOGS -name "$pkg.log*" | sort)
1037 EOT
1038 echo '</div>'
1040 if [ -z "$logfile" ]; then
1041 show_note e "Requested log is absent"
1042 page_footer
1043 exit 0
1044 fi
1046 # Define cook variables for syntax highlighter
1047 if [ -s "$WOK/$pkg/receipt" ]; then
1048 . "$WOK/$pkg/receipt"
1049 _wok='/home/slitaz/wok'
1050 _src="$_wok/$pkg/source/$PACKAGE-$VERSION"
1051 _install="$_wok/$pkg/install"
1052 _fs="$_wok/$pkg/taz/$PACKAGE-$VERSION/fs"
1053 _stuff="$_wok/$pkg/stuff"
1054 fi
1056 # if [ ! -f "gzlog/$pkg.$arg" ]; then
1057 # {
1058 # summary "$logfile" links
1060 # syntax_highlighter log < $logfile | awk '
1061 # BEGIN { print "<pre class=\"log\">"; }
1062 # { printf("<a name=\"l%d\" href=\"#l%d\">%5d</a> %s\n", NR, NR, NR, $0); }
1063 # END { print "</pre>"; }
1064 # '
1066 # page_footer
1067 # } | gzip > gzlog/$pkg.$arg
1068 # fi
1070 blog=$(basename $logfile)
1071 summary "$logfile" links
1073 # disable next `sed` for the 'like2016' theme
1074 theme=$(COOKIE theme); theme=${theme:-default}; [ "$theme" != 'like2016' ] && theme=''
1075 cat $logfile | syntax_highlighter log | \
1076 sed -e "/(pkg\/local$theme):/ s|: \([^<]*\)|<img src='$base/i/$blog/\1'> \1|" | \
1077 awk '
1078 BEGIN { print "<pre class=\"log\">"; }
1079 { printf("<a name=\"l%d\" href=\"#l%d\">%5d</a> %s\n", NR, NR, NR, $0); }
1080 END { print "</pre>"; }
1082 ;;
1085 man|doc|info)
1086 page_header
1087 pkg_info
1088 echo '<div style="max-height: 6.4em; overflow: auto; padding: 0 4px">'
1090 dir="wok/$pkg/install/usr/share/$cmd"
1091 [ "$cmd" == 'doc' ] && dir="$dir wok/$pkg/install/usr/share/gtk-doc"
1092 if [ "$cmd" == 'doc' -a -z "$arg" ]; then
1093 try=$(for i in $dir; do find $i -name 'index.htm*'; done | sed q)
1094 [ -n "$try" ] && arg="$try"
1095 fi
1096 while read i; do
1097 [ -s "$i" ] || continue
1098 case "$i" in
1099 *.jp*g|*.png|*.gif|*.svg|*.css) continue
1100 esac
1101 i=${i#$dir/}
1102 [ -n "$arg" ] || arg="$i"
1103 class=''; [ "$arg" == "$i" ] && class=" doc"
1104 case "$cmd" in
1105 man)
1106 case $i in
1107 man*) lang='';;
1108 *) lang="${i%%/*}: ";;
1109 esac
1110 man=$(basename $i .gz)
1111 echo "<a class='button$class' href='$base/$pkg/man/$i'>$lang${man%.*} (${man##*.})</a>"
1112 ;;
1113 doc)
1114 echo "<a class='button$class' href='$base/$pkg/doc/$i'>$(basename $i .gz)</a>"
1115 ;;
1116 info)
1117 info=$(basename $i)
1118 echo "<a class='button$class' href='$base/$pkg/info/$i#Top'>${info/.info/}</a>"
1119 ;;
1120 esac
1121 done <<EOT
1122 $(for i in $dir; do find $i -type f; done | sort)
1123 EOT
1124 echo '</div>'
1126 [ -f "$arg" ] || arg="$dir/$arg"
1127 if [ -f "$arg" ]; then
1128 tmp="$(mktemp)"
1129 docat "$arg" > $tmp
1130 [ -s "$tmp" ] &&
1131 case "$cmd" in
1132 info)
1133 echo '<div id="content2" class="texinfo"><pre class="first">'
1134 info2html < "$tmp"
1135 echo '</pre></div>'
1136 ;;
1137 doc)
1138 case "$arg" in
1139 *.sgml|*.devhelp2) class='xml';;
1140 *.py) class='python';; # pycurl package
1141 *.css) class='css';;
1142 *) class='asciidoc';;
1143 esac
1144 case "$arg" in
1145 *.htm*)
1146 case $arg in
1147 wok/*) page="${arg#wok/}"; page="$base/$pkg/browse/${page#*/}";;
1148 *) page="$base/$pkg/browse/install/usr/share/$cmd/$arg";;
1149 esac
1150 # make the iframe height so long to contain its content without scrollbar
1151 echo "<iframe id='idoc' src='$page' width='100%' onload='resizeIframe(this)'></iframe>"
1152 ;;
1153 *.pdf)
1154 case $arg in
1155 wok/*) page="${arg#wok/}"; page="$base/$pkg/browse/${page#*/}";;
1156 *) page="$base/$pkg/browse/install/usr/share/$cmd/$arg";;
1157 esac
1158 cat <<EOT
1159 <object id="idoc" data="$page" width="100%" height="100%" type="application/pdf" style="min-height: 600px">
1160 $(show_note w "Missing PDF plugin.<br/>Get the file <a href="$page">$(basename "$page")</a>.")
1161 </object>
1162 EOT
1163 ;;
1164 *)
1165 show_code $class < "$tmp"
1166 ;;
1167 esac
1168 ;;
1169 man)
1170 #export TEXTDOMAIN='man2html'
1171 echo "<div id='content2'>"
1173 html=$(./man2html "$tmp" | sed -e '1,/<header>/d' -e '/<footer>/,$d' \
1174 -e 's|<a href="file:///[^>]*>\([^<]*\)</a>|\1|g' \
1175 -e 's|<a href="?[1-9]\+[^>]*>\([^<]*\)</a>|\1|g')
1177 if [ -n "$(echo "$html" | fgrep 'The requested file /tmp/tmp.')" ]; then
1178 # Process the pre-formatted man-cat page
1179 echo '<pre>'
1180 sed '
1181 s|M-bM-^@M-^S|—|g;
1182 s|M-bM-^@M-^\\|<b>|g;
1183 s|M-bM-^@M-^]|</b>|g
1184 s|M-bM-^@M-^X|<u>|g;
1185 s|M-bM-^@M-^Y|</u>|g;
1186 s|M-BM-||g;
1187 ' "$tmp"
1188 echo '</pre>'
1189 else
1190 echo "$html"
1191 fi
1192 echo "</div>"
1193 ;;
1194 esac
1195 rm -f $tmp
1196 else
1197 show_note e "File “$arg” not exists!"
1198 fi
1199 ;;
1201 esac
1204 page_footer
1205 exit 0