cookutils view cooker @ rev 349

cooker: small fix to setup
author Christophe Lincoln <pankso@slitaz.org>
date Tue Apr 10 16:58:31 2012 +0200 (2012-04-10)
parents 4f9d23e13883
children e4f6883a36fc
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 [ -f "/etc/slitaz/cook.conf" ] && . /etc/slitaz/cook.conf
11 [ -f "cook.conf" ] && . ./cook.conf
13 # Set pkg name and use same wok as cook.
14 pkg="$2"
15 wok="$WOK"
16 flavors="$SLITAZ/flavors"
18 # Cooker DB files.
19 activity="$CACHE/activity"
20 commits="$CACHE/commits"
21 cooklist="$CACHE/cooklist"
22 cookorder="$CACHE/cookorder"
23 command="$CACHE/command"
24 blocked="$CACHE/blocked"
25 broken="$CACHE/broken"
26 cooknotes="$CACHE/cooknotes"
27 crontabs="/var/spool/cron/crontabs/root"
29 # PID file.
30 pidfile='/var/run/cooker.pid'
32 #
33 # Functions
34 #
36 usage() {
37 cat << EOT
39 Usage: cooker [command] [pkg|list|note|hours]
41 Options:
42 usage|-u Display this short usage.
43 setup|-s Setup the Cooker environment.
44 setup-cron Setup a cron job for the Cooker.
45 note|-n Add a note to the cooknotes.
46 notes|-ns Display all the cooknotes.
47 block|-b Block a package so cook will skip it.
48 unblock|-ub Unblock a blocked package.
49 pkg|-p Same as 'cook pkg' but with cooker log.
50 flavor|-f Cook all packages of a flavor.
51 list|-l Cook all packages in the given list.
52 cat|-c Cook all packages of a category.
53 rev|-r Cook packages of a specific revision.
54 all|-a Find and cook all unbuilt packages.
56 EOT
57 exit 0
58 }
60 separator() {
61 echo "================================================================================"
62 }
64 # Lograte activity.
65 [ -s "$activity" ] && tail -n 60 $activity > /tmp/tail && \
66 mv -f /tmp/tail $activity
68 # Log activities, we want first letter capitalized.
69 log() {
70 grep ^[A-Z] | \
71 sed s"#^[A-Z]\([^']*\)#$(date '+%Y-%m-%d %H:%M') : \0#" >> $activity
72 }
74 # Some messages occur in activity but log verbose output when checking for commits
75 # into a log file.
76 log_commits() {
77 sed '/^.\//'d | sed '/^.hg/'d | tee -a $LOGS/commits.log
78 }
80 # Log broken packages.
81 broken() {
82 if ! grep -q "^$pkg$" $broken; then
83 echo "$pkg" >> $broken
84 fi
85 }
87 # Clean up before exit when check and cook commits finish.
88 clean_exit() {
89 rm -f $command && touch $command
90 rm -f $pidfile
91 }
93 # Summary for commits.
94 commits_summary() {
95 msg="from revision $cur to $new"
96 [ "$new" == "$cur" ] && msg="revision $new"
97 echo "Will cook $msg"
98 separator
99 echo -e "\nSummary for commits"
100 separator
101 echo "Hg wok revision : $cur"
102 echo "Pulled revision : $new"
103 echo "Check date : $(date '+%Y-%m-%d %H:%M:%S')"
104 }
106 # Scan packages build deps and fill up cookorder list.
107 cook_order_scan() {
108 rm -f $cookorder
109 for pkg in $(cat $cooklist)
110 do
111 unset WANTED BUILD_DEPENDS
112 . $wok/$pkg/receipt
113 # The :: is for web interface color.
114 [ "$WANTED$BUILD_DEPENDS" ] && echo "$pkg :: $WANTED $BUILD_DEPENDS"
115 for dep in $WANTED $BUILD_DEPENDS
116 do
117 if grep -q "^$dep$" $cooklist; then
118 if ! grep -q "^$dep$" $cookorder; then
119 echo "$dep" >> $cookorder
120 fi
121 fi
122 done
123 done
125 # Append unordered packages to cookorder.
126 for pkg in $(cat $cooklist)
127 do
128 if ! grep -q "^$pkg$" $cookorder; then
129 echo "$pkg" >> $cookorder
130 fi
131 done
132 }
134 # Scan and rescan until the cooklist is ordered then handle WANTED.
135 cook_order() {
136 time=$(date +%s)
137 scan=0
139 # Keep an original cooklist so we do a diff when ordering is finished.
140 cp -f $cooklist $cooklist.0
141 echo "cookorder" > $command
142 echo -e "\nInitial Cooker order scan"
143 separator
144 cook_order_scan
146 # Diff between the cooklist and new ordered list ? So copy the last
147 # cookorder to cooklist and rescan it.
148 while /bin/true
149 do
150 diff $cooklist $cookorder > $cookorder.diff
151 if [ -s "$cookorder.diff" ]; then
152 scan=$(($scan + 1))
153 echo -e "\nDiff scan: $scan"
154 separator
155 mv -f $cookorder $cooklist
156 cook_order_scan
157 else
158 break
159 fi
160 done
162 # Keep a diff between submited cooklist and the ordered.
163 diff $cooklist.0 $cooklist > $cooklist.diff
164 rm -f $cookorder $cookorder.diff $cooklist.0
166 # Scan finished: append pkg to WANTED or leave it in the ordered cooklist.
167 # TODO: grep the line number to get pkg position and keep it higher.
168 echo -e "\nHandle WANTED package"
169 separator
170 for pkg in $(cat $cooklist)
171 do
172 unset WANTED
173 . $wok/$pkg/receipt
174 for wanted in $WANTED
175 do
176 echo "$pkg :: $wanted"
177 if grep -q ^${wanted}$ $cooklist; then
178 sed -i -e "/^$pkg$/"d \
179 -e "/^$wanted$/ a $pkg" $cooklist
180 fi
181 done
182 done
184 # Show ordered cooklist
185 echo -e "\nCooklist order"
186 separator
187 cat $cooklist
188 separator
189 time=$(($(date +%s) - $time))
190 pkgs=$(cat $cooklist | wc -l)
191 echo -e "\nSummary for cookorder"
192 separator
193 cat << EOT
194 Ordered packages : $pkgs
195 Scans executed : $scan
196 Scan duration : ${time}s
197 EOT
198 separator && rm -f $command
199 }
201 # Remove blocked (faster this way than grepping before).
202 strip_blocked() {
203 for pkg in $(cat $blocked)
204 do
205 sed -i /^${pkg}$/d $cooklist
206 done && sed -i /^$/d $cooklist
207 }
209 # Use in default mode and with all cmd.
210 cook_commits() {
211 if [ -s "$commits" ]; then
212 for pkg in $(cat $commits)
213 do
214 echo "cook:$pkg" > $command
215 cook $pkg || broken
216 sed -i /^${pkg}$/d $commits
217 done
218 fi
219 }
221 # Cook all packages in a cooklist.
222 cook_list() {
223 for pkg in $(cat $cooklist)
224 do
225 cook $pkg || broken
226 sed -i /^${pkg}$/d $cooklist
227 done
228 }
230 #
231 # Commands
232 #
233 case "$1" in
234 usage|help|-u|-h)
235 usage ;;
236 setup|-s)
237 # Setup the Cooker environment.
238 echo -e "\nSetting up the Cooker"
239 mkdir -p $CACHE
240 echo "Cooker setup using: $SLITAZ" | log
241 separator
242 for pkg in $SETUP_PKGS mercurial rsync tazlito
243 do
244 [ ! -d "$INSTALLED/$pkg" ] && tazpkg get-install $pkg
245 done
246 mkdir -p $SLITAZ && cd $SLITAZ
247 [ -d "${wok}-hg" ] && echo -e "Hg wok already exists.\n" && exit 1
248 [ -d "$wok" ] && echo -e "Build wok already exists.\n" && exit 1
250 # Directories and files
251 echo "mkdir's and touch files in: $SLITAZ"
252 mkdir -p $PKGS $LOGS $FEEDS $CACHE $SRC
253 for f in $activity $blocked $broken $commits $cooklist $command
254 do
255 touch $f
256 done
257 hg clone $WOK_URL ${wok}-hg || exit 1
258 [ -d "$flavors" ] || hg clone $FLAVORS_URL flavors
259 cp -a ${wok}-hg $wok
260 separator && echo "" ;;
261 setup-cron)
262 # Create cron job for the cooker.
263 [ "$2" ] || hours=2
264 if [ ! -f "$crontabs" ]; then
265 mkdir -p /var/spool/cron/crontabs
266 echo "# Run SliTaz Cooker every $hours hours" > $crontabs
267 echo "0 */$hours * * * /usr/bin/cooker" >> $crontabs
268 /etc/init.d/crond start
269 fi
270 if ! fgrep -q /usr/bin/cooker $crontabs; then
271 echo "# Run SliTaz Cooker every $hours hours" > $crontabs
272 echo "0 */$hours * * * /usr/bin/cooker" >> $crontabs
273 killall crond 2>/dev/null && /etc/init.d/crond start
274 fi ;;
275 check-cron)
276 fgrep /usr/bin/cooker $crontabs ;;
277 note|-n)
278 # Blocked a pkg and want others to know why ? Post a note!
279 note="$2"
280 date=$(date "+%Y-%m-%d %H:%M")
281 [ "$note" ] && echo "$date : $note" >> $cooknotes ;;
282 notes|-ns)
283 # View cooknotes.
284 echo -e "\nCooknotes"
285 separator
286 cat $cooknotes
287 separator && echo "" ;;
288 block|-b)
289 # Block a package.
290 [ "$pkg" ] && cook $pkg --block ;;
291 unblock|-ub)
292 # Unblock a package.
293 [ "$pkg" ] && cook $pkg --unblock ;;
294 reverse|-r)
295 # Cook all reverse dependencies for a package. This command lets us
296 # control the Cooker manually for commits that will cook a lot of packages.
297 #
298 # Use hg commit ? Ex: hg commit -m "Message bla bla | cooker:reverse"
299 #
300 [ ! -d "$wok/$pkg" ] && echo -e "\nNo package $2 found.\n" && exit 0
301 rm -f $cooklist && touch $cooklist && cd $wok
302 echo -e "\nReverse cooklist for: $pkg"
303 separator && cd $wok
304 for rev in *
305 do
306 unset WANTED DEPENDS BUILD_DEPENDS && . $wok/$rev/receipt
307 if echo "$WANTED $DEPENDS $BUILD_DEPENDS" | fgrep -q $pkg; then
308 echo "$rev" | tee -a $cooklist
309 fi
310 done && separator
311 echo -e "Reverse dependencies found: $(cat $cooklist | wc -l)\n"
312 strip_blocked
313 cook_order | tee $LOGS/cookorder.log
314 cook_list ;;
315 pkg|-p)
316 # Same as 'cook pkg' but with log for web interface.
317 cook $pkg || broken
318 clean_exit ;;
319 cat|-c)
320 # Cook all packages of a category.
321 cat="$2"
322 rm -f $cooklist && touch $cooklist && cd $wok
323 for pkg in *
324 do
325 unset CATEGORY && . $pkg/receipt
326 [ "$CATEGORY" == "$cat" ] && echo $pkg >> $cooklist
327 done
328 strip_blocked
329 cook_order | tee $LOGS/cookorder.log
330 cook_list ;;
331 flavor|-f)
332 # Cook all packages of a flavor.
333 name="$2"
334 [ ! -d "$flavors/$name" ] && \
335 echo -e "\nSpecified flavor does not exist: $name\n" && exit 1
336 [ -d "$flavors/.hg" ] && cd $flavors && hg pull -u
337 list=$flavors/$name/packages.list
338 cp -a $list $cooklist
339 strip_blocked
340 cook_order | tee $LOGS/cookorder.log
341 cook_list ;;
342 list|-l)
343 # Cook a list of packages given in argument.
344 list="$2"
345 [ ! -f "$list" ] && \
346 echo -e "\nSpecified list does not exist: $list\n" && exit 1
347 cp -a $list $cooklist
348 strip_blocked
349 cook_order | tee $LOGS/cookorder.log
350 cook_list ;;
351 rev|-r)
352 # Cook or recook a specific Hg revision.
353 rev="$2"
354 [ "$rev" ] || exit 0
355 cd $wok
356 rm -f $cooklist && touch $cooklist
357 for pkg in $(hg log --rev=$rev --template "{files}")
358 do
359 echo "$pkg" | cut -d "/" -f 1 >> $cooklist
360 done
361 strip_blocked
362 cook_order | tee $LOGS/cookorder.log
363 cook_list ;;
364 all|-a)
365 # Try to build all unbuilt packages except blocked's.
366 echo "cooker:all" > $command
367 rm -f $cooklist && touch $cooklist
368 echo "" && cd $wok
369 echo "Cooker cooklist"
370 separator
372 # Find all unbuilt packages. Get EXTRAVERSION from packed receipt
373 # if it exists since extra version is added when packing the package.
374 echo "Searching for all unbuilt packages" | log
375 for pkg in *
376 do
377 unset EXTRAVERSION
378 . $pkg/receipt
379 [ -f "$pkg/taz/$PACKAGE-$VERSION/receipt" ] && \
380 . $pkg/taz/$PACKAGE-$VERSION/receipt
381 if [ ! -f "$PKGS/$PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg" ]
382 then
383 echo $pkg && echo $pkg >> $cooklist
384 fi
385 done
386 strip_blocked
387 cook_order | tee $LOGS/cookorder.log
388 echo "Packages to cook: $(cat $cooklist | wc -l)" | log
389 cook_list ;;
390 *)
391 # Default is to cook all commits if not yet running.
392 [ "$1" ] && usage
393 cooklist=$CACHE/commits
394 if [ -f "$pidfile" ]; then
395 pid=$(cat /var/run/cooker.pid)
396 if [ -s /proc/$pid/status ]; then
397 gettext -e "\nStill cooking latest commits with pid:"
398 echo -e " $pid\n" && exit 0
399 fi
400 rm -f "$pidfile"
401 fi
403 # Start and get a PID file.
404 rm -f $LOGS/commits.log
405 echo ""
406 echo "Checking for commits" | log_commits
407 separator | tee -a $LOGS/commits.log
409 echo $$ > $pidfile
410 trap 'echo -e "\nCooker stopped: PID $$\n" && \
411 rm -f $pidfile $command && exit 1' INT TERM
413 echo "Cooker PID : $$" | log_commits
414 echo "Cooker date : $(date '+%Y-%m-%d %H:%M:%S')" | log_commits
416 # Get revisions. Here we have 2 echoes since we want a msg on screen,
417 # in commits log and activity DB without a space before.
418 cd $wok || exit 1
419 cur=$(hg head --template '{rev}\n')
420 echo "Updating wok : ${wok}-hg (rev $cur)" | log_commits
421 echo "Updating wok: ${wok}-hg" | log
422 echo "hg:pull" > $command
423 cd ${wok}-hg && hg pull -u | log_commits
424 new=$(hg head --template '{rev}\n')
425 # Store last rev to be used by CGI so it doesn't need to call hg head
426 # on each load.
427 echo "$new" > $CACHE/wokrev
429 # Sync build wok with rsync so we don't take care about removing old
430 # files as before.
431 if [ "$new" -gt "$cur" ]; then
432 echo "Changes found from: $cur to $new" | log
433 echo "Syncing build wok with Hg wok..." | log_commits
434 rsync -r -t -c -l -u -v -D -E ${wok}-hg/ $wok/ | \
435 sed '/^$/'d | log_commits
436 else
437 echo "No revision changes: $cur vs $new" | log
438 separator | log_commits
439 clean_exit && echo "" && exit 0
440 fi
442 # Get and display modifications.
443 cd ${wok}-hg
444 commits_summary | log_commits
445 cur=$(($cur + 1))
446 rm -f $commits.tmp && touch $commits.tmp
447 for rev in $(seq $cur $new)
448 do
449 for file in $(hg log --rev=$rev --template "{files}")
450 do
451 pkg=$(echo $file | cut -d "/" -f 1)
452 desc=$(hg log --rev=$rev --template "{desc}" $file)
453 echo "Commited package : $pkg - $desc" | log_commits
454 echo $pkg >> $commits.tmp
455 done
456 done
458 # We may have deleted packages and files in stuff/. Remove it and
459 # clean DB as well as log file.
460 cd $wok
461 for pkg in *
462 do
463 if [ ! -d "${wok}-hg/$pkg" ]; then
464 echo "Removing package: $pkg" | log_commits
465 . $wok/$pkg/receipt
466 rm -rf $PKGS/$PACKAGE-$VERSION* $wok/$pkg $LOGS/$pkg.log
467 sed -i "/^${pkg}$/"d $CACHE/blocked $CACHE/broken $commits.tmp
468 fi
469 done
471 # Keep previous commit and discard duplicate lines
472 cat $commits $commits.tmp | sed /"^$"/d > $commits.new
473 uniq $commits.new > $commits && rm $commits.*
474 pkgs=$(cat $commits | wc -l)
475 echo "Packages to cook: $pkgs" | log
476 echo "Packages to cook : $pkgs" | log_commits
477 separator | log_commits
478 echo ""
479 strip_blocked
480 cook_order | tee $LOGS/cookorder.log
481 cook_commits
482 clean_exit ;;
483 esac
485 exit 0