diff options
author | Leah Rowe <leah@libreboot.org> | 2025-04-26 06:38:38 +0100 |
---|---|---|
committer | Leah Rowe <leah@libreboot.org> | 2025-04-26 06:43:49 +0100 |
commit | 0ab7c6ff9cf1eda98d005381fd99ce794be166c2 (patch) | |
tree | e84ea5328d1011f8e96e4543fb53080e6575ff67 | |
parent | 8edea026c58f06fa7af0ec9b20450641acccbefe (diff) |
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>
-rw-r--r-- | include/lib.sh | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/include/lib.sh b/include/lib.sh index 3ae51b3f..9450bb9b 100644 --- a/include/lib.sh +++ b/include/lib.sh @@ -109,6 +109,17 @@ pybin() 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 + # 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 [ -e "$pypath/$1" ] && [ ! -d "$pypath/$1" ] && \ [ -x "$pypath/$1" ] && printf "%s/%s\n" "$pypath" "$1" && \ |