[pacman-dev] [PATCH 1/2] makepkg: move config loading into libmakepkg
Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> --- For some reason, parsing this does not seem to be intuitive to most thirdparty consumers. For example, devtools uses grep and eval! And for the longest time, it also did not support user makepkg.conf. Other issues seen in the wild by makepkg.conf parsers include ignoring ~/.makepkg.conf, *only* using ~/.makepkg.conf, etc. This is also sort of an uncomfortable amount of code to reimplement, so I figured it could benefit from being split out into a utility library. scripts/Makefile.am | 1 + scripts/libmakepkg/util/config.sh.in | 55 ++++++++++++++++++++++++++++ scripts/libmakepkg/util/meson.build | 1 + scripts/libmakepkg/util/util.sh.in | 16 ++++++++ scripts/makepkg.sh.in | 29 +-------------- 5 files changed, 74 insertions(+), 28 deletions(-) create mode 100644 scripts/libmakepkg/util/config.sh.in diff --git a/scripts/Makefile.am b/scripts/Makefile.am index 2c743e82..4e5876af 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -119,6 +119,7 @@ LIBMAKEPKG_IN = \ libmakepkg/tidy/zipman.sh \ libmakepkg/util.sh \ libmakepkg/util/compress.sh \ + libmakepkg/util/config.sh \ libmakepkg/util/error.sh \ libmakepkg/util/message.sh \ libmakepkg/util/option.sh \ diff --git a/scripts/libmakepkg/util/config.sh.in b/scripts/libmakepkg/util/config.sh.in new file mode 100644 index 00000000..fdfeef89 --- /dev/null +++ b/scripts/libmakepkg/util/config.sh.in @@ -0,0 +1,55 @@ +#!/bin/bash +# +# config.sh - functions for handling makepkg config files +# +# Copyright (c) 2006-2019 Pacman Development Team <pacman-dev@archlinux.org> +# Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +[[ -n "$LIBMAKEPKG_UTIL_CONFIG_SH" ]] && return +LIBMAKEPKG_UTIL_CONFIG_SH=1 + +LIBRARY=${LIBRARY:-'@libmakepkgdir@'} + +source "$LIBRARY/util/error.sh" +source "$LIBRARY/util/util.sh" + +# correctly source makepkg.conf, respecting user precedence and the system conf +source_makepkg_config() { + # $1: override system config file + + local MAKEPKG_CONF=${1:-@sysconfdir@/makepkg.conf} + + # Source the config file; fail if it is not found + if [[ -r $MAKEPKG_CONF ]]; then + source_safe "$MAKEPKG_CONF" + else + error "$(gettext "%s not found.")" "$MAKEPKG_CONF" + plain "$(gettext "Aborting...")" + exit $E_CONFIG_ERROR + fi + + # Source user-specific makepkg.conf overrides, but only if no override config + # file was specified + XDG_PACMAN_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/pacman" + if [[ "$MAKEPKG_CONF" = "@sysconfdir@/makepkg.conf" ]]; then + if [[ -r "$XDG_PACMAN_DIR/makepkg.conf" ]]; then + source_safe "$XDG_PACMAN_DIR/makepkg.conf" + elif [[ -r "$HOME/.makepkg.conf" ]]; then + source_safe "$HOME/.makepkg.conf" + fi + fi +} diff --git a/scripts/libmakepkg/util/meson.build b/scripts/libmakepkg/util/meson.build index 229b004d..c29503b7 100644 --- a/scripts/libmakepkg/util/meson.build +++ b/scripts/libmakepkg/util/meson.build @@ -2,6 +2,7 @@ libmakepkg_module = 'util' sources = [ 'compress.sh.in', + 'config.sh.in', 'error.sh.in', 'message.sh.in', 'option.sh.in', diff --git a/scripts/libmakepkg/util/util.sh.in b/scripts/libmakepkg/util/util.sh.in index c2f5897e..8feb9178 100644 --- a/scripts/libmakepkg/util/util.sh.in +++ b/scripts/libmakepkg/util/util.sh.in @@ -22,6 +22,9 @@ [[ -n "$LIBMAKEPKG_UTIL_UTIL_SH" ]] && return LIBMAKEPKG_UTIL_UTIL_SH=1 +LIBRARY=${LIBRARY:-'@libmakepkgdir@'} + +source "$LIBRARY/util/error.sh" ## # usage : in_array( $needle, $haystack ) @@ -95,3 +98,16 @@ ensure_writable_dir() { return 0 } + +# source a file and fail if it does not succeed +source_safe() { + local shellopts=$(shopt -p extglob) + shopt -u extglob + + if ! source "$@"; then + error "$(gettext "Failed to source %s")" "$1" + exit $E_MISSING_FILE + fi + + eval "$shellopts" +} diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 28f3e3ef..08ec2a15 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -354,15 +354,6 @@ error_function() { exit $E_USER_FUNCTION_FAILED } -source_safe() { - shopt -u extglob - if ! source "$@"; then - error "$(gettext "Failed to source %s")" "$1" - exit $E_MISSING_FILE - fi - shopt -s extglob -} - merge_arch_attrs() { local attr supported_attrs=( provides conflicts depends replaces optdepends @@ -1113,25 +1104,7 @@ restore_envvars=$(declare -p PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT # default config is makepkg.conf MAKEPKG_CONF=${MAKEPKG_CONF:-$confdir/makepkg.conf} -# Source the config file; fail if it is not found -if [[ -r $MAKEPKG_CONF ]]; then - source_safe "$MAKEPKG_CONF" -else - error "$(gettext "%s not found.")" "$MAKEPKG_CONF" - plain "$(gettext "Aborting...")" - exit $E_CONFIG_ERROR -fi - -# Source user-specific makepkg.conf overrides, but only if no override config -# file was specified -XDG_PACMAN_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/pacman" -if [[ "$MAKEPKG_CONF" = "$confdir/makepkg.conf" ]]; then - if [[ -r "$XDG_PACMAN_DIR/makepkg.conf" ]]; then - source_safe "$XDG_PACMAN_DIR/makepkg.conf" - elif [[ -r "$HOME/.makepkg.conf" ]]; then - source_safe "$HOME/.makepkg.conf" - fi -fi +source_makepkg_config "$MAKEPKG_CONF" eval "$restore_envvars" -- 2.21.0
Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> --- scripts/libmakepkg/util/config.sh.in | 20 ++++++++++++++++++++ scripts/makepkg.sh.in | 6 +----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/scripts/libmakepkg/util/config.sh.in b/scripts/libmakepkg/util/config.sh.in index fdfeef89..2a6a0ae3 100644 --- a/scripts/libmakepkg/util/config.sh.in +++ b/scripts/libmakepkg/util/config.sh.in @@ -53,3 +53,23 @@ source_makepkg_config() { fi fi } + +# load makepkg.conf by sourcing the configuration files, and preserving +# existing environment settings +load_makepkg_config() { + # $1: override system config file + + local MAKEPKG_CONF=${1:-@sysconfdir@/makepkg.conf} + + # preserve environment variables to override makepkg.conf + local restore_envvars=$( + for var in PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT SRCEXT GPGKEY PACKAGER CARCH; do + # declare -p does not work inside a function due to being marked as local + [[ -v $var ]] && printf '%s=%s\n' "$var" "${var@Q}" + done + ) + + source_makepkg_config "$MAKEPKG_CONF" + + eval "$restore_envvars" +} diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 08ec2a15..7a65d0f2 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -1098,15 +1098,11 @@ done trap 'trap_exit INT "$(gettext "Aborted by user! Exiting...")"' INT trap 'trap_exit USR1 "$(gettext "An unknown error has occurred. Exiting...")"' ERR -# preserve environment variables to override makepkg.conf -restore_envvars=$(declare -p PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT SRCEXT GPGKEY PACKAGER CARCH 2>/dev/null || true) # default config is makepkg.conf MAKEPKG_CONF=${MAKEPKG_CONF:-$confdir/makepkg.conf} -source_makepkg_config "$MAKEPKG_CONF" - -eval "$restore_envvars" +load_makepkg_config "$MAKEPKG_CONF" # override settings from extra variables on commandline, if any if (( ${#extra_environment[*]} )); then -- 2.21.0
On Tue, Apr 16, 2019 at 12:22:38PM -0400, Eli Schwartz wrote:
Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> --- scripts/libmakepkg/util/config.sh.in | 20 ++++++++++++++++++++ scripts/makepkg.sh.in | 6 +----- 2 files changed, 21 insertions(+), 5 deletions(-)
diff --git a/scripts/libmakepkg/util/config.sh.in b/scripts/libmakepkg/util/config.sh.in index fdfeef89..2a6a0ae3 100644 --- a/scripts/libmakepkg/util/config.sh.in +++ b/scripts/libmakepkg/util/config.sh.in @@ -53,3 +53,23 @@ source_makepkg_config() { fi fi } + +# load makepkg.conf by sourcing the configuration files, and preserving +# existing environment settings +load_makepkg_config() { + # $1: override system config file + + local MAKEPKG_CONF=${1:-@sysconfdir@/makepkg.conf} + + # preserve environment variables to override makepkg.conf + local restore_envvars=$( + for var in PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT SRCEXT GPGKEY PACKAGER CARCH; do + # declare -p does not work inside a function due to being marked as local + [[ -v $var ]] && printf '%s=%s\n' "$var" "${var@Q}"
Shouldn't this be "${!var@Q}"?
+ done + ) + + source_makepkg_config "$MAKEPKG_CONF" + + eval "$restore_envvars" +} diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 08ec2a15..7a65d0f2 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -1098,15 +1098,11 @@ done trap 'trap_exit INT "$(gettext "Aborted by user! Exiting...")"' INT trap 'trap_exit USR1 "$(gettext "An unknown error has occurred. Exiting...")"' ERR
-# preserve environment variables to override makepkg.conf -restore_envvars=$(declare -p PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT SRCEXT GPGKEY PACKAGER CARCH 2>/dev/null || true)
# default config is makepkg.conf MAKEPKG_CONF=${MAKEPKG_CONF:-$confdir/makepkg.conf}
-source_makepkg_config "$MAKEPKG_CONF" - -eval "$restore_envvars" +load_makepkg_config "$MAKEPKG_CONF"
# override settings from extra variables on commandline, if any if (( ${#extra_environment[*]} )); then -- 2.21.0
On 4/16/19 12:59 PM, Dave Reisner wrote:
On Tue, Apr 16, 2019 at 12:22:38PM -0400, Eli Schwartz wrote:
Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> --- scripts/libmakepkg/util/config.sh.in | 20 ++++++++++++++++++++ scripts/makepkg.sh.in | 6 +----- 2 files changed, 21 insertions(+), 5 deletions(-)
diff --git a/scripts/libmakepkg/util/config.sh.in b/scripts/libmakepkg/util/config.sh.in index fdfeef89..2a6a0ae3 100644 --- a/scripts/libmakepkg/util/config.sh.in +++ b/scripts/libmakepkg/util/config.sh.in @@ -53,3 +53,23 @@ source_makepkg_config() { fi fi } + +# load makepkg.conf by sourcing the configuration files, and preserving +# existing environment settings +load_makepkg_config() { + # $1: override system config file + + local MAKEPKG_CONF=${1:-@sysconfdir@/makepkg.conf} + + # preserve environment variables to override makepkg.conf + local restore_envvars=$( + for var in PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT SRCEXT GPGKEY PACKAGER CARCH; do + # declare -p does not work inside a function due to being marked as local + [[ -v $var ]] && printf '%s=%s\n' "$var" "${var@Q}"
Shouldn't this be "${!var@Q}"?
Oops, will fix.
+ done + ) + + source_makepkg_config "$MAKEPKG_CONF" + + eval "$restore_envvars" +} diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 08ec2a15..7a65d0f2 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -1098,15 +1098,11 @@ done trap 'trap_exit INT "$(gettext "Aborted by user! Exiting...")"' INT trap 'trap_exit USR1 "$(gettext "An unknown error has occurred. Exiting...")"' ERR
-# preserve environment variables to override makepkg.conf -restore_envvars=$(declare -p PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT SRCEXT GPGKEY PACKAGER CARCH 2>/dev/null || true)
# default config is makepkg.conf MAKEPKG_CONF=${MAKEPKG_CONF:-$confdir/makepkg.conf}
-source_makepkg_config "$MAKEPKG_CONF" - -eval "$restore_envvars" +load_makepkg_config "$MAKEPKG_CONF"
# override settings from extra variables on commandline, if any if (( ${#extra_environment[*]} )); then -- 2.21.0
-- Eli Schwartz Bug Wrangler and Trusted User
Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> --- v2: fix a missing dependency, rebase on top of https://patchwork.archlinux.org/patch/1074/ because util.sh had a missing dependency scripts/Makefile.am | 1 + scripts/libmakepkg/util/config.sh.in | 56 ++++++++++++++++++++++++++++ scripts/libmakepkg/util/meson.build | 1 + scripts/libmakepkg/util/util.sh.in | 14 +++++++ scripts/makepkg.sh.in | 29 +------------- 5 files changed, 73 insertions(+), 28 deletions(-) create mode 100644 scripts/libmakepkg/util/config.sh.in diff --git a/scripts/Makefile.am b/scripts/Makefile.am index 2c743e82..4e5876af 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -119,6 +119,7 @@ LIBMAKEPKG_IN = \ libmakepkg/tidy/zipman.sh \ libmakepkg/util.sh \ libmakepkg/util/compress.sh \ + libmakepkg/util/config.sh \ libmakepkg/util/error.sh \ libmakepkg/util/message.sh \ libmakepkg/util/option.sh \ diff --git a/scripts/libmakepkg/util/config.sh.in b/scripts/libmakepkg/util/config.sh.in new file mode 100644 index 00000000..7ff6423b --- /dev/null +++ b/scripts/libmakepkg/util/config.sh.in @@ -0,0 +1,56 @@ +#!/bin/bash +# +# config.sh - functions for handling makepkg config files +# +# Copyright (c) 2006-2019 Pacman Development Team <pacman-dev@archlinux.org> +# Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +[[ -n "$LIBMAKEPKG_UTIL_CONFIG_SH" ]] && return +LIBMAKEPKG_UTIL_CONFIG_SH=1 + +LIBRARY=${LIBRARY:-'@libmakepkgdir@'} + +source "$LIBRARY/util/error.sh" +source "$LIBRARY/util/message.sh" +source "$LIBRARY/util/util.sh" + +# correctly source makepkg.conf, respecting user precedence and the system conf +source_makepkg_config() { + # $1: override system config file + + local MAKEPKG_CONF=${1:-@sysconfdir@/makepkg.conf} + + # Source the config file; fail if it is not found + if [[ -r $MAKEPKG_CONF ]]; then + source_safe "$MAKEPKG_CONF" + else + error "$(gettext "%s not found.")" "$MAKEPKG_CONF" + plain "$(gettext "Aborting...")" + exit $E_CONFIG_ERROR + fi + + # Source user-specific makepkg.conf overrides, but only if no override config + # file was specified + XDG_PACMAN_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/pacman" + if [[ $MAKEPKG_CONF = "@sysconfdir@/makepkg.conf" ]]; then + if [[ -r $XDG_PACMAN_DIR/makepkg.conf ]]; then + source_safe "$XDG_PACMAN_DIR/makepkg.conf" + elif [[ -r $HOME/.makepkg.conf ]]; then + source_safe "$HOME/.makepkg.conf" + fi + fi +} diff --git a/scripts/libmakepkg/util/meson.build b/scripts/libmakepkg/util/meson.build index 229b004d..c29503b7 100644 --- a/scripts/libmakepkg/util/meson.build +++ b/scripts/libmakepkg/util/meson.build @@ -2,6 +2,7 @@ libmakepkg_module = 'util' sources = [ 'compress.sh.in', + 'config.sh.in', 'error.sh.in', 'message.sh.in', 'option.sh.in', diff --git a/scripts/libmakepkg/util/util.sh.in b/scripts/libmakepkg/util/util.sh.in index 5ea3ed9b..b20384b8 100644 --- a/scripts/libmakepkg/util/util.sh.in +++ b/scripts/libmakepkg/util/util.sh.in @@ -24,6 +24,7 @@ LIBMAKEPKG_UTIL_UTIL_SH=1 LIBRARY=${LIBRARY:-'@libmakepkgdir@'} +source "$LIBRARY/util/error.sh" source "$LIBRARY/util/message.sh" ## @@ -98,3 +99,16 @@ ensure_writable_dir() { return 0 } + +# source a file and fail if it does not succeed +source_safe() { + local shellopts=$(shopt -p extglob) + shopt -u extglob + + if ! source "$@"; then + error "$(gettext "Failed to source %s")" "$1" + exit $E_MISSING_FILE + fi + + eval "$shellopts" +} diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 28f3e3ef..08ec2a15 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -354,15 +354,6 @@ error_function() { exit $E_USER_FUNCTION_FAILED } -source_safe() { - shopt -u extglob - if ! source "$@"; then - error "$(gettext "Failed to source %s")" "$1" - exit $E_MISSING_FILE - fi - shopt -s extglob -} - merge_arch_attrs() { local attr supported_attrs=( provides conflicts depends replaces optdepends @@ -1113,25 +1104,7 @@ restore_envvars=$(declare -p PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT # default config is makepkg.conf MAKEPKG_CONF=${MAKEPKG_CONF:-$confdir/makepkg.conf} -# Source the config file; fail if it is not found -if [[ -r $MAKEPKG_CONF ]]; then - source_safe "$MAKEPKG_CONF" -else - error "$(gettext "%s not found.")" "$MAKEPKG_CONF" - plain "$(gettext "Aborting...")" - exit $E_CONFIG_ERROR -fi - -# Source user-specific makepkg.conf overrides, but only if no override config -# file was specified -XDG_PACMAN_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/pacman" -if [[ "$MAKEPKG_CONF" = "$confdir/makepkg.conf" ]]; then - if [[ -r "$XDG_PACMAN_DIR/makepkg.conf" ]]; then - source_safe "$XDG_PACMAN_DIR/makepkg.conf" - elif [[ -r "$HOME/.makepkg.conf" ]]; then - source_safe "$HOME/.makepkg.conf" - fi -fi +source_makepkg_config "$MAKEPKG_CONF" eval "$restore_envvars" -- 2.21.0
Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> --- v2: fix typo pointed out by dreisner scripts/libmakepkg/util/config.sh.in | 20 ++++++++++++++++++++ scripts/makepkg.sh.in | 6 +----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/scripts/libmakepkg/util/config.sh.in b/scripts/libmakepkg/util/config.sh.in index 7ff6423b..f6745c78 100644 --- a/scripts/libmakepkg/util/config.sh.in +++ b/scripts/libmakepkg/util/config.sh.in @@ -54,3 +54,23 @@ source_makepkg_config() { fi fi } + +# load makepkg.conf by sourcing the configuration files, and preserving +# existing environment settings +load_makepkg_config() { + # $1: override system config file + + local MAKEPKG_CONF=${1:-@sysconfdir@/makepkg.conf} + + # preserve environment variables to override makepkg.conf + local restore_envvars=$( + for var in PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT SRCEXT GPGKEY PACKAGER CARCH; do + # declare -p does not work inside a function due to being marked as local + [[ -v $var ]] && printf '%s=%s\n' "$var" "${!var@Q}" + done + ) + + source_makepkg_config "$MAKEPKG_CONF" + + eval "$restore_envvars" +} diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 08ec2a15..7a65d0f2 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -1098,15 +1098,11 @@ done trap 'trap_exit INT "$(gettext "Aborted by user! Exiting...")"' INT trap 'trap_exit USR1 "$(gettext "An unknown error has occurred. Exiting...")"' ERR -# preserve environment variables to override makepkg.conf -restore_envvars=$(declare -p PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT SRCEXT GPGKEY PACKAGER CARCH 2>/dev/null || true) # default config is makepkg.conf MAKEPKG_CONF=${MAKEPKG_CONF:-$confdir/makepkg.conf} -source_makepkg_config "$MAKEPKG_CONF" - -eval "$restore_envvars" +load_makepkg_config "$MAKEPKG_CONF" # override settings from extra variables on commandline, if any if (( ${#extra_environment[*]} )); then -- 2.21.0
On 17/4/19 3:17 am, Eli Schwartz wrote:
Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> ---
Comments from v1 indicate this will be useful for (e.g.) devtools to read a makepkg.conf. But devtools uses a custom makepkg.conf plus info from a users ~/.makepkg.conf. This patch will allow reading the custom makepkg.conf, but as it is not the system makepkg.conf it would then not parse the ~/. one. So does this patch achieve its purpose?
v2: fix a missing dependency, rebase on top of https://patchwork.archlinux.org/patch/1074/ because util.sh had a missing dependency
<snip>
merge_arch_attrs() { local attr supported_attrs=( provides conflicts depends replaces optdepends @@ -1113,25 +1104,7 @@ restore_envvars=$(declare -p PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT # default config is makepkg.conf MAKEPKG_CONF=${MAKEPKG_CONF:-$confdir/makepkg.conf}
This is not needed any more. The logic is handled in the source_makepkg_conf() function. The MAKEPKG_CONF variable is not used elsewhere.
-# Source the config file; fail if it is not found -if [[ -r $MAKEPKG_CONF ]]; then - source_safe "$MAKEPKG_CONF" -else - error "$(gettext "%s not found.")" "$MAKEPKG_CONF" - plain "$(gettext "Aborting...")" - exit $E_CONFIG_ERROR -fi - -# Source user-specific makepkg.conf overrides, but only if no override config -# file was specified -XDG_PACMAN_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/pacman" -if [[ "$MAKEPKG_CONF" = "$confdir/makepkg.conf" ]]; then - if [[ -r "$XDG_PACMAN_DIR/makepkg.conf" ]]; then - source_safe "$XDG_PACMAN_DIR/makepkg.conf" - elif [[ -r "$HOME/.makepkg.conf" ]]; then - source_safe "$HOME/.makepkg.conf" - fi -fi +source_makepkg_config "$MAKEPKG_CONF"
eval "$restore_envvars"
On 17/4/19 3:17 am, Eli Schwartz wrote:
Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> ---
Comments from v1 indicate this will be useful for (e.g.) devtools to read a makepkg.conf. But devtools uses a custom makepkg.conf plus info from a users ~/.makepkg.conf. This patch will allow reading the custom makepkg.conf, but as it is not the system makepkg.conf it would then not parse the ~/. one. So does this patch achieve its purpose? , In devtools, there is one "pristine" makepkg.conf used for configuring
On 5/8/19 1:08 AM, Allan McRae wrote: the chroot, but we also want to extract certain variables from "whatever plain old makepkg would see". MAKEFLAGS and PACKAGER as well as the *DEST settings. As per makepkg, these could be set in /etc/makepkg.conf or in ~/. -- so the objective would be to use this function and load the default /etc/makepkg.conf + user-specific ones, and then eventually echo "MAKEFLAGS='$MAKEFLAGS'" >> $chrootdir/etc/makepkg.conf It would also be useful in another script, to obtain the $PKGDEST -- the script will run makechrootpkg on a remote build server, then scp the files back to the local machine, and it would be nice to scp it into the same directory makepkg would have used if run locally. I expect there are other programs out there which also desire to do high-level things around makepkg, while still using makepkg's configuration directories. -- Eli Schwartz Bug Wrangler and Trusted User
Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> --- v2: remove no-longer-needed MAKEPKG_CONF handling in makepkg.sh.in scripts/Makefile.am | 1 + scripts/libmakepkg/util/config.sh.in | 56 ++++++++++++++++++++++++++++ scripts/libmakepkg/util/meson.build | 1 + scripts/libmakepkg/util/util.sh.in | 14 +++++++ scripts/makepkg.sh.in | 32 +--------------- 5 files changed, 73 insertions(+), 31 deletions(-) create mode 100644 scripts/libmakepkg/util/config.sh.in diff --git a/scripts/Makefile.am b/scripts/Makefile.am index f9e7bd32..00e0c038 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -123,6 +123,7 @@ LIBMAKEPKG_IN = \ libmakepkg/tidy/zipman.sh \ libmakepkg/util.sh \ libmakepkg/util/compress.sh \ + libmakepkg/util/config.sh \ libmakepkg/util/error.sh \ libmakepkg/util/message.sh \ libmakepkg/util/option.sh \ diff --git a/scripts/libmakepkg/util/config.sh.in b/scripts/libmakepkg/util/config.sh.in new file mode 100644 index 00000000..69d36f21 --- /dev/null +++ b/scripts/libmakepkg/util/config.sh.in @@ -0,0 +1,56 @@ +#!/bin/bash +# +# config.sh - functions for handling makepkg config files +# +# Copyright (c) 2006-2019 Pacman Development Team <pacman-dev@archlinux.org> +# Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +[[ -n "$LIBMAKEPKG_UTIL_CONFIG_SH" ]] && return +LIBMAKEPKG_UTIL_CONFIG_SH=1 + +LIBRARY=${LIBRARY:-'@libmakepkgdir@'} + +source "$LIBRARY/util/error.sh" +source "$LIBRARY/util/message.sh" +source "$LIBRARY/util/util.sh" + +# correctly source makepkg.conf, respecting user precedence and the system conf +source_makepkg_config() { + # $1: override system config file + + local MAKEPKG_CONF=${MAKEPKG_CONF:-${1:-@sysconfdir@/makepkg.conf}} + + # Source the config file; fail if it is not found + if [[ -r $MAKEPKG_CONF ]]; then + source_safe "$MAKEPKG_CONF" + else + error "$(gettext "%s not found.")" "$MAKEPKG_CONF" + plain "$(gettext "Aborting...")" + exit $E_CONFIG_ERROR + fi + + # Source user-specific makepkg.conf overrides, but only if no override config + # file was specified + XDG_PACMAN_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/pacman" + if [[ $MAKEPKG_CONF = "@sysconfdir@/makepkg.conf" ]]; then + if [[ -r $XDG_PACMAN_DIR/makepkg.conf ]]; then + source_safe "$XDG_PACMAN_DIR/makepkg.conf" + elif [[ -r $HOME/.makepkg.conf ]]; then + source_safe "$HOME/.makepkg.conf" + fi + fi +} diff --git a/scripts/libmakepkg/util/meson.build b/scripts/libmakepkg/util/meson.build index 229b004d..c29503b7 100644 --- a/scripts/libmakepkg/util/meson.build +++ b/scripts/libmakepkg/util/meson.build @@ -2,6 +2,7 @@ libmakepkg_module = 'util' sources = [ 'compress.sh.in', + 'config.sh.in', 'error.sh.in', 'message.sh.in', 'option.sh.in', diff --git a/scripts/libmakepkg/util/util.sh.in b/scripts/libmakepkg/util/util.sh.in index 5ea3ed9b..b20384b8 100644 --- a/scripts/libmakepkg/util/util.sh.in +++ b/scripts/libmakepkg/util/util.sh.in @@ -24,6 +24,7 @@ LIBMAKEPKG_UTIL_UTIL_SH=1 LIBRARY=${LIBRARY:-'@libmakepkgdir@'} +source "$LIBRARY/util/error.sh" source "$LIBRARY/util/message.sh" ## @@ -98,3 +99,16 @@ ensure_writable_dir() { return 0 } + +# source a file and fail if it does not succeed +source_safe() { + local shellopts=$(shopt -p extglob) + shopt -u extglob + + if ! source "$@"; then + error "$(gettext "Failed to source %s")" "$1" + exit $E_MISSING_FILE + fi + + eval "$shellopts" +} diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 3cd025d6..b7f4cb40 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -354,15 +354,6 @@ error_function() { exit $E_USER_FUNCTION_FAILED } -source_safe() { - shopt -u extglob - if ! source "$@"; then - error "$(gettext "Failed to source %s")" "$1" - exit $E_MISSING_FILE - fi - shopt -s extglob -} - merge_arch_attrs() { local attr supported_attrs=( provides conflicts depends replaces optdepends @@ -1110,28 +1101,7 @@ trap 'trap_exit USR1 "$(gettext "An unknown error has occurred. Exiting...")"' E # preserve environment variables to override makepkg.conf restore_envvars=$(declare -p PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT SRCEXT GPGKEY PACKAGER CARCH 2>/dev/null || true) -# default config is makepkg.conf -MAKEPKG_CONF=${MAKEPKG_CONF:-$confdir/makepkg.conf} - -# Source the config file; fail if it is not found -if [[ -r $MAKEPKG_CONF ]]; then - source_safe "$MAKEPKG_CONF" -else - error "$(gettext "%s not found.")" "$MAKEPKG_CONF" - plain "$(gettext "Aborting...")" - exit $E_CONFIG_ERROR -fi - -# Source user-specific makepkg.conf overrides, but only if no override config -# file was specified -XDG_PACMAN_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/pacman" -if [[ "$MAKEPKG_CONF" = "$confdir/makepkg.conf" ]]; then - if [[ -r "$XDG_PACMAN_DIR/makepkg.conf" ]]; then - source_safe "$XDG_PACMAN_DIR/makepkg.conf" - elif [[ -r "$HOME/.makepkg.conf" ]]; then - source_safe "$HOME/.makepkg.conf" - fi -fi +source_makepkg_config eval "$restore_envvars" -- 2.21.0
Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> --- scripts/libmakepkg/util/config.sh.in | 20 ++++++++++++++++++++ scripts/makepkg.sh.in | 7 +------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/scripts/libmakepkg/util/config.sh.in b/scripts/libmakepkg/util/config.sh.in index 69d36f21..049040b8 100644 --- a/scripts/libmakepkg/util/config.sh.in +++ b/scripts/libmakepkg/util/config.sh.in @@ -54,3 +54,23 @@ source_makepkg_config() { fi fi } + +# load makepkg.conf by sourcing the configuration files, and preserving +# existing environment settings +load_makepkg_config() { + # $1: override system config file + + local MAKEPKG_CONF=${MAKEPKG_CONF:-${1:-@sysconfdir@/makepkg.conf}} + + # preserve environment variables to override makepkg.conf + local restore_envvars=$( + for var in PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT SRCEXT GPGKEY PACKAGER CARCH; do + # declare -p does not work inside a function due to being marked as local + [[ -v $var ]] && printf '%s=%s\n' "$var" "${!var@Q}" + done + ) + + source_makepkg_config "$MAKEPKG_CONF" + + eval "$restore_envvars" +} diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index b7f4cb40..f2dbd092 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -1098,12 +1098,7 @@ done trap 'trap_exit INT "$(gettext "Aborted by user! Exiting...")"' INT trap 'trap_exit USR1 "$(gettext "An unknown error has occurred. Exiting...")"' ERR -# preserve environment variables to override makepkg.conf -restore_envvars=$(declare -p PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT SRCEXT GPGKEY PACKAGER CARCH 2>/dev/null || true) - -source_makepkg_config - -eval "$restore_envvars" +load_makepkg_config # override settings from extra variables on commandline, if any if (( ${#extra_environment[*]} )); then -- 2.21.0
When scripting/automating around makepkg, it is sometimes desirable to know how makepkg will be configured to operate. One example is the archlinux devtools, which must forward select makepkg.conf variables into a build chroot (for example PACKAGER) or use those variables itself (for example {SRC,PKG,LOG}DEST). The configuration file can be in up to 3 places, and should be capable of being overridden via environment variables. It is sufficiently complex to represent distinct functionality, and sufficiently useful to merit easy accessibility in other scripts, therefore, let us move it into a publicly exposed utility library. Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> --- v4: expand the commit message a bit. scripts/Makefile.am | 1 + scripts/libmakepkg/util/config.sh.in | 56 ++++++++++++++++++++++++++++ scripts/libmakepkg/util/meson.build | 1 + scripts/libmakepkg/util/util.sh.in | 14 +++++++ scripts/makepkg.sh.in | 32 +--------------- 5 files changed, 73 insertions(+), 31 deletions(-) create mode 100644 scripts/libmakepkg/util/config.sh.in diff --git a/scripts/Makefile.am b/scripts/Makefile.am index f9e7bd32..00e0c038 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -123,6 +123,7 @@ LIBMAKEPKG_IN = \ libmakepkg/tidy/zipman.sh \ libmakepkg/util.sh \ libmakepkg/util/compress.sh \ + libmakepkg/util/config.sh \ libmakepkg/util/error.sh \ libmakepkg/util/message.sh \ libmakepkg/util/option.sh \ diff --git a/scripts/libmakepkg/util/config.sh.in b/scripts/libmakepkg/util/config.sh.in new file mode 100644 index 00000000..69d36f21 --- /dev/null +++ b/scripts/libmakepkg/util/config.sh.in @@ -0,0 +1,56 @@ +#!/bin/bash +# +# config.sh - functions for handling makepkg config files +# +# Copyright (c) 2006-2019 Pacman Development Team <pacman-dev@archlinux.org> +# Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +[[ -n "$LIBMAKEPKG_UTIL_CONFIG_SH" ]] && return +LIBMAKEPKG_UTIL_CONFIG_SH=1 + +LIBRARY=${LIBRARY:-'@libmakepkgdir@'} + +source "$LIBRARY/util/error.sh" +source "$LIBRARY/util/message.sh" +source "$LIBRARY/util/util.sh" + +# correctly source makepkg.conf, respecting user precedence and the system conf +source_makepkg_config() { + # $1: override system config file + + local MAKEPKG_CONF=${MAKEPKG_CONF:-${1:-@sysconfdir@/makepkg.conf}} + + # Source the config file; fail if it is not found + if [[ -r $MAKEPKG_CONF ]]; then + source_safe "$MAKEPKG_CONF" + else + error "$(gettext "%s not found.")" "$MAKEPKG_CONF" + plain "$(gettext "Aborting...")" + exit $E_CONFIG_ERROR + fi + + # Source user-specific makepkg.conf overrides, but only if no override config + # file was specified + XDG_PACMAN_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/pacman" + if [[ $MAKEPKG_CONF = "@sysconfdir@/makepkg.conf" ]]; then + if [[ -r $XDG_PACMAN_DIR/makepkg.conf ]]; then + source_safe "$XDG_PACMAN_DIR/makepkg.conf" + elif [[ -r $HOME/.makepkg.conf ]]; then + source_safe "$HOME/.makepkg.conf" + fi + fi +} diff --git a/scripts/libmakepkg/util/meson.build b/scripts/libmakepkg/util/meson.build index 229b004d..c29503b7 100644 --- a/scripts/libmakepkg/util/meson.build +++ b/scripts/libmakepkg/util/meson.build @@ -2,6 +2,7 @@ libmakepkg_module = 'util' sources = [ 'compress.sh.in', + 'config.sh.in', 'error.sh.in', 'message.sh.in', 'option.sh.in', diff --git a/scripts/libmakepkg/util/util.sh.in b/scripts/libmakepkg/util/util.sh.in index 5ea3ed9b..b20384b8 100644 --- a/scripts/libmakepkg/util/util.sh.in +++ b/scripts/libmakepkg/util/util.sh.in @@ -24,6 +24,7 @@ LIBMAKEPKG_UTIL_UTIL_SH=1 LIBRARY=${LIBRARY:-'@libmakepkgdir@'} +source "$LIBRARY/util/error.sh" source "$LIBRARY/util/message.sh" ## @@ -98,3 +99,16 @@ ensure_writable_dir() { return 0 } + +# source a file and fail if it does not succeed +source_safe() { + local shellopts=$(shopt -p extglob) + shopt -u extglob + + if ! source "$@"; then + error "$(gettext "Failed to source %s")" "$1" + exit $E_MISSING_FILE + fi + + eval "$shellopts" +} diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 3cd025d6..b7f4cb40 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -354,15 +354,6 @@ error_function() { exit $E_USER_FUNCTION_FAILED } -source_safe() { - shopt -u extglob - if ! source "$@"; then - error "$(gettext "Failed to source %s")" "$1" - exit $E_MISSING_FILE - fi - shopt -s extglob -} - merge_arch_attrs() { local attr supported_attrs=( provides conflicts depends replaces optdepends @@ -1110,28 +1101,7 @@ trap 'trap_exit USR1 "$(gettext "An unknown error has occurred. Exiting...")"' E # preserve environment variables to override makepkg.conf restore_envvars=$(declare -p PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT SRCEXT GPGKEY PACKAGER CARCH 2>/dev/null || true) -# default config is makepkg.conf -MAKEPKG_CONF=${MAKEPKG_CONF:-$confdir/makepkg.conf} - -# Source the config file; fail if it is not found -if [[ -r $MAKEPKG_CONF ]]; then - source_safe "$MAKEPKG_CONF" -else - error "$(gettext "%s not found.")" "$MAKEPKG_CONF" - plain "$(gettext "Aborting...")" - exit $E_CONFIG_ERROR -fi - -# Source user-specific makepkg.conf overrides, but only if no override config -# file was specified -XDG_PACMAN_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/pacman" -if [[ "$MAKEPKG_CONF" = "$confdir/makepkg.conf" ]]; then - if [[ -r "$XDG_PACMAN_DIR/makepkg.conf" ]]; then - source_safe "$XDG_PACMAN_DIR/makepkg.conf" - elif [[ -r "$HOME/.makepkg.conf" ]]; then - source_safe "$HOME/.makepkg.conf" - fi -fi +source_makepkg_config eval "$restore_envvars" -- 2.21.0
Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> --- scripts/libmakepkg/util/config.sh.in | 20 ++++++++++++++++++++ scripts/makepkg.sh.in | 7 +------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/scripts/libmakepkg/util/config.sh.in b/scripts/libmakepkg/util/config.sh.in index 69d36f21..049040b8 100644 --- a/scripts/libmakepkg/util/config.sh.in +++ b/scripts/libmakepkg/util/config.sh.in @@ -54,3 +54,23 @@ source_makepkg_config() { fi fi } + +# load makepkg.conf by sourcing the configuration files, and preserving +# existing environment settings +load_makepkg_config() { + # $1: override system config file + + local MAKEPKG_CONF=${MAKEPKG_CONF:-${1:-@sysconfdir@/makepkg.conf}} + + # preserve environment variables to override makepkg.conf + local restore_envvars=$( + for var in PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT SRCEXT GPGKEY PACKAGER CARCH; do + # declare -p does not work inside a function due to being marked as local + [[ -v $var ]] && printf '%s=%s\n' "$var" "${!var@Q}" + done + ) + + source_makepkg_config "$MAKEPKG_CONF" + + eval "$restore_envvars" +} diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index b7f4cb40..f2dbd092 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -1098,12 +1098,7 @@ done trap 'trap_exit INT "$(gettext "Aborted by user! Exiting...")"' INT trap 'trap_exit USR1 "$(gettext "An unknown error has occurred. Exiting...")"' ERR -# preserve environment variables to override makepkg.conf -restore_envvars=$(declare -p PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT SRCEXT GPGKEY PACKAGER CARCH 2>/dev/null || true) - -source_makepkg_config - -eval "$restore_envvars" +load_makepkg_config # override settings from extra variables on commandline, if any if (( ${#extra_environment[*]} )); then -- 2.21.0
When scripting/automating around makepkg, it is sometimes desirable to know how makepkg will be configured to operate. One example is the archlinux devtools, which must forward select makepkg.conf variables into a build chroot (for example PACKAGER) or use those variables itself (for example {SRC,PKG,LOG}DEST). The configuration file can be in up to 3 places, and should be capable of being overridden via environment variables. It is sufficiently complex to represent distinct functionality, and sufficiently useful to merit easy accessibility in other scripts, therefore, let us move it into a publicly exposed utility library. Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> --- v5: prioritize $1 over $MAKEPKG_CONF in the public API. scripts/Makefile.am | 1 + scripts/libmakepkg/util/config.sh.in | 56 ++++++++++++++++++++++++++++ scripts/libmakepkg/util/meson.build | 1 + scripts/libmakepkg/util/util.sh.in | 14 +++++++ scripts/makepkg.sh.in | 32 +--------------- 5 files changed, 73 insertions(+), 31 deletions(-) create mode 100644 scripts/libmakepkg/util/config.sh.in diff --git a/scripts/Makefile.am b/scripts/Makefile.am index f9e7bd32..00e0c038 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -123,6 +123,7 @@ LIBMAKEPKG_IN = \ libmakepkg/tidy/zipman.sh \ libmakepkg/util.sh \ libmakepkg/util/compress.sh \ + libmakepkg/util/config.sh \ libmakepkg/util/error.sh \ libmakepkg/util/message.sh \ libmakepkg/util/option.sh \ diff --git a/scripts/libmakepkg/util/config.sh.in b/scripts/libmakepkg/util/config.sh.in new file mode 100644 index 00000000..9201bc01 --- /dev/null +++ b/scripts/libmakepkg/util/config.sh.in @@ -0,0 +1,56 @@ +#!/bin/bash +# +# config.sh - functions for handling makepkg config files +# +# Copyright (c) 2006-2019 Pacman Development Team <pacman-dev@archlinux.org> +# Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +[[ -n "$LIBMAKEPKG_UTIL_CONFIG_SH" ]] && return +LIBMAKEPKG_UTIL_CONFIG_SH=1 + +LIBRARY=${LIBRARY:-'@libmakepkgdir@'} + +source "$LIBRARY/util/error.sh" +source "$LIBRARY/util/message.sh" +source "$LIBRARY/util/util.sh" + +# correctly source makepkg.conf, respecting user precedence and the system conf +source_makepkg_config() { + # $1: override system config file + + local MAKEPKG_CONF=${1:-${MAKEPKG_CONF:-@sysconfdir@/makepkg.conf}} + + # Source the config file; fail if it is not found + if [[ -r $MAKEPKG_CONF ]]; then + source_safe "$MAKEPKG_CONF" + else + error "$(gettext "%s not found.")" "$MAKEPKG_CONF" + plain "$(gettext "Aborting...")" + exit $E_CONFIG_ERROR + fi + + # Source user-specific makepkg.conf overrides, but only if no override config + # file was specified + XDG_PACMAN_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/pacman" + if [[ $MAKEPKG_CONF = "@sysconfdir@/makepkg.conf" ]]; then + if [[ -r $XDG_PACMAN_DIR/makepkg.conf ]]; then + source_safe "$XDG_PACMAN_DIR/makepkg.conf" + elif [[ -r $HOME/.makepkg.conf ]]; then + source_safe "$HOME/.makepkg.conf" + fi + fi +} diff --git a/scripts/libmakepkg/util/meson.build b/scripts/libmakepkg/util/meson.build index 229b004d..c29503b7 100644 --- a/scripts/libmakepkg/util/meson.build +++ b/scripts/libmakepkg/util/meson.build @@ -2,6 +2,7 @@ libmakepkg_module = 'util' sources = [ 'compress.sh.in', + 'config.sh.in', 'error.sh.in', 'message.sh.in', 'option.sh.in', diff --git a/scripts/libmakepkg/util/util.sh.in b/scripts/libmakepkg/util/util.sh.in index 5ea3ed9b..b20384b8 100644 --- a/scripts/libmakepkg/util/util.sh.in +++ b/scripts/libmakepkg/util/util.sh.in @@ -24,6 +24,7 @@ LIBMAKEPKG_UTIL_UTIL_SH=1 LIBRARY=${LIBRARY:-'@libmakepkgdir@'} +source "$LIBRARY/util/error.sh" source "$LIBRARY/util/message.sh" ## @@ -98,3 +99,16 @@ ensure_writable_dir() { return 0 } + +# source a file and fail if it does not succeed +source_safe() { + local shellopts=$(shopt -p extglob) + shopt -u extglob + + if ! source "$@"; then + error "$(gettext "Failed to source %s")" "$1" + exit $E_MISSING_FILE + fi + + eval "$shellopts" +} diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 832f45cd..59a738a0 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -354,15 +354,6 @@ error_function() { exit $E_USER_FUNCTION_FAILED } -source_safe() { - shopt -u extglob - if ! source "$@"; then - error "$(gettext "Failed to source %s")" "$1" - exit $E_MISSING_FILE - fi - shopt -s extglob -} - merge_arch_attrs() { local attr supported_attrs=( provides conflicts depends replaces optdepends @@ -1110,28 +1101,7 @@ trap 'trap_exit USR1 "$(gettext "An unknown error has occurred. Exiting...")"' E # preserve environment variables to override makepkg.conf restore_envvars=$(declare -p PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT SRCEXT GPGKEY PACKAGER CARCH 2>/dev/null || true) -# default config is makepkg.conf -MAKEPKG_CONF=${MAKEPKG_CONF:-$confdir/makepkg.conf} - -# Source the config file; fail if it is not found -if [[ -r $MAKEPKG_CONF ]]; then - source_safe "$MAKEPKG_CONF" -else - error "$(gettext "%s not found.")" "$MAKEPKG_CONF" - plain "$(gettext "Aborting...")" - exit $E_CONFIG_ERROR -fi - -# Source user-specific makepkg.conf overrides, but only if no override config -# file was specified -XDG_PACMAN_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/pacman" -if [[ "$MAKEPKG_CONF" = "$confdir/makepkg.conf" ]]; then - if [[ -r "$XDG_PACMAN_DIR/makepkg.conf" ]]; then - source_safe "$XDG_PACMAN_DIR/makepkg.conf" - elif [[ -r "$HOME/.makepkg.conf" ]]; then - source_safe "$HOME/.makepkg.conf" - fi -fi +source_makepkg_config eval "$restore_envvars" -- 2.21.0
Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> --- v5: prioritize $1 over $MAKEPKG_CONF in the public API. scripts/libmakepkg/util/config.sh.in | 20 ++++++++++++++++++++ scripts/makepkg.sh.in | 7 +------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/scripts/libmakepkg/util/config.sh.in b/scripts/libmakepkg/util/config.sh.in index 9201bc01..4c7d39b7 100644 --- a/scripts/libmakepkg/util/config.sh.in +++ b/scripts/libmakepkg/util/config.sh.in @@ -54,3 +54,23 @@ source_makepkg_config() { fi fi } + +# load makepkg.conf by sourcing the configuration files, and preserving +# existing environment settings +load_makepkg_config() { + # $1: override system config file + + local MAKEPKG_CONF=${1:-${MAKEPKG_CONF:-@sysconfdir@/makepkg.conf}} + + # preserve environment variables to override makepkg.conf + local restore_envvars=$( + for var in PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT SRCEXT GPGKEY PACKAGER CARCH; do + # declare -p does not work inside a function due to being marked as local + [[ -v $var ]] && printf '%s=%s\n' "$var" "${!var@Q}" + done + ) + + source_makepkg_config "$MAKEPKG_CONF" + + eval "$restore_envvars" +} diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 59a738a0..dcd6b4b3 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -1098,12 +1098,7 @@ done trap 'trap_exit INT "$(gettext "Aborted by user! Exiting...")"' INT trap 'trap_exit USR1 "$(gettext "An unknown error has occurred. Exiting...")"' ERR -# preserve environment variables to override makepkg.conf -restore_envvars=$(declare -p PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT SRCEXT GPGKEY PACKAGER CARCH 2>/dev/null || true) - -source_makepkg_config - -eval "$restore_envvars" +load_makepkg_config # override settings from extra variables on commandline, if any if (( ${#extra_environment[*]} )); then -- 2.21.0
On 23/5/19 12:44 am, Eli Schwartz wrote:
When scripting/automating around makepkg, it is sometimes desirable to know how makepkg will be configured to operate. One example is the archlinux devtools, which must forward select makepkg.conf variables into a build chroot (for example PACKAGER) or use those variables itself (for example {SRC,PKG,LOG}DEST).
The configuration file can be in up to 3 places, and should be capable of being overridden via environment variables. It is sufficiently complex to represent distinct functionality, and sufficiently useful to merit easy accessibility in other scripts, therefore, let us move it into a publicly exposed utility library.
Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> ---
Good. A
participants (3)
-
Allan McRae
-
Dave Reisner
-
Eli Schwartz