lib.sh: use realpath to get sys python on venv

In the previous revision, I make hardcoded use of
/usr/local/bin and /usr/bin as search locations, instead
of relying on PATH, when the user has a python venv, because
in those cases, we cannot rely on PATH so we use a python
command to detect the venv and then force use of the
normal system path for python.

However, there's no guarantee that the real Python will
indeed live at these locations. For example, some distros
like Nix or Guix will use many locations for different
versions of a given package, and it's for the birds as to
what given package version the user might be running.

Therefore, this patch retains that current hardcoded
assumption of /usr/local/bin and /usr/bin but *only* as
a fallback solution, instead checking realpath first.

The "realpath" command isn't technically POSIX standard,
but in practise it is available on GNU coreutils, Busybox,
and the various BSD userlands.

I could perhaps *import* a realpath utility, and use that,
but this should be fine.

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2025-04-26 06:38:38 +01:00
parent 8edea026c5
commit 0ab7c6ff9c
+11
View File
@@ -109,6 +109,17 @@ pybin()
command -v "$1" 1>/dev/null 2>/dev/null || venv=0 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 -lt 1 ] || "$1" -c "$py" 1>/dev/null 2>/dev/null || venv=0
# ideally, don't rely on PATH or hardcoded paths if python venv.
# use the *real*, direct executable linked to by the venv symlink
if [ $venv -gt 0 ] && [ -L "`command -v "$1" 2>/dev/null`" ]; then
# realpath isn't posix, but available mostly universally
pypath="$(realpath \
"$(command -v "$1" 2>/dev/null)" 2>/dev/null || :)"
[ -e "$pypath" ] && [ ! -d "$pypath" ] && \
[ -x "$pypath" ] && printf "%s\n" "$pypath" && return 0; :
fi
# if python venv: fall back to common PATH directories for checking
[ $venv -gt 0 ] && for pypath in "/usr/local/bin" "/usr/bin"; do [ $venv -gt 0 ] && for pypath in "/usr/local/bin" "/usr/bin"; do
[ -e "$pypath/$1" ] && [ ! -d "$pypath/$1" ] && \ [ -e "$pypath/$1" ] && [ ! -d "$pypath/$1" ] && \
[ -x "$pypath/$1" ] && printf "%s/%s\n" "$pypath" "$1" && \ [ -x "$pypath/$1" ] && printf "%s/%s\n" "$pypath" "$1" && \