wok view tazbb/stuff/tazbb @ rev 4174

tazbb: sort cook list
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Sep 22 21:25:03 2009 +0200 (2009-09-22)
parents 9df47d7de942
children 8b114a32b85f
line source
1 #!/bin/sh
2 # Tazbb - SliTaz Build Bot.
3 # System wide config file: /etc/slitaz/tazbb.conf
4 #
5 # Tazbb is a tool to automate package building, it can be run manually
6 # or via a cron job. On SliTaz build host, tazbb is run in a chroot env.
7 #
8 # (c) 2009 SliTaz GNU/Linux project - GNU gpl v3
9 #
11 # Include config file or exit if no file found.
12 if [ -f "./tazbb.conf" ]; then
13 . ./tazbb.conf
14 elif [ -f "/etc/slitaz/tazbb.conf" ]; then
15 . /etc/slitaz/tazbb.conf
16 else
17 echo -e "\nNo config file found: tazbb.conf...\n" && exit 0
18 fi
20 # Tazbb is only for root.
21 if test $(id -u) != 0 ; then
22 echo -e "\nYou must be root to run: `basename $0`.\n" && exit 0
23 fi
25 # Let tazbb finish is work and make sure needed files exist.
26 if [ -f $LOCK_FILE ]; then
27 case $1 in
28 usage|list-*|*block)
29 continue ;;
30 *)
31 echo -e "\nTazbb is already running and locked...\n"
32 exit 0 ;;
33 esac
34 else
35 mkdir -p $DB_DIR $LOG_DIR
36 touch $LOCK_FILE $DB_DIR/blocked
37 fi
39 usage()
40 {
41 echo -e "\nSliTaz developers and build host tool\n
42 \033[1mUsage: \033[0m `basename $0` [command] [--option]
43 \033[1mCommands: \033[0m\n
44 usage Print this short usage and command list.
45 list-pkgs List last cooked packages with date.
46 report Run in report mode and dont cook anything [--verbose].
47 cook Cook, install and log a single package build.
48 cook-all Cook all missing, modified or unbuilt packages.
49 cook-commit Cook all packages affected by a commit in the last update.
50 test-pkgs Execute a test suite on all packages [--verbose].
51 [un]block Block or unblock a package to skip or enable building.
52 mail Send mail to package maintainer with tazbbmail.
53 clean-up Remove old packages [--verbose|--dry-run].
54 clean-log Remove all generated build log files.\n"
55 }
57 status()
58 {
59 local CHECK=$?
60 echo -en "\033[70G"
61 if [ $CHECK = 0 ]; then
62 echo "Done"
63 else
64 echo "Failed"
65 fi
66 return $CHECK
67 }
69 top_summary()
70 {
71 cat > $DB_DIR/summary << _EOT_
72 Update : `date`
73 Revision : $NEW_REV (<a href="$HG_URL/log/$NEW_REV">changelog</a>)
74 _EOT_
75 }
77 packages_summary()
78 {
79 if ! grep -q "^Packages" $DB_DIR/summary; then
80 cat >> $DB_DIR/summary << _EOT_
81 Packages : `ls $BUILD_WOK | wc -l` in the wok, `cat $DB_DIR/cooklist | wc -l` to cook, \
82 `cat $DB_DIR/blocked | wc -l` blocked, `cat $DB_DIR/corrupted | wc -l` corrupted
83 _EOT_
84 fi
85 }
87 packages_summary_update()
88 {
89 sed -i s/"[0-9]* in the wok"/"`ls $BUILD_WOK | wc -l` in the wok"/ \
90 $DB_DIR/summary
91 sed -i s/"[0-9]* to cook"/"`cat $DB_DIR/cooklist | wc -l` to cook"/ \
92 $DB_DIR/summary
93 sed -i s/"[0-9]* blocked"/"`cat $DB_DIR/blocked | wc -l` blocked"/ \
94 $DB_DIR/summary
95 sed -i s/"[0-9]* corrupted"/"`cat $DB_DIR/corrupted | wc -l` corrupted"/ \
96 $DB_DIR/summary
97 }
99 list_packages()
100 {
101 cd $PACKAGES_REPOSITORY
102 ls -1t *.tazpkg | head -20 | \
103 while read file
104 do
105 echo -n $(stat -c '%y' $PACKAGES_REPOSITORY/$file | cut -d. -f1)
106 echo " $file"
107 done
108 }
110 show_report()
111 {
112 echo "Cooklist"
113 echo "================================================================================"
114 cat $DB_DIR/cooklist && echo ""
115 echo "Blocked"
116 echo "================================================================================"
117 cat $DB_DIR/blocked && echo ""
118 echo ""
119 }
121 # URL encoding
122 escape()
123 {
124 echo $1 | sed -e 's/+/%2B/g' -e 's|/|%2F|g' -e 's/:/%3A/g'
125 }
127 update_wok()
128 {
129 echo ""
130 echo "(updating wok)" > $DB_DIR/running
131 cd $HG_WOK
132 LAST_REV=`hg head --template '{rev}\n'`
133 hg pull && hg update
134 NEW_REV=`hg head --template '{rev}\n'`
135 # Gen a new summary and link last revision for the web interface.
136 echo -e "\nHg wok : $HG_WOK ($NEW_REV)"
137 echo -e "Build wok : $BUILD_WOK ($LAST_REV)\n"
138 top_summary
139 # Copy Hg wok if new revision or exit to stop process since nothing
140 # have change (--forced can be used).
141 if [ "$NEW_REV" != "$LAST_REV" ]; then
142 size=`du -sh $HG_WOK | awk '{ print $1 }'`
143 echo -n "Copying Hg wok to the build wok ($size)... "
144 #rsync -r -n -t $HG_WOK/ $BUILD_WOK/
145 cp -a $HG_WOK/* $BUILD_WOK
146 cp -a $HG_WOK/.hg $BUILD_WOK
147 echo -e "Done\n"
148 else
149 if [ "$1" = "cook-all" ] || [ "$1" = "cook-commit" ]; then
150 if [ "$2" != "--forced" ]; then
151 echo -e "Nothing to cook...\n"
152 packages_summary
153 rm -f $LOCK_FILE && exit 0
154 fi
155 fi
156 fi
157 }
159 # Running 'tazbb report' should not cook anything and --verbose option
160 # can be used to display more messages.
161 check_wok()
162 {
163 # Clean up last results.
164 rm -f $DB_DIR/cooklist && touch $DB_DIR/cooklist
165 rm -f $DB_DIR/report && touch $DB_DIR/report
166 rm -f $DB_DIR/unbuilt && touch $DB_DIR/unbuilt
167 echo "Checking all files in: $HG_WOK"
168 echo "================================================================================"
169 echo "(checking wok)" > $DB_DIR/running
170 for pkg in $HG_WOK/*
171 do
172 EXTRAVERSION=""
173 WANTED=""
174 . $pkg/receipt
175 [ "$2" = "--verbose" ] && echo "Package : $PACKAGE"
176 # Skip blocked packages.
177 if grep -qs "^$PACKAGE$" $DB_DIR/blocked; then
178 echo "Blocked : $PACKAGE ($VERSION)" && continue
179 fi
181 # Bristuff hack until the receipt are improved...
182 #[ "$VERSION" = "bristuff" ] && VERSION=`get_version`
183 if [ "$VERSION" = "bristuff" ]; then
184 . $BUILD_WOK/$PACKAGE/taz/*/receipt
185 fi
187 # First check if package exit. Package naming _must_ be in the form of:
188 # $PACKAGE-$VERSION or $PACKAGE-${VERSION}$EXTRAVERSION (Kernel string).
189 if [ ! -f $PACKAGES_REPOSITORY/$PACKAGE-$VERSION.tazpkg ]; then
190 [ -z "$EXTRAVERSION" ] && EXTRAVERSION="_$KERNEL"
191 if [ ! -f $PACKAGES_REPOSITORY/$PACKAGE-${VERSION}$EXTRAVERSION.tazpkg ]; then
192 [ "$1" = "report" ] && echo "Missing : $PACKAGE ($VERSION)"
193 echo "Missing : $PACKAGE ($VERSION)" >> $DB_DIR/report
194 echo "$PACKAGE" >> $DB_DIR/cooklist
195 fi
196 else
197 # Check if package is up-to-date.
198 PKG_DATE=`date -u -r $PACKAGES_REPOSITORY/$PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg '+%m%d%H%M%Y'`
199 for file in `find $pkg -type f`
200 do
201 FILE_DATE=`date -u -r $file '+%m%d%H%M%Y'`
202 [ "$2" = "--verbose" ] && echo " -> Checking: $file"
203 if [ "$FILE_DATE" -gt "$PKG_DATE" ] && ! grep -q $PACKAGE $DB_DIR/cooklist; then
204 [ "$1" = "report" ] && echo "Refresh : $PACKAGE ($VERSION)"
205 echo "Refresh : $PACKAGE ($VERSION)" >> $DB_DIR/report
206 echo "$PACKAGE" >> $DB_DIR/cooklist
207 fi
208 done
209 fi
210 # Now check if package is built and not already in the list.
211 if [ ! -d $BUILD_WOK/$PACKAGE/taz ] && ! grep -q $PACKAGE $DB_DIR/cooklist; then
212 [ "$1" = "report" ] && echo "Unbuilt : $PACKAGE ($VERSION)"
213 echo "Unbuilt : $PACKAGE ($VERSION)" >> $DB_DIR/report
214 echo "$PACKAGE" >> $DB_DIR/cooklist
215 fi
216 # Rebuild unbuilt packages list with link to log file. This list
217 # is also generated by cook_inslall to have real time stats.
218 if [ ! -d $BUILD_WOK/$PACKAGE/taz ]; then
219 echo "<a href=\"log.php?package=$(escape $PACKAGE)\">$PACKAGE</a>" \
220 >> $DB_DIR/unbuilt
221 fi
222 done
223 packages_summary
224 }
226 # Create a new cooklist and summary (dont modify report) so 'tazbb cook-commit'
227 # can cook last changes.
228 check_commit()
229 {
230 echo "(checking commit)" > $DB_DIR/running
231 cd $HG_WOK
232 # Clean up last results.
233 rm -f $DB_DIR/cooklist && touch $DB_DIR/cooklist
234 # Get the name of modified packages by the revision range. +1 last
235 # commit was build by the previous build.
236 LAST_REV=$(($LAST_REV+1))
237 echo -e "Will cook from revision $LAST_REV to $NEW_REV\n"
238 for file in `hg log --rev=$LAST_REV:$NEW_REV --template '{files}\n'`
239 do
240 pkg=`echo $file | cut -d "/" -f 1`
241 if ! grep -q ^$pkg$ $DB_DIR/cooklist; then
242 . $pkg/receipt
243 echo "Commit : $PACKAGE ($VERSION)" >> $DB_DIR/report
244 echo "$PACKAGE" >> $DB_DIR/cooklist
245 fi
246 done
247 packages_summary
248 }
250 # Cook one package
251 cook_package()
252 {
253 EXTRAVERSION=""
254 DEPENDS=""
255 BUILD_DEPENDS=""
256 SOURCE=""
257 WANTED=""
258 echo "(cooking <a href=\"log.php?package=$(escape $pkg)\">$pkg</a>)" > $DB_DIR/running
259 tazwok clean $pkg
260 script -c "echo 'install' | tazwok cook $pkg" $LOG_DIR/$pkg.log
261 # Install new package (important for new shared libs). Note
262 # that tests are done separatly with 'test_packages' and should
263 # be done by tazwok.
264 if [ -f $BUILD_WOK/$pkg/taz/*/receipt ]; then
265 . $BUILD_WOK/$pkg/taz/*/receipt
266 echo "(installing $PACKAGE-${VERSION}$EXTRAVERSION.tazpkg)" \
267 > $DB_DIR/running
268 yes | tazpkg install \
269 $PACKAGES_REPOSITORY/$PACKAGE-${VERSION}$EXTRAVERSION.tazpkg \
270 --forced
271 return 0
272 fi
273 return 1
274 }
276 # Sort list according WANTED and BUILD_DEPENDS
277 sort_cook_list()
278 {
279 sort | while read pkg; do
280 echo -n "$pkg"
281 WANTED=""
282 BUILD_DEPENDS=""
283 . $BUILD_WOK/$pkg/receipt
284 for i in $WANTED $BUILD_DEPENDS ; do
285 if [ ! -f $BUILD_WOK/$i/taz/*/receipt ]; then
286 echo -n " $i"
287 fi
288 done
289 echo ""
290 done | awk '{
291 if ($2 == "") print;
292 else {
293 depcnt[$1] = NF - 1;
294 unres = unres " " $1;
295 for (i = 2; i <= NF; i++) {
296 revdepcnt[$i]++;
297 revdep[$i] = revdep[$i] " " $1;
298 }
299 }
300 if (revdepcnt[$1] > 0)
301 for (i = split(revdep[$1], pkg, " "); i > 0; i--)
302 if (--depcnt[pkg[i]] == 0) print pkg[i];
303 }
304 END {
305 for (i = split(unres, pkg, " "); i > 0; i--)
306 if (depcnt[pkg[i]] > 0) print pkg[i];
307 }
308 '
309 }
311 # Here we cook all packages found in the cooklist.
312 cook_install()
313 {
314 echo "" > $DB_DIR/unbuilt
315 for pkg in `cat $DB_DIR/cooklist | sort_cook_list`
316 do
317 if ! cook_package $pkg; then
318 # Link to build log.
319 echo "<a href=\"log.php?package=$(escape $pkg)\">$pkg</a>" >> \
320 $DB_DIR/unbuilt
321 fi
322 # Remove package from the cooklist and empty lines for HTML <pre>.
323 sed -i /"^$pkg$"/d $DB_DIR/cooklist
324 sed -i '/^$/d' $DB_DIR/cooklist
325 packages_summary_update
326 done
327 }
329 # Remove old packages in the build wok and clean pkgs repository. The
330 # Hg wok is copied into the build wok so packages removed by hg must be
331 # removed. To remove old packages in the repository we look into the
332 # build wok and dont remove unbuilt packages. Clean-up will also remove
333 # all corrupted packages.
334 clean_up()
335 {
336 touch $DB_DIR/removed
337 echo -e "\nCleaning the build wok, old and corrupted packages...\n"
338 echo "(cleaning)" > $DB_DIR/running
339 for pkg in `ls $HG_WOK`
340 do
341 if [ ! -d $BUILD_WOK/$pkg ]; then
342 case $2 in
343 --dry-run)
344 echo "Removing directory : $pkg" ;;
345 --verbose)
346 echo "Removing directory : $pkg"
347 rm -rf $BUILD_WOK/$pkg ;;
348 *)
349 rm -rf $BUILD_WOK/$pkg ;;
350 esac
351 fi
352 done
353 # Build a packages list with EXTRAVERSION so we can grep into it.
354 rm -f $DB_DIR/packaged && touch $DB_DIR/packaged
355 for receipt in $BUILD_WOK/*/taz/*/receipt
356 do
357 EXTRAVERSION=""
358 . $receipt
359 echo "$PACKAGE-${VERSION}$EXTRAVERSION.tazpkg" >> $DB_DIR/packaged
360 done
361 for pkg in `cd $PACKAGES_REPOSITORY && ls *.tazpkg`
362 do
363 if ! grep -q "^$pkg$" $DB_DIR/packaged; then
364 case $2 in
365 --dry-run)
366 echo "Removing package : $pkg" ;;
367 --verbose)
368 echo "Removing package : $pkg"
369 echo "$pkg" >> $DB_DIR/removed
370 rm -f $PACKAGES_REPOSITORY/$pkg ;;
371 *)
372 echo "$pkg" >> $DB_DIR/removed
373 rm -f $PACKAGES_REPOSITORY/$pkg ;;
374 esac
375 fi
376 done
377 # Remove all corrupted packages
378 for pkg in `cat $DB_DIR/corrupted | awk '{ print $3 }'`
379 do
380 case $2 in
381 --dry-run)
382 echo "Removing corrupted: $pkg" ;;
383 --verbose)
384 echo "Removing corrupted: $pkg"
385 echo "$pkg" >> $DB_DIR/removed
386 rm -rf $PACKAGES_REPOSITORY/$pkg ;;
387 *)
388 echo "$pkg" >> $DB_DIR/removed
389 rm -rf $PACKAGES_REPOSITORY/$pkg ;;
390 esac
391 done
392 echo ""
393 # Keep the 20 last removed packages list.
394 cat $DB_DIR/removed | tail -n 20 > /tmp/removed.tail
395 mv -f /tmp/removed.tail $DB_DIR/removed
396 }
398 blocked_urls()
399 {
400 rm -f $DB_DIR/blocked.urls
401 for pkg in `cat $DB_DIR/blocked`
402 do
403 if [ -f $LOG_DIR/$pkg.log ]; then
404 echo "<a href=\"log.php?package=$(escape $pkg)\">$pkg</a>" >> \
405 $DB_DIR/blocked.urls
406 else
407 echo "$pkg" >> $DB_DIR/blocked.urls
408 fi
409 done
410 }
412 # 4k, not a meta or a get-* package and no files = buggy package
413 test_packages()
414 {
415 echo -e "\nTesting all packages in: $PACKAGES_REPOSITORY"
416 echo "================================================================================"
417 echo "(testing packages)" > $DB_DIR/running
418 rm -f $DB_DIR/corrupted && touch $DB_DIR/corrupted
419 for pkg in $PACKAGES_REPOSITORY/*.tazpkg
420 do
421 tmp=/tmp/bb-test.$$
422 CATEGORY=""
423 if du $pkg | grep -qw '^4' && ! echo `basename $pkg` | grep -q '^get-'; then
424 mkdir -p $tmp && cd $tmp
425 cpio -i receipt 2>/dev/null < $pkg
426 . ./receipt
427 if [ "$CATEGORY" != "meta" ]; then
428 [ "$2" = "--verbose" ] && echo "Testing: $PACKAGE"
429 cpio -i fs.cpio.gz 2>/dev/null < $pkg
430 if [ ! -f fs.cpio.gz ]; then
431 echo "Missing filesystem `basename $pkg`"
432 if [ -f $LOG_DIR/$PACKAGE.log ];then
433 echo "Missing filesystem `basename $pkg` <a href=\"log.php?package=$(escape $PACKAGE)\">Log</a>" \
434 >> $DB_DIR/corrupted
435 else
436 echo "Missing filesystem `basename $pkg`" \
437 >> $DB_DIR/corrupted
438 fi
439 else
440 zcat fs.cpio.gz | cpio -id 2>/dev/null
441 files=`find fs -type f`
442 if [ -z "$files" ]; then
443 echo "Empty filesystem `basename $pkg`"
444 if [ -f $LOG_DIR/$PACKAGE.log ]; then
445 echo "Empty filesystem `basename $pkg` <a href=\"log.php?package=$(escape $PACKAGE)\">Log</a>" \
446 >> $DB_DIR/corrupted
447 else
448 echo "Empty filesystem `basename $pkg`" \
449 >> $DB_DIR/corrupted
450 fi
451 fi
452 fi
453 fi
454 cd .. && rm -rf $tmp
455 fi
456 done
457 packages_summary_update
458 echo ""
459 }
461 case "$1" in
462 list-pkgs)
463 # List last cooked packages.
464 list_packages ;;
465 report)
466 # Run in report mode. If an update is done we must cook-all to
467 # rebuild all updated packages.
468 [ "$2" == "--update" ] && update_wok $@ || echo ""
469 check_wok $@
470 test_packages $@
471 show_report ;;
472 cook)
473 # Cook, install and log a single package build.
474 if [ -z $2 ]; then
475 echo "Please specify a package on the command line."
476 rm -f $LOCK_FILE && exit 0
477 fi
478 pkg=$2
479 echo "Starting to cook and install: $pkg"
480 if ! cook_package $pkg; then
481 echo "Unable to install: $pkg"
482 fi ;;
483 cook-all)
484 # Update wok, gen report (with cooklist), cook all packages, test,
485 # clean, gen new report and lists.
486 update_wok $@
487 check_wok $@
488 cook_install
489 test_packages $@
490 clean_up $@
491 check_wok $@
492 echo "(generating lists)" > $DB_DIR/running
493 tazwok gen-list --text
494 echo "" ;;
495 cook-commit)
496 # Cook all packages affected by the last commits in the wok.
497 # Clean up is done only by cook-all to avoid rebuild of corrupted
498 # packages on each commit.
499 update_wok $@
500 check_commit
501 cook_install
502 test_packages $@
503 check_wok $@
504 echo "(generating lists)" > $DB_DIR/running
505 tazwok gen-list --text
506 echo "" ;;
507 block)
508 # Add a pkg name to the list of blocked packages.
509 echo ""
510 if grep -qs "^$2$" $DB_DIR/blocked; then
511 echo -e "$2 is already in the blocked packages list."
512 else
513 echo -n "Adding $2 to : $DB_DIR/blocked... "
514 echo "$2" >> $DB_DIR/blocked && echo "Done"
515 if grep -q "^$2$" $DB_DIR/cooklist; then
516 echo -n "Removing $2 from : $DB_DIR/cooklist... "
517 sed -i /"^$2$"/d $DB_DIR/cooklist && echo "Done"
518 packages_summary_update
519 fi
520 fi
521 blocked_urls
522 echo "" ;;
523 unblock)
524 # Remove a pkg name from the list of blocked packages.
525 echo ""
526 if grep -qs "^$2$" $DB_DIR/blocked; then
527 echo -n "Removing $2 from : $DB_DIR/blocked... "
528 sed -i /"^$2$"/d $DB_DIR/blocked
529 sed -i '/^$/d' $DB_DIR/blocked && echo "Done"
530 echo -n "Adding $2 to : $DB_DIR/cooklist... "
531 echo "$2" >> $DB_DIR/cooklist && echo "Done"
532 packages_summary_update
533 else
534 echo -e "$2 is not in the blocked packages list."
535 fi
536 blocked_urls
537 echo "" ;;
538 test-pkgs)
539 # Start a test suite on all builded packages.
540 test_packages $@ ;;
541 test-suite)
542 # Start a test suite on all builded package and the wok using
543 # the great 'tazwok check'.
544 #
545 # test_packages > $LOG_DIR/test-suite.log
546 # tazwok check >> $LOG_DIR/test-suite.log
547 #
548 test_packages $@
549 script -c "tazwok check" $LOG_DIR/test-suite.log ;;
550 mail)
551 # Tazbbmail Pythom script wrapper.
552 PACKAGE=$2
553 tazbbmail $PACKAGE ;;
554 clean-up)
555 # Remove old packages and generate new packages lists.
556 update_wok $@
557 clean_up $@
558 packages_summary_update
559 [ "$2" != "--dry-run" ] && tazwok gen-list --text ;;
560 clean-log)
561 logs=`ls $LOG_DIR | wc -l`
562 echo -n "Cleaning: $LOG_DIR... "
563 rm -rf $LOG_DIR/*
564 echo "$logs log removed" ;;
565 *)
566 usage ;;
567 esac
569 echo "" > $DB_DIR/running
570 rm -f $LOCK_FILE
572 exit 0