[PATCH aiaiai 1/2] aiaiai: add checks to configuration file

Jacob Keller jacob.e.keller at intel.com
Mon Apr 7 16:27:18 PDT 2014


This patch adds configuration check functions and performs some basic
checks against the configuration file. A future effort may be to clean
up these checks so that it outputs a bit more error cases.

The main value of these checks is to prevent weird errors later when we
assume that values have some standard meaning. In addition, it allows
cleaning up boolean values to always be canonicalized into 0 or 1.

Signed-off-by: Jacob Keller <jacob.e.keller at intel.com>
---
 email/aiaiai-email-sh-functions | 72 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)

diff --git a/email/aiaiai-email-sh-functions b/email/aiaiai-email-sh-functions
index 1d23decfb89b..ad3ee1d299f9 100644
--- a/email/aiaiai-email-sh-functions
+++ b/email/aiaiai-email-sh-functions
@@ -142,6 +142,52 @@ get_cfgfile_projects_list()
 	LC_ALL=C sed -n -e "s/^\[prj_\(.*\)\]$/\1/p" "$cfgfile"
 }
 
+# Like opt_check_number, except doesn't fail on empty value
+# Arguments: $1 is config name, $2 is config value.
+# If $2 is a positive decimal number (or empty), outputs it, otherwise fails.
+config_check_number()
+{
+	[ -n "${2##0*}" -a -n "${2##*[!0-9]*}" ] ||
+		fatal "$1: $2: invalid number."
+	printf %s "$2"
+}
+
+# Like opt_check_file, except requires executable permissions
+# Arguments: $1 is config name, $2 is config value.
+# If $2 is an executable file, outputs canonicalized file name,
+# otherwise fails.
+config_check_exec()
+{
+	local value
+	[ -x "$2" ] &&
+	value="$(readlink -ev "$2")" ||
+		fatal "$1: $2: not an executable file."
+	printf %s "$value"
+}
+
+# Check whether config value is boolean. Empty string is considered false.
+# Arguments: $1 is config name, $2 is config value.
+# If $2 is a boolean value, output canonicalized value,
+# otherwise, fails.
+config_check_boolean()
+{
+	local value
+	value="$(echo "$1" | tr '[:upper:]' '[:lower:]')"
+
+	# Prefixing with _ makes it easier to catch empty string as false
+	case "_$value" in
+	_|_0|_false|no)
+		printf %s "0"
+		;;
+	_1|_true|yes)
+		printf %s "1"
+		;;
+	*)
+		fatal "$1: $2: not a boolean value."
+		;;
+	esac
+}
+
 # Parse the "global" section of the config file. The result is a set of
 # per-option variables and their values are exactly as in the configuration
 # file:
@@ -166,18 +212,24 @@ parse_config()
 	ini_config_get_or_die cfg_adminmail      "$cfgfile" "global" "adminmail"
 	ini_config_get_or_die cfg_adminname      "$cfgfile" "global" "adminname"
 	ini_config_get_or_die cfg_workdir        "$cfgfile" "global" "workdir"
+	cfg_workdir="$(opt_check_dir "workdir" "$cfg_workdir")"
 	ini_config_get_or_die cfg_max_validators "$cfgfile" "global" "max_validators"
+	cfg_max_validators="$(config_check_number "max_validators" "$cfg_max_validators")"
 	ini_config_get_or_die cfg_jobs           "$cfgfile" "global" "jobs"
+	cfg_jobs="$(config_check_number "jobs" "$cfg_jobs")"
 	ini_config_get_or_die cfg_preamble       "$cfgfile" "global" "preamble"
 	ini_config_get_or_die cfg_signature      "$cfgfile" "global" "signature"
 	ini_config_get_or_die cfg_built_preamble "$cfgfile" "global" "built_preamble"
 
 	# Get the location of email hook(s)
 	cfg_email_hook="$(ini_config_get "$cfgfile" "hooks" "email")"
+	cfg_email_hook="$(config_check_exec "email" "$cfg_email_hook")"
 
 	# Debug options
 	cfg_disable_notifications="$(ini_config_get "$cfgfile" "debug" "disable_notifications")"
+	cfg_disable_notifications="$(config_check_boolean "disable_notifications" "$cfg_disable_notifications")"
 	cfg_preserve_files="$(ini_config_get "$cfgfile" "debug" "preserve_files")"
+	cfg_preserve_files="$(config_check_boolean "preserve_files" "$cfg_preserve_files")"
 
 	# Get the contents of the preamble file
 	cfg_preamble="$(cat "$cfg_preamble")"
