diff options
| author | Leah Rowe <leah@libreboot.org> | 2025-05-18 11:38:11 +0100 | 
|---|---|---|
| committer | Leah Rowe <leah@libreboot.org> | 2025-05-18 11:38:11 +0100 | 
| commit | d0bee6b4ebba914b0ac2f16689bc23c5f6cbf866 (patch) | |
| tree | 66114dc32dba0965e300e1aff06c74be829dee4b /include | |
| parent | 4aa69a7d1f02733d3776e7c18ec081ff8182a75e (diff) | |
init.sh: Set python version only on parent
Do it after the creation of xbmkpath.
This avoids performing an unnecessary check, since
PATH will have already been corrected for child
instances; Python will already be correct there.
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'include')
| -rw-r--r-- | include/init.sh | 96 | 
1 files changed, 48 insertions, 48 deletions
| diff --git a/include/init.sh b/include/init.sh index ccbad2b5..6c45b44d 100644 --- a/include/init.sh +++ b/include/init.sh @@ -30,8 +30,8 @@ xbmk_init()  	id -u 1>/dev/null 2>/dev/null || err "suid check failed (id -u)"  	[ "$(id -u)" != "0" ] || err "this command as root is not permitted" -	for init_cmd in set_pyver set_version set_env lock git_init \ -	    mkdirs create_pathdirs child_exec; do +	for init_cmd in set_version set_env lock git_init \ +	    mkdirs set_pyver create_pathdirs child_exec; do  		xbmk_$init_cmd "$@" || break  	done  } @@ -50,52 +50,6 @@ xbmkpkg()  	    printf "You need AUR packages: %s\n" "$aur_notice" 1>&2; :  } -xbmk_set_pyver() -{ -	pyv="import sys; print(sys.version_info[:])" -	python="python3" -	pybin python3 1>/dev/null || python="python" -	pyver="2" && [ "$python" = "python3" ] && pyver="3" -	pybin "$python" 1>/dev/null || pyver="" -	[ -z "$pyver" ] || "`pybin "$python"`" -c "$pyv" 1>/dev/null \ -	    2>/dev/null || err "Cannot detect host Python version." -	[ -n "$pyver" ] && \ -	    pyver="$("$(pybin "$python")" -c "$pyv" | awk '{print $1}')" && \ -	    pyver="${pyver#(}" && pyver="${pyver%,}" -	[ "${pyver%%.*}" = "3" ] || err "Bad python version (must by 3.x)"; : -} - -# 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 - -	# 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 -		pypath="$(findpath \ -		    "$(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" && \ -		    return 0 -	done && return 1 - -	# Defer to normal command -v if not a venv -	command -v "$1" 2>/dev/null || return 1 -} -  xbmk_set_version()  {  	[ ! -f ".version" ] || read -r version < ".version" || :; : @@ -193,6 +147,52 @@ xbmk_mkdirs()  	remkdir "$XBMK_CACHE/gnupath" "$XBMK_CACHE/xbmkpath"  } +xbmk_set_pyver() +{ +	pyv="import sys; print(sys.version_info[:])" +	python="python3" +	pybin python3 1>/dev/null || python="python" +	pyver="2" && [ "$python" = "python3" ] && pyver="3" +	pybin "$python" 1>/dev/null || pyver="" +	[ -z "$pyver" ] || "`pybin "$python"`" -c "$pyv" 1>/dev/null \ +	    2>/dev/null || err "Cannot detect host Python version." +	[ -n "$pyver" ] && \ +	    pyver="$("$(pybin "$python")" -c "$pyv" | awk '{print $1}')" && \ +	    pyver="${pyver#(}" && pyver="${pyver%,}" +	[ "${pyver%%.*}" = "3" ] || err "Bad python version (must by 3.x)"; : +} + +# 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 + +	# 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 +		pypath="$(findpath \ +		    "$(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" && \ +		    return 0 +	done && return 1 + +	# Defer to normal command -v if not a venv +	command -v "$1" 2>/dev/null || return 1 +} +  xbmk_create_pathdirs()  {  	( | 
