cookutils view cooker @ rev 731

cook pkgdb: display files sizes in the less chatty way.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri May 08 15:52:28 2015 +0300 (2015-05-08)
parents 38b417993ec2
children 75fe6eb7e7bd
line source
1 #!/bin/sh
2 #
3 # SliTaz Build Bot. The Cooker is a tool to automate and test SliTaz package
4 # building. Please read the Cookbook documentation for more information
5 # and discuss with the AUTHORS before adding anything here. PS: no translations
6 # here since it's not an end user tool and it's not useful. All devs should
7 # at least understand basic English.
8 #
10 . /usr/lib/slitaz/libcook.sh
12 # Set pkg name and use same wok as cook.
13 pkg="$2"
14 wok="$WOK"
16 # PID file.
17 pidfile='/var/run/cooker.pid'
19 #
20 # Functions
21 #
23 usage() {
24 cat <<EOT
26 Usage: cooker [command] [pkg|list|note|hours]
28 Options:
29 usage|-u Display this short usage.
30 setup|-s Setup the Cooker environment.
31 setup-cron Setup a cron job for the Cooker.
32 arch-db Create host arch packages DB.
33 note|-n Add a note to the cooknotes.
34 notes|-ns Display all the cooknotes.
35 block|-b Block a package so cook will skip it.
36 unblock|-ub Unblock a blocked package.
37 pkg|-p Same as 'cook pkg' but with cooker log.
38 flavor|-f Cook all packages of a flavor.
39 list|-l Cook all packages in the given list.
40 cat|-c Cook all packages of a category.
41 rev|-r Cook packages of a specific revision.
42 all|-a Find and cook all unbuilt packages.
44 EOT
45 exit 0
46 }
49 # Some messages occur in activity but log verbose output when checking for commits
50 # into a log file.
52 log_commits() {
53 sed '/^.\//'d | sed '/^.hg/'d | tee -a $LOGS/commits.log
54 }
57 # Clean up before exit when check and cook commits finish.
59 clean_exit() {
60 rm -f $command; touch $command
61 rm -f $pidfile
62 }
65 # Summary for commits.
67 commits_summary() {
68 msg="from revision $cur to $new"
69 [ "$new" == "$cur" ] && msg="revision $new"
70 echo "Will cook $msg"
71 separator
72 echo -e "\nSummary for commits"
73 separator
74 echo "Hg wok revision : $cur"
75 echo "Pulled revision : $new"
76 echo "Check date : $(date '+%F %T')"
77 }
80 # Scan packages build deps and fill up cookorder list.
82 cook_order_scan() {
83 rm -f $cookorder
84 touch $cookorder
85 for pkg in $(cat $cooklist); do
86 unset WANTED BUILD_DEPENDS
87 . $wok/$pkg/receipt
88 # The :: is for web interface color.
89 [ "$WANTED$BUILD_DEPENDS" ] && echo "$pkg :: $WANTED $BUILD_DEPENDS"
90 for dep in $WANTED $BUILD_DEPENDS; do
91 if grep -q "^$dep$" $cooklist; then
92 if ! grep -q "^$dep$" $cookorder; then
93 echo "$dep" >> $cookorder
94 fi
95 fi
96 done
97 done
99 # Append unordered packages to cookorder.
100 for pkg in $(cat $cooklist); do
101 if ! grep -q "^$pkg$" $cookorder; then
102 echo "$pkg" >> $cookorder
103 fi
104 done
105 }
108 # Scan and rescan until the cooklist is ordered then handle WANTED.
110 cook_order() {
111 time=$(date +%s)
112 scan=0
114 # Keep an original cooklist so we do a diff when ordering is finished.
115 cp -f $cooklist $cooklist.0
116 echo 'cookorder' > $command
117 echo -e '\nInitial Cooker order scan'
118 separator
119 cook_order_scan
121 # Diff between the cooklist and new ordered list ? So copy the last
122 # cookorder to cooklist and rescan it.
123 while /bin/true; do
124 diff $cooklist $cookorder > $cookorder.diff
125 if [ -s "$cookorder.diff" ]; then
126 scan=$(($scan + 1))
127 echo -e "\nDiff scan: $scan"
128 separator
129 mv -f $cookorder $cooklist
130 cook_order_scan
131 else
132 break
133 fi
134 done
136 # Keep a diff between submitted cooklist and the ordered.
137 diff $cooklist.0 $cooklist > $cooklist.diff
138 rm -f $cookorder $cookorder.diff $cooklist.0
140 # Scan finished: append pkg to WANTED or leave it in the ordered cooklist.
141 # TODO: grep the line number to get pkg position and keep it higher.
142 echo -e '\nHandle WANTED package'
143 separator
144 for pkg in $(cat $cooklist); do
145 unset WANTED
146 . $wok/$pkg/receipt
147 for wanted in $WANTED; do
148 echo "$pkg :: $wanted"
149 if grep -q ^${wanted}$ $cooklist; then
150 sed -i -e "/^$pkg$/"d \
151 -e "/^$wanted$/ a $pkg" $cooklist
152 fi
153 done
154 done
156 # Show ordered cooklist
157 echo -e '\nCooklist order'
158 separator
159 cat $cooklist
160 separator
162 time=$(($(date +%s) - $time))
163 pkgs=$(cat $cooklist | wc -l)
164 echo -e '\nSummary for cookorder'
165 separator
166 cat <<EOT
167 Ordered packages : $pkgs
168 Scans executed : $scan
169 Scan duration : ${time}s
170 EOT
171 separator
173 rm -f $command
174 }
177 # Remove blocked (faster this way than grepping before).
179 strip_blocked() {
180 for pkg in $(cat $blocked); do
181 sed -i /^${pkg}$/d $cooklist
182 done
183 sed -i /^$/d $cooklist
184 }
187 # Use in default mode and with all cmd.
189 cook_commits() {
190 if [ -s "$commits" ]; then
191 for pkg in $(cat $commits); do
192 echo "cook:$pkg" > $command
193 cook $pkg || broken
194 sed -i /^${pkg}$/d $commits
195 done
196 fi
197 }
200 # Cook all packages in a cooklist.
202 cook_list() {
203 for pkg in $(cat $cooklist); do
204 cook $pkg || broken
205 sed -i /^${pkg}$/d $cooklist
206 done
207 }
210 # Create a arch.$ARCH file for each package cooked for the target host
211 # architecture
212 #
213 # The deal: we dont want all packages handled by cooker commands and stats,
214 # since we dont cross compile all packages for each arch but only a set of
215 # packages to provide one full featured desktop, servers and goodies useful
216 # for the host system.
217 #
219 arch_db() {
220 count=0
221 echo "Cleaning packages DB : arch.$ARCH"
222 rm -f $wok/*/arch.$ARCH && cd $wok
223 echo "Creating $ARCH packages DB..."
224 for pkg in *; do
225 HOST_ARCH=
226 . $wok/$pkg/receipt
227 if [ -n "$HOST_ARCH" ]; then
228 if $(echo "$HOST_ARCH" | egrep -q "$ARCH|any"); then
229 count=$(($count + 1))
230 echo "Adding: $pkg"
231 touch $pkg/arch.$ARCH
232 fi
233 unset HOST_ARCH
234 else
235 # HOST_ARCH not set --> in i486
236 if [ "$ARCH" == 'i486' ]; then
237 count=$(($count + 1))
238 echo "Adding: $pkg"
239 touch $pkg/arch.$ARCH
240 fi
241 fi
242 done
243 echo "Packages for $ARCH : $count"
244 }
247 #
248 # Commands
249 #
251 case "$1" in
252 usage|help|-u|-h)
253 usage ;;
255 setup|-s)
256 # Setup the Cooker environment.
257 echo -e '\nSetting up the Cooker'
258 mkdir -p $CACHE
259 echo "Cooker setup using: $SLITAZ" | log
260 separator
261 for pkg in $SETUP_PKGS mercurial rsync tazlito; do
262 [ ! -d "$INSTALLED/$pkg" ] && tazpkg get-install $pkg
263 done
264 mkdir -p $SLITAZ && cd $SLITAZ
265 if [ -d "${wok}-hg" ]; then
266 echo -e 'Hg wok already exists.\n'
267 exit 1
268 fi
269 if [ -d "$wok" ]; then
270 echo -e 'Build wok already exists.\n'
271 exit 1
272 fi
274 # Directories and files
275 echo "mkdir's and touch files in: $SLITAZ"
276 mkdir -p $PKGS $LOGS $FEEDS $CACHE $SRC
277 for f in $activity $blocked $broken $commits $cooklist $command; do
278 touch $f
279 done
280 hg clone $WOK_URL ${wok}-hg || exit 1
281 [ -d "$flavors" ] || hg clone $FLAVORS_URL flavors
282 cp -a ${wok}-hg $wok
283 separator; newline ;;
285 arch-db)
286 # Manually create arch packages DB.
287 arch_db ;;
289 setup-cron)
290 # Create cron job for the cooker.
291 [ "$2" ] || hours=2
292 if [ ! -f "$crontabs" ]; then
293 mkdir -p /var/spool/cron/crontabs
294 fi
295 if ! fgrep -q /usr/bin/cooker $crontabs; then
296 echo "# Run SliTaz Cooker every $hours hours" > $crontabs
297 echo "0 */$hours * * * /usr/bin/cooker --output=html" >> $crontabs
298 killall crond 2>/dev/null && /etc/init.d/crond start
299 fi ;;
301 check-cron)
302 if [ ! -f "$crontabs" ]; then
303 echo "There is no $crontabs here. Use setup-cron option."
304 exit 1
305 fi
306 fgrep /usr/bin/cooker $crontabs ;;
308 note|-n)
309 # Blocked a pkg and want others to know why? Post a note!
310 [ -n "$2" ] && echo "$(date '+%F %R') : $2" >> $cooknotes ;;
312 notes|-ns)
313 # View cooknotes.
314 echo -e '\nCooknotes'
315 separator
316 cat $cooknotes
317 separator; newline ;;
319 block|-b)
320 # Block a package.
321 [ "$pkg" ] && cook $pkg --block ;;
323 unblock|-ub)
324 # Unblock a package.
325 [ "$pkg" ] && cook $pkg --unblock ;;
327 reverse|-r)
328 # Cook all reverse dependencies for a package. This command lets us
329 # control the Cooker manually for commits that will cook a lot of packages.
330 #
331 # Use hg commit? Ex: hg commit -m "Message bla bla | cooker:reverse"
332 #
333 if [ ! -d "$wok/$pkg" ]; then
334 echo -e "\nNo package $2 found.\n"
335 exit 0
336 fi
337 rm -f $cooklist; touch $cooklist
338 echo -e "\nReverse cooklist for: $pkg"
339 separator
341 cd $wok
342 for rev in *; do
343 unset WANTED DEPENDS BUILD_DEPENDS; . $wok/$rev/receipt
344 if echo "$WANTED $DEPENDS $BUILD_DEPENDS" | fgrep -q $pkg; then
345 echo "$rev" | tee -a $cooklist
346 fi
347 done
348 separator
349 echo -e "Reverse dependencies found: $(cat $cooklist | wc -l)\n"
350 strip_blocked
351 cook_order | tee $LOGS/cookorder.log
352 cook_list ;;
354 pkg|-p)
355 # Same as 'cook pkg' but with log for web interface.
356 cook $pkg || broken
357 clean_exit ;;
359 cat|-c)
360 # Cook all packages of a category.
361 cat="$2"
362 rm -f $cooklist; touch $cooklist
364 cd $wok
365 for pkg in *; do
366 unset CATEGORY; . $pkg/receipt
367 [ "$CATEGORY" == "$cat" ] && echo $pkg >> $cooklist
368 done
369 strip_blocked
370 cook_order | tee $LOGS/cookorder.log
371 cook_list ;;
373 flavor|-f)
374 # Cook all packages of a flavor.
375 name="$2"
376 if [ ! -d "$flavors/$name" ]; then
377 echo -e "\nSpecified flavor does not exist: $name\n"
378 exit 1
379 fi
380 if [ -d "$flavors/.hg" ]; then
381 cd $flavors; hg pull -u
382 fi
383 list="$flavors/$name/packages.list"
384 cp -a $list $cooklist
385 strip_blocked
386 cook_order | tee $LOGS/cookorder.log
387 cook_list ;;
389 list|-l)
390 # Cook a list of packages given in argument.
391 list="$2"
392 if [ ! -f "$list" ]; then
393 echo -e "\nSpecified list does not exist: $list\n"
394 exit 1
395 fi
396 cp -a $list $cooklist
397 strip_blocked
398 cook_order | tee $LOGS/cookorder.log
399 cook_list ;;
401 rev|-r)
402 # Cook or recook a specific Hg revision.
403 rev="$2"
404 [ "$rev" ] || exit 0
405 rm -f $cooklist; touch $cooklist
407 cd $wok
408 for pkg in $(hg log --rev=$rev --template "{files}"); do
409 echo "$pkg" | cut -d/ -f1 >> $cooklist
410 done
411 strip_blocked
412 cook_order | tee $LOGS/cookorder.log
413 cook_list ;;
415 all|-a)
416 # Try to build all unbuilt packages except blocked's.
417 echo 'cooker:all' > $command
418 rm -f $cooklist; touch $cooklist
419 newline
420 echo 'Cooker cooklist'
421 separator
423 # Find all unbuilt packages. Get EXTRAVERSION from packed receipt
424 # if it exists since extra version is added when packing the package.
425 echo 'Searching for all unbuilt packages' | log
427 cd $wok
428 for pkg in *; do
429 unset EXTRAVERSION
430 . $pkg/receipt
431 [ -f "$pkg/taz/$PACKAGE-$VERSION/receipt" ] && \
432 . $pkg/taz/$PACKAGE-$VERSION/receipt
433 if [ ! -f "$PKGS/$PACKAGE-$VERSION$EXTRAVERSION.tazpkg" ]; then
434 echo $pkg; echo $pkg >> $cooklist
435 fi
436 done
437 strip_blocked
438 cook_order | tee $LOGS/cookorder.log
439 echo "Packages to cook: $(cat $cooklist | wc -l)" | log
440 cook_list ;;
442 *)
443 # Default is to cook all commits if not yet running.
444 [ -n "$1" ] && usage
445 cooklist=$commits
446 if [ -f "$pidfile" ]; then
447 pid=$(cat $pidfile)
448 if [ -s /proc/$pid/status ]; then
449 echo -e "\nStill cooking latest commits with pid:"
450 echo -e " $pid\n"
451 exit 0
452 fi
453 rm -f "$pidfile"
454 fi
456 # Start and get a PID file.
457 rm -f $LOGS/commits.log
458 newline
459 echo 'Checking for commits' | log_commits
460 separator | tee -a $LOGS/commits.log
462 echo $$ > $pidfile
463 trap 'echo -e "\nCooker stopped: PID $$\n" && \
464 rm -f $pidfile $command && exit 1' INT TERM
466 echo "Cooker PID : $$" | log_commits
467 echo "Cooker date : $(date '+%F %T')" | log_commits
469 # Get revisions. Here we have 2 echoes since we want a msg on screen,
470 # in commits log and activity DB without a space before.
471 cd $wok || exit 1
472 cur=$(hg head --template '{rev}\n')
473 echo "Updating wok : ${wok}-hg (rev $cur)" | log_commits
474 echo "Updating wok: ${wok}-hg" | log
475 echo 'hg:pull' > $command
476 cd $wok-hg; hg pull -u | log_commits
477 new=$(hg head --template '{rev}\n')
478 # Store last rev to be used by CGI so it doesn't need to call hg head
479 # on each load.
480 echo "$new" > $wokrev
482 # Sync build wok with rsync so we don't take care about removing old
483 # files as before.
484 if [ "$new" -gt "$cur" ]; then
485 echo "Changes found from: $cur to $new" | log
486 echo 'Syncing build wok with Hg wok...' | log_commits
487 rsync -r -t -c -l -u -v -D -E $wok-hg/ $wok/ | \
488 sed '/^$/'d | log_commits
489 else
490 echo "No revision changes: $cur vs $new" | log
491 separator | log_commits
492 clean_exit; newline
493 exit 0
494 fi
496 # Get and display modifications.
497 cd $wok-hg
498 commits_summary | log_commits
499 cur=$(($cur + 1))
500 rm -f $commits.tmp; touch $commits.tmp
501 for rev in $(seq $cur $new); do
502 for file in $(hg log --rev=$rev --template "{files}"); do
503 pkg=$(echo $file | cut -d/ -f1)
504 desc=$(hg log --rev=$rev --template "{desc}" $file)
505 echo "Committed package : $pkg - $desc" | log_commits
506 echo $pkg >> $commits.tmp
507 done
508 done
510 # We may have deleted packages and files in stuff/. Remove it and
511 # clean DB as well as log file.
512 cd $wok
513 for pkg in *; do
514 if [ ! -d "${wok}-hg/$pkg" ]; then
515 echo "Removing package: $pkg" | log_commits
516 . $wok/$pkg/receipt
517 rm -rf $PKGS/$PACKAGE-$VERSION* $wok/$pkg $LOGS/$pkg.log
518 sed -i "/^${pkg}$/"d $blocked $broken $commits.tmp
519 fi
520 done
522 # Keep previous commit and discard duplicate lines
523 cat $commits $commits.tmp | sed /"^$"/d > $commits.new
524 uniq $commits.new > $commits; rm $commits.*
526 # Handle cross compilation. Create arch packages DB and remove pkgs
527 # not cooked for this arch from the commits list.
528 arch_db
529 for pkg in $(cat $commits); do
530 if [ ! -f "$wok/$pkg/arch.$ARCH" ]; then
531 echo "Cooker arch : skip $pkg (not included in: $ARCH)" | \
532 log_commits
533 sed -i "/^${pkg}$/"d $commits
534 else
535 echo "Cooker arch : $ARCH" | log_commits
536 fi
537 done
539 # Stats
540 pkgs=$(cat $commits | wc -l)
541 echo "Packages to cook: $pkgs" | log
542 echo "Packages to cook : $pkgs" | log_commits
543 separator | log_commits
544 newline
545 strip_blocked
546 cook_order | tee $LOGS/cookorder.log
547 cook_commits
548 clean_exit ;;
549 esac
551 exit 0