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>
20241206_branch
Leah Rowe 2025-01-06 03:54:38 +00:00
parent 8c7ba6131c
commit 4210ee68ea
1 changed files with 7 additions and 1 deletions

View File

@ -82,7 +82,13 @@ pyver="2"
python="python3" python="python3"
command -v python3 1>/dev/null || python="python" command -v python3 1>/dev/null || python="python"
command -v $python 1>/dev/null || pyver="" 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 if [ "${pyver%%.*}" != "3" ]; then
printf "Wrong python version, or python missing. Must be v 3.x.\n" 1>&2 printf "Wrong python version, or python missing. Must be v 3.x.\n" 1>&2
exit 1 exit 1