[pacman-dev] [PATCH v2 2/3] bacman: proper option handling plus more options

Gordian Edenhofer gordian.edenhofer at gmail.com
Sun Aug 14 20:39:55 UTC 2016


* Add option for printing fewer status updates
* Add option for controlling the output directory
* Add option for specyfying the the maximum number of jobs
* Adjust output to the number of jobs
* Rewrite usage page
* Alter version information

Signed-off-by: Gordian Edenhofer <gordian.edenhofer at gmail.com>
---
* Streamline option syntax and version function with other scripts (mainly paccache)
* Use the provided parseopts instead of getopt

 contrib/bacman.sh.in | 136 +++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 98 insertions(+), 38 deletions(-)

diff --git a/contrib/bacman.sh.in b/contrib/bacman.sh.in
index 6c9b1e5..ba9177e 100755
--- a/contrib/bacman.sh.in
+++ b/contrib/bacman.sh.in
@@ -28,10 +28,12 @@ declare -r myname='bacman'
 declare -r myver='@PACKAGE_VERSION@'
 USE_COLOR='y'
 INCLUDE_PACNEW='n'
+QUIET='n'
 # Required for fakeroot because options are shifted off the array.
 ARGS=("$@")
 
 m4_include(../scripts/library/output_format.sh)
+m4_include(../scripts/library/parseopts.sh)
 
 # Lazy recursive clean up of temporary dirs
 work_dir_root="${TMPDIR:-/tmp}/bacman"
@@ -40,57 +42,98 @@ clean_up() {
 	echo
 	exit
 }
-
 # Trap termination signals
 trap clean_up SIGHUP SIGINT SIGTERM
 
 # Print usage information
 usage() {
-	echo "${myname} (pacman) v${myver}"
-	echo
-	echo "Recreate a package using pacman's database and system files"
-	echo
-	echo "Usage: ${myname} [--nocolor] [--pacnew] <installed package name>"
-	echo
-	echo "Example: ${myname} linux-headers"
+	cat <<-EOF
+	$myname (pacman) v$myver
+	This script was design to reassemble installed packages from its deliverd files.
+	It comes in handy if you have no access to an up-to-date package cache.
+
+	Usage: $myname [options] <package(s)>
+	    -h, --help              display this help message and exit
+	    -q, --quiet             silence most of the status reporting
+	    --nocolor               turn off color in output
+	    --pacnew                package .pacnew files
+	    -j, --jobs <jobs>       build in parallel - you may want to set XZ_OPT
+	    -o, --out  <dir>        write output to <dir>
+
+	Examples:   # $myname linux-headers
+	            # $myname gzip munge binutils -o ~/Downloads
+	            # $myname --nocolor --pacnew -o /tmp -j 5 gzip munge binutils
+	            # $myname \$(pacman -Qsq)
+	EOF
 }
 
 # Print version information
 version() {
 	printf "%s %s\n" "$myname" "$myver"
 	echo 'Copyright (C) 2008 locci <carlocci_at_gmail_dot_com>'
+	echo 'Copyright (C) 2016 Gordian Edenhofer <gordian.edenhofer at gmail.com>'
 	echo 'Copyright (C) 2008-2016 Pacman Development Team <pacman-dev at archlinux.org>'
 }
 
-# Check for specified arguments
-while [[ ! -z $1 ]]; do
-	if [[ $1 == "--nocolor" ]]; then
-		USE_COLOR='n'
-		shift
-	elif [[ $1 == "--pacnew" ]]; then
-		INCLUDE_PACNEW='y'
-		shift
-	else
-		break
-	fi
-done
-
 # Configure colored output
 m4_include(../scripts/library/term_colors.sh)
 
 # Break if no argument was given
-if (( $# < 1 )); then
+if (( $# == 0 )); then
+	usage
+	exit 1
+fi
+
+# Printing the usage information takes precedence over every other parameter
+for option in "$@"; do
+	[[ $option == "-h" || $option == "--help" ]] && usage && exit 0
+done
+
+# Parse arguments
+OPT_SHORT=':o:j:qv'
+OPT_LONG=('out:' 'jobs:' 'quiet' 'nocolor' 'pacnew' 'version')
+if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then
 	usage
 	exit 1
 fi
+set -- "${OPTRET[@]}"
+unset OPT_SHORT OPT_LONG OPTRET
+
+while :; do
+	case "$1" in
+		-o|--out)
+			pkg_dest="$(readlink -e $2)"
+			[[ $? -ne 0 ]] && echo -e "The directory \e[39;1m$2\e[0m does not exist!" && exit 3
+			shift ;;
+		-j|--jobs)
+			if [[ $2 =~ ^-?[0-9]+$ ]]; then
+				MAX_JOBS=$2
+			else
+				echo -e "\e[39;1m$2\e[0m is not a valid integer!"
+				exit -1
+			fi
+			shift ;;
+		-q|--quiet)
+			QUIET='y' ;;
+		--nocolor)
+			USE_COLOR='n' ;;
+		--pacnew)
+			INCLUDE_PACNEW='y' ;;
+		-v|--version)
+			version
+			exit 0 ;;
+		--)
+			shift
+			break 2 ;;
+	esac
+	shift
+done
 
