[arch-projects] [dbscripts] [PATCH 4/5] Centralize all SVN access into 'abs_*' functions in a new 'db-abs' file

Luke Shumaker lukeshu at lukeshu.com
Tue Jun 19 04:21:03 UTC 2018


From: Luke Shumaker <lukeshu at parabola.nu>

Moving all SVN code in to a separate file means both that:
 1. It is easier to identify the interactions with SVN, when considering a
    replacement.
 2. It is easier to swap out the one file if/when replacing SVN with
    something else.

Put another way: try to be less tightly coupled with SVN.
---
 cron-jobs/sourceballs |   4 +-
 db-abs                | 106 ++++++++++++++++++++++++++++++++++++++++++
 db-functions          |  23 ++++-----
 db-move               |  31 ++----------
 db-remove             |   9 +---
 5 files changed, 121 insertions(+), 52 deletions(-)
 create mode 100644 db-abs

diff --git a/cron-jobs/sourceballs b/cron-jobs/sourceballs
index 6be28ab..f56f0cf 100755
--- a/cron-jobs/sourceballs
+++ b/cron-jobs/sourceballs
@@ -79,8 +79,8 @@ for repo in "${PKGREPOS[@]}"; do
 
 			# Get the sources from svn
 			mkdir -p -m0770 "${WORKDIR}/pkgbuilds/${repo}-${pkgarch}"
-			arch_svn export -q "${SVNREPO}/${pkgbase}/repos/${repo}-${pkgarch}" \
-				"${WORKDIR}/pkgbuilds/${repo}-${pkgarch}/${pkgbase}" >/dev/null 2>&1
+			abs_export "$repo" "$pkgarch" "$pkgbase" \
+				"${WORKDIR}/pkgbuilds/${repo}-${pkgarch}/${pkgbase}"
 			if (( $? >= 1 )); then
 				failedpkgs+=("${pkgbase}-${pkgver}${SRCEXT}")
 				continue
