[pacman-dev] [PATCH] makepkg: refactor absolute filename detection
Move the absolute filename detection to a new function to reduce code duplication. This patch also fixes the --allsource option that did not include remote source files if they reside in $startdir instead of $SRCDEST. Signed-off-by: Cedric Staniewski <cedric@gmx.ca> --- Changes: - renamed error_source_file_not_found to missing_source_exit - removed differentiation in download_sources: now it prints "Found %s" instead of "Found %s in build dir" and "Using cached copy of %s" scripts/makepkg.sh.in | 99 ++++++++++++++++++++++++------------------------- 1 files changed, 48 insertions(+), 51 deletions(-) diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 8c0da8b..00167f7 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -179,6 +179,33 @@ trap 'trap_exit "$(gettext "An unknown error has occurred. Exiting...")"' ERR # 1) "filename::http://path/to/file" # 2) "http://path/to/file" +# Return the absolute filename of a source entry +# +# This function accepts a source entry or the already extracted filename of a +# source entry as input +get_absolute_filename() { + local file="$(get_filename "$1")" + + if [[ -f "$startdir/$file" ]] ; then + file="$startdir/$file" + else + if [[ -f "$SRCDEST/$file" ]] ; then + file="$SRCDEST/$file" + else + return 1 + fi + fi + + echo "$file" +} + +# Print 'source not found' error message and exit makepkg +missing_source_exit() { + error "$(gettext "Unable to find source file %s.")" "$(get_filename "$1")" + plain "$(gettext "Aborting...")" + exit 1 # $E_MISSING_FILE +} + # extract the filename from a source entry get_filename() { # if a filename is specified, use it @@ -458,20 +485,16 @@ download_sources() { local netfile for netfile in "${source[@]}"; do - local file=$(get_filename "$netfile") - local url=$(get_url "$netfile") - if [[ -f $startdir/$file ]]; then - msg2 "$(gettext "Found %s in build dir")" "$file" - rm -f "$srcdir/$file" - ln -s "$startdir/$file" "$srcdir/" - continue - elif [[ -f $SRCDEST/$file ]]; then - msg2 "$(gettext "Using cached copy of %s")" "$file" - rm -f "$srcdir/$file" - ln -s "$SRCDEST/$file" "$srcdir/" + local file + if file=$(get_absolute_filename "$netfile"); then + msg2 "$(gettext "Found %s")" "${file##*/}" + ln -sf "$file" "$srcdir/" continue fi + file=$(get_filename "$netfile") + local url=$(get_url "$netfile") + # if we get here, check to make sure it was a URL, else fail if [[ $file = $url ]]; then error "$(gettext "%s was not found in the build directory and is not a URL.")" "$file" @@ -553,18 +576,7 @@ generate_checksums() { local netfile for netfile in "${source[@]}"; do - local file="$(get_filename "$netfile")" - - if [[ ! -f $file ]] ; then - if [[ ! -f $SRCDEST/$file ]] ; then - error "$(gettext "Unable to find source file %s to generate checksum.")" "$file" - plain "$(gettext "Aborting...")" - exit 1 - else - file="$SRCDEST/$file" - fi - fi - + local file="$(get_absolute_filename "$netfile")" || missing_source_exit "$netfile" local sum="$(openssl dgst -${integ} "$file")" sum=${sum##* } (( ct )) && echo -n "$indent" @@ -600,14 +612,10 @@ check_checksums() { file="$(get_filename "$file")" echo -n " $file ... " >&2 - if [[ ! -f $file ]] ; then - if [[ ! -f $SRCDEST/$file ]] ; then - echo "$(gettext "NOT FOUND")" >&2 - errors=1 - found=0 - else - file="$SRCDEST/$file" - fi + if ! file="$(get_absolute_filename "$file")"; then + echo "$(gettext "NOT FOUND")" >&2 + errors=1 + found=0 fi if (( $found )) ; then @@ -645,25 +653,17 @@ extract_sources() { msg "$(gettext "Extracting Sources...")" local netfile for netfile in "${source[@]}"; do - file=$(get_filename "$netfile") + absfile="$(get_absolute_filename "$netfile")" || missing_source_exit "$netfile" + file=${absfile##*/} if in_array "$file" ${noextract[@]}; then #skip source files in the noextract=() array # these are marked explicitly to NOT be extracted continue fi - if [[ ! -f $file ]] ; then - if [[ ! -f $SRCDEST/$file ]] ; then - error "$(gettext "Unable to find source file %s for extraction.")" "$file" - plain "$(gettext "Aborting...")" - exit 1 - else - file="$SRCDEST/$file" - fi - fi # fix flyspray #6246 - local file_type=$(file -bizL "$file") + local file_type=$(file -bizL "$absfile") local ext=${file##*.} local cmd='' case "$file_type" in @@ -693,10 +693,10 @@ extract_sources() { local ret=0 msg2 "$(gettext "Extracting %s with %s")" "$file" "$cmd" if [[ $cmd = bsdtar ]]; then - $cmd -xf "$file" || ret=$? + $cmd -xf "$absfile" || ret=$? else rm -f "${file%.*}" - $cmd -dcf "$file" > "${file%.*}" || ret=$? + $cmd -dcf "$absfile" > "${file%.*}" || ret=$? fi if (( ret )); then error "$(gettext "Failed to extract %s")" "$file" @@ -1097,13 +1097,10 @@ create_srcpackage() { local netfile for netfile in "${source[@]}"; do - local file=$(get_filename "$netfile") - if [[ -f $netfile ]]; then - msg2 "$(gettext "Adding %s...")" "$netfile" - ln -s "${startdir}/$netfile" "${srclinks}/${pkgbase}" - elif (( SOURCEONLY == 2 )) && [[ -f $SRCDEST/$file ]]; then - msg2 "$(gettext "Adding %s...")" "$file" - ln -s "$SRCDEST/$file" "${srclinks}/${pkgbase}/" + if [[ -f $netfile ]] || (( SOURCEONLY == 2 )); then + local file=$(get_absolute_filename "$netfile") || missing_source_exit "$netfile" + msg2 "$(gettext "Adding %s...")" "${file##*/}" + ln -s "$file" "${srclinks}/${pkgbase}" fi done -- 1.7.1
On 28/05/10 00:35, Cedric Staniewski wrote:
Move the absolute filename detection to a new function to reduce code duplication.
This patch also fixes the --allsource option that did not include remote source files if they reside in $startdir instead of $SRCDEST.
Signed-off-by: Cedric Staniewski<cedric@gmx.ca> ---
Changes: - renamed error_source_file_not_found to missing_source_exit - removed differentiation in download_sources: now it prints "Found %s" instead of "Found %s in build dir" and "Using cached copy of %s"
I have very minor comments below. Suggested naming comments are debatable...
scripts/makepkg.sh.in | 99 ++++++++++++++++++++++++------------------------- 1 files changed, 48 insertions(+), 51 deletions(-)
diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 8c0da8b..00167f7 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -179,6 +179,33 @@ trap 'trap_exit "$(gettext "An unknown error has occurred. Exiting...")"' ERR # 1) "filename::http://path/to/file" # 2) "http://path/to/file"
+# Return the absolute filename of a source entry +# +# This function accepts a source entry or the already extracted filename of a +# source entry as input +get_absolute_filename() {
get_filepath()?
+ local file="$(get_filename "$1")" + + if [[ -f "$startdir/$file" ]] ; then + file="$startdir/$file" + else
I'd prefer and "elif" here as it seems cleaner.
+ if [[ -f "$SRCDEST/$file" ]] ; then + file="$SRCDEST/$file" + else + return 1 + fi + fi + + echo "$file" +} + +# Print 'source not found' error message and exit makepkg +missing_source_exit() {
I do not really think a three line function needs documented. Also, I think "missing_source_file" sounds a better name especially when you read it after a "||" as it is used.
+ error "$(gettext "Unable to find source file %s.")" "$(get_filename "$1")" + plain "$(gettext "Aborting...")" + exit 1 # $E_MISSING_FILE +} + # extract the filename from a source entry get_filename() { # if a filename is specified, use it
<snip>
@@ -645,25 +653,17 @@ extract_sources() { msg "$(gettext "Extracting Sources...")" local netfile for netfile in "${source[@]}"; do - file=$(get_filename "$netfile") + absfile="$(get_absolute_filename "$netfile")" || missing_source_exit "$netfile" + file=${absfile##*/} if in_array "$file" ${noextract[@]}; then #skip source files in the noextract=() array # these are marked explicitly to NOT be extracted continue fi
- if [[ ! -f $file ]] ; then - if [[ ! -f $SRCDEST/$file ]] ; then - error "$(gettext "Unable to find source file %s for extraction.")" "$file" - plain "$(gettext "Aborting...")" - exit 1 - else - file="$SRCDEST/$file" - fi - fi
# fix flyspray #6246 - local file_type=$(file -bizL "$file") + local file_type=$(file -bizL "$absfile")
I do not think that this change is not needed.
local ext=${file##*.} local cmd='' case "$file_type" in @@ -693,10 +693,10 @@ extract_sources() { local ret=0 msg2 "$(gettext "Extracting %s with %s")" "$file" "$cmd" if [[ $cmd = bsdtar ]]; then - $cmd -xf "$file" || ret=$? + $cmd -xf "$absfile" || ret=$? else rm -f "${file%.*}" - $cmd -dcf "$file"> "${file%.*}" || ret=$? + $cmd -dcf "$absfile"> "${file%.*}" || ret=$?
or these two. We have set up symlinks in $srcdir so we should just use them
fi if (( ret )); then error "$(gettext "Failed to extract %s")" "$file" @@ -1097,13 +1097,10 @@ create_srcpackage() {
local netfile for netfile in "${source[@]}"; do - local file=$(get_filename "$netfile") - if [[ -f $netfile ]]; then - msg2 "$(gettext "Adding %s...")" "$netfile" - ln -s "${startdir}/$netfile" "${srclinks}/${pkgbase}" - elif (( SOURCEONLY == 2 ))&& [[ -f $SRCDEST/$file ]]; then - msg2 "$(gettext "Adding %s...")" "$file" - ln -s "$SRCDEST/$file" "${srclinks}/${pkgbase}/" + if [[ -f $netfile ]] || (( SOURCEONLY == 2 )); then + local file=$(get_absolute_filename "$netfile") || missing_source_exit "$netfile" + msg2 "$(gettext "Adding %s...")" "${file##*/}" + ln -s "$file" "${srclinks}/${pkgbase}" fi done
While this works, I would prefer to keep the distinction between local and downloaded files. So something like: local file for file in "${source[@]}"; do if [[ -f $file ]]; then msg2 "$(gettext "Adding %s...")" "$file" ln -s "${startdir}/$file" "${srclinks}/${pkgbase}" elif (( SOURCEONLY == 2 )); then local absfile=$(get_absolute_filename "$file") || missing_source_exit "$file" msg2 "$(gettext "Adding %s...")" "${absfile##*/}" ln -s "$absfile" "${srclinks}/${pkgbase}" fi done Note the renaming of "netfile" as the name makes little sense anymore... With those comments addressed, this patch is good to go. I would like this in the 3.4 release if possible since it is a bug fix. Given it has string changes, that would mean it would need resubmitted soon. I apologize for rushing you after taking so long to comment, but I can take care of the changes if you are busy at the moment. Allan
On 03.06.2010 05:18, Allan McRae wrote:
With those comments addressed, this patch is good to go.
I would like this in the 3.4 release if possible since it is a bug fix. Given it has string changes, that would mean it would need resubmitted soon. I apologize for rushing you after taking so long to comment, but I can take care of the changes if you are busy at the moment.
Thanks, I'll try to resubmit it in the next few hours.
- local file_type=$(file -bizL "$file") + local file_type=$(file -bizL "$absfile")
I do not think that this change is not needed.
Was there a "buy one, get one free" offer? :)
Move the absolute filename detection to a new function to reduce code duplication. This patch also fixes the --allsource option that did not include remote source files if they reside in $startdir instead of $SRCDEST. Signed-off-by: Cedric Staniewski <cedric@gmx.ca> --- scripts/makepkg.sh.in | 93 +++++++++++++++++++++++------------------------- 1 files changed, 45 insertions(+), 48 deletions(-) diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 76b6183..29f6ede 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -179,6 +179,31 @@ trap 'trap_exit "$(gettext "An unknown error has occurred. Exiting...")"' ERR # 1) "filename::http://path/to/file" # 2) "http://path/to/file" +# Return the absolute filename of a source entry +# +# This function accepts a source entry or the already extracted filename of a +# source entry as input +get_filepath() { + local file="$(get_filename "$1")" + + if [[ -f "$startdir/$file" ]]; then + file="$startdir/$file" + elif [[ -f "$SRCDEST/$file" ]]; then + file="$SRCDEST/$file" + else + return 1 + fi + + echo "$file" +} + +# Print 'source not found' error message and exit makepkg +missing_source_file() { + error "$(gettext "Unable to find source file %s.")" "$(get_filename "$1")" + plain "$(gettext "Aborting...")" + exit 1 # $E_MISSING_FILE +} + # extract the filename from a source entry get_filename() { # if a filename is specified, use it @@ -458,20 +483,16 @@ download_sources() { local netfile for netfile in "${source[@]}"; do - local file=$(get_filename "$netfile") - local url=$(get_url "$netfile") - if [[ -f $startdir/$file ]]; then - msg2 "$(gettext "Found %s in build dir")" "$file" - rm -f "$srcdir/$file" - ln -s "$startdir/$file" "$srcdir/" - continue - elif [[ -f $SRCDEST/$file ]]; then - msg2 "$(gettext "Using cached copy of %s")" "$file" - rm -f "$srcdir/$file" - ln -s "$SRCDEST/$file" "$srcdir/" + local file + if file=$(get_filepath "$netfile"); then + msg2 "$(gettext "Found %s")" "${file##*/}" + ln -sf "$file" "$srcdir/" continue fi + file=$(get_filename "$netfile") + local url=$(get_url "$netfile") + # if we get here, check to make sure it was a URL, else fail if [[ $file = $url ]]; then error "$(gettext "%s was not found in the build directory and is not a URL.")" "$file" @@ -553,18 +574,7 @@ generate_checksums() { local netfile for netfile in "${source[@]}"; do - local file="$(get_filename "$netfile")" - - if [[ ! -f $file ]] ; then - if [[ ! -f $SRCDEST/$file ]] ; then - error "$(gettext "Unable to find source file %s to generate checksum.")" "$file" - plain "$(gettext "Aborting...")" - exit 1 - else - file="$SRCDEST/$file" - fi - fi - + local file="$(get_filepath "$netfile")" || missing_source_file "$netfile" local sum="$(openssl dgst -${integ} "$file")" sum=${sum##* } (( ct )) && echo -n "$indent" @@ -600,14 +610,10 @@ check_checksums() { file="$(get_filename "$file")" echo -n " $file ... " >&2 - if [[ ! -f $file ]] ; then - if [[ ! -f $SRCDEST/$file ]] ; then - echo "$(gettext "NOT FOUND")" >&2 - errors=1 - found=0 - else - file="$SRCDEST/$file" - fi + if ! file="$(get_filepath "$file")"; then + echo "$(gettext "NOT FOUND")" >&2 + errors=1 + found=0 fi if (( $found )) ; then @@ -652,15 +658,6 @@ extract_sources() { continue fi - if [[ ! -f $file ]] ; then - if [[ ! -f $SRCDEST/$file ]] ; then - error "$(gettext "Unable to find source file %s for extraction.")" "$file" - plain "$(gettext "Aborting...")" - exit 1 - else - file="$SRCDEST/$file" - fi - fi # fix flyspray #6246 local file_type=$(file -bizL "$file") @@ -1095,15 +1092,15 @@ create_srcpackage() { msg2 "$(gettext "Adding %s...")" "$BUILDSCRIPT" ln -s "${BUILDFILE}" "${srclinks}/${pkgbase}/${BUILDSCRIPT}" - local netfile - for netfile in "${source[@]}"; do - local file=$(get_filename "$netfile") - if [[ -f $netfile ]]; then - msg2 "$(gettext "Adding %s...")" "$netfile" - ln -s "${startdir}/$netfile" "${srclinks}/${pkgbase}" - elif (( SOURCEONLY == 2 )) && [[ -f $SRCDEST/$file ]]; then + local file + for file in "${source[@]}"; do + if [[ -f $file ]]; then msg2 "$(gettext "Adding %s...")" "$file" - ln -s "$SRCDEST/$file" "${srclinks}/${pkgbase}/" + ln -s "$file" "$srclinks/$pkgbase" + elif (( SOURCEONLY == 2 )); then + local absfile=$(get_filepath "$file") || missing_source_file "$file" + msg2 "$(gettext "Adding %s...")" "${absfile##*/}" + ln -s "$absfile" "$srclinks/$pkgbase" fi done -- 1.7.1
On 04/06/10 01:32, Cedric Staniewski wrote:
Move the absolute filename detection to a new function to reduce code duplication.
This patch also fixes the --allsource option that did not include remote source files if they reside in $startdir instead of $SRCDEST.
Signed-off-by: Cedric Staniewski<cedric@gmx.ca> --- scripts/makepkg.sh.in | 93 +++++++++++++++++++++++------------------------- 1 files changed, 45 insertions(+), 48 deletions(-)
Great! Tested building and making source packages with the source in $startdir and $SRCDEST and can not spot any issues. Pushed to my working branch. Allan
participants (2)
-
Allan McRae
-
Cedric Staniewski