-# Print usage or version if requested
-if [[ $1 = -@(h|-help) ]]; then
+# Retrieve the list of packages to be assembled and break if none was specified
+pkg_list=($*)
+if [[ ${#pkg_list[@]} == 0 ]]; then
 	usage
-	exit 0
-elif [[ $1 = -@(V|-version) ]]; then
-	version
-	exit 0
+	exit 1
 fi
 
 # Run with fake root privileges if EUID is not root
@@ -106,7 +149,7 @@ if (( EUID )); then
 	fi
 fi
 
-# Source environmental variables and specify fallbacks
+# Read in environmental variables
 if [[ ! -r @sysconfdir@/pacman.conf ]]; then
 	error "unable to read @sysconfdir@/pacman.conf"
 	exit 1
@@ -121,8 +164,9 @@ source "@sysconfdir@/makepkg.conf"
 if [[ -r ~/.makepkg.conf ]]; then
 	source ~/.makepkg.conf
 fi
-pkg_dest="${PKGDEST:-$PWD}"
-pkg_pkger=${PACKAGER:-'Unknown Packager'}
+PKGDEST="${PKGDEST:-$PWD}"
+pkg_dest="${pkg_dest:-$PKGDEST}"
+pkg_pkger="${PACKAGER:-'Unknown Packager'}"
 
 # Check for an existing database
 if [[ ! -d $pac_db ]]; then
@@ -155,7 +199,12 @@ fakebuild() {
 	cd "$work_dir" || exit 1
 
 	# Assemble list of files which belong to the package and tar them
-	msg2 "Copying package files..."
+	if [[ $MAX_JOBS -gt 1 && "$QUIET" != "y" ]]; then
+		msg2 "${pkg_name}: Copying package files..."
+	elif [[ "$QUIET" != "y" ]]; then
+		msg2 "Copying package files..."
+	fi
+
 	while read i; do
 		if [[ -z $i ]]; then
 			continue
@@ -229,7 +278,11 @@ fakebuild() {
 
 	# Reconstruct .PKGINFO from database
 	# TODO adopt makepkg's write_pkginfo() into this or scripts/library
-	msg2 "Generating .PKGINFO metadata..."
+	if [[ $MAX_JOBS -gt 1 && "$QUIET" != "y" ]]; then
+		msg2 "${pkg_name}: Generating .PKGINFO metadata..."
+	elif [[ "$QUIET" != "y" ]]; then
+		msg2 "Generating .PKGINFO metadata..."
+	fi
 	echo "# Generated by $myname $myver"    > .PKGINFO
 	if [[ $INFAKEROOT == "1" ]]; then
 		echo "# Using $(fakeroot -v)"    >> .PKGINFO
@@ -317,11 +370,16 @@ fakebuild() {
 	chmod 644 "$work_dir"/{.PKGINFO,.CHANGELOG,.INSTALL} 2> /dev/null
 
 	# Generate the package
-	msg2 "Generating the package..."
+	if [[ $MAX_JOBS -gt 1 && "$QUIET" != "y" ]]; then
+		msg2 "${pkg_name}: Generating the package..."
+	elif [[ "$QUIET" != "y" ]]; then
+		msg2 "Generating the package..."
+	fi
 
 	pkg_file="$pkg_dest/$pkg_namver-$pkg_arch${PKGEXT}"
 	ret=0
 
+	# Move compressed package to destination
 	# TODO: Maybe this can be set globally for robustness
 	shopt -s -o pipefail
 	bsdtar -cf - $comp_files * |
@@ -335,7 +393,7 @@ fakebuild() {
 		"$PKGEXT"; cat ;;
 	esac > "${pkg_file}"; ret=$?
 
-	# Move compressed package to destination
+	# Evaluate return code
 	if (( ret )); then
 		error "Unable to write package to $pkg_dest"
 		plain "       Maybe the disk is full or you do not have write access"
@@ -345,7 +403,6 @@ fakebuild() {
 
 	# Clean up working directory
 	rm -rf "$work_dir"
-	msg "Done."
 }
 
 # Run fakebuild in parralel with at maximum $MAX_JOBS jobs
@@ -365,11 +422,14 @@ parallelize() {
 
 # Initiate assembly function
 if [[ $MAX_JOBS -gt "1" ]]; then
-	parallelize "$@"
+	parallelize "${pkg_list[@]}"
 else
-	for PKG in $@; do fakebuild $PKG; done
+	for PKG in ${pkg_list[@]}; do
+		fakebuild $PKG
+	done
 fi
 
+msg "Done."
 exit 0
 
 # vim: set noet:
-- 
2.9.2


More information about the pacman-dev mailing list