feat: replace `apt-compare-versions` script with `termux_pkg_is_update_needed`

Signed-off-by: Aditya Alok <dev.aditya.alok@gmail.com>
This commit is contained in:
Aditya Alok 2022-03-29 00:50:29 +05:30
parent 92ea572ede
commit c23d019058
No known key found for this signature in database
GPG Key ID: 5A52117417798AC7
2 changed files with 32 additions and 38 deletions

View File

@ -1,29 +0,0 @@
#!/usr/bin/env python3
# apt-compare-versions: Simple script which takes two arguments and compares
# their version according to apt rules. This can be used to verify the ordering
# between two versions.
#
# Note that ~ (tilde) construct, which allows for beta and preview releases.
# A version with ~ is considered earlier than one without, so 1.6~beta1 is
# considered earlier than 1.6. If both versions contains ~ the version comparison
# is made first on the part preceding the tilde, then the part coming after,
# so 1.6~beta1 comes before 1.6~beta2.
import apt_pkg, sys
apt_pkg.init_system()
if len(sys.argv) != 3:
sys.exit('usage: apt-compare-versions <first-version> <second-version>')
version1 = sys.argv[1]
version2 = sys.argv[2]
comparison_result = apt_pkg.version_compare(version1, version2)
if comparison_result > 0:
operator = ' > '
elif comparison_result < 0:
operator = ' < '
elif comparison_result == 0:
operator = ' == '
print(version1 + operator + version2)

View File

@ -0,0 +1 @@
../updates/utils/termux_pkg_is_update_needed.sh

View File

@ -12,11 +12,10 @@
# But hopefully, all this can be avoided if TERMUX_PKG_AUTO_UPDATE_TAG_REGEXP is set.
#
termux_pkg_is_update_needed() {
if [[ "$#" -lt 2 ]] || [[ "$#" -gt 3 ]]; then
termux_error_exit <<-EndOfUsage
Usage: ${FUNCNAME[0]} <current-version> <latest-version> [version-regexp]
Returns: 0 if update is needed, 1 if not.
EndOfUsage
# USAGE: termux_pkg_is_update_needed <current-version> <latest-version> [regexp]
if [[ -z "$1" ]] || [[ -z "$2" ]]; then
termux_error_exit "${BASH_SOURCE[0]}: at least 2 arguments expected"
fi
local CURRENT_VERSION="$1"
@ -29,8 +28,8 @@ termux_pkg_is_update_needed() {
if [[ -z "${LATEST_VERSION}" ]]; then
termux_error_exit <<-EndOfError
ERROR: failed to check latest version. Ensure whether the version regex '${VERSION_REGEX}'
works correctly with latest release tag.
ERROR: failed to compare versions. Ensure whether the version regex '${VERSION_REGEX}'
works correctly with given versions.
EndOfError
fi
fi
@ -58,8 +57,31 @@ termux_pkg_is_update_needed() {
return 1 # false. Update not needed.
}
show_help() {
echo "Usage: ${BASH_SOURCE[0]} [--help] <first-version> <second-version>] [version-regex]"
echo "--help - show this help message and exit"
echo " <first-version> - first version to compare"
echo " <second-version> - second version to compare"
echo " [version-regex] - optional regular expression to filter version numbers"
exit 0
}
# Make script sourceable as well as executable.
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
declare -f termux_error_exit >/dev/null || . "$(dirname "${BASH_SOURCE[0]}")/termux_error_exit.sh"
termux_pkg_is_update_needed "$@"
declare -f termux_error_exit >/dev/null ||
. "$(dirname "$(realpath "${BASH_SOURCE[0]}")")/termux_error_exit.sh" # realpath is used to resolve symlinks.
if [[ "${1}" == "--help" ]]; then
show_help
fi
# Print in human readable format.
first_version="$1"
second_version="$2"
version_regexp="${3:-}"
if termux_pkg_is_update_needed "${first_version}" "${second_version}" "${version_regexp}"; then
echo "${first_version} < ${second_version}"
else
echo "${first_version} >= ${second_version}"
fi
fi