[pacman-dev] [PATCH v2 1/2] makepkg: move config loading into libmakepkg

Eli Schwartz eschwartz at archlinux.org
Tue Apr 16 17:17:03 UTC 2019


Signed-off-by: Eli Schwartz <eschwartz at 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 at archlinux.org>
+#   Copyright (c) 2002-2006 by Judd Vinet <jvinet at 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:- at 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


More information about the pacman-dev mailing list