From dfef2f3a6aa76d67f2dfab66df6acbd4172d4757 Mon Sep 17 00:00:00 2001 From: Anders Roxell Date: Fri, 11 Aug 2023 10:19:49 +0200 Subject: [PATCH] automated: lib: sh-test-lib: check python version Add a generic way to check python version if a specific minimum version is needed. Signed-off-by: Anders Roxell --- automated/lib/sh-test-lib | 44 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/automated/lib/sh-test-lib b/automated/lib/sh-test-lib index a18f127af..f4f25cb5e 100755 --- a/automated/lib/sh-test-lib +++ b/automated/lib/sh-test-lib @@ -766,3 +766,47 @@ check_config() { check_return "config_value_${c}" done } + +major() { + # shellcheck disable=SC2039 + local pv="${1}" + echo "${pv}" | awk -F'.' '{print $1}' +} + +minor() { + # shellcheck disable=SC2039 + local pv="${1}" + echo "${pv}" | awk -F'.' '{print $2}' +} + +check_python_version() { + # send in "python --version" + # shellcheck disable=SC2039 + local pv=$(echo "${1}" | awk -F' ' '{print $NF}') + # example "3.9" + local min_version="${2}" + local err="${3}" + local x=$(major ${python_version}) + local y=$(minor ${python_version}) + local mx=$(major ${min_version}) + local my=$(minor ${min_version}) + local str="Wrong Python version installed $(python --version)" + if [ ${x} -lt ${mx} ]; then + if [ "${err}" == "Error" ]; then + error_msg "${str}" + else + warn_msg "${str}" + return + fi + fi + + if [ ${y} -lt ${my} ]; then + if [ ${x} -le ${mx} ]; then + if [ "${err}" == "Error" ]; then + error_msg "${str}" + else + warn_msg "${str}" + fi + fi + fi +}