[pacman-dev] [PATCH 1/6] libmakepkg/util/option: Refactor checking to reduce code duplication
Pull out the expected=y/n check into a separate function and make use of the fact we can just prepend the fallback arrays to get the same result. --- scripts/libmakepkg/util/option.sh.in | 85 +++++++++------------------- 1 file changed, 28 insertions(+), 57 deletions(-) diff --git a/scripts/libmakepkg/util/option.sh.in b/scripts/libmakepkg/util/option.sh.in index 46e0568d..3299df9c 100644 --- a/scripts/libmakepkg/util/option.sh.in +++ b/scripts/libmakepkg/util/option.sh.in @@ -48,95 +48,66 @@ in_opt_array() { } +## +# usage : check_opt_array( $option, $expected_val, $haystack ) +# return : 0 - matches expected +# 1 - does not match expected +# 127 - not found +## +check_opt_array() { + local option=$1 expected=$2; shift 2 + + in_opt_array "$option" "$@" + case $? in + 0) # assert enabled + [[ $expected = y ]] + return ;; + 1) # assert disabled + [[ $expected = n ]] + return ;; + esac + + # not found + return 127 +} + + ## # Checks to see if options are present in makepkg.conf or PKGBUILD; # PKGBUILD options always take precedence. # # usage : check_option( $option, $expected_val ) # return : 0 - matches expected # 1 - does not match expected # 127 - not found ## check_option() { - in_opt_array "$1" ${options[@]} - case $? in - 0) # assert enabled - [[ $2 = y ]] - return ;; - 1) # assert disabled - [[ $2 = n ]] - return - esac - - # fall back to makepkg.conf options - in_opt_array "$1" ${OPTIONS[@]} - case $? in - 0) # assert enabled - [[ $2 = y ]] - return ;; - 1) # assert disabled - [[ $2 = n ]] - return - esac - - # not found - return 127 + check_opt_array "$@" "${OPTIONS[@]}" "${options[@]}" } ## # Check if option is present in BUILDENV # # usage : check_buildenv( $option, $expected_val ) # return : 0 - matches expected # 1 - does not match expected # 127 - not found ## check_buildenv() { - in_opt_array "$1" ${BUILDENV[@]} - case $? in - 0) # assert enabled - [[ $2 = "y" ]] - return ;; - 1) # assert disabled - [[ $2 = "n" ]] - return ;; - esac - - # not found - return 127 + check_opt_array "$@" "${BUILDENV[@]}" } + ## # Checks to see if options are present in BUILDENV or PKGBUILD; # PKGBUILD options always take precedence. # # usage : check_buildoption( $option, $expected_val ) # return : 0 - matches expected # 1 - does not match expected # 127 - not found ## check_buildoption() { - in_opt_array "$1" ${options[@]} - case $? in - 0) # assert enabled - [[ $2 = y ]] - return ;; - 1) # assert disabled - [[ $2 = n ]] - return - esac - - in_opt_array "$1" ${BUILDENV[@]} - case $? in - 0) # assert enabled - [[ $2 = y ]] - return ;; - 1) # assert disabled - [[ $2 = n ]] - return - esac - - # not found - return 127 + check_opt_array "$@" "${BUILDENV[@]}" "${options[@]}" } -- 2.17.1
--- scripts/completion/bash_completion.in | 2 +- scripts/completion/zsh_completion.in | 8 ++++---- .../libmakepkg/integrity/verify_signature.sh.in | 2 +- scripts/libmakepkg/util/util.sh.in | 2 +- scripts/makepkg.sh.in | 14 +++++++------- scripts/pacman-key.sh.in | 6 +++--- scripts/repo-add.sh.in | 2 +- 7 files changed, 18 insertions(+), 18 deletions(-) diff --git a/scripts/completion/bash_completion.in b/scripts/completion/bash_completion.in index d750e12f..d99fba53 100644 --- a/scripts/completion/bash_completion.in +++ b/scripts/completion/bash_completion.in @@ -131,7 +131,7 @@ _pacman() { D|R) _pacman_pkg Qq;; F) - _arch_incomp 'l list' && _pacman_pkg Slq; + _arch_incomp 'l list' && _pacman_pkg Slq ;; Q) { _arch_incomp 'g groups' && _pacman_pkg Qg sort; } || diff --git a/scripts/completion/zsh_completion.in b/scripts/completion/zsh_completion.in index 77449955..c114ae02 100644 --- a/scripts/completion/zsh_completion.in +++ b/scripts/completion/zsh_completion.in @@ -370,7 +370,7 @@ _pacman_get_command() { # main dispatcher _pacman_zsh_comp() { - local -a args cmds; + local -a args cmds local tmp args=( ${${${(M)words:#-*}#-}:#-*} ) for tmp in $words; do @@ -465,7 +465,7 @@ _pacman_zsh_comp() { if (( ${(w)#cmds} == 1 )); then _pacman_action_help else - return 0; + return 0 fi ;; *--sync*) @@ -554,7 +554,7 @@ _pacman_key() { "$_key_longopts[@]" ;; *) - i=$#; + i=$# while [[ $words[$i] != -* ]] && [[ $words[$i] != "pacman-key" ]];do i=$(($i-1)) done @@ -681,7 +681,7 @@ _makepkg(){ *) i=$# while [[ $words[i] != -* ]] && [[ $words[$i] != "makepkg" ]];do - i=$((i-1)); + i=$((i-1)) done case $words[$i] in -*) diff --git a/scripts/libmakepkg/integrity/verify_signature.sh.in b/scripts/libmakepkg/integrity/verify_signature.sh.in index ea877822..b5f9eee9 100644 --- a/scripts/libmakepkg/integrity/verify_signature.sh.in +++ b/scripts/libmakepkg/integrity/verify_signature.sh.in @@ -137,7 +137,7 @@ verify_file_signature() { for ext in "" gz bz2 xz lrz lzo Z; do if sourcefile="$(get_filepath "${file%.*}${ext:+.$ext}")"; then found=1 - break; + break fi done if (( ! found )); then diff --git a/scripts/libmakepkg/util/util.sh.in b/scripts/libmakepkg/util/util.sh.in index e1ca5cb7..75bdffc0 100644 --- a/scripts/libmakepkg/util/util.sh.in +++ b/scripts/libmakepkg/util/util.sh.in @@ -51,7 +51,7 @@ is_array() { # Canonicalize a directory path if it exists canonicalize_path() { - local path="$1"; + local path="$1" if [[ -d $path ]]; then ( diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 32423262..053e6569 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -507,23 +507,23 @@ run_package() { } find_libdepends() { - local d sodepends; + local d sodepends - sodepends=0; + sodepends=0 for d in "${depends[@]}"; do if [[ $d = *.so ]]; then - sodepends=1; - break; + sodepends=1 + break fi done if (( sodepends == 0 )); then (( ${#depends[@]} )) && printf '%s\n' "${depends[@]}" return 0 fi - local libdeps filename soarch sofile soname soversion; - declare -A libdeps; + local libdeps filename soarch sofile soname soversion + declare -A libdeps while read -r filename; do # get architecture of the file; if soarch is empty it's not an ELF binary @@ -1235,7 +1235,7 @@ OPT_LONG=('allsource' 'check' 'clean' 'cleanbuild' 'config:' 'force' 'geninteg' OPT_LONG+=('asdeps' 'noconfirm' 'needed' 'noprogressbar') if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then - exit $E_INVALID_OPTION; + exit $E_INVALID_OPTION fi set -- "${OPTRET[@]}" unset OPT_SHORT OPT_LONG OPTRET diff --git a/scripts/pacman-key.sh.in b/scripts/pacman-key.sh.in index 5e75230f..bfa5c82c 100644 --- a/scripts/pacman-key.sh.in +++ b/scripts/pacman-key.sh.in @@ -521,14 +521,14 @@ OPT_LONG=('add' 'config:' 'delete' 'edit-key' 'export' 'finger' 'gpgdir:' 'lsign-key' 'nocolor' 'populate' 'recv-keys' 'refresh-keys' 'updatedb' 'verify' 'version') if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then - exit 1 # E_INVALID_OPTION; + exit 1 # E_INVALID_OPTION fi set -- "${OPTRET[@]}" unset OPT_SHORT OPT_LONG OPTRET if [[ $1 == "--" ]]; then - usage; - exit 0; + usage + exit 0 fi while (( $# )); do diff --git a/scripts/repo-add.sh.in b/scripts/repo-add.sh.in index e80e1278..04582fff 100644 --- a/scripts/repo-add.sh.in +++ b/scripts/repo-add.sh.in @@ -350,7 +350,7 @@ db_write_entry() { if [[ -d $tmpdir/db/$pkgname-$pkgver ]]; then warning "$(gettext "An entry for '%s' already existed")" "$pkgname-$pkgver" if (( ONLYADDNEW )); then - return 0; + return 0 fi else if (( DELTA || RMEXISTING )); then -- 2.17.1
--- scripts/makepkg.sh.in | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 053e6569..331249ea 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -496,14 +496,7 @@ run_check() { } run_package() { - local pkgfunc - if [[ -z $1 ]]; then - pkgfunc="package" - else - pkgfunc="package_$1" - fi - - run_function_safe "$pkgfunc" + run_function_safe "package${1:+_$1}" } find_libdepends() { -- 2.17.1
We don't need to re-backup the variables we restored on the previous iteration. --- scripts/makepkg.sh.in | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 331249ea..bf228202 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -1124,11 +1124,10 @@ backup_package_variables() { run_split_packaging() { local pkgname_backup=("${pkgname[@]}") - local restore_package_variables + local restore_package_variables="$(backup_package_variables)" for pkgname in ${pkgname_backup[@]}; do pkgdir="$pkgdirbase/$pkgname" mkdir "$pkgdir" - restore_package_variables="$(backup_package_variables)" run_package $pkgname tidy_install lint_package || exit $E_PACKAGE_FAILED -- 2.17.1
Merge the similar code handling unsplit PKGBUILDs and individual packages in a split PKGBUILD and make it a new function. --- scripts/makepkg.sh.in | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index bf228202..70a83a40 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -1122,20 +1122,25 @@ backup_package_variables() { done } +run_solo_packaging() { + pkgdir="$pkgdirbase/$pkgname" + mkdir "$pkgdir" + if [[ -n $1 ]] || (( PKGFUNC )); then + run_package $1 + fi + tidy_install + lint_package || exit $E_PACKAGE_FAILED + create_package +} + run_split_packaging() { local pkgname_backup=("${pkgname[@]}") local restore_package_variables="$(backup_package_variables)" for pkgname in ${pkgname_backup[@]}; do - pkgdir="$pkgdirbase/$pkgname" - mkdir "$pkgdir" - run_package $pkgname - tidy_install - lint_package || exit $E_PACKAGE_FAILED - create_package + run_solo_packaging $pkgname eval "$restore_package_variables" done pkgname=("${pkgname_backup[@]}") - create_debug_package } usage() { @@ -1538,19 +1543,13 @@ if (( INFAKEROOT )); then chmod 755 "$pkgdirbase" if (( ! SPLITPKG )); then - pkgdir="$pkgdirbase/$pkgname" - mkdir "$pkgdir" - if (( PKGFUNC )); then - run_package - fi - tidy_install - lint_package || exit $E_PACKAGE_FAILED - create_package - create_debug_package + run_solo_packaging else run_split_packaging fi + create_debug_package + msg "$(gettext "Leaving %s environment.")" "fakeroot" exit $E_OK fi -- 2.17.1
Causes it to be reset (to $pkgdirbase/$pkgbase) between subpackages. This shouldn't be visible. --- scripts/makepkg.sh.in | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 70a83a40..ceca55dd 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -775,13 +775,13 @@ create_package() { } create_debug_package() { + local pkgdir="$pkgdirbase/$pkgbase-@DEBUGSUFFIX@" + # check if a debug package was requested if ! check_option "debug" "y" || ! check_option "strip" "y"; then return 0 fi - pkgdir="$pkgdirbase/$pkgbase-@DEBUGSUFFIX@" - # check if we have any debug symbols to package if dir_is_empty "$pkgdir/usr/lib/debug"; then return 0 @@ -1123,7 +1123,7 @@ backup_package_variables() { } run_solo_packaging() { - pkgdir="$pkgdirbase/$pkgname" + local pkgdir="$pkgdirbase/$pkgname" mkdir "$pkgdir" if [[ -n $1 ]] || (( PKGFUNC )); then run_package $1 -- 2.17.1
participants (1)
-
Jan Alexander Steffens (heftig)