cookutils view cook @ rev 9

Add command test and improve Quality Assurance
author Christophe Lincoln <pankso@slitaz.org>
date Wed May 04 04:30:28 2011 +0200 (2011-05-04)
parents b222a2ef4b1a
children b543222cb3e0
line source
1 #!/bin/sh
2 #
3 # Cook - A tool to cook and generate SliTaz packages. Read the README
4 # before adding or modifing any code in cook!
5 #
6 # Copyright (C) SliTaz GNU/Linux - GNU gpl v3
7 # Author: Christophe Lincoln <pankso@slitaz.org>
8 #
10 [ -f "/etc/slitaz/cook.conf" ] && . /etc/slitaz/cook.conf
11 [ -f "cook.conf" ] && . ./cook.conf
13 # Share activity with the Cooker.
14 activity="$CACHE/activity"
16 #
17 # Functions
18 #
20 usage() {
21 cat << EOT
23 $(echo -e "\033[1m$(gettext "Usage:")\033[0m") cook [package|command|list] [--option]
25 $(echo -e "\033[1m$(gettext "Commands:")\033[0m")
26 usage|help $(gettext "Display this short usage.")
27 list-wok $(gettext "List packages in the wok.")
28 setup $(gettext "Setup your build environment.")
29 test $(gettext "Test environment and cook a package.")
30 new $(gettext "Create a new package with receipt".)
31 list $(gettext "Cook a list of packages.")
32 clean-wok $(gettext "Clean-up all packages files.")
33 clean-src $(gettext "Clean-up all packages source.")
34 pkglist $(gettext "Create all packages.* lists.")
36 $(echo -e "\033[1m$(gettext "Options:")\033[0m")
37 --clean|-c $(gettext "Clean the package in the wok.")
38 --install|-i $(gettext "Cook and install the package.")
39 --wok|-w $(gettext "Setup also a wok from Hg repo.")
41 EOT
42 exit 0
43 }
45 # Be sure we root.
46 check_root() {
47 [ $(id -u) != 0 ] && gettext -e "\nYou must be root to cook.\n\n" && exit 0
48 }
50 separator() {
51 echo "================================================================================"
52 }
54 status() {
55 echo -en "\\033[70G[ "
56 if [ $? = 0 ]; then
57 echo -en "\\033[1;32mOK"
58 else
59 echo -en "\\033[1;31mFailed"
60 fi
61 echo -e "\\033[0;39m ]"
62 }
64 # Log activities.
65 log() {
66 grep ^[a-zA-Z0-9] | \
67 sed s"#^[A-Z]\([^']*\)#$(date '+%Y-%m-%d %H:%M') : \0#" >> $activity
68 }
70 # We dont want those escape in web interface.
71 clean_log() {
72 sed -i -e s'|\[70G\[ \[1;32m| |' \
73 -e s'|\[0;39m \]||' $LOGS/$pkg.log
74 }
76 # Be sure package exist in wok.
77 check_pkg_in_wok() {
78 if [ ! -d "$WOK/$pkg" ]; then
79 gettext -e "\nUnable to find package in the wok:"
80 echo -e " $pkg\n" && exit 1
81 fi
82 }
84 if_empty_value() {
85 if [ -z "$value" ]; then
86 gettext "QA: empty variable:"; echo -e " ${var}=\"\"\n"
87 exit 1
88 fi
89 }
91 # QA: check a receip consistency befor building.
92 receipt_quality() {
93 gettext -e "QA: checking package receipt...\n"
94 unset online
95 if ifconfig | grep -q -A 1 "^[a-z]*[0-9]" | fgrep 'addr:'; then
96 online="online"
97 fi
98 for var in PACKAGE VERSION CATEGORY SHORT_DESC MAINTAINER WEB_SITE
99 do
100 unset value
101 value=$(grep ^$var= receipt | cut -d \" -f 2)
102 case "$var" in
103 PACKAGE|VERSION|SHORT_DESC)
104 if_empty_value ;;
105 CATEGORY)
106 [ -z "$value" ] && value="empty"
107 valid="base-system x-window utilities network graphics \
108 multimedia office development system-tools security games \
109 misc meta non-free"
110 if ! echo "$valid" | grep -q -w "$value"; then
111 gettext "QA: unknow category:"; echo -e " $value\n"
112 exit 1
113 fi ;;
114 WEB_SITE)
115 # We dont check WGET_URL since if dl is needed it will fail.
116 # Break also if we not online. Here error is not fatal.
117 if_empty_value
118 [ -z "$online" ] || break
119 if ! busybox wget -s $value 2>/dev/null; then
120 gettext "QA: Unable to reach:"; echo -e " $value\n"
121 fi ;;
122 esac
123 done
124 }
126 # Executed before sourcing a receipt.
127 unset_receipt() {
128 unset DEPENDS BUILD_DEPENDS WANTED EXTRAVERSION WGET_URL PROVIDE TARBALL
129 }
131 # Path's used in receipt and by cook itself.
132 set_paths() {
133 pkgdir=$WOK/$PACKAGE
134 src=$pkgdir/source/$PACKAGE-$VERSION
135 pack=$pkgdir/taz/$PACKAGE-${VERSION}${EXTRAVERSION}
136 fs=$pack/fs
137 stuff=$pkgdir/stuff
138 install=$pkgdir/install
139 if [ "$WANTED" ]; then
140 src=$WOK/$WANTED/source/$WANTED-$VERSION
141 install=$WOK/$WANTED/install
142 fi
143 # Old way compatibility.
144 _pkg=$install
145 }
147 # Get package source.
148 get_source() {
149 case "$WGET_URL" in
150 http://*|ftp://*)
151 # Busybox Wget is better!
152 busybox wget -c -P $SRC $WGET_URL ;;
153 hg*|mercurial*)
154 # We are in cache so clone here and create a tarball
155 pwd=$(pwd)
156 if $(echo "$WGET_URL" | fgrep -q hg); then
157 url=${WGET_URL#hg|}
158 else
159 url=${WGET_URL#mercurial|}
160 fi
161 pkgsrc=$PACKAGE-$VERSION
162 [ "$SOURCE" ] && pkgsrc=$SOURCE-$VERSION
163 tarball=$pkgsrc.tar.bz2
164 gettext "Getting source from Hg: "; echo $url
165 gettext "Cloning to: "; echo "$pwd/$pkgsrc"
166 hg clone $url $pkgsrc || exit 1
167 gettext "Creating tarball: "; echo "$tarball"
168 tar cjf $tarball $pkgsrc || exit 1
169 mv $tarball $SRC && rm -rf $pkgsrc ;;
170 git*)
171 echo "TODO: git implementation in cook" && exit 1 ;;
172 svn*)
173 echo "TODO: svn implementation in cook" && exit 1 ;;
174 *)
175 gettext -e "\nERROR: Unable to handle:"; echo -e " $WGET_URL \n" | \
176 tee -a $LOGS/$PACKAGE.log
177 exit 1 ;;
178 esac
179 }
181 # Extract source package.
182 extract_source() {
183 gettext "Extracting:"; echo " $TARBALL"
184 case "$TARBALL" in
185 *.tar.gz|*.tgz) tar xzf $SRC/$TARBALL ;;
186 *.tar.bz2) tar xjf $SRC/$TARBALL ;;
187 *.tar.lzma) tar xaf $SRC/$TARBALL ;;
188 *.zip) unzip $SRC/$TARBALL ;;
189 esac
190 }
192 # Display cooked package summary.
193 summary() {
194 cd $WOK/$pkg
195 [ -d install ] && prod=$(du -sh install | awk '{print $1}' 2>/dev/null)
196 fs=$(du -sh taz/* | awk '{print $1}')
197 size=$(du -sh $PKGS/$PACKAGE-${VERSION}${EXTRAVERSION}.* | awk '{print $1}')
198 files=$(cat taz/$PACKAGE-*/files.list | wc -l)
199 gettext "Summary for:"; echo " $PACKAGE $VERSION"
200 separator
201 [ "$prod" ] && echo "Produce : $prod"
202 cat << EOT
203 Packed : $fs
204 Compressed : $size
205 Cook time : ${time}s
206 Files : $files
207 $(separator)
209 EOT
210 }
212 # Copy all generic files (locale, pixmaps, .desktop). We use standard paths,
213 # so some packages need to copy these files with the receipt and genpkg_rules.
214 copy_generic_files()
215 {
216 # $LOCALE is set in cook.conf
217 if [ "$LOCALE" ]; then
218 if [ -d "$_pkg/usr/share/locale" ]; then
219 mkdir -p $fs/usr/share/locale
220 for i in $LOCALE
221 do
222 if [ -d "$_pkg/usr/share/locale/$i" ]; then
223 cp -a $_pkg/usr/share/locale/$i $fs/usr/share/locale
224 fi
225 done
226 fi
227 fi
229 # Generic pixmaps copy can be disabled with GENERIC_PIXMAPS="no"
230 if [ "$GENERIC_PIXMAPS" != "no" ]; then
231 if [ -d "$_pkg/usr/share/pixmaps" ]; then
232 mkdir -p $fs/usr/share/pixmaps
233 cp -a $_pkg/usr/share/pixmaps/$PACKAGE.png \
234 $fs/usr/share/pixmaps 2>/dev/null
235 cp -a $_pkg/usr/share/pixmaps/$PACKAGE.xpm \
236 $fs/usr/share/pixmaps 2>/dev/null
237 fi
239 # Custom or homemade PNG pixmap can be in stuff.
240 if [ -f "$stuff/$PACKAGE.png" ]; then
241 mkdir -p $fs/usr/share/pixmaps
242 cp -a $stuff/$PACKAGE.png $fs/usr/share/pixmaps
243 fi
244 fi
246 # Desktop entry (.desktop).
247 if [ -d "$_pkg/usr/share/applications" ]; then
248 cp -a $_pkg/usr/share/applications $fs/usr/share
249 fi
251 # Homemade desktop file(s) can be in stuff.
252 if [ -d "$stuff/applications" ]; then
253 mkdir -p $fs/usr/share
254 cp -a $stuff/applications $fs/usr/share
255 fi
256 if [ -f "$stuff/$PACKAGE.desktop" ]; then
257 mkdir -p $fs/usr/share/applications
258 cp -a $stuff/$PACKAGE.desktop $fs/usr/share/applications
259 fi
260 }
262 # Find and strip : --strip-all (-s) or --strip-debug on static libs.
263 strip_package()
264 {
265 gettext "Executing strip on all files"
266 for dir in $fs/bin $fs/sbin $fs/usr/bin $fs/usr/sbin $fs/usr/games
267 do
268 if [ -d "$dir" ]; then
269 find $dir -type f -exec strip -s '{}' 2>/dev/null \;
270 fi
271 done
272 find $fs -name "*.so*" -exec strip -s '{}' 2>/dev/null \;
273 find $fs -name "*.a" -exec strip --strip-debug '{}' 2>/dev/null \;
274 status
275 }
277 # Remove installed deps.
278 remove_deps() {
279 # Now remove installed build deps.
280 diff="$CACHE/installed.diff"
281 deps=$(cat $diff | grep ^+[a-zA-Z0-9] | sed s/^+//)
282 nb=$(cat $diff | grep ^+[a-zA-Z0-9] | wc -l)
283 if [ -s "$CACHE/installed.diff" ]; then
284 gettext "Build dependencies to remove:"; echo " $nb"
285 gettext "Removing:"
286 for dep in $deps
287 do
288 echo -n " $dep"
289 yes | tazpkg remove $dep >/dev/null
290 done
291 echo ""
292 mv -f $CACHE/installed.diff $CACHE/installed.last.diff
293 fi
294 }
296 # The main cook function.
297 cookit() {
298 echo "Cooking: $PACKAGE $VERSION"
299 echo "Cooking: $PACKAGE $VERSION" | log
300 separator
301 set_paths
302 [ "$QA" ] && receipt_quality
303 rm -rf install taz source $CACHE/error
305 # Disable -pipe if less than 512Mb free RAM.
306 free=$(free | fgrep '/+ buffers' | tr -s ' ' | cut -f 4 -d ' ')
307 if [ "$free" -lt 524288 ] && [ "$CFLAGS" != "${CFLAGS/-pipe}" ]; then
308 gettext -e "Disabling -pipe compile flag: $free RAM\n"
309 CFLAGS="${CFLAGS/-pipe}" && CFLAGS=$(echo "$CFLAGS" | tr -s ' ')
310 CXXFLAGS="${CXXFLAGS/-pipe}" && CXXFLAGS=$(echo "$CXXFLAGS" | tr -s ' ')
311 fi
312 unset free
314 # Export flags and path to be used by make
315 DESTDIR=$WOK/$PACKAGE/install
316 export DESTDIR MAKEFLAGS CFLAGS CXXFLAGS BUILD_HOST CONFIG_SITE
317 local LC_ALL=POSIX LANG=POSIX
319 # Check for build dep.
320 cd $INSTALLED && ls -1 > $CACHE/installed.list
321 [ "$DEPENDS" ] && gettext -e "Checking build dependencies...\n"
322 for dep in $BUILD_DEPENDS
323 do
324 if [ ! -d "$INSTALLED/$dep" ]; then
325 # Try local package first
326 if [ -f "$PKGS/$dep-*.tazpkg" ]; then
327 gettext "Installing dep (local):"; echo " $dep"
328 cd $PKGS && tazpkg install $dep-*.tazpkg >/dev/null
329 else
330 gettext "Installing dep (web/cache):"; echo " $dep"
331 tazpkg get-install $dep >/dev/null
332 fi
333 fi
334 done
335 ls -1 > $CACHE/installed.cook && cd $CACHE
337 # If a cook failed deps are not remove since we exit 1.
338 [ ! -s "installed.diff" ] && \
339 diff installed.list installed.cook > installed.diff
340 deps=$(cat installed.diff | grep ^+[a-zA-Z0-9] | wc -l)
342 # Get source tarball and make sure we have source dir named:
343 # $PACKAGE-$VERSION to be standard in receipts. Her we use tar.lzma
344 # tarball if it exist.
345 if [ "$WGET_URL" ] && [ ! -f "$SRC/$TARBALL" ]; then
346 if [ -f "$SRC/${SOURCE:-$PACKAGE}-$VERSION.tar.lzma" ]; then
347 TARBALL=$SRC/${SOURCE:-$PACKAGE}-$VERSION.tar.lzma
348 else
349 get_source || exit 1
350 fi
351 fi
352 if [ ! "$WANTED" ] && [ "$TARBALL" ] && [ ! -d "$src" ]; then
353 mkdir -p $pkgdir/source/tmp && cd $pkgdir/source/tmp
354 extract_source || exit 1
355 mv * ../$PACKAGE-$VERSION
356 cd .. && rm -rf tmp
357 fi
359 # Execute receipt rules.
360 if grep -q ^compile_rules $pkgdir/receipt; then
361 gettext -e "Executing: compile_rules\n"
362 [ -d "$src" ] && cd $src
363 compile_rules || exit 1
364 # QA: compile_rules success so valid.
365 mkdir -p $install
366 # Stay compatible with _pkg
367 [ -d $src/_pkg ] && mv $src/_pkg $install
368 else
369 # QA: No compile_rules so no error, valid.
370 mkdir -p $install
371 fi
372 if grep -q ^genpkg_rules $pkgdir/receipt; then
373 gettext -e "Executing: genpkg_rules\n"
374 mkdir -p $fs && genpkg_rules || ( echo -e \
375 "\nERROR: genpkg_rules failed\n" | \
376 tee -a $LOGS/$pkg.log && exit 1 )
377 fi
378 separator && echo ""
379 }
381 # Cook quality assurance.
382 cookit_quality() {
383 if [ ! -d "$WOK/$pkg/install" ] && [ ! "$WANTED" ]; then
384 echo -e "\nERROR: cook failed\n" | tee -a $LOGS/$pkg.log
385 fi
386 # ERROR can be echoed any time in cookit()
387 if grep -q ^ERROR $LOGS/$pkg.log; then
388 exit 1
389 fi
390 }
392 # Create the package.
393 packit() {
394 set_paths
395 echo "Packing: $PACKAGE ${VERSION}${EXTRAVERSION}"
396 separator
397 cd $pkgdir/taz
398 strip_package
399 for file in receipt description.txt
400 do
401 [ ! -f "../$file" ] && continue
402 gettext "Copying"; echo -n " $file..."
403 cp -f ../$file $pack && chown 0.0 $pack/$file && status
404 done
405 copy_generic_files
406 # Use Tazpkg to create a tazpkg package...
407 tazpkg pack $PACKAGE-${VERSION}${EXTRAVERSION} | grep "\[*\]"
408 separator && echo ""
409 }
411 # Verify package quality and consitensy.
412 packit_quality() {
413 if grep -q ^ERROR $LOGS/$pkg.log; then
414 exit 1
415 fi
416 if ! grep -q ^/ $WOK/$pkg/taz/$pkg-*/files.list; then
417 echo -e "ERROR: empty package\n" | tee -a $LOGS/$pkg.log && exit 1
418 else
419 mv -f $WOK/$pkg/taz/$pkg-*.tazpkg $PKGS
420 fi
421 }
423 #
424 # Commands
425 #
427 case "$1" in
428 usage|help)
429 usage ;;
430 list-wok)
431 gettext "List of packages in:"; echo " $WOK"
432 separator
433 cd $WOK && ls -1
434 separator
435 echo -n "Packages: " && ls | wc -l
436 echo "" ;;
437 setup)
438 # Setup a build environment
439 check_root
440 gettext -e "\nSetting up your environment\n"
441 separator && cd $SLITAZ
442 gettext "Creating directories structure in:"; echo " $SLITAZ"
443 mkdir -p $WOK $PKGS $SRC $CACHE $LOGS
444 gettext -e "Checking for packages to install...\n"
445 for pkg in slitaz-toolchain tazdev intltool gettext
446 do
447 [ ! -d "$INSTALLED/$pkg" ] && tazpkg get-install $pkg
448 done
450 # Handle --options
451 case "$2" in
452 --wok)
453 [ ! -d "$INSTALLED/mercurial" ] && tazpkg get-install mercurial
454 [ -d "$WOK" ] && echo -e "A wok already exist.\n" && exit 1
455 hg clone $HG_URL ;;
456 --chroot)
457 echo "TODO: create a chroot with tazdev" ;;
458 esac
460 # SliTaz group and permissions
461 if ! grep -q ^slitaz /etc/group; then
462 gettext -e "Adding group: slitaz\n"
463 addgroup slitaz
464 fi
465 gettext -e "Setting permissions for slitaz group...\n"
466 chown -R root.slitaz $SLITAZ
467 chmod -R g+w $SLITAZ
468 separator
469 gettext -e "All done, ready to cook packages :-)\n\n" ;;
470 test)
471 # Test a cook environment.
472 echo "TODO: Use $DATA/cooktest \$(cp cooktest \$WOK and cook)"
473 [ ! -d "$WOK" ] && exit 1
474 [ ! -d "$WOK/cooktest" ] && cp -r $DATA/cooktest $WOK
475 cook cooktest ;;
476 new)
477 # Create the package folder and an empty receipt.
478 pkg="$2"
479 [ "$pkg" ] || usage
480 echo ""
481 if [ -d "$WOK/$pkg" ]; then
482 echo -n "$pkg " && gettext "package already exist."
483 echo -e "\n" && exit 1
484 fi
485 gettext "Creating"; echo -n " $WOK/$pkg"
486 mkdir $WOK/$pkg && cd $WOK/$pkg && status
487 gettext "Preparing the package receipt..."
488 cp $DATA/receipt .
489 sed -i s"/^PACKAGE=.*/PACKAGE=\"$pkg\"/" receipt
490 status && echo "" ;;
491 list)
492 # Cook a list of packages (better use the Cooker since it will order
493 # packages before executing cook).
494 check_root
495 [ -z "$2" ] && gettext -e "\nNo list in argument.\n\n" && exit 1
496 [ ! -f "$2" ] && gettext -e "\nNo list found:" && \
497 echo -e " $2\n" && exit 1
498 for pkg in $(cat $2)
499 do
500 cook $pkg || broken
501 done ;;
502 clean-wok)
503 check_root
504 gettext -e "\nCleaning all packages files..."
505 rm -rf $WOK/*/taz $WOK/*/install $WOK/*/source
506 status && echo "" ;;
507 clean-src)
508 check_root
509 gettext -e "\nCleaning all packages source..."
510 rm -rf $WOK/*/source
511 status && echo "" ;;
512 pkglist)
513 # Create suitable packages list for TazPKG and only for builded packages.
514 [ "$2" ] && PKGS="$2"
515 [ ! -d "$PKGS" ] && \
516 gettext -e "\nPackages directory dont exist\n\n" && exit 1
517 cd $PKGS
518 gettext -e "\nCreating lists for:"; echo " $PKGS"
519 separator
520 rm -f packages.* files.list*
521 gettext -e "Creating: packages.list\n"
522 ls -1 | sed s'/.tazpkg//' > $PKGS/packages.list
523 gettext -e "Creating: packages.md5\n"
524 md5sum *.tazpkg > $PKGS/packages.md5
525 gettext -e "Creating: packages.desc\n"
526 gettext -e "Creating: packages.equiv\n"
527 cd $WOK
528 for pkg in *
529 do
530 unset_receipt
531 . $pkg/receipt
532 # packages.desc let us search easily in DB
533 if [ -f "$PKGS/$PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg" ]; then
534 cat >> $PKGS/packages.desc << EOT
535 $PACKAGE | $VERSION$EXTRAVERSION | $SHORT_DESC | $CATEGORY | $WEB_SITE
536 EOT
537 # Packages.equiv is used by tazpkg install to check depends.
538 for i in $PROVIDE; do
539 DEST=""
540 echo $i | fgrep -q : && DEST="${i#*:}:"
541 if grep -qs ^${i%:*}= $PKGS/packages.equiv; then
542 sed -i "s/^${i%:*}=/${i%:*}=$DEST$PACKAGE /" \
543 $PKGS/packages.equiv
544 else
545 echo "${i%:*}=$DEST$PACKAGE" >> $PKGS/packages.equiv
546 fi
547 done
548 fi
549 done
550 # files.list.lzma
551 #lzma e files.list files.list.lzma
552 separator
553 nb=$(ls $PKGS/*.tazpkg | wc -l)
554 echo -e "Packages: $nb\n" ;;
555 *)
556 # Just cook and generate a package.
557 check_root
558 time=$(date +%s)
559 pkg="$1"
560 [ -z "$pkg" ] && usage
561 check_pkg_in_wok && echo ""
562 unset inst
563 unset_receipt
564 cd $WOK/$pkg && . ./receipt
566 # Handle --options
567 case "$2" in
568 --clean|-c)
569 gettext -e "Cleaning package:"; echo -n " $pkg"
570 cd $WOK/$pkg && rm -rf install taz source
571 status && echo "" && exit 0 ;;
572 --install|-i)
573 inst='yes' ;;
574 esac
576 # Check if wanted is build now so we have separate log files.
577 if [ "$WANTED" ] && [ ! -d "$WOK/$WANTED/install" ]; then
578 cook "$WANTED"
579 fi
581 # Cook and pack or exit on error and log everything.
582 cookit | tee $LOGS/$pkg.log
583 remove_deps
584 cookit_quality
585 packit | tee -a $LOGS/$pkg.log
586 clean_log
587 packit_quality
589 # Time and summary
590 time=$(($(date +%s) - $time))
591 summary | tee -a $LOGS/$pkg.log
593 # Install package if requested
594 if [ "$inst" ]; then
595 if [ -f "$PKGS/$PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg" ]; then
596 cd $PKGS && tazpkg install \
597 $PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg --forced
598 else
599 gettext -e "Unable to install package, build have failed.\n\n"
600 exit 1
601 fi
602 fi
603 # Finally we DONT WANT to build the *-dev or packages with WANTED="$pkg"
604 # You automation: use the Cooker Build Bot.
605 #[ -d "$WOK/$pkg-dev" ] && cook $pkg-dev
606 ;;
607 esac
609 exit 0