summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2025-01-06 03:54:38 +0000
committerLeah Rowe <leah@libreboot.org>2025-01-06 03:54:38 +0000
commit4210ee68ea25dd41c05f3a2a6d8880e5aedeb2c3 (patch)
treebfb7c82f5140738519a9b4ad51ca51f5f8f7e349
parent8c7ba6131cc0bd5fb31bbe80ec5f526f3c9b6843 (diff)
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 <leah@libreboot.org>
-rw-r--r--include/lib.sh8
1 files changed, 7 insertions, 1 deletions
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