cookutils view cooker @ rev 765

cooker: do not recook while cooking...
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Oct 10 14:17:56 2015 +0200 (2015-10-10)
parents 91cb129f0b7b
children a537875d79f9
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 ps | grep -q "${previous_command/:/ }" &&
62 echo "$previous_command" > $command
63 rm -f $pidfile
64 }
67 # Summary for commits.
69 commits_summary() {
70 msg="from revision $cur to $new"
71 [ "$new" == "$cur" ] && msg="revision $new"
72 echo "Will cook $msg"
73 separator
74 echo -e "\nSummary for commits"
75 separator
76 echo "Hg wok revision : $cur"
77 echo "Pulled revision : $new"
78 echo "Check date : $(date '+%F %T')"
79 }
82 # Scan packages build deps and fill up cookorder list.
84 cook_order_scan() {
85 rm -f $cookorder
86 touch $cookorder
87 for pkg in $(cat $cooklist); do
88 unset WANTED BUILD_DEPENDS
89 . $wok/$pkg/receipt
90 # The :: is for web interface color.
91 [ "$WANTED$BUILD_DEPENDS" ] && echo "$pkg :: $WANTED $BUILD_DEPENDS"
92 for dep in $WANTED $BUILD_DEPENDS; do
93 if grep -q "^$dep$" $cooklist; then
94 if ! grep -q "^$dep$" $cookorder; then
95 echo "$dep" >> $cookorder
96 fi
97 fi
98 done
99 done
101 # Append unordered packages to cookorder.
102 for pkg in $(cat $cooklist); do
103 if ! grep -q "^$pkg$" $cookorder; then
104 echo "$pkg" >> $cookorder
105 fi
106 done
107 }
110 # Scan and rescan until the cooklist is ordered then handle WANTED.
112 cook_order() {
113 time=$(date +%s)
114 scan=0
116 # Keep an original cooklist so we do a diff when ordering is finished.
117 cp -f $cooklist $cooklist.0
118 echo 'cookorder' > $command
119 echo -e '\nInitial Cooker order scan'
120 separator
121 cook_order_scan
123 # Diff between the cooklist and new ordered list ? So copy the last
124 # cookorder to cooklist and rescan it.
125 while /bin/true; do
126 diff $cooklist $cookorder > $cookorder.diff
127 if [ -s "$cookorder.diff" ]; then
128 scan=$(($scan + 1))
129 echo -e "\nDiff scan: $scan"
130 separator
131 mv -f $cookorder $cooklist
132 cook_order_scan
133 else
134 break
135 fi
136 done
138 # Keep a diff between submitted cooklist and the ordered.
139 diff $cooklist.0 $cooklist > $cooklist.diff
140 rm -f $cookorder $cookorder.diff $cooklist.0
142 # Scan finished: append pkg to WANTED or leave it in the ordered cooklist.
143 # TODO: grep the line number to get pkg position and keep it higher.
144 echo -e '\nHandle WANTED package'
145 separator
146 for pkg in $(cat $cooklist); do
147 unset WANTED
148 . $wok/$pkg/receipt
149 for wanted in $WANTED; do
150 echo "$pkg :: $wanted"
151 if grep -q ^${wanted}$ $cooklist; then
152 sed -i -e "/^$pkg$/"d \
153 -e "/^$wanted$/ a $pkg" $cooklist
154 fi
155 done
156 done
158 # Show ordered cooklist
159 echo -e '\nCooklist order'
160 separator
161 cat $cooklist
162 separator
164 time=$(($(date +%s) - $time))
165 pkgs=$(cat $cooklist | wc -l)
166 echo -e '\nSummary for cookorder'
167 separator
168 cat <<EOT
169 Ordered packages : $pkgs
170 Scans executed : $scan
171 Scan duration : ${time}s
172 EOT
173 separator
175 rm -f $command
176 }
179 # Remove blocked (faster this way than grepping before).
181 strip_blocked() {
182 for pkg in $(cat $blocked); do
183 sed -i /^${pkg}$/d $cooklist
184 done
185 sed -i /^$/d $cooklist
186 }
189 # Use in default mode and with all cmd.
191 cook_commits() {
192 if [ -s "$commits" ]; then
193 for pkg in $(cat $commits); do
194 ps | grep -q "cook $pkg$" && continue
195 echo "cook:$pkg" > $command
196 cook $pkg || broken
197 sed -i /^${pkg}$/d $commits
198 done
199 fi
200 }
203 # Cook all packages in a cooklist.
205 cook_list() {
206 for pkg in $(cat $cooklist); do
207 ps | grep -q "cook $pkg$" && continue
208 cook $pkg || broken
209 sed -i /^${pkg}$/d $cooklist
210 done
211 }
214 # Create a arch.$ARCH file for each package cooked for the target host
215 # architecture
216 #
217 # The deal: we dont want all packages handled by cooker commands and stats,
218 # since we dont cross compile all packages for each arch but only a set of
219 # packages to provide one full featured desktop, servers and goodies useful
220 # for the host system.
221 #
223 arch_db() {
224 count=0
225 echo "Cleaning packages DB : arch.$ARCH"
226 rm -f $wok/*/arch.$ARCH && cd $wok
227 echo "Creating $ARCH packages DB..."
228 for pkg in *; do
229 HOST_ARCH=
230 . $wok/$pkg/receipt
231 if [ -n "$HOST_ARCH" ]; then
232 if $(echo "$HOST_ARCH" | egrep -q "$ARCH|any"); then
233 count=$(($count + 1))
234 echo "Adding: $pkg"
235 touch $pkg/arch.$ARCH
236 fi
237 unset HOST_ARCH
238 else
239 # HOST_ARCH not set --> in i486
240 if [ "$ARCH" == 'i486' ]; then
241 count=$(($count + 1))
242 echo "Adding: $pkg"
243 touch $pkg/arch.$ARCH
244 fi
245 fi
246 done
247 echo "Packages for $ARCH : $count"
248 }
251 #
252 # Commands
253 #
255 previous_command="$(cat $command)"
256 case "$1" in
257 usage|help|-u|-h)
258 usage ;;
260 setup|-s)
261 # Setup the Cooker environment.
262 echo -e '\nSetting up the Cooker'
263 mkdir -p $CACHE
264 echo "Cooker setup using: $SLITAZ" | log
265 separator
266 for pkg in $SETUP_PKGS mercurial rsync tazlito; do
267 [ ! -d "$INSTALLED/$pkg" ] && tazpkg get-install $pkg
268 done
269 mkdir -p $SLITAZ && cd $SLITAZ
270 if [ -d "${wok}-hg" ]; then
271 echo -e 'Hg wok already exists.\n'
272 exit 1
273 fi
274 if [ -d "$wok" ]; then
275 echo -e 'Build wok already exists.\n'
276 exit 1
277 fi
279 # Directories and files
280 echo "mkdir's and touch files in: $SLITAZ"
281 mkdir -p $PKGS $LOGS $FEEDS $CACHE $SRC
282 for f in $activity $blocked $broken $commits $cooklist $command; do
283 touch $f
284 done
285 hg clone $WOK_URL ${wok}-hg || exit 1
286 [ -d "$flavors" ] || hg clone $FLAVORS_URL flavors
287 cp -a ${wok}-hg $wok
288 separator; newline ;;
290 arch-db)
291 # Manually create arch packages DB.
292 arch_db ;;
294 setup-cron)
295 # Create cron job for the cooker.
296 [ "$2" ] || hours=2
297 if [ ! -f "$crontabs" ]; then
298 mkdir -p /var/spool/cron/crontabs
299 fi
300 if ! fgrep -q /usr/bin/cooker $crontabs; then
301 cat > $crontabs << EOT
302 # Run SliTaz Cooker every $hours hours
303 0 */$hours * * * /usr/bin/cooker --output=html
304 */5 * * * * [ $CACHE/cooker-request -nt $CACHE/activity ] && /usr/bin/cooker --output=html
305 EOT
306 touch $CACHE/cooker-request
307 chmod 666 $CACHE/cooker-request
308 killall crond 2>/dev/null && /etc/init.d/crond start
309 fi ;;
311 check-cron)
312 if [ ! -f "$crontabs" ]; then
313 echo "There is no $crontabs here. Use setup-cron option."
314 exit 1
315 fi
316 fgrep /usr/bin/cooker $crontabs ;;
318 note|-n)
319 # Blocked a pkg and want others to know why? Post a note!
320 [ -n "$2" ] && echo "$(date '+%F %R') : $2" >> $cooknotes ;;
322 notes|-ns)
323 # View cooknotes.
324 echo -e '\nCooknotes'
325 separator
326 cat $cooknotes
327 separator; newline ;;
329 block|-b)
330 # Block a package.
331 [ "$pkg" ] && cook $pkg --block ;;
333 unblock|-ub)
334 # Unblock a package.
335 [ "$pkg" ] && cook $pkg --unblock ;;
337 reverse|-r)
338 # Cook all reverse dependencies for a package. This command lets us
339 # control the Cooker manually for commits that will cook a lot of packages.
340 #
341 # Use hg commit? Ex: hg commit -m "Message bla bla | cooker:reverse"
342 #
343 if [ ! -d "$wok/$pkg" ]; then
344 echo -e "\nNo package $2 found.\n"
345 exit 0
346 fi
347 rm -f $cooklist; touch $cooklist
348 echo -e "\nReverse cooklist for: $pkg"
349 separator
351 cd $wok
352 for rev in *; do
353 unset WANTED DEPENDS BUILD_DEPENDS; . $wok/$rev/receipt
354 if echo "$WANTED $DEPENDS $BUILD_DEPENDS" | fgrep -q $pkg; then
355 echo "$rev" | tee -a $cooklist
356 fi
357 done
358 separator
359 echo -e "Reverse dependencies found: $(cat $cooklist | wc -l)\n"
360 strip_blocked
361 cook_order | tee $LOGS/cookorder.log
362 cook_list ;;
364 pkg|-p)
365 # Same as 'cook pkg' but with log for web interface.
366 ps | grep -q "cook $pkg$" && echo 'Already running' && continue
367 cook $pkg || broken
368 clean_exit ;;
370 cat|-c)
371 # Cook all packages of a category.
372 cat="$2"
373 rm -f $cooklist; touch $cooklist
375 cd $wok
376 for pkg in *; do
377 unset CATEGORY; . $pkg/receipt
378 [ "$CATEGORY" == "$cat" ] && echo $pkg >> $cooklist
379 done
380 strip_blocked
381 cook_order | tee $LOGS/cookorder.log
382 cook_list ;;
384 flavor|-f)
385 # Cook all packages of a flavor.
386 name="$2"
387 if [ ! -d "$flavors/$name" ]; then
388 echo -e "\nSpecified flavor does not exist: $name\n"
389 exit 1
390 fi
391 if [ -d "$flavors/.hg" ]; then
392 cd $flavors; hg pull -u
393 fi
394 list="$flavors/$name/packages.list"
395 cp -a $list $cooklist
396 strip_blocked
397 cook_order | tee $LOGS/cookorder.log
398 cook_list ;;
400 list|-l)
401 # Cook a list of packages given in argument.
402 list="$2"
403 if [ ! -f "$list" ]; then
404 echo -e "\nSpecified list does not exist: $list\n"
405 exit 1
406 fi
407 cp -a $list $cooklist
408 strip_blocked
409 cook_order | tee $LOGS/cookorder.log
410 cook_list ;;
412 rev|-r)
413 # Cook or recook a specific Hg revision.
414 rev="$2"
415 [ "$rev" ] || exit 0
416 rm -f $cooklist; touch $cooklist
418 cd $wok
419 for pkg in $(hg log --rev=$rev --template "{files}"); do
420 echo "$pkg" | cut -d/ -f1 >> $cooklist
421 done
422 strip_blocked
423 cook_order | tee $LOGS/cookorder.log
424 cook_list ;;
426 all|-a)
427 # Try to build all unbuilt packages except blocked's.
428 echo 'cooker:all' > $command
429 rm -f $cooklist; touch $cooklist
430 newline
431 echo 'Cooker cooklist'
432 separator
434 # Find all unbuilt packages. Get EXTRAVERSION from packed receipt
435 # if it exists since extra version is added when packing the package.
436 echo 'Searching for all unbuilt packages' | log
438 cd $wok
439 for pkg in *; do
440 unset EXTRAVERSION
441 . $pkg/receipt
442 [ -f "$pkg/taz/$PACKAGE-$VERSION/receipt" ] && \
443 . $pkg/taz/$PACKAGE-$VERSION/receipt
444 if [ ! -f "$PKGS/$PACKAGE-$VERSION$EXTRAVERSION.tazpkg" ]; then
445 echo $pkg; echo $pkg >> $cooklist
446 fi
447 done
448 strip_blocked
449 cook_order | tee $LOGS/cookorder.log
450 echo "Packages to cook: $(cat $cooklist | wc -l)" | log
451 cook_list ;;
453 *)
454 # Default is to cook all commits if not yet running.
455 [ -n "$1" ] && usage
456 cooklist=$commits
457 if [ -f "$pidfile" ]; then
458 pid=$(cat $pidfile)
459 if [ -s /proc/$pid/status ]; then
460 echo -e "\nStill cooking latest commits with pid:"
461 echo -e " $pid\n"
462 exit 0
463 fi
464 rm -f "$pidfile"
465 fi
467 # Start and get a PID file.
468 rm -f $LOGS/commits.log
469 newline
470 echo 'Checking for commits' | log_commits
471 separator | tee -a $LOGS/commits.log
473 echo $$ > $pidfile
474 trap 'echo -e "\nCooker stopped: PID $$\n" && \
475 rm -f $pidfile $command && exit 1' INT TERM
477 echo "Cooker PID : $$" | log_commits
478 echo "Cooker date : $(date '+%F %T')" | log_commits
480 # Get revisions. Here we have 2 echoes since we want a msg on screen,
481 # in commits log and activity DB without a space before.
482 cd $wok || exit 1
483 cur=$(hg head --template '{rev}\n')
484 echo "Updating wok : ${wok}-hg (rev $cur)" | log_commits
485 echo "Updating wok: ${wok}-hg" | log
486 echo 'hg:pull' > $command
487 cd $wok-hg; hg pull -u | log_commits
488 new=$(hg head --template '{rev}\n')
489 # Store last rev to be used by CGI so it doesn't need to call hg head
490 # on each load.
491 echo "$new" > $wokrev
493 # Sync build wok with rsync so we don't take care about removing old
494 # files as before.
495 if [ "$new" -gt "$cur" ]; then
496 echo "Changes found from: $cur to $new" | log
497 echo 'Syncing build wok with Hg wok...' | log_commits
498 rsync -r -t -c -l -u -v -D -E $wok-hg/ $wok/ | \
499 sed '/^$/'d | log_commits
500 else
501 echo "No revision changes: $cur vs $new" | log
502 separator | log_commits
503 clean_exit; newline
504 exit 0
505 fi
507 # Get and display modifications.
508 cd $wok-hg
509 commits_summary | log_commits
510 cur=$(($cur + 1))
511 rm -f $commits.tmp; touch $commits.tmp
512 for rev in $(seq $cur $new); do
513 for file in $(hg log --rev=$rev --template "{files}"); do
514 pkg=$(echo $file | cut -d/ -f1)
515 desc=$(hg log --rev=$rev --template "{desc}" $file)
516 echo "Committed package : $pkg - $desc" | log_commits
517 echo $pkg >> $commits.tmp
518 done
519 done
521 # We may have deleted packages and files in stuff/. Remove it and
522 # clean DB as well as log file.
523 cd $wok
524 for pkg in *; do
525 if [ ! -d "${wok}-hg/$pkg" ]; then
526 echo "Removing package: $pkg" | log_commits
527 . $wok/$pkg/receipt
528 rm -rf $PKGS/$PACKAGE-$VERSION* $wok/$pkg $LOGS/$pkg.log
529 sed -i "/^${pkg}$/"d $blocked $broken $commits.tmp
530 fi
531 done
533 # Keep previous commit and discard duplicate lines
534 cat $commits $commits.tmp | sed /"^$"/d > $commits.new
535 uniq $commits.new > $commits; rm $commits.*
537 # Handle cross compilation. Create arch packages DB and remove pkgs
538 # not cooked for this arch from the commits list.
539 arch_db
540 for pkg in $(cat $commits); do
541 if [ ! -f "$wok/$pkg/arch.$ARCH" ]; then
542 echo "Cooker arch : skip $pkg (not included in: $ARCH)" | \
543 log_commits
544 sed -i "/^${pkg}$/"d $commits
545 else
546 echo "Cooker arch : $ARCH" | log_commits
547 fi
548 done
550 # Stats
551 pkgs=$(cat $commits | wc -l)
552 echo "Packages to cook: $pkgs" | log
553 echo "Packages to cook : $pkgs" | log_commits
554 separator | log_commits
555 newline
556 strip_blocked
557 cook_order | tee $LOGS/cookorder.log
558 cook_commits
559 clean_exit ;;
560 esac
562 exit 0