From d790b861a03fb23c689eaa8a32ca8dbba1b82a0b Mon Sep 17 00:00:00 2001 From: langfingaz Date: Sun, 25 Apr 2021 15:22:27 +0200 Subject: [PATCH] fix/improve repository manager --- pkg/de-p1st-repo/PKGBUILD | 2 +- pkg/de-p1st-repo/arch-repo-receive-new.sh | 107 +++++++++++++++++++++- 2 files changed, 103 insertions(+), 6 deletions(-) diff --git a/pkg/de-p1st-repo/PKGBUILD b/pkg/de-p1st-repo/PKGBUILD index 77eed4d..4cf831b 100644 --- a/pkg/de-p1st-repo/PKGBUILD +++ b/pkg/de-p1st-repo/PKGBUILD @@ -2,7 +2,7 @@ _pkgname=repo _reponame=arch-pkg pkgname="de-p1st-$_pkgname" -pkgver=0.0.6 +pkgver=0.0.7 pkgrel=1 pkgdesc="Bash script to manage remote Arch Linux repository" arch=('any') diff --git a/pkg/de-p1st-repo/arch-repo-receive-new.sh b/pkg/de-p1st-repo/arch-repo-receive-new.sh index 7e7295f..4d26fa1 100644 --- a/pkg/de-p1st-repo/arch-repo-receive-new.sh +++ b/pkg/de-p1st-repo/arch-repo-receive-new.sh @@ -10,13 +10,110 @@ source /etc/de-p1st-repo/arch-repo.cfg || exit function add_to_db(){ echo "Adding new packages to db ..." - mapfile -t PKGS < <(cat new-pkg.txt) - for PKG in "${PKGS[@]}"; do - repo-add -n "${REMOTE_DB_NAME}.db.tar.gz" "${PKG}" || exit + sort_pkgname_pkgver || return $? + + echo "For each package: Add latest version to database ..." + for PKGNAME in ./pkg/*; do + + # + # get latest version for PKGNAME + # + + local LATEST + for PKGVER in ./pkg/"${PKGNAME}"/*; do + LATEST="$PKGVER" + break + done + + local cmp + for PKGVER in ./pkg/"$PKGNAME"/*; do + # compare the currently known latest version + # with the next version + cmp=$(vercmp "$LATEST" "$PKGVER") || return $? + # if the new version is larger, save it as LATEST + if [ "$cmp" -lt "0" ]; then + LATEST="$PKGVER" + fi + done + + # + # add latest version of PKGNAME to database + # + PKG=$(cat "./pkg/${PKGNAME}/${LATEST}") || return $? + repo-add -n "${REMOTE_DB_NAME}.db.tar.gz" "${PKG}" || return $? + + true done } +# +# create files "./pkg/$pkgname/$pkgver" with content "$path_to_file" = "$PKG" +# +function sort_pkgname_pkgver(){ + echo "Sorting packages by package name and package version ..." + + for PKG in ./*.pkg.tar.{xz,zst}; do + get_pkginfo "$PKG" || return $? + get_pkgname "$PKGINFO" || return $? + get_pkgver "$PKGINFO" || return $? + + echo "Creating file pkg/${PKGNAME} with content ${PKG} ..." + mkdir -p "pkg/${PKGNAME}" || return $? + echo "${PKG}" > "pkg/${PKGNAME}/${PKGVER}" || return $? + done +} + +# +# get .PKGINFO +# +function get_pkginfo(){ + # $1: path to package file + # return: 0 on success + + if endswith "$1" ".pkg.tar.xz"; then + PKGINFO=$(tar -xvf "$1" -O .PKGINFO) || return $? + elif endswith "$1" ".pkg.tar.zst"; then + PKGINFO=$(tar -I zstd -xvf "$1" -O .PKGINFO) || return $? + else + return 1 + fi +} + +# +# get pkgver from $PKGINFO +# +function get_pkgver(){ + # return: 0 on success + + # remove "pkgver = " as well as tailing whitespace characters + local tmp + tmp=$(echo "$PKGINFO" | grep '^pkgver') || return $? + PKGVER=$(echo "$tmp" | sed 's|^pkgver\s*=\s*||; s|\s*$||') || return $? +} +# +# get pkgname from &PKGINFO +# +function get_pkgname(){ + # return: 0 on success + + # remove "pkgname = " as well as tailing whitespace characters + local tmp + tmp=$(echo "$PKGINFO" | grep '^pkname') || return $? + PKGVER=$(echo "$tmp" | sed 's|^pkgname\s*=\s*||; s|\s*$||') || return $? +} + + +# Inspired by: https://stackoverflow.com/questions/2172352/in-bash-how-can-i-check-if-a-string-begins-with-some-value/18558871#18558871 +# +# $1 begins with $2 +# +beginswith() { case $1 in "$2"*) true;; *) false;; esac; } +# +# $1 ends with $2 +# +endswith() { case $1 in *"$2") true;; *) false;; esac; } + # # generate index.html @@ -50,5 +147,5 @@ function generate_index(){ cd "${REMOTE_PKG_DIR}" || exit -add_to_db -generate_index +add_to_db || exit +generate_index || exit