bacman: allow for multiple packages as arguments

To enable the creation of multiple packages with one command move the
assembly process into its own function.

Signed-off-by: Gordian Edenhofer <gordian.edenhofer@gmail.com>
Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Gordian Edenhofer 2016-09-04 18:13:56 +02:00 committed by Allan McRae
parent 45b6a3074a
commit 7568928e71
1 changed files with 200 additions and 205 deletions

View File

@ -66,7 +66,8 @@ done
m4_include(../scripts/library/term_colors.sh) m4_include(../scripts/library/term_colors.sh)
if (( $# != 1 )); then # Break if no argument was given
if (( $# < 1 )); then
usage usage
exit 1 exit 1
fi fi
@ -118,10 +119,6 @@ fi
pkg_dest="${PKGDEST:-$PWD}" pkg_dest="${PKGDEST:-$PWD}"
pkg_pkger=${PACKAGER:-'Unknown Packager'} pkg_pkger=${PACKAGER:-'Unknown Packager'}
pkg_name="$1"
pkg_dir=("$pac_db/$pkg_name"-+([^-])-+([^-]))
pkg_namver=("${pkg_dir[@]##*/}")
# #
# Checks everything is in place # Checks everything is in place
# #
@ -130,227 +127,225 @@ if [[ ! -d $pac_db ]]; then
exit 1 exit 1
fi fi
if (( ${#pkg_dir[@]} != 1 )); then # Assemble a single package: $1 = pkgname
error "%d entries for package %s found in pacman database" \ fakebuild() {
${#pkg_dir[@]} "${pkg_name}" pkg_name="$1"
msg2 "%s" "${pkg_dir[@]}" pkg_dir=("$pac_db/$pkg_name"-+([^-])-+([^-]))
exit 1 pkg_namver=("${pkg_dir[@]##*/}")
fi
if [[ ! -d $pkg_dir ]]; then # Checks database for specified package
error "package %s is found in pacman database," "${pkg_name}" if (( ${#pkg_dir[@]} != 1 )); then
plain " but '%s' is not a directory" "${pkg_dir}" error "%d entries for package %s found in pacman database" \
exit 1 ${#pkg_dir[@]} "${pkg_name}"
fi msg2 "%s" "${pkg_dir[@]}"
exit 1
# fi
# Begin if [[ ! -d $pkg_dir ]]; then
# error "package %s is found in pacman database," "${pkg_name}"
msg "Package: ${pkg_namver}" plain " but '%s' is not a directory" "${pkg_dir}"
work_dir=$(mktemp -d "${TMPDIR:-/tmp}/bacman.XXXXXXXXXX") exit 1
cd "$work_dir" || exit 1
#
# File copying
#
msg2 "Copying package files..."
while read i; do
if [[ -z $i ]]; then
continue
fi fi
if [[ $i == %+([A-Z])% ]]; then # Create working directory
current=$i msg "Package: ${pkg_namver}"
continue work_dir=$(mktemp -d "${work_dir_root}.XXXXXXXXXX")
fi cd "$work_dir" || exit 1
case "$current" in # Assemble list of files which belong to the package and tar them
%FILES%) msg2 "Copying package files..."
local_file="/$i" while read i; do
package_file="$work_dir/$i" if [[ -z $i ]]; then
if [[ ! -e $local_file ]]; then
warning "package file $local_file is missing"
continue
fi
;;
%BACKUP%)
# Get the MD5 checksum.
original_md5="${i##*$'\t'}"
# Strip the md5sum after the tab.
i="${i%$'\t'*}"
local_file="/$i.pacnew"
package_file="$work_dir/$i"
# Include unmodified .pacnew files.
local_md5="$(md5sum "$local_file" | cut -d' ' -f1)"
if [[ $INCLUDE_PACNEW == 'n' ]] \
|| [[ ! -e $local_file ]] \
|| [[ $local_md5 != $original_md5 ]]; then
# Warn about modified files.
local_md5="$(md5sum "/$i" | cut -d' ' -f1)"
if [[ $local_md5 != $original_md5 ]]; then
warning "package file /$i has been modified"
fi
# Let the normal file be included in the %FILES% list.
continue
fi
;;
*)
continue continue
;; fi
esac
ret=0 if [[ $i == %+([A-Z])% ]]; then
bsdtar -cnf - -s'/.pacnew$//' "$local_file" 2> /dev/null | bsdtar -xpf - 2> /dev/null current=$i
continue
fi
# Workaround to bsdtar not reporting a missing file as an error case "$current" in
if ! [[ -e $package_file || -L $package_file ]]; then %FILES%)
error "unable to add $local_file to the package" local_file="/$i"
plain " If your user does not have permission to read this file, then" package_file="$work_dir/$i"
plain " you will need to run $myname as root."
if [[ ! -e $local_file ]]; then
warning "package file $local_file is missing"
continue
fi
;;
%BACKUP%)
# Get the MD5 checksum.
original_md5="${i##*$'\t'}"
# Strip the md5sum after the tab.
i="${i%$'\t'*}"
local_file="/$i.pacnew"
package_file="$work_dir/$i"
# Include unmodified .pacnew files.
local_md5="$(md5sum "$local_file" | cut -d' ' -f1)"
if [[ $INCLUDE_PACNEW == 'n' ]] \
|| [[ ! -e $local_file ]] \
|| [[ $local_md5 != $original_md5 ]]; then
# Warn about modified files.
local_md5="$(md5sum "/$i" | cut -d' ' -f1)"
if [[ $local_md5 != $original_md5 ]]; then
warning "package file /$i has been modified"
fi
# Let the normal file be included in the %FILES% list.
continue
fi
;;
*)
continue
;;
esac
# Tar files
ret=0
bsdtar -cnf - -s'/.pacnew$//' "$local_file" 2> /dev/null | bsdtar -xpf - 2> /dev/null
# Workaround to bsdtar not reporting a missing file as an error
if ! [[ -e $package_file || -L $package_file ]]; then
error "unable to add $local_file to the package"
plain " If your user does not have permission to read this file, then"
plain " you will need to run $myname as root."
rm -rf "$work_dir"
exit 1
fi
done < "$pkg_dir"/files
ret=$?
if (( ret )); then
rm -rf "$work_dir" rm -rf "$work_dir"
exit 1 exit 1
fi fi
done < "$pkg_dir"/files
ret=$? # Calculate package size
if (( ret )); then pkg_size=$(du -sk | awk '{print $1 * 1024}')
rm -rf "$work_dir"
exit 1
fi
pkg_size=$(du -sk | awk '{print $1 * 1024}') # Reconstruct .PKGINFO from database
# TODO adopt makepkg's write_pkginfo() into this or scripts/library
msg2 "Generating .PKGINFO metadata..."
echo "# Generated by $myname $myver" > .PKGINFO
if [[ $INFAKEROOT == "1" ]]; then
echo "# Using $(fakeroot -v)" >> .PKGINFO
fi
echo "# $(LC_ALL=C date)" >> .PKGINFO
echo "#" >> .PKGINFO
while read i; do
if [[ -z $i ]]; then
continue;
fi
if [[ $i == %+([A-Z])% ]]; then
current=$i
continue
fi
# case "$current" in
# .PKGINFO stuff # desc
# TODO adopt makepkg's write_pkginfo() into this or scripts/library %NAME%)
# echo "pkgname = $i" >> .PKGINFO
msg2 "Generating .PKGINFO metadata..." ;;
echo "# Generated by $myname $myver" > .PKGINFO %VERSION%)
if [[ $INFAKEROOT == "1" ]]; then echo "pkgver = $i" >> .PKGINFO
echo "# Using $(fakeroot -v)" >> .PKGINFO ;;
fi %DESC%)
echo "# $(LC_ALL=C date)" >> .PKGINFO echo "pkgdesc = $i" >> .PKGINFO
echo "#" >> .PKGINFO ;;
%URL%)
echo "url = $i" >> .PKGINFO
;;
%LICENSE%)
echo "license = $i" >> .PKGINFO
;;
%ARCH%)
echo "arch = $i" >> .PKGINFO
pkg_arch="$i"
;;
%BUILDDATE%)
echo "builddate = $(date -u "+%s")" >> .PKGINFO
;;
%PACKAGER%)
echo "packager = $pkg_pkger" >> .PKGINFO
;;
%SIZE%)
echo "size = $pkg_size" >> .PKGINFO
;;
%GROUPS%)
echo "group = $i" >> .PKGINFO
;;
%REPLACES%)
echo "replaces = $i" >> .PKGINFO
;;
%DEPENDS%)
echo "depend = $i" >> .PKGINFO
;;
%OPTDEPENDS%)
echo "optdepend = $i" >> .PKGINFO
;;
%CONFLICTS%)
echo "conflict = $i" >> .PKGINFO
;;
%PROVIDES%)
echo "provides = $i" >> .PKGINFO
;;
%BACKUP%)
# Strip the md5sum after the tab
echo "backup = ${i%%$'\t'*}" >> .PKGINFO
;;
esac
done < <(cat "$pkg_dir"/{desc,files})
while read i; do comp_files=".PKGINFO"
if [[ -z $i ]]; then
continue; # Add instal file if present
if [[ -f $pkg_dir/install ]]; then
cp "$pkg_dir/install" "$work_dir/.INSTALL"
comp_files+=" .INSTALL"
fi
if [[ -f $pkg_dir/changelog ]]; then
cp "$pkg_dir/changelog" "$work_dir/.CHANGELOG"
comp_files+=" .CHANGELOG"
fi fi
if [[ $i == %+([A-Z])% ]]; then # Fixes owner:group and permissions for .PKGINFO, .CHANGELOG, .INSTALL
current=$i chown root:root "$work_dir"/{.PKGINFO,.CHANGELOG,.INSTALL} 2> /dev/null
continue chmod 644 "$work_dir"/{.PKGINFO,.CHANGELOG,.INSTALL} 2> /dev/null
# Generate the package
msg2 "Generating the package..."
pkg_file="$pkg_dest/$pkg_namver-$pkg_arch${PKGEXT}"
ret=0
# TODO: Maybe this can be set globally for robustness
shopt -s -o pipefail
bsdtar -cf - $comp_files * |
case "$PKGEXT" in
*tar.gz) gzip -c -f -n ;;
*tar.bz2) bzip2 -c -f ;;
*tar.xz) xz -c -z - ;;
*tar.Z) compress -c -f ;;
*tar) cat ;;
*) warning "'%s' is not a valid archive extension." \
"$PKGEXT"; cat ;;
esac > "${pkg_file}"; ret=$?
# Move compressed package to destination
if (( ret )); then
error "Unable to write package to $pkg_dest"
plain " Maybe the disk is full or you do not have write access"
rm -rf "$work_dir"
exit 1
fi fi
case "$current" in # Clean up working directory
# desc
%NAME%)
echo "pkgname = $i" >> .PKGINFO
;;
%VERSION%)
echo "pkgver = $i" >> .PKGINFO
;;
%DESC%)
echo "pkgdesc = $i" >> .PKGINFO
;;
%URL%)
echo "url = $i" >> .PKGINFO
;;
%LICENSE%)
echo "license = $i" >> .PKGINFO
;;
%ARCH%)
echo "arch = $i" >> .PKGINFO
pkg_arch="$i"
;;
%BUILDDATE%)
echo "builddate = $(date -u "+%s")" >> .PKGINFO
;;
%PACKAGER%)
echo "packager = $pkg_pkger" >> .PKGINFO
;;
%SIZE%)
echo "size = $pkg_size" >> .PKGINFO
;;
%GROUPS%)
echo "group = $i" >> .PKGINFO
;;
%REPLACES%)
echo "replaces = $i" >> .PKGINFO
;;
%DEPENDS%)
echo "depend = $i" >> .PKGINFO
;;
%OPTDEPENDS%)
echo "optdepend = $i" >> .PKGINFO
;;
%CONFLICTS%)
echo "conflict = $i" >> .PKGINFO
;;
%PROVIDES%)
echo "provides = $i" >> .PKGINFO
;;
# files
%BACKUP%)
# Strip the md5sum after the tab
echo "backup = ${i%%$'\t'*}" >> .PKGINFO
;;
esac
done < <(cat "$pkg_dir"/{desc,files})
comp_files=".PKGINFO"
if [[ -f $pkg_dir/install ]]; then
cp "$pkg_dir/install" "$work_dir/.INSTALL"
comp_files+=" .INSTALL"
fi
if [[ -f $pkg_dir/changelog ]]; then
cp "$pkg_dir/changelog" "$work_dir/.CHANGELOG"
comp_files+=" .CHANGELOG"
fi
#
# Fixes owner:group and permissions for .PKGINFO, .CHANGELOG, .INSTALL
#
chown root:root "$work_dir"/{.PKGINFO,.CHANGELOG,.INSTALL} 2> /dev/null
chmod 644 "$work_dir"/{.PKGINFO,.CHANGELOG,.INSTALL} 2> /dev/null
#
# Generate the package
#
msg2 "Generating the package..."
pkg_file="$pkg_dest/$pkg_namver-$pkg_arch${PKGEXT}"
ret=0
# TODO: Maybe this can be set globally for robustness
shopt -s -o pipefail
bsdtar -cf - $comp_files * |
case "$PKGEXT" in
*tar.gz) gzip -c -f -n ;;
*tar.bz2) bzip2 -c -f ;;
*tar.xz) xz -c -z - ;;
*tar.Z) compress -c -f ;;
*tar) cat ;;
*) warning "'%s' is not a valid archive extension." \
"$PKGEXT"; cat ;;
esac > "${pkg_file}"; ret=$?
if (( ret )); then
error "Unable to write package to $pkg_dest"
plain " Maybe the disk is full or you do not have write access"
rm -rf "$work_dir" rm -rf "$work_dir"
exit 1 }
fi
rm -rf "$work_dir"
for PKG in $@; do fakebuild $PKG; done
msg "Done." msg "Done."
exit 0 exit 0