cookutils view web/cooker.cgi @ rev 720

cooker.cgi: allow spaces filename for ?files=
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Apr 16 19:26:45 2015 +0200 (2015-04-16)
parents a7e3087f5ace
children 851a2b8075dc
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 wokrev="$CACHE/wokrev"
23 # We're not logged and want time zone to display correct server date.
24 export TZ=$(cat /etc/TZ)
26 if [ "${QUERY_STRING%%=*}" == "download" ]; then
27 file=$PKGS/${QUERY_STRING#*=}
28 cat <<EOT
29 Content-Type: application/octet-stream
30 Content-Length: $(stat -c %s $file)
31 Content-Disposition: attachment; filename=$(basename $file)
33 EOT
34 cat $file
35 exit
36 fi
38 echo -n "Content-Type: "
39 if [ "$QUERY_STRING" == "rss" ]; then
40 echo "application/rss+xml"
41 else
42 echo "text/html; charset=utf-8"
43 fi
44 echo ""
46 # RSS feed generator
47 if [ "$QUERY_STRING" == "rss" ]; then
48 pubdate=$(date -R)
49 cat << EOT
50 <?xml version="1.0" encoding="utf-8" ?>
51 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
52 <channel>
53 <title>SliTaz Cooker</title>
54 <description>The SliTaz packages cooker feed</description>
55 <link>$COOKER_URL</link>
56 <lastBuildDate>$pubdate</lastBuildDate>
57 <pubDate>$pubdate</pubDate>
58 <atom:link href="http://cook.slitaz.org/cooker.cgi?rss" rel="self" type="application/rss+xml" />
59 EOT
60 for rss in $(ls -lt $FEEDS/*.xml | head -n 12)
61 do
62 cat $rss | sed 's|<guid|& isPermaLink="false"|g;s|</pubDate| GMT&|g'
63 done
64 cat << EOT
65 </channel>
66 </rss>
67 EOT
68 exit 0
69 fi
71 #
72 # Functions
73 #
75 # Put some colors in log and DB files.
76 syntax_highlighter() {
77 case $1 in
78 log)
79 sed -e 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g' \
80 -e 's#OK$#<span class="span-ok">OK</span>#g' \
81 -e 's#Done$#<span class="span-ok">Done</span>#g' \
82 -e 's#yes$#<span class="span-ok">yes</span>#g' \
83 -e 's#no$#<span class="span-no">no</span>#g' \
84 -e 's#error$#<span class="span-red">error</span>#g' \
85 -e 's#ERROR:#<span class="span-red">ERROR:</span>#g' \
86 -e 's#WARNING:#<span class="span-red">WARNING:</span>#g' \
87 -e s"#^Executing:\([^']*\).#<span class='sh-val'>\0</span>#"g \
88 -e s"#^====\([^']*\).#<span class='span-line'>\0</span>#"g \
89 -e s"#^[a-zA-Z0-9]\([^']*\) :: #<span class='span-sky'>\0</span>#"g \
90 -e s"#ftp://[^ '\"]*#<a href='\0'>\0</a>#"g \
91 -e s"#http://[^ '\"]*#<a href='\0'>\0</a>#"g ;;
92 receipt)
93 sed -e s'|&|\&amp;|g' -e 's|<|\&lt;|g' -e 's|>|\&gt;|'g \
94 -e s"#^\#\([^']*\)#<span class='sh-comment'>\0</span>#"g \
95 -e s"#\"\([^']*\)\"#<span class='sh-val'>\0</span>#"g ;;
96 diff)
97 sed -e 's|&|\&amp;|g' -e 's|<|\&lt;|g' -e 's|>|\&gt;|g' \
98 -e s"#^-\([^']*\).#<span class='span-red'>\0</span>#"g \
99 -e s"#^+\([^']*\).#<span class='span-ok'>\0</span>#"g \
100 -e s"#@@\([^']*\)@@#<span class='span-sky'>@@\1@@</span>#"g ;;
101 activity)
102 sed s"#^\([^']* : \)#<span class='log-date'>\0</span>#"g ;;
103 esac
104 }
106 # Latest build pkgs.
107 list_packages() {
108 cd $PKGS
109 ls -1t *.tazpkg | head -20 | \
110 while read file
111 do
112 echo -n $(stat -c '%y' $PKGS/$file | cut -d . -f 1 | sed s/:[0-9]*$//)
113 echo " : $file"
114 done
115 }
117 # Optional full list button
118 more_button() {
119 [ $(wc -l < ${3:-$CACHE/$1}) -gt ${4:-12} ] &&
120 echo "<a class=\"button\" href=\"cooker.cgi?file=$1\">$2</a>"
121 }
123 # xHTML header. Pages can be customized with a separated html.header file.
124 if [ -f "header.html" ]; then
125 cat header.html
126 else
127 cat << EOT
128 <!DOCTYPE html>
129 <html xmlns="http://www.w3.org/1999/xhtml">
130 <head>
131 <title>SliTaz Cooker</title>
132 <meta charset="utf-8" />
133 <link rel="shortcut icon" href="favicon.ico" />
134 <link rel="stylesheet" type="text/css" href="style.css" />
135 </head>
136 <body>
138 <div id="header">
139 <div id="logo"></div>
140 <h1><a href="cooker.cgi">SliTaz Cooker</a></h1>
141 </div>
143 <!-- Content -->
144 <div id="content">
145 EOT
146 fi
148 #
149 # Load requested page
150 #
152 case "${QUERY_STRING}" in
153 pkg=*)
154 pkg=${QUERY_STRING#pkg=}
155 log=$LOGS/$pkg.log
156 echo "<h2>Package: $pkg</h2>"
158 # Package info.
159 echo '<div id="info">'
160 if [ -f "$wok/$pkg/receipt" ]; then
161 echo "<a href='cooker.cgi?receipt=$pkg'>receipt</a>"
162 unset WEB_SITE
163 . $wok/$pkg/receipt
164 [ -n "$WEB_SITE" ] && # busybox wget -s $WEB_SITE &&
165 echo "<a href='$WEB_SITE'>home</a>"
166 if [ -f "$wok/$pkg/taz/$PACKAGE-$VERSION/receipt" ]; then
167 echo "<a href='cooker.cgi?files=$pkg'>files</a>"
168 unset EXTRAVERSION
169 . $wok/$pkg/taz/$PACKAGE-$VERSION/receipt
170 if [ -f $wok/$pkg/taz/$PACKAGE-$VERSION/description.txt ]; then
171 echo "<a href='cooker.cgi?description=$pkg'>description</a>"
172 fi
173 if [ -f $PKGS/$PACKAGE-$VERSION$EXTRAVERSION.tazpkg ]; then
174 echo "<a href='cooker.cgi?download=$PACKAGE-$VERSION$EXTRAVERSION.tazpkg'>download</a>"
175 fi
176 if [ -f $PKGS/$PACKAGE-$VERSION$EXTRAVERSION-$ARCH.tazpkg ]; then
177 echo "<a href='cooker.cgi?download=$PACKAGE-$VERSION$EXTRAVERSION-$ARCH.tazpkg'>download</a>"
178 fi
179 echo "<a href='ftp://${HTTP_HOST%:*}/$pkg/'>browse</a>"
180 fi
181 else
182 if [ $(ls $wok/*$pkg*/receipt 2> /dev/null | wc -l) -eq 0 ]; then
183 echo "No package named: $pkg"
184 else
185 ls $wok/$pkg/receipt >/dev/null 2>&1 || pkg="*$pkg*"
186 echo '<table style="width:100%">'
187 for i in $(cd $wok ; ls $pkg/receipt); do
188 pkg=$(dirname $i)
189 unset SHORT_DESC CATEGORY
190 . $wok/$pkg/receipt
191 cat <<EOT
192 <tr>
193 <td><a href="cooker.cgi?pkg=$pkg">$pkg</a></td>
194 <td>$SHORT_DESC</td>
195 <td>$CATEGORY</td>
196 </tr>
197 EOT
198 done
199 echo '</table>'
200 unset pkg
201 fi
202 fi
203 echo '</div>'
205 # Check for a log file and display summary if it exists.
206 if [ -f "$log" ]; then
207 if grep -q "cook:$pkg$" $command; then
208 echo "<pre>The Cooker is currently building: $pkg</pre>"
209 fi
210 if fgrep -q "Summary for:" $LOGS/$pkg.log; then
211 echo "<h3>Cook summary</h3>"
212 echo '<pre>'
213 grep -A 12 "^Summary for:" $LOGS/$pkg.log | sed /^$/d | \
214 syntax_highlighter log
215 echo '</pre>'
216 fi
217 if fgrep -q "Debug information" $LOGS/$pkg.log; then
218 echo "<h3>Cook failed</h3>"
219 echo '<pre>'
220 grep -A 8 "^Debug information" $LOGS/$pkg.log | sed /^$/d | \
221 syntax_highlighter log
222 echo '</pre>'
223 fi
224 echo "<h3>Cook log</h3>"
225 echo '<pre>'
226 cat $log | syntax_highlighter log
227 echo '</pre>'
228 else
229 [ "$pkg" ] && echo "<pre>No log: $pkg</pre>"
230 fi ;;
231 file=*)
232 # Dont allow all files on the system for security reasons.
233 file=${QUERY_STRING#file=}
234 case "$file" in
235 activity|cooknotes|cooklist)
236 [ "$file" == "cooklist" ] && \
237 nb="- Packages: $(cat $cooklist | wc -l)"
238 echo "<h2>DB: $file $nb</h2>"
239 echo '<pre>'
240 tac $CACHE/$file | syntax_highlighter activity
241 echo '</pre>' ;;
242 broken)
243 nb=$(cat $broken | wc -l)
244 echo "<h2>DB: broken - Packages: $nb</h2>"
245 echo '<pre>'
246 cat $CACHE/$file | sort | \
247 sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g
248 echo '</pre>' ;;
249 *.diff)
250 diff=$CACHE/$file
251 echo "<h2>Diff for: ${file%.diff}</h2>"
252 [ "$file" == "installed.diff" ] && echo \
253 "<p>This is the latest diff between installed packages \
254 and installed build dependencies to cook.</p>"
255 echo '<pre>'
256 cat $diff | syntax_highlighter diff
257 echo '</pre>' ;;
258 *.log)
259 log=$LOGS/$file
260 name=$(basename $log)
261 echo "<h2>Log for: ${name%.log}</h2>"
262 if [ -f "$log" ]; then
263 if fgrep -q "Summary" $log; then
264 echo '<pre>'
265 grep -A 20 "^Summary" $log | sed /^$/d | \
266 syntax_highlighter log
267 echo '</pre>'
268 fi
269 echo '<pre>'
270 cat $log | syntax_highlighter log
271 echo '</pre>'
272 else
273 echo "<pre>No log file: $log</pre>"
274 fi ;;
275 esac ;;
276 stuff=*)
277 file=${QUERY_STRING#stuff=}
278 echo "<h2>$file</h2>"
279 echo '<pre>'
280 cat $wok/$file | sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
281 echo '</pre>' ;;
282 receipt=*)
283 pkg=${QUERY_STRING#receipt=}
284 echo "<h2>Receipt for: $pkg</h2>"
285 if [ -f "$wok/$pkg/receipt" ]; then
286 ( cd $wok/$pkg ; find stuff -type f 2> /dev/null ) | \
287 while read file ; do
288 echo "<a href=\"?stuff=$pkg/$file\">$file</a>"
289 done
290 echo '<pre>'
291 cat $wok/$pkg/receipt | \
292 syntax_highlighter receipt
293 echo '</pre>'
294 else
295 echo "<pre>No receipt for: $pkg</pre>"
296 fi ;;
297 files=*)
298 pkg=${QUERY_STRING#files=}
299 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
300 if [ -d "$dir/fs" ]; then
301 echo "<h2>Installed files by: $pkg ($(du -hs $dir/fs | awk '{ print $1 }'))</h2>"
302 echo '<pre>'
303 find $dir/fs -not -type d -print0 | xargs -0 ls -ld | \
304 sed "s|\(.*\) /.*\(${dir#*wok}/fs\)\(.*\)|\1 <a href=\"?download=../wok\2\3\">\3</a>|;s|^\([^-].*\)\(<a.*\)\">\(.*\)</a>|\1\3|"
305 echo '</pre>'
306 else
307 echo "<pre>No files list for: $pkg</pre>"
308 fi ;;
309 description=*)
310 pkg=${QUERY_STRING#description=}
311 echo "<h2>Description of $pkg</h2>"
312 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
313 if [ -s "$dir/description.txt" ]; then
314 echo '<pre>'
315 cat $dir/description.txt | \
316 sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
317 echo '</pre>'
318 else
319 echo "<pre>No description for: $pkg</pre>"
320 fi ;;
321 *)
322 # We may have a toolchain.cgi script for cross cooker's
323 if [ -f "toolchain.cgi" ]; then
324 toolchain='toolchain.cgi'
325 else
326 toolchain='cooker.cgi?pkg=slitaz-toolchain'
327 fi
328 # Main page with summary. Count only package include in ARCH,
329 # use 'cooker arch-db' to manually create arch.$ARCH files.
330 inwok=$(ls $WOK/*/arch.$ARCH | wc -l)
331 cooked=$(ls $PKGS/*.tazpkg | wc -l)
332 unbuilt=$(($inwok - $cooked))
333 pct=0
334 [ $inwok -gt 0 ] && pct=$(( ($cooked * 100) / $inwok ))
335 cat << EOT
336 <div style="float: right;">
337 <form method="get" action="$SCRIPT_NAME">
338 Package:
339 <input type="text" name="pkg" />
340 </form>
341 </div>
343 <h2>Summary</h2>
345 <pre>
346 Running command : $([ -s "$command" ] && cat $command || echo "Not running")
347 Wok revision : <a href="$WOK_URL">$(cat $wokrev)</a>
348 Commits to cook : $(cat $commits | wc -l)
349 Current cooklist : $(cat $cooklist | wc -l)
350 Broken packages : $(cat $broken | wc -l)
351 Blocked packages : $(cat $blocked | wc -l)
352 </pre>
354 <p class="info">
355 Packages: $inwok in the wok - $cooked cooked - $unbuilt unbuilt -
356 Server date: $(date -u '+%Y-%m-%d %H:%M %Z')
357 </p>
358 <div class="pctbar">
359 <div class="pct" style="width: ${pct}%;">${pct}%</div>
360 </div>
362 <p>
363 Latest:
364 <a href="cooker.cgi?file=cookorder.log">cookorder.log</a>
365 <a href="cooker.cgi?file=commits.log">commits.log</a>
366 <a href="cooker.cgi?file=installed.diff">installed.diff</a>
367 - Architecture $ARCH:
368 <a href="$toolchain">toolchain</a>
369 </p>
371 <a name="activity"></a>
372 <h2>Activity</h2>
373 <pre>
374 $(tac $CACHE/activity | head -n 12 | syntax_highlighter activity)
375 </pre>
376 $(more_button activity "More activity" $CACHE/activity 12)
378 <a name="cooknotes"></a>
379 <h2>Cooknotes</h2>
380 <pre>
381 $(tac $cooknotes | head -n 12 | syntax_highlighter activity)
382 </pre>
383 $(more_button cooknotes "More notes" $cooknotes 12)
385 <a name="commits"></a>
386 <h2>Commits</h2>
387 <pre>
388 $(cat $commits)
389 </pre>
391 <a name="cooklist"></a>
392 <h2>Cooklist</h2>
393 <pre>
394 $(cat $cooklist | head -n 20)
395 </pre>
396 $(more_button cooklist "Full cooklist" $cooklist 20)
398 <a name="broken"></a>
399 <h2>Broken</h2>
400 <pre>
401 $(cat $broken | head -n 20 | sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g)
402 </pre>
403 $(more_button broken "All broken packages" $broken 20)
405 <a name="blocked"></a>
406 <h2>Blocked</h2>
407 <pre>
408 $(cat $blocked | sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g)
409 </pre>
411 <a name="lastcook"></a>
412 <h2>Latest cook</h2>
413 <pre>
414 $(list_packages | sed s"#^\([^']*\).* : #<span class='log-date'>\0</span>#"g)
415 </pre>
416 EOT
417 ;;
418 esac
420 # Close xHTML page
421 cat << EOT
422 </div>
424 <div id="footer">
425 <a href="http://www.slitaz.org/">SliTaz Website</a>
426 <a href="cooker.cgi">Cooker</a>
427 <a href="http://hg.slitaz.org/cookutils/raw-file/tip/doc/cookutils.en.html">
428 Documentation</a>
429 </div>
431 </body>
432 </html>
433 EOT
435 exit 0