diff options
author | Dave Reisner <dreisner@archlinux.org> | 2013-10-20 16:17:45 -0400 |
---|---|---|
committer | Pierre Schmitz <pierre@archlinux.de> | 2013-11-01 18:52:03 +0100 |
commit | 27441f201c2394a991427f6a47199dd40024497b (patch) | |
tree | 6c97472ae1045e4c49d8ff261235c7a41241e997 /lib | |
parent | c4f72f781b851578793b31e2c0b3f7ec839335cf (diff) | |
download | devtools32-27441f201c2394a991427f6a47199dd40024497b.tar.xz |
common: implement find_cached_package
This function (currently) searches through $PWD and $PKGDEST looking
for a tarball matching the requested package name, architecture, and
pkgver. If found, it writes the full path to the located package to
stdout and returns 0, else 1. If more than 1 match is found, it's
treated as an error and the user will need to figure out what to do.
Use this in checkpkg and commitpkg, which previously implemented their
own less complete logic, to locate the build artifacts they rely on.
Signed-off-by: Dave Reisner <dreisner@archlinux.org>
Signed-off-by: Pierre Schmitz <pierre@archlinux.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/common.sh | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/common.sh b/lib/common.sh index 3ec26ff..1812cb7 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -1,6 +1,8 @@ # Avoid any encoding problems export LANG=C +shopt -s extglob + # check if messages are to be printed using color unset ALL_OFF BOLD BLUE GREEN RED YELLOW if [[ -t 2 ]]; then @@ -154,3 +156,70 @@ slock() { stat_done fi } + +## +# usage: pkgver_equal( $pkgver1, $pkgver2 ) +## +pkgver_equal() { + local left right + + if [[ $1 = *-* && $2 = *-* ]]; then + # if both versions have a pkgrel, then they must be an exact match + [[ $1 = "$2" ]] + else + # otherwise, trim any pkgrel and compare the bare version. + [[ ${1%%-*} = "${2%%-*}" ]] + fi +} + +## +# usage: find_cached_package( $pkgname, $pkgver, $arch ) +# +# $pkgver can be supplied with or without a pkgrel appended. +# If not supplied, any pkgrel will be matched. +## +find_cached_package() { + local searchdirs=("$PWD" "$PKGDEST") results=() + local targetname=$1 targetver=$2 targetarch=$3 + local dir pkg pkgbasename pkgparts name ver rel arch size results + + for dir in "${searchdirs[@]}"; do + [[ -d $dir ]] || continue + + for pkg in "$dir"/*.pkg.tar?(.?z); do + [[ -f $pkg ]] || continue + + # split apart package filename into parts + pkgbasename=${pkg##*/} + pkgbasename=${pkgbasename%.pkg.tar?(.?z)} + + arch=${pkgbasename##*-} + pkgbasename=${pkgbasename%-"$arch"} + + rel=${pkgbasename##*-} + pkgbasename=${pkgbasename%-"$rel"} + + ver=${pkgbasename##*-} + name=${pkgbasename%-"$ver"} + + if [[ $targetname = "$name" && $targetarch = "$arch" ]] && + pkgver_equal "$targetver" "$ver-$rel"; then + results+=("$pkg") + fi + done + done + + case ${#results[*]} in + 0) + return 1 + ;; + 1) + printf '%s\n' "$results" + return 0 + ;; + *) + error 'Multiple packages found:' + printf '\t%s\n' "${results[@]}" + return 1 + esac +} |