cookutils view cooker @ rev 342

Tiny edits
author Paul Issott <paul@slitaz.org>
date Sun Mar 18 00:57:01 2012 +0000 (2012-03-18)
parents d77b53b9e478
children d57763119073
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 echo "Cooker setup using: $SLITAZ" | log
240 separator
241 for pkg in $SETUP_PKGS mercurial rsync tazlito
242 do
243 [ ! -d "$INSTALLED/$pkg" ] && tazpkg get-install $pkg
244 done
245 mkdir -p $SLITAZ && cd $SLITAZ
246 [ -d "${wok}-hg" ] && echo -e "Hg wok already exists.\n" && exit 1
247 [ -d "$wok" ] && echo -e "Build wok already exists.\n" && exit 1
249 # Directories and files
250 echo "mkdir's and touch files in: $SLITAZ"
251 mkdir -p $PKGS $LOGS $FEEDS $CACHE $SRC
252 for f in $activity $blocked $broken $commits $cooklist $command
253 do
254 touch $f
255 done
256 hg clone $WOK_URL ${wok}-hg || exit 1
257 [ -d "$flavors" ] || hg clone $FLAVORS_URL flavors
258 cp -a ${wok}-hg $wok
259 separator && echo "" ;;
260 setup-cron)
261 # Create cron job for the cooker.
262 [ "$2" ] || hours=2
263 if [ ! -f "$crontabs" ]; then
264 mkdir -p /var/spool/cron/crontabs
265 echo "# Run SliTaz Cooker every $hours hours" > $crontabs
266 echo "0 */$hours * * * /usr/bin/cooker" >> $crontabs
267 /etc/init.d/crond start
268 fi
269 if ! fgrep -q /usr/bin/cooker $crontabs; then
270 echo "# Run SliTaz Cooker every $hours hours" > $crontabs
271 echo "0 */$hours * * * /usr/bin/cooker" >> $crontabs
272 killall crond 2>/dev/null && /etc/init.d/crond start
273 fi ;;
274 check-cron)
275 fgrep /usr/bin/cooker $crontabs ;;
276 note|-n)
277 # Blocked a pkg and want others to know why ? Post a note!
278 note="$2"
279 date=$(date "+%Y-%m-%d %H:%M")
280 [ "$note" ] && echo "$date : $note" >> $cooknotes ;;
281 notes|-ns)
282 # View cooknotes.
283 echo -e "\nCooknotes"
284 separator
285 cat $cooknotes
286 separator && echo "" ;;
287 block|-b)
288 # Block a package.
289 [ "$pkg" ] && cook $pkg --block ;;
290 unblock|-ub)
291 # Unblock a package.
292 [ "$pkg" ] && cook $pkg --unblock ;;
293 reverse|-r)
294 # Cook all reverse dependencies for a package. This command lets us
295 # control the Cooker manually for commits that will cook a lot of packages.
296 #
297 # Use hg commit ? Ex: hg commit -m "Message bla bla | cooker:reverse"
298 #
299 [ ! -d "$wok/$pkg" ] && echo -e "\nNo package $2 found.\n" && exit 0
300 rm -f $cooklist && touch $cooklist && cd $wok
301 echo -e "\nReverse cooklist for: $pkg"
302 separator && cd $wok
303 for rev in *
304 do
305 unset WANTED DEPENDS BUILD_DEPENDS && . $wok/$rev/receipt
306 if echo "$WANTED $DEPENDS $BUILD_DEPENDS" | fgrep -q $pkg; then
307 echo "$rev" | tee -a $cooklist
308 fi
309 done && separator
310 echo -e "Reverse dependencies found: $(cat $cooklist | wc -l)\n"
311 strip_blocked
312 cook_order | tee $LOGS/cookorder.log
313 cook_list ;;
314 pkg|-p)
315 # Same as 'cook pkg' but with log for web interface.
316 cook $pkg || broken
317 clean_exit ;;
318 cat|-c)
319 # Cook all packages of a category.
320 cat="$2"
321 rm -f $cooklist && touch $cooklist && cd $wok
322 for pkg in *
323 do
324 unset CATEGORY && . $pkg/receipt
325 [ "$CATEGORY" == "$cat" ] && echo $pkg >> $cooklist
326 done
327 strip_blocked
328 cook_order | tee $LOGS/cookorder.log
329 cook_list ;;
330 flavor|-f)
331 # Cook all packages of a flavor.
332 name="$2"
333 [ ! -d "$flavors/$name" ] && \
334 echo -e "\nSpecified flavor does not exist: $name\n" && exit 1
335 [ -d "$flavors/.hg" ] && cd $flavors && hg pull -u
336 list=$flavors/$name/packages.list
337 cp -a $list $cooklist
338 strip_blocked
339 cook_order | tee $LOGS/cookorder.log
340 cook_list ;;
341 list|-l)
342 # Cook a list of packages given in argument.
343 list="$2"
344 [ ! -f "$list" ] && \
345 echo -e "\nSpecified list does not exist: $list\n" && exit 1
346 cp -a $list $cooklist
347 strip_blocked
348 cook_order | tee $LOGS/cookorder.log
349 cook_list ;;
350 rev|-r)
351 # Cook or recook a specific Hg revision.
352 rev="$2"
353 [ "$rev" ] || exit 0
354 cd $wok
355 rm -f $cooklist && touch $cooklist
356 for pkg in $(hg log --rev=$rev --template "{files}")
357 do
358 echo "$pkg" | cut -d "/" -f 1 >> $cooklist
359 done
360 strip_blocked
361 cook_order | tee $LOGS/cookorder.log
362 cook_list ;;
363 all|-a)
364 # Try to build all unbuilt packages except blocked's.
365 echo "cooker:all" > $command
366 rm -f $cooklist && touch $cooklist
367 echo "" && cd $wok
368 echo "Cooker cooklist"
369 separator
371 # Find all unbuilt packages. Get EXTRAVERSION from packed receipt
372 # if it exists since extra version is added when packing the package.
373 echo "Searching for all unbuilt packages" | log
374 for pkg in *
375 do
376 unset EXTRAVERSION
377 . $pkg/receipt
378 [ -f "$pkg/taz/$PACKAGE-$VERSION/receipt" ] && \
379 . $pkg/taz/$PACKAGE-$VERSION/receipt
380 if [ ! -f "$PKGS/$PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg" ]
381 then
382 echo $pkg && echo $pkg >> $cooklist
383 fi
384 done
385 strip_blocked
386 cook_order | tee $LOGS/cookorder.log
387 echo "Packages to cook: $(cat $cooklist | wc -l)" | log
388 cook_list ;;
389 *)
390 # Default is to cook all commits if not yet running.
391 [ "$1" ] && usage
392 cooklist=$CACHE/commits
393 if [ -f "$pidfile" ]; then
394 pid=$(cat /var/run/cooker.pid)
395 if [ -s /proc/$pid/status ]; then
396 gettext -e "\nStill cooking latest commits with pid:"
397 echo -e " $pid\n" && exit 0
398 fi
399 rm -f "$pidfile"
400 fi
402 # Start and get a PID file.
403 rm -f $LOGS/commits.log
404 echo ""
405 echo "Checking for commits" | log_commits
406 separator | tee -a $LOGS/commits.log
408 echo $$ > $pidfile
409 trap 'echo -e "\nCooker stopped: PID $$\n" && \
410 rm -f $pidfile $command && exit 1' INT TERM
412 echo "Cooker PID : $$" | log_commits
413 echo "Cooker date : $(date '+%Y-%m-%d %H:%M:%S')" | log_commits
415 # Get revisions. Here we have 2 echoes since we want a msg on screen,
416 # in commits log and activity DB without a space before.
417 cd $wok || exit 1
418 cur=$(hg head --template '{rev}\n')
419 echo "Updating wok : ${wok}-hg (rev $cur)" | log_commits
420 echo "Updating wok: ${wok}-hg" | log
421 echo "hg:pull" > $command
422 cd ${wok}-hg && hg pull -u | log_commits
423 new=$(hg head --template '{rev}\n')
424 # Store last rev to be used by CGI so it doesn't need to call hg head
425 # on each load.
426 echo "$new" > $CACHE/wokrev
428 # Sync build wok with rsync so we don't take care about removing old
429 # files as before.
430 if [ "$new" -gt "$cur" ]; then
431 echo "Changes found from: $cur to $new" | log
432 echo "Syncing build wok with Hg wok..." | log_commits
433 rsync -r -t -c -l -u -v -D -E ${wok}-hg/ $wok/ | \
434 sed '/^$/'d | log_commits
435 else
436 echo "No revision changes: $cur vs $new" | log
437 separator | log_commits
438 clean_exit && echo "" && exit 0
439 fi
441 # Get and display modifications.
442 cd ${wok}-hg
443 commits_summary | log_commits
444 cur=$(($cur + 1))
445 rm -f $commits.tmp && touch $commits.tmp
446 for rev in $(seq $cur $new)
447 do
448 for file in $(hg log --rev=$rev --template "{files}")
449 do
450 pkg=$(echo $file | cut -d "/" -f 1)
451 desc=$(hg log --rev=$rev --template "{desc}" $file)
452 echo "Commited package : $pkg - $desc" | log_commits
453 echo $pkg >> $commits.tmp
454 done
455 done
457 # We may have deleted packages and files in stuff/. Remove it and
458 # clean DB as well as log file.
459 cd $wok
460 for pkg in *
461 do
462 if [ ! -d "${wok}-hg/$pkg" ]; then
463 echo "Removing package: $pkg" | log_commits
464 . $wok/$pkg/receipt
465 rm -rf $PKGS/$PACKAGE-$VERSION* $wok/$pkg $LOGS/$pkg.log
466 sed -i "/^${pkg}$/"d $CACHE/blocked $CACHE/broken $commits.tmp
467 fi
468 done
470 # Keep previous commit and discard duplicate lines
471 cat $commits $commits.tmp | sed /"^$"/d > $commits.new
472 uniq $commits.new > $commits && rm $commits.*
473 pkgs=$(cat $commits | wc -l)
474 echo "Packages to cook: $pkgs" | log
475 echo "Packages to cook : $pkgs" | log_commits
476 separator | log_commits
477 echo ""
478 strip_blocked
479 cook_order | tee $LOGS/cookorder.log
480 cook_commits
481 clean_exit ;;
482 esac
484 exit 0