@@ -262,30 +314,50 @@ parse_prj_config()
 	# project config does not specify anything.
 	pcfg_configs="$(ini_config_get "$cfgfile" "prj_$prj" "configs")"
 	ini_config_is_set "$cfgfile" "prj_$prj" "configs" || pcfg_configs="$__dcfg_configs"
+
 	pcfg_reply_to_all="$(ini_config_get "$cfgfile" "prj_$prj" "reply_to_all")"
 	ini_config_is_set "$cfgfile" "prj_$prj" "reply_to_all" || pcfg_reply_to_all="$__dcfg_reply_to_all"
+	pcfg_reply_to_all="$(config_check_boolean "reply_to_all" "$pcfg_reply_to_all")"
+
 	pcfg_accept_notify="$(ini_config_get "$cfgfile" "prj_$prj" "accept_notify")"
 	ini_config_is_set "$cfgfile" "prj_$prj" "accept_notify" || pcfg_accept_notify="$__dcfg_accept_notify"
+	pcfg_accept_notify="$(config_check_boolean "accept_notify" "$pcfg_accept_notify")"
+
 	pcfg_always_cc="$(ini_config_get "$cfgfile" "prj_$prj" "always_cc")"
 	ini_config_is_set "$cfgfile" "prj_$prj" "always_cc" || pcfg_always_cc="$__dcfg_always_cc"
+
 	pcfg_unwanted_keywords="$(ini_config_get "$cfgfile" "prj_$prj" "unwanted_keywords")"
 	ini_config_is_set "$cfgfile" "prj_$prj" "unwanted_keywords" || pcfg_unwanted_keywords="$__dcfg_unwanted_keywords"
+
 	pcfg_kmake_opts="$(ini_config_get "$cfgfile" "prj_$prj" "kmake_opts")"
 	ini_config_is_set "$cfgfile" "prj_$prj" "kmake_opts" || pcfg_kmake_opts="$__dcfg_kmake_opts"
+
 	pcfg_targets="$(ini_config_get "$cfgfile" "prj_$prj" "targets")"
 	ini_config_is_set "$cfgfile" "prj_$prj" "targets" || pcfg_targets="$__dcfg_targets"
+
 	pcfg_defconfigdir="$(ini_config_get "$cfgfile" "prj_$prj" "defconfigdir")"
 	ini_config_is_set "$cfgfile" "prj_$prj" "defconfigdir" || pcfg_defconfigdir="$__dcfg_defconfigdir"
+	[ -z "$pcfg_defconfigdir" ] || pcfg_defconfigdir="$(opt_check_dir "defconfigdir" "$pcfg_defconfigdir")"
+
 	pcfg_bisectability="$(ini_config_get "$cfgfile" "prj_$prj" "bisectability")"
 	ini_config_is_set "$cfgfile" "prj_$prj" "bisectability" || pcfg_bisectability="$__dcfg_bisectability"
+	pcfg_bisectability="$(config_check_boolean "bisectability" "$pcfg_bisectability")"
+
 	pcfg_sparse="$(ini_config_get "$cfgfile" "prj_$prj" "sparse")"
 	ini_config_is_set "$cfgfile" "prj_$prj" "sparse" || pcfg_sparse="$__dcfg_sparse"
+	pcfg_sparse="$(config_check_boolean "sparse" "$pcfg_sparse")"
+
 	pcfg_smatch="$(ini_config_get "$cfgfile" "prj_$prj" "smatch")"
 	ini_config_is_set "$cfgfile" "prj_$prj" "smatch" || pcfg_smatch="$__dcfg_smatch"
+	pcfg_smatch="$(config_check_boolean "smatch" "$pcfg_smatch")"
+
 	pcfg_cppcheck="$(ini_config_get "$cfgfile" "prj_$prj" "cppcheck")"
 	ini_config_is_set "$cfgfile" "prj_$prj" "cppcheck" || pcfg_cppcheck="$__dcfg_cppcheck"
+	pcfg_cppcheck="$(config_check_boolean "cppcheck" "$pcfg_cppcheck")"
+
 	pcfg_coccinelle="$(ini_config_get "$cfgfile" "prj_$prj" "coccinelle")"
 	ini_config_is_set "$cfgfile" "prj_$prj" "coccinelle" || pcfg_coccinelle="$__dcfg_coccinelle"
+	pcfg_coccinelle="$(config_check_boolean "coccinelle" "$pcfg_coccinelle")"
 }
 
 # Compose (but not send) e-mail reply. This function assumes that the following
-- 
1.8.3.1




More information about the aiaiai mailing list