[pacman-dev] [PATCH] makepkg: replace basename emulation via sed with a bash substitution

Cedric Staniewski cedric at gmx.ca
Sun Oct 18 10:52:30 EDT 2009


Allan McRae wrote:
> Cedric Staniewski wrote:
>> Signed-off-by: Cedric Staniewski <cedric at gmx.ca>
>> ---
>>  scripts/makepkg.sh.in |    4 ++--
>>  1 files changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in
>> index 24fddf6..ad8b6da 100644
>> --- a/scripts/makepkg.sh.in
>> +++ b/scripts/makepkg.sh.in
>> @@ -190,7 +190,7 @@ get_filename() {
>>      # if a filename is specified, use it
>>      local filename=$(echo $1 | sed 's|::.*||')
>>   
> 
> We could also use bash substitution for that line.
> 
>>      # if it is just an URL, we only keep the last component
>> -    echo "$filename" | sed 's|^.*://.*/||g'
>> +    echo "${filename##*/}"
>>  }
>>  
>>  # extract the URL from a source entry
>>   
> 
> and about two lines below here...
> 
>> @@ -282,7 +282,7 @@ in_array() {
>>  get_downloadclient() {
>>      # $1 = URL with valid protocol prefix
>>      local url=$1
>> -    local proto=$(echo "$url" | sed 's|://.*||')
>> +    local proto=$(echo ${url##*/})
>>  
>>      # loop through DOWNLOAD_AGENTS variable looking for protocol
>>      local i
>>   
> 
> 

There are several commands which could be replaced by bash substitutions. I attached a patch which also contains the first patch of this thread. So, if we decide for the 'bash way' rather than using basename, this one could be used.



>From 23b7c7346e58293e403b00a8ddff0c7e37498f46 Mon Sep 17 00:00:00 2001
From: Cedric Staniewski <cedric at gmx.ca>
Date: Sun, 18 Oct 2009 16:32:51 +0200
Subject: [PATCH] makepkg, repo-add: replace external commands with bash substitutions where possible

This also removes the awk dependency from makepkg and repo-add.

Signed-off-by: Cedric Staniewski <cedric at gmx.ca>
---

Probably, it is better to keep the sed command for extracting the latest svn revision. The 'bash way' is imo more complex, and the slow part of this command is anyway svn info.

Besides, I'm not sure about the change in write_pkginfo. `du` separates size and file/directory name by a tab and the output looks like this:
 	346616	.
Currently, I only remove the period at the end and bash strips the trailing tab by itself. Of course, it would be possible to use size=${size%%	*} with the whitespace being a tab (bash does not support \t in substitutions as it seems) which I do not really like, so the remaining possibility would be:

local size="$(du -sk | tr '\t' ' ')"
size="$(( ${size%% *} * 1024 ))"

which is not that good either.


 scripts/makepkg.sh.in  |   43 ++++++++++++++++++++++++-------------------
 scripts/repo-add.sh.in |    6 ++++--
 2 files changed, 28 insertions(+), 21 deletions(-)

diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in
index 24fddf6..41078e9 100644
--- a/scripts/makepkg.sh.in
+++ b/scripts/makepkg.sh.in
@@ -27,7 +27,7 @@
 
 # makepkg uses quite a few external programs during its execution. You
 # need to have at least the following installed for makepkg to function:
-#   awk, bsdtar (libarchive), bzip2, coreutils, fakeroot, find (findutils),
+#   bsdtar (libarchive), bzip2, coreutils, fakeroot, find (findutils),
 #   getopt (util-linux), gettext, grep, gzip, openssl, sed
 
 # gettext initialization
@@ -188,15 +188,15 @@ trap 'trap_exit "$(gettext "An unknown error has occurred. Exiting...")"' ERR
 # extract the filename from a source entry
 get_filename() {
 	# if a filename is specified, use it
-	local filename=$(echo $1 | sed 's|::.*||')
+	local filename="${1%%::*}"
 	# if it is just an URL, we only keep the last component
-	echo "$filename" | sed 's|^.*://.*/||g'
+	echo "${filename##*/}"
 }
 
 # extract the URL from a source entry
 get_url() {
 	# strip an eventual filename
-	echo $1 | sed 's|.*::||'
+	echo "${1#*::}"
 }
 
 ##
@@ -246,11 +246,11 @@ check_buildenv() {
 #          ? - not found
 ##
 in_opt_array() {
-	local needle=$(echo $1 | tr '[:upper:]' '[:lower:]'); shift
+	local needle="${1,,}"; shift
 
 	local opt
 	for opt in "$@"; do
-		opt=$(echo $opt | tr '[:upper:]' '[:lower:]')
+		opt="${opt,,}"
 		if [ "$opt" = "$needle" ]; then
 			echo 'y' # Enabled
 			return
@@ -282,14 +282,14 @@ in_array() {
 get_downloadclient() {
 	# $1 = URL with valid protocol prefix
 	local url=$1
-	local proto=$(echo "$url" | sed 's|://.*||')
+	local proto="${url%%://*}"
 
 	# loop through DOWNLOAD_AGENTS variable looking for protocol
 	local i
 	for i in "${DLAGENTS[@]}"; do
-		local handler=$(echo $i | sed 's|::.*||')
+		local handler="${i%%::*}"
 		if [ "$proto" = "$handler" ]; then
-			agent=$(echo $i | sed 's|^.*::||')
+			agent="${i##*::}"
 			break
 		fi
 	done
@@ -302,7 +302,7 @@ get_downloadclient() {
 	fi
 
 	# ensure specified program is installed
-	local program="$(echo $agent | awk '{print $1 }')"
+	local program="${agent%% *}"
 	if [ ! -x "$program" ]; then
 		local baseprog=$(basename $program)
 		error "$(gettext "The download program %s is not installed.")" "$baseprog"
@@ -321,7 +321,7 @@ download_file() {
 	# destination file
 	local file=$3
 	# temporary download file, default to last component of the URL
-	local dlfile=$(echo "$url" | sed 's|^.*://.*/||g')
+	local dlfile="${url##*/}"
 
 	# replace %o by the temporary dlfile if it exists
 	if echo "$dlcmd" | grep -q "%o" ; then
@@ -431,7 +431,7 @@ remove_deps() {
 	local dep depstrip deplist
 	deplist=""
 	for dep in $pkgdeps; do
-		depstrip=$(echo $dep | sed -e 's|=.*$||' -e 's|>.*$||' -e 's|<.*$||')
+		depstrip="${dep%%[<=>]*}"
 		deplist="$deplist $depstrip"
 	done
 
@@ -513,7 +513,7 @@ generate_checksums() {
 
 	local integ
 	for integ in ${INTEGRITY_CHECK[@]}; do
-		integ="$(echo $integ | tr '[:upper:]' '[:lower:]')"
+		integ="${integ,,}"
 		case "$integ" in
 			md5|sha1|sha256|sha384|sha512) : ;;
 			*)
@@ -546,7 +546,8 @@ generate_checksums() {
 				fi
 			fi
 
-			local sum="$(openssl dgst -${integ} "$file" | awk '{print $NF}')"
+			local sum="$(openssl dgst -${integ} "$file")"
+			sum=${sum##* }
 			[ $ct -gt 0 ] && echo -n "$indent"
 			echo -n "'$sum'"
 			ct=$(($ct+1))
@@ -591,8 +592,9 @@ check_checksums() {
 				fi
 
 				if [ $found -gt 0 ] ; then
-					local expectedsum="$(echo ${integrity_sums[$idx]} | tr '[A-F]' '[a-f]')"
-					local realsum="$(openssl dgst -${integ} "$file" | awk '{print $NF}')"
+					local expectedsum="${integrity_sums[$idx],,}"
+					local realsum="$(openssl dgst -${integ} "$file")"
+					realsum="${realsum##* }"
 					if [ "$expectedsum" = "$realsum" ]; then
 						echo "$(gettext "Passed")" >&2
 					else
@@ -882,7 +884,8 @@ write_pkginfo() {
 	else
 		local packager="Unknown Packager"
 	fi
-	local size=$(du -sk | awk '{print $1 * 1024}')
+	local size="$(du -sk)"
+	size="$(( ${size%%.*} * 1024 ))"
 
 	msg2 "$(gettext "Generating .PKGINFO file...")"
 	echo "# Generated by makepkg $myver" >.PKGINFO
@@ -1265,7 +1268,9 @@ devel_check() {
 		elif [ -n "${_svntrunk}" -a -n "${_svnmod}" ] ; then
 			[ $(type -p svn) ] || return 0
 			msg "$(gettext "Determining latest svn revision...")"
-			newpkgver=$(LC_ALL=C svn info $_svntrunk | sed -n 's/^Last Changed Rev: \([0-9]*\)$/\1/p')
+			newpkgver="$(LC_ALL=C svn info $_svntrunk)"
+			newpkgver="${newpkgver##*Last Changed Rev: }"
+			newpkgver=${newpkgver%%[A-Z]*}
 		elif [ -n "${_bzrtrunk}" -a -n "${_bzrmod}" ] ; then
 			[ $(type -p bzr) ] || return 0
 			msg "$(gettext "Determining latest bzr revision...")"
@@ -1588,7 +1593,7 @@ if [ "$CLEANCACHE" -eq 1 ]; then
 		echo -n "$(gettext "    Are you sure you wish to do this? ")"
 		echo -n "$(gettext "[Y/n]")"
 		read answer
-		answer=$(echo $answer | tr '[:lower:]' '[:upper:]')
+		answer="${answer^^}"
 		if [ "$answer" = "$(gettext "YES")" -o "$answer" = "$(gettext "Y")" ]; then
 			rm "$SRCDEST"/*
 			if [ $? -ne 0 ]; then
diff --git a/scripts/repo-add.sh.in b/scripts/repo-add.sh.in
index 2a62d2b..3f00441 100644
--- a/scripts/repo-add.sh.in
+++ b/scripts/repo-add.sh.in
@@ -136,7 +136,8 @@ db_write_delta()
 		echo -e "%DELTAS%" >>$deltas
 	fi
 	# get md5sum and compressed size of package
-	md5sum="$(openssl dgst -md5 "$deltafile" | awk '{print $NF}')"
+	md5sum="$(openssl dgst -md5 "$deltafile")"
+	md5sum="${md5sum##* }"
 	csize=$(@SIZECMD@ "$deltafile")
 
 	oldfile=$(xdelta3 printhdr $deltafile | grep "XDELTA filename (source)" | sed 's/.*: *//')
@@ -213,7 +214,8 @@ db_write_entry()
 	IFS=$OLDIFS
 
 	# get md5sum and compressed size of package
-	md5sum="$(openssl dgst -md5 "$pkgfile" | awk '{print $NF}')"
+	md5sum="$(openssl dgst -md5 "$pkgfile")"
+	md5sum="${md5sum##* }"
 	csize=$(@SIZECMD@ "$pkgfile")
 
 	# ensure $pkgname and $pkgver variables were found
-- 
1.6.5


More information about the pacman-dev mailing list