diff --git a/db-abs b/db-abs
new file mode 100644
index 0000000..5a34b84
--- /dev/null
+++ b/db-abs
@@ -0,0 +1,106 @@
+#!/hint/bash
+
+arch_svn() {
+	if [[ -z ${SVNUSER} ]]; then
+		/usr/bin/svn "${@}"
+	else
+		sudo -u "${SVNUSER}" -- /usr/bin/svn --username "${USER}" "${@}"
+	fi
+}
+
+_abs_checkout() {
+	local pkgbase=$1
+	if ! [[ -d ${WORKDIR}/svn ]]; then
+		arch_svn checkout -q -N "${SVNREPO}" "${WORKDIR}/svn" >/dev/null
+	fi
+	if ! [[ -d ${WORKDIR}/svn/${pkgbase} ]]; then
+		arch_svn up -q "${WORKDIR}/svn/${pkgbase}" >/dev/null
+	fi
+}
+
+abs_move_preflight_check() {
+	local repo_from=$1
+	local pkgarch=$2
+	local pkgbase=$3
+
+	_abs_checkout "$pkgbase"
+	local svnrepo_from="${WORKDIR}/svn/${pkgbase}/repos/${repo_from}-${pkgarch}"
+	[[ -r ${svnrepo_from}/PKGBUILD ]]
+}
+
+abs_move_start() {
+	abs_move_repo_from=$1
+	abs_move_repo_to=$2
+	abs_move_pkgbase=$3
+
+	abs_move_tag_list=""
+}
+
+# If the "from" PKGBUILD doesn't exist, this is a no-op (not an
+# error), so that it can be run for each arch, and the invoker doesn't
+# need to worry about hoisting it out of the loop if arch=(any).  If
+# the nonexistence is such that it should be an error, we count on
+# abs_move_preflight_check having already caught that.
+abs_move_arch() {
+	local pkgarch=$1
+
+	local repo_from=$abs_move_repo_from
+	local repo_to=$abs_move_repo_to
+	local pkgbase=$abs_move_pkgbase
+
+	local svnrepo_from="${WORKDIR}/svn/${pkgbase}/repos/${repo_from}-${pkgarch}"
+	local svnrepo_to="${WORKDIR}/svn/${pkgbase}/repos/${repo_to}-${pkgarch}"
+	if [[ -f ${svnrepo_from}/PKGBUILD ]]; then
+		msg2 "%s (%s)" "$pkgbase" "$pkgarch"
+
+		if [[ -d ${svnrepo_to} ]]; then
+			for file in $(arch_svn ls "${svnrepo_to}"); do
+				arch_svn rm -q "${svnrepo_to}/$file@"
+			done
+		else
+			mkdir "${svnrepo_to}"
+			arch_svn add -q "${svnrepo_to}"
+		fi
+
+		for file in $(arch_svn ls "${svnrepo_from}"); do
+			arch_svn mv -q -r HEAD "${svnrepo_from}/$file@" "${svnrepo_to}/"
+		done
+		arch_svn rm --force -q "${svnrepo_from}"
+		abs_move_tag_list+=", $pkgarch"
+	fi
+}
+
+abs_move_finish() {
+	local repo_from=$abs_move_repo_from
+	local repo_to=$abs_move_repo_to
+	local pkgbase=$abs_move_pkgbase
+
+	local tag_list="${abs_move_tag_list#, }"
+	arch_svn commit -q "${WORKDIR}/svn/${pkgbase}" -m "${0##*/}: moved ${pkgbase} from [${repo_from}] to [${repo_to}] (${tag_list})"
+}
+
+abs_remove() {
+	local repo=$1
+	local arch=$2
+	local pkgbase=$3
+
+	local svnrepo="$repo-$arch"
+
+	_abs_checkout "$pkgbase"
+	if [[ -d ${WORKDIR}/svn/$pkgbase/repos/$svnrepo ]]; then
+		arch_svn rm --force -q "${WORKDIR}/svn/$pkgbase/repos/$svnrepo"
+		arch_svn commit -q "${WORKDIR}/svn/$pkgbase" -m "${0##*/}: $pkgbase removed by $(id -un)"
+	else
+		warning "pkgbase '%s' not found in svn; unable to commit removal to svn" "$pkgbase"
+	fi
+}
+
+abs_export() {
+	local repo=$1
+	local pkgarch=$2
+	local pkgbase=$3
+	local dest=$4
+
+	arch_svn export -q "${SVNREPO}/${pkgbase}/repos/${repo}-${pkgarch}" \
+		 "${dest}" >/dev/null 2>&1
+}
diff --git a/db-functions b/db-functions
index 69f35b4..340c794 100644
--- a/db-functions
+++ b/db-functions
@@ -1,6 +1,7 @@
 #!/hint/bash
 
 . /usr/share/makepkg/util.sh
+. "$(dirname "${BASH_SOURCE[0]}")/db-abs"
 
 # global shell options for enhanced bash scripting
 shopt -s extglob globstar nullglob
