source: rpmbuild-bot/rpmbuild-bot.sh@ 949

Last change on this file since 949 was 949, checked in by dmik, 9 years ago

rpmbuild-bot: Add support for automatic generation of legacy runtime sub-packages.

File size: 30.9 KB
Line 
1#!/bin/sh
2
3#
4# rpmbuild-bot.sh: RPM Build Bot version 1.1.0.
5#
6# Author: Dmitriy Kuminov <coding@dmik.org>
7#
8# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
9# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
10#
11# Synopsis
12# --------
13#
14# This script performs a build of RPM packages from a given .spec file in
15# controlled environment to guarantee consistent results when building RPMs
16# different machines. It uses a separate file rpmbuild-bot-env.sh located
17# in the same directory to set up the environment and control the build
18# process. The main purpose of this script is to build RPM packages for a
19# specific distribution channel maintaining distribution-specific rules.
20#
21# Usage
22# -----
23#
24# > rpmbuild-bot.sh SPEC[=VERSION]
25# > [ upload[=REPO] | test[=MODE] | clean[=test] |
26# > move[=FROM_REPO=TO_REPO] | remove[=REPO] ]
27# > [-f]
28#
29# MYAPP is the name of the RPM package spec file (extension is optional,
30# .spec is assumed). The spec file is searched in the SPECS directory of the
31# rpmbuild tree (usually $USER/rpmbuild/SPECS, rpmbuild --eval='%_specdir'
32# will show the exact location). This may be overridden by giving a spec file
33# with a path specification.
34#
35# The second argument defines the command to perform. The default command is
36# "build". The following sections will describe each command.
37#
38# Building packages
39# -----------------
40#
41# The "build" is the main command which is used to generate packages for
42# distribution. This command does the following:
43#
44# 1. Build all RPM packages for all architectures specified in
45# $RPMBUILD_BOT_ARCH_LIST. The packages are stored in the RPMS
46# directory of the rpmbuild tree.
47# 2. Build the source RPM. Stored in the SRPMS directory.
48# 3. Create a ZIP package from the RPMs for the architecture specified
49# last in $RPMBUILD_BOT_ARCH_LIST.
50#
51# The build process for each architecture is stored in a log file in the logs
52# directory of the rpmbuild tree (`rpmbuild --eval='%_topdir'/logs`). Creation
53# of the source RPM and ZIP files is also logged, into separate log files.
54#
55# The "build" command will fail if the log directory contains files from a
56# successful run of another "build" command for this package. This is to
57# prevent overwriting successfully built packages that are not yet uploaded to
58# the distribution repository (see the "upload" command). You should either
59# run the "upload" command or use the -f option to force removal of these
60# log files and the corresponding package files if you are absolutely sure they
61# should be discarded.
62#
63# The "build" command will also check if there is a directory named SPEC in the
64# same directory where the given .spec file resides. If such a directory
65# exists, all files in it are assumed to be auxiliary source files used by the
66# .spec file via SourceN: directives. These files will be automatically copied
67# to the SOURCES directory in the rpmbuild tree before starting the build
68# process.
69#
70# Doing test builds
71# -----------------
72#
73# The "test" command is used to build packages for one architecture for testing
74# purposes. In this more, neither the source RPM nor the ZIP file is created.
75# Also, a special option is automatically passed to rpmbuild to clear the %dist
76# variable to indicate that this is a local, non-distribution build. Such
77# packages will always be "older" than the corresponding builds with %dist
78# so that they will be automatically updated on the next yum update to the
79# %dist ones. The packages built in "test" mode are NOT intended for
80# distribution, they are meant only for local testing before performing the
81# full build of everything.
82#
83# It is possible to configure which steps of the build to perform using the MODE
84# parameter to the "test" command which may take one of the following values:
85#
86# prep Execute the %prep section of the spec file only.
87# build Execute the %build section only (requires %prep to be executed
88# before). May be run multiple times.
89# install Execute the %install section only (requires "prep" and "build"
90# to be executed before). May be run multiple times.
91# pack Create the RPM packages (requires "prep", "build" and "install"
92# to be executed before). Note that this step will also execute
93# the %clean section so that a new "install" execution is
94# necessary for "pack" to succeed.
95#
96# When no MODE parameter is given, all steps are executed in a proper order.
97#
98# The results of the "test" command are stored in a log file in the logs/test
99# directory of the rpmbuild tree. The previous log file, if any, is given a .bak
100# extension (the previous .bak file will be deleted).
101#
102# The "test" command will copy auxiliary source files for the .spec file, if any,
103# to the proper location before rpmbuild execution -- the same way the "build"
104# command does it.
105#
106# Uploading packages
107# ------------------
108#
109# The "upload" command will upload the packages built with the "build"
110# command to the official distribution channel configured in
111# rpmbuild-bot-env.sh. The REPO parameter specifies one of the configured
112# repositories. When REPO is not given, the default repository is used
113# (usually experimental or similar).
114#
115# The upload command also requires the spec file to be under SVN version control
116# and will try to commit it after uploading the RPMs to the repository with the
117# automatic commit message that says "spec: PROJECT: Release version VERSION."
118# (where PROJECT is the spec file name and VERSION is the full version,
119# excluding the distribution mark, as specified by the spec file).This to ensure
120# that the spec file is published at the same time the RPMs are published - to
121# guarantee their consistency and simplify further maintenance. Note that the
122# auxiliary source directory named SPEC and located near the spec file (see the
123# "build" command), if it exists, will also be committed. If the spec file is
124# not under version control, the "upload" command will fail.
125#
126# Note that the "upload" command needs log files from the "build" command
127# and will fail otherwise.
128#
129# Upon successful completion, the "upload" command will remove all uploaded
130# RPM and ZIP packages and will move all "build" log files to logs/archive.
131#
132# Cleaning packages
133# -----------------
134#
135# The "clean" command will delete packages built with the "build" command
136# and their log files without uploading them to a repository. This is useful
137# when the successful build needs to be canceled for some reason (wrong source
138# tree state, wrong patches etc.). Note that normally you don't need to use
139# this command; it's an emergency-only tool.
140#
141# The "clean" command needs log files from the "build" command and will fail
142# otherwise.
143#
144# If the "clean" command is given a "test" parameter, it will clean up the
145# results of the "test" command instead of "build". The log file from the
146# "test" command needs to be present or the command will fail.
147#
148# Moving packages between repositories
149# ------------------------------------
150#
151# The "move" command allows to move a particular version of the packages
152# built with the "build" command and uploaded with the "upload" command from one
153# repository to another one. The "move" command is normally used to move
154# packages from a test repository to a production one when they are ready for
155# wide distribution.
156#
157# The "move" command needs log files from the "build" and "upload" commands
158# and will fail otherwise. It also requires the VERSION parameter for the SPEC
159# argument to be given (to specify the version of the packages to remove) and
160# requires the FROM_REPO=TO_REPO parameter itself to specify the source
161# repository and the target repository, respectively.
162#
163# The log files from the "build" and "upload" commands are not removed by the
164# "move" command so it may be performed multiple times. The current location
165# of the packages is not tracked in the log files so the command will fail
166# if the source repository doesn't have the package files or if the target
167# repository already has them.
168#
169# Removing packages
170# -----------------
171#
172# The "remove" command allows to remove a particular version of the packages
173# built with the "build" command and uploaded with the "upload" command from a
174# repository. This is useful when the successful build needs to be canceled for
175# some reason (wrong source tree state, wrong patches etc.). Note that normally
176# you don't need to use this command; it's an emergency-only tool.
177#
178# The "remove" command needs log files from the "build" and "upload" commands
179# and will fail otherwise. It also requires the VERSION parameter for the SPEC
180# argument to be given (to specify the version of the packages to remove) and
181# accepts the REPO parameter itself just like the "upload" command does (to
182# specify a repository to remove the packages from).
183#
184# Note that the log files from the "build" and "upload" commands are also
185# removed by this command upon successful package removal.
186#
187# Return value
188# ------------
189#
190# The rpmbuild-bot.sh script will return a zero exit code upon successful
191# completion and non-zero otherwise. The script output and log files should be
192# inspected to check for a reason of the failure.
193#
194
195#
196# Helpers.
197#
198
199print_elapsed()
200{
201 # $1 = start timestamp, in seconds (as returned by `date +%s`)
202 # $2 = string containg \$e (will be replaced with the elapsed time)
203
204 [ -z "$1" -o -z "$2" ] && return
205
206 local e=$(($(date +%s) - $1))
207 local e_min=$(($e / 60))
208 local e_sec=$(($e % 60))
209 local e_hrs=$((e_min / 60))
210 e_min=$((e_min % 60))
211 e="${e_hrs}h ${e_min}m ${e_sec}s"
212
213 eval "echo \"$2\""
214}
215
216quit()
217{
218 if [ -n "$start_time" ] ; then
219 echo "Build ended on $(date -R)."
220 if [ $1 = 0 ] ; then
221 print_elapsed start_time "Build succeeded (took \$e)."
222 else
223 print_elapsed start_time "Build failed (took \$e)."
224 fi
225 fi
226 exit $1
227}
228
229run()
230{
231 "$@"
232 local rc=$?
233 if test $rc != 0 ; then
234 echo "ERROR: The following command failed with error code $rc:"
235 echo $@
236 quit $rc
237 fi
238}
239
240log_run()
241{
242 log="$1"
243 shift
244 rm -f "$log"
245 "$@" >"$log" 2>&1
246 local rc=$?
247 if test $rc != 0 ; then
248 echo "ERROR: The following command failed with error code $rc:"
249 echo $@
250 echo "You will find more information in file '$log'."
251 echo "Here are the last 10 lines of output:"
252 echo "------------------------------------------------------------------------------"
253 tail "$log" -n 10
254 echo "------------------------------------------------------------------------------"
255 quit $rc
256 fi
257}
258
259warn()
260{
261 echo "WARNING: $1"
262}
263
264die()
265{
266 echo "ERROR: $1"
267 quit 1
268}
269
270check_dir_var()
271{
272 eval local val="\$$1"
273 [ -n "$val" ] || die "$1 is empty."
274 [ -d "$val" ] || die "$1 is '$val', not a valid directory."
275}
276
277read_file_list()
278{
279 # $1 = file list filename
280 # $2 = var name where to save the list of read file names (optional)
281 # $3 = function name to call for each file (optional), it may assign a new
282 # file name to $file and also set $file_pre and $file_post that will
283 # be prepended and appended to $file when saving it to the list in $2
284 # (but not when checking for file existence and timestamp)
285
286 local list="$1"
287 local _read_file_list_ret=
288
289 # Check timestamps.
290 while read l; do
291 local file_pre=
292 local file_post=
293 local file="${l#*|}"
294 local ts="${l%%|*}"
295 [ "$file" = "$ts" ] && die "Line '$l' in '$list' does not contain timestamps."
296 [ -n "$3" ] && eval $3
297 [ -f "$file" ] || die "File '$file' is not found."
298 echo "Checking timestamp of $file..."
299 local act_ts=`stat -c '%Y' "$file"`
300 # Drop fractional part of seconds reported by older coreutils
301 ts="${ts%%.*}"
302 act_ts="${act_ts%%.*}"
303 if [ "$ts" != "$act_ts" ] ; then
304 die "Recorded timestamp $ts doesn't match actual timestamp $act_ts for '$file'."
305 fi
306 _read_file_list_ret="$_read_file_list_ret${_read_file_list_ret:+ }$file_pre$file$file_post"
307 done < "$list"
308 # Return the files (if requested).
309 [ -n "$2" ] && eval "$2=\$_read_file_list_ret"
310}
311
312usage()
313{
314 echo "Usage:"
315 sed -n -e "s/rpmbuild-bot.sh/${0##*/}/g" -e 's/^# > / /p' < "$0"
316 quit 255
317}
318
319sync_aux_src()
320{
321 [ -n "$src_dir" ] || die "src_dir is empty."
322 [ -n "$spec_full" ] || die "spec_full is empty."
323
324 # Aux source dir is expected along the .spec file.
325 local aux_src_dir="${spec_full%.spec}"
326
327 # Return early if there is no aux source dir.
328 [ -d "$aux_src_dir" ] || return
329
330 echo "Copying auxiliary sources for '$spec' to $src_dir..."
331
332 for f in "$aux_src_dir"/* ; do
333 local ts=`stat -c '%Y' "$f"`
334 local trg_ts=
335 local trg_f="$src_dir/${f##*/}"
336 [ -f "$trg_f" ] && trg_ts=`stat -c '%Y' "$trg_f"`
337 if [ "$ts" != "$trg_ts" ] ; then
338 echo "Copying $f..."
339 run cp -p "$f" "$trg_f"
340 fi
341 done
342}
343
344get_legacy_runtime()
345{
346 [ -n "$src_dir" ] || die "src_dir is empty."
347 [ -n "$RPMBUILD_BOT_UPLOAD_REPO_STABLE" ] || die "RPMBUILD_BOT_UPLOAD_REPO_STABLE is empty."
348
349 local spec_name_=`echo "${spec_name}" | tr - _`
350 eval local arch_list="\${RPMBUILD_BOT_ARCH_LIST_${spec_name_}}"
351 [ -z "$arch_list" ] && arch_list="${RPMBUILD_BOT_ARCH_LIST}"
352
353 eval local rpm_list="\${RPMBUILD_BOT_LEGACY_${spec_name_}}"
354 [ -z "$rpm_list" ] && return # nothing to do
355
356 eval local base="\$RPMBUILD_BOT_UPLOAD_${RPMBUILD_BOT_UPLOAD_REPO_STABLE}_DIR"
357
358 local abi_list=
359
360 for rpm_spec in "$rpm_list" ; do
361 local abi name ver mask legacy_arch
362 IFS='|' read abi name ver mask legacy_arch <<EOF
363${rpm_spec}
364EOF
365 [ -z "$abi" -o -z "$name" -o -z "$ver" ] && die "Value '${rpm_spec}' in RPMBUILD_BOT_LEGACY_${spec_name_} is invalid."
366 [ -z "$mask" ] && mask="*.dll"
367
368 abi_list="$abi_list${abi_list:+ }$abi"
369
370 # Enumerate RPMs for all archs and extract them
371 echo "Getting legacy runtime ($mask) for ABI '$abi'..."
372 for arch in ${legacy_arch:-${arch_list}} ; do
373 eval local rpm="$RPMBUILD_BOT_UPLOAD_REPO_LAYOUT_rpm/$name-$ver$dist_mark.$arch.rpm"
374 local tgt_dir="$src_dir/$spec_name-legacy/$abi/$arch"
375 # Check filenames and timestamps
376 echo "Checking package $rpm..."
377 [ -f "$rpm" ] || die "File '$rpm' is not found."
378 local old_ts old_rpm old_name old_ver
379 [ -f "$tgt_dir.list" ] && IFS='|' read old_ts old_rpm old_name old_ver < "$tgt_dir.list"
380 local ts=`stat -c '%Y' "$rpm"`
381 # Drop fractional part of seconds reported by older coreutils
382 ts="${ts%%.*}"
383 old_ts="${old_ts%%.*}"
384 if [ "$old_rpm" != "$rpm" -o "$ts" != "$old_ts" -o "$name" != "$old_name" -o "$ver" != "$old_ver" ] ; then
385 echo "Extracting to $tgt_dir..."
386 run rm -f "$tgt_dir.list" && rm -rf "$tgt_dir"
387 (run mkdir -p "$tgt_dir" && cd "$tgt_dir"; run rpm2cpio "$rpm" | eval cpio -idm $mask)
388 # save the list for later use
389 find "$tgt_dir" -type f -printf '/%P\n' > "$tgt_dir.files.list"
390 # now try to locate the debuginfo package and extract *.dbg from it
391 eval local debug_rpm="$RPMBUILD_BOT_UPLOAD_REPO_LAYOUT_rpm/$name-debuginfo-$ver$dist_mark.$arch.rpm"
392 [ ! -f "$debug_rpm" ] && eval debug_rpm="$RPMBUILD_BOT_UPLOAD_REPO_LAYOUT_rpm/$name-debug-$ver$dist_mark.$arch.rpm"
393 if [ -f "$debug_rpm" ] ; then
394 echo "Found debug info package $debug_rpm, extracting..."
395 local dbgfilelist="$tgt_dir.debugfiles.list"
396 run rm -rf "$dbgfilelist"
397 local masks=
398 local f
399 while read -r f ; do
400 f="${f%.*}.dbg"
401 masks="$masks${masks:+ }'*$f'"
402 # Save the file for later inclusion into debugfiles.list (%debug_package magic in brp-strip.os2)
403 run echo "$f" >> "$dbgfilelist"
404 done < "$tgt_dir.files.list"
405 (run cd "$tgt_dir"; run rpm2cpio "$debug_rpm" | eval cpio -idm $masks)
406 fi
407 # put the 'done' mark
408 run echo "$ts|$rpm|$name|$ver" > "$tgt_dir.list"
409 fi
410 done
411 done
412
413 run echo "$abi_list" > "$src_dir/$spec_name-legacy/abi.list"
414}
415
416build_prepare()
417{
418 sync_aux_src
419 get_legacy_runtime
420}
421
422build_cmd()
423{
424 local spec_name_=`echo "${spec_name}" | tr - _`
425 eval local arch_list="\${RPMBUILD_BOT_ARCH_LIST_${spec_name_}}"
426 [ -z "$arch_list" ] && arch_list="${RPMBUILD_BOT_ARCH_LIST}"
427
428 local base_arch="${arch_list##* }"
429
430 echo "Spec file: $spec_full"
431 echo "Targets: $arch_list + SRPM + ZIP ($base_arch)"
432
433 build_prepare
434
435 if [ -f "$spec_list" ] ; then
436 if [ -z "$force" ] ; then
437 die "File '$spec_list' already exists.
438This file indicates a successful build that was not yet uploaded.
439Either run the '"'upload'"' command to upload the generated RPM and ZIP
440packages to the distribution repository or use the -f option to
441force their removal if you are sure they should be discarded."
442 fi
443
444 echo "Detected successful build in $spec_list, cleaning up due to -f option..."
445 local files=
446 read_file_list "$spec_list" files
447 for f in $files; do
448 echo "Removing $f..."
449 run rm -f "$f"
450 done
451 unset files
452
453 echo "Removing $log_base.*.log and .list files..."
454 rm -f "$log_base".*.log "$log_base".*.list "$log_base".list
455 fi
456
457 local noarch_only=
458 local start_time=
459
460 # Generate RPMs (note that base_arch always goes first).
461 for arch in $base_arch ${arch_list%${base_arch}} ; do
462 echo "Creating RPMs for '$arch' target (logging to $log_base.$arch.log)..."
463 start_time=$(date +%s)
464 log_run "$log_base.$arch.log" rpmbuild.exe --target=$arch -bb "$spec_full"
465 print_elapsed start_time "Completed in \$e."
466 if ! grep -q "^Wrote: \+.*\.$arch\.rpm$" "$log_base.$arch.log" ; then
467 if ! grep -q "^Wrote: \+.*\.noarch\.rpm$" "$log_base.$arch.log" ; then
468 die "Target '$arch' did not produce any RPMs."
469 fi
470 noarch_only=1
471 echo "Skipping other targets because '$arch' produced only 'noarch' RPMs."
472 break
473 fi
474 done
475
476 # Generate SRPM.
477 echo "Creating SRPM (logging to $log_base.srpm.log)..."
478 start_time=$(date +%s)
479 log_run "$log_base.srpm.log" rpmbuild -bs "$spec_full"
480 print_elapsed start_time "Completed in \$e."
481
482 # Find SRPM file name in the log.
483 local src_rpm=`grep "^Wrote: \+.*\.src\.rpm$" "$log_base.srpm.log" | sed -e "s#^Wrote: \+##g"`
484 [ -n "$src_rpm" ] || die "Cannot find .src.rpm file name in '$log_base.srpm.log'."
485
486 # Find package version.
487 local ver_full="${src_rpm%.src.rpm}"
488 ver_full="${ver_full##*/}"
489 [ "${ver_full%%-[0-9]*}" = "$spec_name" ] || die \
490"SRPM name '${src_rpm##*/}' does not match .spec name ('$spec_name').
491Either rename '$spec_name.spec' to '${ver_full%%-[0-9]*}.spec' or set 'Name:' tag to '$spec_name'."
492 ver_full="${ver_full#${spec_name}-}"
493 [ -n "$ver_full" ] || die "Cannot deduce package version from '$src_rpm'."
494
495 # Find all RPM packages for the base arch (note the quotes around `` - it's to preserve multi-line result).
496 local rpms="`grep "^Wrote: \+.*\.\($base_arch\.rpm\|noarch\.rpm\)$" "$log_base.$base_arch.log" | sed -e "s#^Wrote: \+##g"`"
497 [ -n "$rpms" ] || die "Cannot find .$base_arch.rpm/.noarch.rpm file names in '$log_base.base_arch.log'."
498
499 local ver_full_zip=`echo $ver_full | tr . _`
500 local zip="$zip_dir/$spec_name-$ver_full_zip.zip"
501
502 # Generate ZIP.
503 echo "Creating ZIP (logging to $log_base.zip.log)..."
504 start_time=$(date +%s)
505 create_zip()
506 {(
507 run cd "$zip_dir"
508 rm -r "@unixroot" 2> /dev/null
509 # Note no quoters around $rpms - it's to split at EOL.
510 for f in $rpms ; do
511 echo "Unpacking $f..."
512 run rpm2cpio "$f" | cpio -idm
513 done
514 rm -f "$zip" 2> /dev/null
515 echo "Creating '$zip'..."
516 run zip -mry9 "$zip" "@unixroot"
517 )}
518 log_run "$log_base.zip.log" create_zip
519 print_elapsed start_time "Completed in \$e."
520
521 local ver_list="$log_base.$ver_full.list"
522
523 # Generate list of all generated packages for further reference.
524 echo "Creating list file ($ver_list)..."
525 rm -f "$ver_list"
526 echo `stat -c '%Y' "$src_rpm"`"|$src_rpm" > "$ver_list"
527 echo `stat -c '%Y' "$zip"`"|$zip" >> "$ver_list"
528 # Save base arch RPMs.
529 for f in $rpms ; do
530 echo `stat -c '%Y' "$f"`"|$f" >> "$ver_list"
531 done
532 # Save other arch RPMs (only if there is anything but noarch).
533 if [ -z "$noarch_only" ] ; then
534 for arch in ${arch_list%${base_arch}} ; do
535 rpms="`grep "^Wrote: \+.*\.$arch\.rpm$" "$log_base.$arch.log" | sed -e "s#^Wrote: \+##g"`"
536 [ -n "$rpms" ] || die "Cannot find .$arch.rpm file names in '$log_base.arch.log'."
537 for f in $rpms ; do
538 echo `stat -c '%Y' "$f"`"|$f" >> "$ver_list"
539 done
540 done
541 fi
542
543 # Everything succeeded. Symlink the list file so that "upload" can find it.
544 run ln -s "${ver_list##*/}" "$log_base.list"
545}
546
547test_cmd()
548{
549 echo "Spec file: $spec_full"
550
551 local base_arch="${RPMBUILD_BOT_ARCH_LIST##* }"
552 local cmds=
553
554 [ -z "$command_arg" ] && command_arg="all"
555
556 case "$command_arg" in
557 all)
558 build_prepare
559 cmds="$cmds -bb"
560 ;;
561 prep)
562 cmds="$cmds -bp --short-circuit"
563 ;;
564 build)
565 cmds="$cmds -bc --short-circuit"
566 ;;
567 install)
568 cmds="$cmds -bi --short-circuit"
569 ;;
570 pack)
571 cmds="$cmds -bb --short-circuit"
572 ;;
573 *)
574 die "Invalid test build sub-command '$command_arg'."
575 ;;
576 esac
577
578 local log_file="$log_dir/test/$spec_name.log"
579
580 if [ -f "$log_file" ] ; then
581 rm -f "$log_file.bak"
582 run mv "$log_file" "$log_file.bak"
583 fi
584
585 echo "Doing test RPM build for '$base_arch' target (logging to $log_file)..."
586 log_run "$log_file" rpmbuild.exe "--define=dist %nil" --target=$base_arch $cmds $spec_full
587
588 # Show the generated RPMs when appropriate.
589 if [ "$command_arg" = "all" -o "$command_arg" = "pack" ] ; then
590 # Find all RPM packages for the base arch (note the quotes around `` - it's to preserve multi-line result).
591 local rpms="`grep "^Wrote: \+.*\.\($base_arch\.rpm\|noarch\.rpm\)$" "$log_file" | sed -e "s#^Wrote: \+##g"`"
592 if [ -n "$rpms" ] ; then
593 echo "Successfully generated the following RPMs:"
594 for f in $rpms; do
595 echo "$f"
596 done
597 else
598 warn "Cannot find .$base_arch.rpm/.noarch.rpm file names in '$log_file'."
599 fi
600 fi
601}
602
603repo_dir_for_file()
604{
605 # $1 = input file name
606 # $2 = var name to save dir to
607
608 [ -n "$1" -a -n "$2" ] || die "Invalid arguments."
609
610 local _repo_dir_for_file_ret=
611 case "$1" in
612 *.src.rpm)
613 eval _repo_dir_for_file_ret="$RPMBUILD_BOT_UPLOAD_REPO_LAYOUT_srpm"
614 ;;
615 *.*.rpm)
616 local arch="${1%.rpm}"
617 arch="${arch##*.}"
618 [ -n "$arch" ] || die "No arch spec in file name '$1'."
619 eval _repo_dir_for_file_ret="$RPMBUILD_BOT_UPLOAD_REPO_LAYOUT_rpm"
620 ;;
621 *.zip)
622 eval _repo_dir_for_file_ret="$RPMBUILD_BOT_UPLOAD_REPO_LAYOUT_zip"
623 ;;
624 esac
625
626 eval "$2=\$_repo_dir_for_file_ret"
627}
628
629upload_cmd()
630{
631 # Check settings.
632 test -n "$RPMBUILD_BOT_UPLOAD_REPO_LIST" || die "RPMBUILD_BOT_UPLOAD_REPO_LIST is empty."
633
634 local repo="$command_arg"
635 [ -z "$repo" ] && repo="${RPMBUILD_BOT_UPLOAD_REPO_LIST%% *}"
636
637 check_dir_var "RPMBUILD_BOT_UPLOAD_${repo}_DIR"
638
639 eval local base="\$RPMBUILD_BOT_UPLOAD_${repo}_DIR"
640
641 [ -f "$spec_list" ] || die \
642"File '$spec_list' is not found.
643You may need to build the packages using the 'build' command."
644
645 # Prepare for committing the SPEC and auxiliary source files.
646 local ver_list=
647 [ -L "$spec_list" ] && ver_list=`readlink "$spec_list"`
648 [ -z "$ver_list" ] && die "File '$spec_list' is not a valid symbolic link."
649
650 local ver_full="${ver_list#${spec_name}.}"
651 ver_full="${ver_full%.*}"
652 [ -n "$dist_mark" ] && ver_full="${ver_full%${dist_mark}}"
653
654 [ -n "$ver_full" ] || die "Full version string is empty."
655
656 local commit_items="$spec_full"
657 # Commit the auxiliary source directory, if any, as well.
658 [ -d "${spec_full%.spec}" ] && commit_items="$commit_items ${spec_full%.spec}"
659
660 local commit_msg="spec: $spec_name: Release version $ver_full."
661
662 echo \
663"Uploading requires the following items to be committed to SPEC repository:
664 $commit_items
665With the following commit message:
666 $commit_msg
667The repository will be updated now and then you will get a diff for careful
668inspection. Type YES to continue."
669
670 local answer=
671 read answer
672 if [ "$answer" = "YES" ] ; then
673 local pager=`which less`
674 if [ ! -x "$pager" ] ; then
675 pager="more"
676 # OS/2 more doesn't understand LF, feed it through sed.
677 [ -x `which sed` ] && pager="sed -e '' | $pager"
678 fi
679 run svn up "$spec_dir"
680 echo
681 svn diff $commit_items | "$pager"
682 echo "
683Type YES if the diff is okay to be committed."
684 read answer
685 fi
686
687 [ "$answer" = "YES" ] || die "Your answer is not YES, upload is aborted."
688
689 # First, copy RPM files over to the repository.
690 local files=
691 read_file_list "$spec_list" files
692 for f in $files; do
693 local d=
694 repo_dir_for_file "$f" d
695 [ -n "$d" ] || die "Unsupported file name '$f' in '$spec_list'."
696 [ -d "$d" ] || die "'$d' is not a directory."
697 [ -f "$d/${f##*/}" -a -z "$force" ] && die \
698"File '$d/${f##*/}' already exists.
699Use the -f option to force uploading if you are sure the existing
700packages in the repository should be discarded."
701 echo "Copying $f to $d..."
702 run cp -p "$f" "$d"
703 done
704
705 # On success, delete the uploaded packages and archive log files.
706 for f in $files; do
707 echo "Removing $f..."
708 run rm -f "$f"
709 done
710
711 # And finally commit the SPEC file.
712 run svn commit $commit_items -m "$commit_msg"
713
714 # Note: versioned .list file will remain in archive forever (for further reference).
715 echo "Removing old '$spec_name' logs from $log_dir/archive..."
716 rm -f "$log_dir/archive/$spec_name".*.log "$log_dir/archive/$spec_name".list
717 echo "Moving '$spec_name' logs to $log_dir/archive..."
718 run mv "$log_base".*.log "$log_base".*.list "$log_base".list "$log_dir/archive/"
719}
720
721clean_cmd()
722{
723 if [ "$command_arg" = "test" ] ; then
724 # Cleanup after "test" command.
725 local base_arch="${RPMBUILD_BOT_ARCH_LIST##* }"
726 local log_file="$log_dir/test/$spec_name.log"
727
728 [ -f "$log_file" ] || die "File '$test_log' is not found."
729
730 # Find all RPM packages for the base arch (note the quotes around `` - it's to preserve multi-line result).
731 local rpms="`grep "^Wrote: \+.*\.\($base_arch\.rpm\|noarch\.rpm\)$" "$log_file" | sed -e "s#^Wrote: \+##g"`"
732 if [ -n "$rpms" ] ; then
733 for f in $rpms; do
734 echo "Removing $f..."
735 run rm -f "$f"
736 done
737 echo "Removing $log_file[.bak]..."
738 run rm -f "$log_file" "$log_file".bak
739 else
740 die "Cannot find .$base_arch.rpm/.noarch.rpm file names in '$log_file'."
741 fi
742
743 return
744 fi
745
746 # Cleanup after "build command".
747 [ -f "$spec_list" ] || die \
748"File '$spec_list' is not found.
749You man need to build the packages using the 'build' command."
750
751 local files=
752 read_file_list "$spec_list" files
753
754 for f in $files; do
755 echo "Removing $f..."
756 run rm -f "$f"
757 done
758
759 echo "Removing '$spec_name' logs from $log_dir..."
760 rm -f "$log_base".*.log "$log_base".*.list "$log_base".list
761}
762
763move_cmd()
764{
765 # Check settings.
766 [ -n "$spec_ver" ] || die "SPEC parameter lacks version specification."
767
768 test -n "$RPMBUILD_BOT_UPLOAD_REPO_LIST" || die "RPMBUILD_BOT_UPLOAD_REPO_LIST is empty."
769
770 local from_repo="${command_arg%%=*}"
771 local to_repo="${command_arg#*=}"
772
773 [ -n "$from_repo" ] || die "FROM_REPO parameter is missing."
774 [ "$from_repo" = "$to_repo" ] && die "TO_REPO parameter is missing (or equals to FROM_REPO)."
775
776 check_dir_var "RPMBUILD_BOT_UPLOAD_${from_repo}_DIR"
777 check_dir_var "RPMBUILD_BOT_UPLOAD_${to_repo}_DIR"
778
779 local ver_list="$log_dir/archive/$spec_name.$spec_ver.list"
780 [ -f "$ver_list" ] || die "File '$ver_list' is not found."
781
782 eval local from_base="\$RPMBUILD_BOT_UPLOAD_${from_repo}_DIR"
783 eval local to_base="\$RPMBUILD_BOT_UPLOAD_${to_repo}_DIR"
784
785 local files=
786 read_file_list "$ver_list" files '
787local dir=
788local base="$from_base"; repo_dir_for_file "$file" dir; file="${dir}/${file##*/}"
789base="$to_base"; repo_dir_for_file "$file" dir; file_post=">${dir}"
790'
791 for f in $files; do
792 local from="${f%%>*}"
793 local to="${f#*>}"
794 echo "Moving $from to $to/..."
795 run mv "$from" "$to/"
796 done
797}
798
799remove_cmd()
800{
801 # Check settings.
802 [ -n "$spec_ver" ] || die "SPEC parameter lacks version specification."
803
804 test -n "$RPMBUILD_BOT_UPLOAD_REPO_LIST" || die "RPMBUILD_BOT_UPLOAD_REPO_LIST is empty."
805
806 local repo="$command_arg"
807 [ -z "$repo" ] && repo="${RPMBUILD_BOT_UPLOAD_REPO_LIST%% *}"
808
809 check_dir_var "RPMBUILD_BOT_UPLOAD_${repo}_DIR"
810
811 local ver_list="$log_dir/archive/$spec_name.$spec_ver.list"
812 [ -f "$ver_list" ] || die "File '$ver_list' is not found."
813
814 eval local base="\$RPMBUILD_BOT_UPLOAD_${repo}_DIR"
815
816 local files=
817 read_file_list "$ver_list" files 'local dir=; repo_dir_for_file $file dir; file="${dir}/${file##*/}"'
818
819 for f in $files; do
820 echo "Removing $f..."
821 run rm -f "$f"
822 done
823
824 echo "Removing $ver_list..."
825 run rm -f "$ver_list"
826
827 # Also remove the logs of last "build" if we are removing the last "build" package.
828 if [ -L "$log_dir/archive/$spec_name.list" -a \
829 `readlink "$log_dir/archive/$spec_name.list"` = "$spec_name.$spec_ver.list" ] ; then
830 echo "Removing '$spec_name' logs from $log_dir/archive..."
831 rm -f "$log_dir/archive/$spec_name".*.log "$log_dir/archive/$spec_name".list
832 fi
833}
834
835#
836# Main.
837#
838
839# Parse command line.
840while [ -n "$1" ] ; do
841 case "$1" in
842 -*)
843 options="$*"
844 while [ -n "$1" ] ; do
845 case "$1" in
846 -f) force="-f"
847 ;;
848 *) usage
849 ;;
850 esac
851 shift
852 done
853 break
854 ;;
855 *)
856 if [ -z "$spec" ] ; then
857 spec="$1"
858 else
859 command="$1"
860 fi
861 ;;
862 esac
863 shift
864done
865
866[ -n "$spec" ] || usage
867[ -z "$command" ] && command="build"
868
869spec_ver="${spec#*=}"
870spec="${spec%=*}"
871[ "$spec" = "$spec_ver" ] && spec_ver=
872
873command_name="${command%%=*}"
874command_arg="${command#*=}"
875[ "$command_name" = "$command_arg" ] && command_arg=
876
877need_spec_file=
878
879# Validate commands.
880case "$command_name" in
881 build|test)
882 need_spec_file=1
883 ;;
884 upload|clean|move|remove)
885 ;;
886 *) usage
887 ;;
888esac
889
890# Query all rpmbuild macros in a single run as it may be slow.
891eval `rpmbuild.exe --eval='rpmbuild_dir="%_topdir" ; spec_dir="%_specdir" ; src_dir="%_sourcedir" ; dist_mark="%dist"' | tr '\\\' /`
892
893[ -n "$rpmbuild_dir" -a -d "$rpmbuild_dir" ] || die "Falied to get %_topdir from rpmbuild or not directory ($rpmbuild_dir)."
894[ -n "$spec_dir" -a -d "$spec_dir" ] || die "Falied to get %_specdir from rpmbuild or not directory ($spec_dir)."
895[ -n "$src_dir" -a -d "$src_dir" ] || die "Falied to get %_sourcedir from rpmbuild or not directory ($src_dir)."
896
897log_dir="$rpmbuild_dir/logs"
898zip_dir="$rpmbuild_dir/zip"
899
900spec=`echo $spec | tr '\\\' /`
901
902spec_name="${spec##*/}"
903
904if [ "$spec_name" = "$spec" ] ; then
905 # No path information, use SPECS
906 spec_full="$spec_dir/${spec_name%.spec}.spec"
907else
908 # Has some path, use it.
909 spec_full="${spec%.spec}.spec"
910fi
911
912spec_name="${spec_name%.spec}"
913
914[ -z "$need_spec_file" -o -f "$spec_full" ] || die "Spec file '$spec_full' is not found."
915
916# Prepare some (non-rpmbuild-standard) directories.
917run mkdir -p "$log_dir"
918run mkdir -p "$log_dir/archive"
919run mkdir -p "$log_dir/test"
920run mkdir -p "$zip_dir"
921
922log_base="$log_dir/$spec_name"
923spec_list="$log_base.list"
924
925start_time=$(date +%s)
926
927echo "Build started on $(date -R)."
928echo "Package: $spec_name"
929echo "Command: $command $options"
930
931# Set up the rpmbuild-bot environment.
932. "${0%%.sh}-env.sh"
933
934# Check common settings.
935test -n "$RPMBUILD_BOT_ARCH_LIST" || die "RPMBUILD_BOT_ARCH_LIST is empty."
936
937run eval "${command_name}_cmd"
938
939quit 0
Note: See TracBrowser for help on using the repository browser.