[X2Go-Commits] [buildscripts] 04/04: bin/build-rpm-package: implement cache_mock_config () and use it in generate_mock_config ().

git-admin at x2go.org git-admin at x2go.org
Sat Oct 29 01:16:41 CEST 2016


This is an automated email from the git hooks/post-receive script.

x2go pushed a commit to branch master
in repository buildscripts.

commit 92b0df7405fdbe05691c14ea52241b768b85be8b
Author: Mihai Moldovan <ionic at ionic.de>
Date:   Sat Oct 29 01:16:30 2016 +0200

    bin/build-rpm-package: implement cache_mock_config () and use it in generate_mock_config ().
---
 bin/build-rpm-package |   68 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)

diff --git a/bin/build-rpm-package b/bin/build-rpm-package
index 57b7787..c46dc00 100755
--- a/bin/build-rpm-package
+++ b/bin/build-rpm-package
@@ -71,6 +71,7 @@ test -f "${HOME}/.buildscripts/${PREFIX}.conf" && . "${HOME}/.buildscripts/${PRE
 TMP_MOCK_CFG_DIR=""
 TMP_MOCK_CFG_FILE=""
 TEMP_BASE="${HOME}/tmp/"
+MOCK_CACHE_DIR="${HOME}/mock-cache/"
 typeset -ag temp_cleanup
 # End of non-user-serviceable part.
 
@@ -131,6 +132,9 @@ set_vars() {
 	mkdir -p -- "${TEMP_BASE}"
 	chmod 2770 -- "${TEMP_BASE}"
 
+	mkdir -p -- "${MOCK_CACHE_DIR}"
+	chmod 2770 -- "${MOCK_CACHE_DIR}"
+
 	# first argv is the name of the Git project
 	PROJECT_PATH="${1}"
 	PROJECT_PATH="${PROJECT_PATH/%.git/}"
@@ -419,9 +423,73 @@ create_mock_config () { # MOCK_BASE CUSTOM_REPO COMPONENT TARGET
 	# Reset input field separator to its original value.
 	IFS="${OLDIFS}"
 
+	# Cache config file to work around mock constantly rebuilding its cache if the
+	# config file's modification time is newer than the cache, even though the
+	# the config file's content didn't change between runs.
+	cache_mock_config
+
 	return 0
 }
 
+# Either caches, updates or uses a cached mock config file.
+#
+# Calling this function in a subshell is an error, as it NEEDS to modify
+# variables in global scope!
+cache_mock_config () {
+	# Check argument sanity.
+
+	typeset tmp_mock_config="${TMP_MOCK_CFG_FILE}"
+
+	# Append .cfg if not already specified.
+	typeset tmp_regex='^.*\.cfg$'
+	if [[ ! "${tmp_mock_config}" =~ ${tmp_regex} ]]; then
+		echo "Error: mock temporary config path must end in '.cfg'." >&2
+		exit 1
+	fi
+
+	# Must exist and be readable.
+	if [ ! -f "${tmp_mock_config}" ] || [ ! -r "${tmp_mock_config}" ]; then
+		echo "Error: ${tmp_mock_config} parameter must exist, be a regular file and readable." >&2
+		exit 1
+	fi
+
+	# Fetch file name only and delete the extension.
+	typeset mock_cache_file_name="$(basename -s '.cfg' "${tmp_mock_config}")"
+	typeset tmp="${mock_cache_file_name}"
+	mock_cache_file_name="${mock_cache_file_name%.*}"
+
+	if [ "${mock_cache_file_name}" = "${tmp}" ]; then
+		echo "Error: unable to remove the random suffix of the temporary config file name '${tmp}'." >&2
+		exit 1
+	fi
+
+	typeset mock_cache_file_path="${MOCK_CACHE_DIR}/${mock_cache_file_name}"
+	if [ -f "${mock_cache_file_path}" ]; then
+		# A cached config file exists.
+		if [ -r "${mock_cache_file_path}" ]; then
+			# It's also readable. Compare it.
+			cmp "${TMP_MOCK_CFG_FILE}" "${mock_cache_file_path}" && {
+				echo "Cached mock config file matches generated one, using the cached file." >&2
+			} || {
+				echo "Refreshing cached mock config file due to differing content." >&2
+				cp -av "${TMP_MOCK_CFG_FILE}" "${mock_cache_file_path}"
+			}
+		else
+			echo "Error: a cached mock config file already exists as '${mock_cache_file_path}', but is not readable." >&2
+			exit 1
+		fi
+	else
+		# No cached config file exists yet, copy the file
+		# and set it as the new mock config file.
+		echo "Caching mock config file."
+		cp -av "${TMP_MOCK_CFG_FILE}" "${mock_cache_file_path}"
+	fi
+
+	TMP_MOCK_CFG_FILE="${mock_cache_file_path}"
+
+	return "0"
+}
+
 # Fetches extras repositories, if requested.
 # Takes the build system type (suse or redhat -- implicitly means OBS or mock),
 # the distribution, its version (either a real number or a codename),

--
Alioth's /srv/git/code.x2go.org/buildscripts.git//..//_hooks_/post-receive-email on /srv/git/code.x2go.org/buildscripts.git


More information about the x2go-commits mailing list