diff --git a/scripts/bin/apt-compare-versions b/scripts/bin/apt-compare-versions deleted file mode 100755 index b1225448f..000000000 --- a/scripts/bin/apt-compare-versions +++ /dev/null @@ -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 ') - -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) diff --git a/scripts/bin/apt-compare-versions b/scripts/bin/apt-compare-versions new file mode 120000 index 000000000..00f20e6cf --- /dev/null +++ b/scripts/bin/apt-compare-versions @@ -0,0 +1 @@ +../updates/utils/termux_pkg_is_update_needed.sh \ No newline at end of file diff --git a/scripts/updates/utils/termux_pkg_is_update_needed.sh b/scripts/updates/utils/termux_pkg_is_update_needed.sh index 266c48a71..250725e52 100755 --- a/scripts/updates/utils/termux_pkg_is_update_needed.sh +++ b/scripts/updates/utils/termux_pkg_is_update_needed.sh @@ -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]} [version-regexp] - Returns: 0 if update is needed, 1 if not. - EndOfUsage + # USAGE: termux_pkg_is_update_needed [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] ] [version-regex]" + echo "--help - show this help message and exit" + echo " - first version to compare" + echo " - 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