cookutils view cooker @ rev 311

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