@@ -348,16 +349,16 @@ check_pkgsvn() {
 
 	in_array "${repo}" "${PKGREPOS[@]}" || return 1
 
-	if [[ ! -f ${WORKDIR}/pkgbuilds/${repo}-${_pkgarch}/${_pkgbase} ]]; then
+	if [[ ! -f ${WORKDIR}/pkgbuilds/${repo}-${_pkgarch}/${_pkgbase}/PKGBUILD ]]; then
 		mkdir -p "${WORKDIR}/pkgbuilds/${repo}-${_pkgarch}"
-		arch_svn export -q "${SVNREPO}/${_pkgbase}/repos/${repo}-${_pkgarch}/PKGBUILD" \
+		abs_export "$repo" "$_pkgarch" "$_pkgbase" \
 			"${WORKDIR}/pkgbuilds/${repo}-${_pkgarch}/${_pkgbase}" >/dev/null || return 1
 	fi
 
-	local svnver="$(. "${WORKDIR}/pkgbuilds/${repo}-${_pkgarch}/${_pkgbase}"; get_full_version)"
+	local svnver="$(. "${WORKDIR}/pkgbuilds/${repo}-${_pkgarch}/${_pkgbase}/PKGBUILD"; get_full_version)"
 	[[ "${svnver}" = "${_pkgver}" ]] || return 1
 
-	local svnnames=($(. "${WORKDIR}/pkgbuilds/${repo}-${_pkgarch}/${_pkgbase}"; echo "${pkgname[@]}"))
+	local svnnames=($(. "${WORKDIR}/pkgbuilds/${repo}-${_pkgarch}/${_pkgbase}/PKGBUILD"; echo "${pkgname[@]}"))
 	in_array "${_pkgname}" "${svnnames[@]}" || return 1
 
 	return 0
@@ -382,13 +383,13 @@ check_splitpkgs() {
 		mkdir -p "${repo}/${_pkgarch}/${_pkgbase}"
 		echo "${_pkgname}" >> "${repo}/${_pkgarch}/${_pkgbase}/staging"
 
-		if [[ ! -f ${WORKDIR}/pkgbuilds/${repo}-${_pkgarch}/${_pkgbase} ]]; then
+		if [[ ! -f ${WORKDIR}/pkgbuilds/${repo}-${_pkgarch}/${_pkgbase}/PKGBUILD ]]; then
 			mkdir -p "${WORKDIR}/pkgbuilds/${repo}-${_pkgarch}"
-			arch_svn export -q "${SVNREPO}/${_pkgbase}/repos/${repo}-${_pkgarch}/PKGBUILD" \
+			abs_export "$repo" "$_pkgarch" "$_pkgbase" \
 				"${WORKDIR}/pkgbuilds/${repo}-${_pkgarch}/${_pkgbase}" >/dev/null || return 1
 		fi
 
-		local svnnames=($(. "${WORKDIR}/pkgbuilds/${repo}-${_pkgarch}/${_pkgbase}"; echo "${pkgname[@]}"))
+		local svnnames=($(. "${WORKDIR}/pkgbuilds/${repo}-${_pkgarch}/${_pkgbase}/PKGBUILD"; echo "${pkgname[@]}"))
 		printf '%s\n' "${svnnames[@]}" >> "${repo}/${_pkgarch}/${_pkgbase}/svn"
 	done
 	popd >/dev/null
@@ -489,11 +490,3 @@ arch_repo_modify() {
 
 	REPO_MODIFIED=1
 }
-
-arch_svn() {
-	if [[ -z ${SVNUSER} ]]; then
-		/usr/bin/svn "${@}"
-	else
-		sudo -u "${SVNUSER}" -- /usr/bin/svn --username "${USER}" "${@}"
-	fi
-}
diff --git a/db-move b/db-move
index 5f197b8..8a1c24d 100755
--- a/db-move
+++ b/db-move
@@ -25,15 +25,12 @@ for pkgarch in "${ARCHES[@]}"; do
 done
 
 # check if packages to be moved exist in svn and ftp dir
-arch_svn checkout -q -N "${SVNREPO}" "${WORKDIR}/svn" >/dev/null
 for pkgbase in "${args[@]:2}"; do
-	arch_svn up -q "${WORKDIR}/svn/${pkgbase}" >/dev/null
 	for tarch in "${ARCHES[@]}"; do
 		found=false
 		while read -r pkgarch pkgfile; do
 
-			svnrepo_from="${WORKDIR}/svn/${pkgbase}/repos/${repo_from}-${pkgarch}"
-			if ! [[ -r ${svnrepo_from}/PKGBUILD ]]; then
+			if ! abs_move_preflight_check "$repo_from" "$pkgarch" "$pkgbase"; then
 				die "%s not found in %s" "$pkgbase" "$repo_from"
 			fi
 
@@ -52,29 +49,10 @@ msg "Moving packages from [%s] to [%s]..." "$repo_from" "$repo_to"
 declare -A add_pkgs
 declare -A remove_pkgs
 for pkgbase in "${args[@]:2}"; do
-	tag_list=""
+	abs_move_start "$repo_from" "$repo_to" "$pkgbase"
 	for tarch in "${ARCHES[@]}"; do
 		while read -r pkgname pkgver pkgarch pkgfile; do
-			svnrepo_from="${WORKDIR}/svn/${pkgbase}/repos/${repo_from}-${pkgarch}"
-			svnrepo_to="${WORKDIR}/svn/${pkgbase}/repos/${repo_to}-${pkgarch}"
-			if [[ -f ${svnrepo_from}/PKGBUILD ]]; then
-				msg2 "%s (%s)" "$pkgbase" "$pkgarch"
-
-				if [[ -d ${svnrepo_to} ]]; then
-					for file in $(arch_svn ls "${svnrepo_to}"); do
-						arch_svn rm -q "${svnrepo_to}/$file@"
-					done
-				else
-					mkdir "${svnrepo_to}"
-					arch_svn add -q "${svnrepo_to}"
-				fi
-
-				for file in $(arch_svn ls "${svnrepo_from}"); do
-					arch_svn mv -q -r HEAD "${svnrepo_from}/$file@" "${svnrepo_to}/"
-				done
-				arch_svn rm --force -q "${svnrepo_from}"
-				tag_list+=", $pkgarch"
-			fi
+			abs_move_arch "$pkgarch"
 
 			ln -s "../../../${PKGPOOL}/${pkgfile}" "${ftppath_to}/${tarch}/"
 			if [[ -f ${FTP_BASE}/${PKGPOOL}/${pkgfile}.sig ]]; then
@@ -84,8 +62,7 @@ for pkgbase in "${args[@]:2}"; do
 			remove_pkgs[${tarch}]+="${pkgname} "
 		done < <(getdbinfo "$repo_from" "$tarch" "$pkgbase" NAME,VERSION,ARCH,FILENAME)
 	done
-	tag_list="${tag_list#, }"
-	arch_svn commit -q "${WORKDIR}/svn/${pkgbase}" -m "${0##*/}: moved ${pkgbase} from [${repo_from}] to [${repo_to}] (${tag_list})"
+	abs_move_finish
 done
 
 for tarch in "${ARCHES[@]}"; do
diff --git a/db-remove b/db-remove
index 3017026..93430b6 100755
--- a/db-remove
+++ b/db-remove
@@ -13,7 +13,6 @@ arch="$2"
 pkgbases=("${@:3}")
 
 ftppath="$FTP_BASE/$repo/os"
-svnrepo="$repo-$arch"
 
 if ! check_repo_permission "$repo"; then
 	die "You don't have permission to remove packages from %s" "$repo"
@@ -36,13 +35,7 @@ for pkgbase in "${pkgbases[@]}"; do
 	mapfile -t pkgnames < <(getdbinfo "$repo" "${tarches[0]}" "$pkgbase" NAME)
 	remove_pkgs+=("${pkgnames[@]}")
 
-	arch_svn checkout -q "${SVNREPO}/${pkgbase}" "${WORKDIR}/svn/${pkgbase}" >/dev/null
-	if [[ -d ${WORKDIR}/svn/$pkgbase/repos/$svnrepo ]]; then
-		arch_svn rm --force -q "${WORKDIR}/svn/$pkgbase/repos/$svnrepo"
-		arch_svn commit -q "${WORKDIR}/svn/$pkgbase" -m "${0##*/}: $pkgbase removed by $(id -un)"
-	else
-		warning "pkgbase '%s' not found in svn; unable to commit removal to svn" "$pkgbase"
-	fi
+	abs_remove "$repo" "$arch" "$pkgbase"
 done
 
 for tarch in "${tarches[@]}"; do
-- 
2.17.1


More information about the arch-projects mailing list