New script compare_version

New standalone script for comparing two #.#.# format version strings
using standard functions included with python.  We also benefit from the
fact the python function is more flexible than my bash function and it
keeps the logic of a rather high-level script high-level.
This commit is contained in:
T. Joseph Carter
2018-04-11 03:33:25 -07:00
parent 9382387000
commit 2b52ad8429
2 changed files with 42 additions and 62 deletions
+35
View File
@@ -0,0 +1,35 @@
#! /usr/bin/env python3
# vim: set tabstop=4 shiftwidth=4 noexpandtab filetype=python:
import sys
from pkg_resources import parse_version
def usage():
print("Usage: compare_version <version> <operator> <version>")
print(" where operator is one of lt, le, eq, ge, gt, or ne")
sys.exit(2)
if len(sys.argv) != 4:
usage()
ver1 = parse_version(sys.argv[1])
ver2 = parse_version(sys.argv[3])
# I suppose you could do something clever with meta-method mapping here
result = False
if sys.argv[2] == 'lt':
result = ver1 < ver2
elif sys.argv[2] == 'le':
result = ver1 <= ver2
elif sys.argv[2] == 'eq':
result = ver1 == ver2
elif sys.argv[2] == 'ge':
result = ver1 >= ver2
elif sys.argv[2] == 'gt':
result = ver1 > ver2
elif sys.argv[2] == 'ne':
result = ver1 != ver2
else:
usage()
sys.exit(0 if result else 1)
+7 -62
View File
@@ -19,6 +19,8 @@ if [[ ! -f "$a2sSource/.a2server_source" ]]; then
exit 1
fi
compare_version="$a2sSource/scripts/compare_version"
isRpi=
[[ -f /usr/bin/raspi-config ]] && isRpi=1
@@ -39,66 +41,9 @@ if [[ -f /usr/local/etc/A2SERVER-version ]]; then
fi
echo "A2SERVER version available: $a2serverVersion"
echo "A2SERVER version installed: ${installedVersion:=None}"
function a2sCmpInstalled() {
local iMajor iMinor iRev
local cMajor cMinor cRev
[[ -z "$installedVersion" ]] && return 0 # No, not installed
[[ -z "$1" ]] && return 1
iMajor=${installedVersion%%.*}
iMinor=${installedVersion#*.}
iMinor=${iMinor%%.*}
iRev=${installedVersion##*.}
cMajor=${2%%.*}
cMinor=${2#*.}
cMinor=${cMinor%%.*}
cRev=${2##*.}
case "$1" in
lt|le)
if [[ "$iMajor" -lt "$cMajor" ]]; then
return 0
elif [[ "$iMajor" -eq "$cMajor" ]]; then
if [[ "$iMinor" -lt "$cMinor" ]]; then
return 0
elif [[ "$iMinor" -eq "$cMinor" ]]; then
if [[ "$iRev" -le "$cRev" ]]; then
if [[ "$1" == "le" || "$iRev" -lt "$cRev" ]]; then
return 0
fi
fi
fi
fi
;;
gt|ge)
if [[ "$iMajor" -gt "$cMajor" ]]; then
return 0
elif [[ "$iMajor" -eq "$cMajor" ]]; then
if [[ "$iMinor" -gt "$cMinor" ]]; then
return 0
elif [[ "$iMinor" -eq "$cMinor" ]]; then
if [[ "$iRev" -ge "$cRev" ]]; then
if [[ "$1" == "ge" || "$iRev" -gt "$cRev" ]]; then
return 0
fi
fi
fi
fi
;;
eq)
if [[ "$iMajor" -eq "$cMajor" ]]; then
if [[ "$iMinor" -eq "$cMinor" ]]; then
if [[ "$iRev" -eq "$cRev" ]]; then
return 0
fi
fi
fi
;;
esac
return 1
}
if [[ $installedVersion == "None" ]]; then
installedVersion=0
fi
skipRepoUpdate=
autoAnswerYes=
@@ -157,7 +102,7 @@ while [[ $1 ]]; do
fi
done
if a2sCmpInstalled lt 1.1.0; then
if "$compare_version" $installedVersion lt 1.1.0; then
echo
echo "WARNING: The current A2SERVER installer scripts haven't been tested for"
echo "updating the earlier version of A2SERVER that you have. A fresh install"
@@ -168,7 +113,7 @@ fi
a2server_update=0
doSetup=1
if a2sCmpInstalled lt 1.5.2; then
if "$compare_version" $installedVersion lt 1.5.2; then
a2server_update=1
fi