From 4210ee68ea25dd41c05f3a2a6d8880e5aedeb2c3 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Mon, 6 Jan 2025 03:54:38 +0000 Subject: lib.sh: Much safer python version check See: https://docs.python.org/3/library/sys.html#sys.version_info The sys.version_info tuple is a more reliable way to get the version. Our previous logic assumed that Python would always output "Python versionnumber", but this may not always be how it works. We've seen this for example where Debian modifies some GNU toolchains to include Debian something in the output. Python has a standard method built in for outputting exact the information we need. In my system, what I got was this: (3, 11, 2, 'final', 0) That output was from running this command: python -c 'import sys; print(sys.version_info[:])' This is much more robust, so use this instead. Signed-off-by: Leah Rowe --- include/lib.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/include/lib.sh b/include/lib.sh index 894a2cac..b43d83f3 100644 --- a/include/lib.sh +++ b/include/lib.sh @@ -82,7 +82,13 @@ pyver="2" python="python3" command -v python3 1>/dev/null || python="python" command -v $python 1>/dev/null || pyver="" -[ -n "$pyver" ] && pyver="$($python --version | awk '{print $2}')" +[ -z "$pyver" ] || \ + python -c 'import sys; print(sys.version_info[:])' 1>/dev/null \ + 2>/dev/null || $err "Cannot determine which Python version." +[ -n "$pyver" ] && \ + pyver="`python -c 'import sys; print(sys.version_info[:])' | \ + awk '{print $1}'`" && \ + pyver="${pyver#(}" && pyver="${pyver%,}" if [ "${pyver%%.*}" != "3" ]; then printf "Wrong python version, or python missing. Must be v 3.x.\n" 1>&2 exit 1 -- cgit v1.2.1