diff options
author | Leah Rowe <leah@libreboot.org> | 2025-04-24 13:24:45 +0100 |
---|---|---|
committer | Leah Rowe <leah@libreboot.org> | 2025-04-26 05:25:28 +0100 |
commit | 8edea026c58f06fa7af0ec9b20450641acccbefe (patch) | |
tree | 9207000887f57a9205adcde000bca8fefe6152e2 /include/lib.sh | |
parent | b1b964fa5c393cb4651861b14fb4e861ee7c6737 (diff) |
lib.sh: Force use of System Python to prevent hang
If the user has a virtual environment, the current logic
will cause lbmk to hang. A useful workaround is to force
use of the direct path to the system binary of python.
This works by detecting a virtual environment first, and
deferring to the old behaviour if no venv is found. If one
is found, then it will not rely on PATH, but instead only
search the standard locations /usr/local/bin and /usr/bin.
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'include/lib.sh')
-rw-r--r-- | include/lib.sh | 42 |
1 files changed, 34 insertions, 8 deletions
diff --git a/include/lib.sh b/include/lib.sh index cd3e14ae..3ae51b3f 100644 --- a/include/lib.sh +++ b/include/lib.sh @@ -98,15 +98,41 @@ for fv in version versiondate; do eval "[ ! -f \".$fv\" ] || read -r $fv < \".$fv\" || :" done +# Use direct path, to prevent a hang if Python is using a virtual environment, +# not command -v, to prevent a hang when checking python's version +# See: https://docs.python.org/3/library/venv.html#how-venvs-work +pybin() +{ + py="import sys; quit(1) if sys.prefix == sys.base_prefix else quit(0)" + + venv=1 + command -v "$1" 1>/dev/null 2>/dev/null || venv=0 + [ $venv -lt 1 ] || "$1" -c "$py" 1>/dev/null 2>/dev/null || venv=0 + + [ $venv -gt 0 ] && for pypath in "/usr/local/bin" "/usr/bin"; do + [ -e "$pypath/$1" ] && [ ! -d "$pypath/$1" ] && \ + [ -x "$pypath/$1" ] && printf "%s/%s\n" "$pypath" "$1" && \ + return 0 + done + [ $venv -gt 0 ] && return 1 + + # Defer to normal command -v if not a venv + command -v "$1" 2>/dev/null || return 1 +} + python="python3" -command -v python3 1>/dev/null || python="python" +pybin python3 1>/dev/null || python="python" pyver="2" && [ "$python" = "python3" ] && pyver="3" -command -v $python 1>/dev/null || pyver="" -[ -z "$pyver" ] || $python -c 'import sys; print(sys.version_info[:])' \ - 1>/dev/null 2>/dev/null || $err "Cannot detect host Python version." -[ -n "$pyver" ] && \ - pyver="`$python -c 'import sys; print(sys.version_info[:])' | \ - awk '{print $1}'`" && pyver="${pyver#(}" && pyver="${pyver%,}" +pybin "$python" 1>/dev/null || pyver="" +[ -z "$pyver" ] || "`pybin "$python"`" -c \ + 'import sys; print(sys.version_info[:])' 1>/dev/null 2>/dev/null || \ + $err "Cannot detect host Python version." +if [ -n "$pyver" ]; then + pyver="$("$(pybin "$python")" -c \ + 'import sys; print(sys.version_info[:])' | awk '{print $1}')" + pyver="${pyver#(}" + pyver="${pyver%,}" +fi [ "${pyver%%.*}" = "3" ] || $err "Wrong python version (must be v 3.x)" # XBMK_CACHE is a directory, for caching downloads and git repositories @@ -133,7 +159,7 @@ if [ -z "${TMPDIR+x}" ]; then # set up python v3.x in PATH, in case it's not set up correctly. # see code above that detected the correct python3 command. cd "$XBMK_CACHE/xbmkpath" || $err "can't cd $XBMK_CACHE/xbmkpath" - x_ ln -s "`command -v "$python"`" python + x_ ln -s "`pybin "$python"`" python ) || $err "Can't set up python symlink in $XBMK_CACHE/xbmkpath" xbmk_rval=0 |