If PYTHON_COMMAND is set, use that. If PYTHON_COMMAND is not set, use first working of "python", "python3", "python2". If none of those work, search the path for python*[0-9], using the highest version number across x.y.z format.
Finally, set PYTHON3_ENABLE if selected python is python 3. Signed-off-by: Leif Lindholm <leif.lindh...@linaro.org> --- This is my somewhat overkill proposal as an alternative to Rebecca's 5/6. It is certainly more complex than that patch, and arguably as complex as the current upstream implementation, but the semantics and flow is more clear (to me, at least). An alternative version to *this* patch would be one that drops the FindHighestVersionedExecutable() function, and the if-statement that calls it. This would still leave us with a solution that would use (in order): * PYTHON_COMMAND * python * python3 * python2 and fail with an error if $PYTHON_COMMAND (if set externally) or none of the others could execute $PYTHON_COMMAND --version. / Leif edksetup.sh | 126 ++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 67 insertions(+), 59 deletions(-) diff --git a/edksetup.sh b/edksetup.sh index 06d2f041e635..59ce6582693a 100755 --- a/edksetup.sh +++ b/edksetup.sh @@ -1,6 +1,6 @@ # # Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR> -# Copyright (c) 2016, Linaro Ltd. All rights reserved.<BR> +# Copyright (c) 2016-2018, Linaro Ltd. All rights reserved.<BR> # SPDX-License-Identifier: BSD-2-Clause-Patent # # In *inux environment, the build tools's source is required and need to be compiled @@ -105,74 +105,82 @@ function SetupEnv() fi } -function SetupPython3() +function FindHighestVersionedExecutable() { - if [ $origin_version ];then - origin_version= - fi - for python in $(whereis python3) - do - python=$(echo $python | grep "[[:digit:]]$" || true) - python_version=${python##*python} - if [ -z "${python_version}" ] || (! command -v $python >/dev/null 2>&1);then - continue - fi - if [ -z $origin_version ];then - origin_version=$python_version - export PYTHON_COMMAND=$python - continue - fi - if [[ "$origin_version" < "$python_version" ]]; then - origin_version=$python_version - export PYTHON_COMMAND=$python - fi + BEST_EXECUTABLE= + BEST_MAJOR=0 + BEST_MINOR=0 + BEST_PATCH=0 + + IFS=":" + for dir in $PATH; do + for file in $dir/$1[0-9]*; do + # Filter out directories that don't have any $1* executables, + # and hence give back the wildcard path. + if [ -f $file ]; then + # Only care about names ending with a digit. + case $file in + *[0-9]) + ;; + *) + continue + ;; + esac + + EXECUTABLE=`basename $file` + VERSION=`echo $EXECUTABLE | sed 's/[^0-9.]//g'` + + MAJOR=`echo $VERSION | sed 's/\([0-9]*\)\.*.*/\1/'` + MINOR=`echo $VERSION | sed 's/[0-9]*\.*\([0-9]*\).*/\1/'` + PATCH=`echo $VERSION | sed 's/[0-9]*\.*[0-9]*\.*\([0-9]*\)/\1/'` + + if [ -n $MAJOR ] && [ $MAJOR -gt $BEST_MAJOR ]; then + BEST_EXECUTABLE=$EXECUTABLE + BEST_MAJOR=$MAJOR + BEST_MINOR=${MINOR:-0} + BEST_PATCH=${PATCH:-0} + elif [ -n $MINOR ] && [ $MAJOR -eq $BEST_MAJOR ] && [ $MINOR -gt $BEST_MINOR ]; then + BEST_EXECUTABLE=$EXECUTABLE + BEST_MINOR=$MINOR + BEST_PATCH=${PATCH:-0} + elif [ -n $PATCH ] && [ $MAJOR -eq $BEST_MAJOR ] && [ $MINOR -eq $BEST_MINOR ] && [ $PATCH -gt $BEST_PATCH ]; then + BEST_EXECUTABLE=$EXECUTABLE + BEST_PATCH=$PATCH + fi + fi + done done - return 0 + IFS=" " + PYTHON_COMMAND=$BEST_EXECUTABLE } function SetupPython() { - if [ $PYTHON_COMMAND ] && [ -z $PYTHON3_ENABLE ];then - if ( command -v $PYTHON_COMMAND >/dev/null 2>&1 );then - return 0 - else - echo $PYTHON_COMMAND Cannot be used to build or execute the python tools. - return 1 - fi - fi - - if [ $PYTHON3_ENABLE ] && [ $PYTHON3_ENABLE == TRUE ] - then - SetupPython3 - fi - - if [ $PYTHON3_ENABLE ] && [ $PYTHON3_ENABLE != TRUE ] - then - if [ $origin_version ];then - origin_version= - fi - for python in $(whereis python2) - do - python=$(echo $python | grep "[[:digit:]]$" || true) - python_version=${python##*python} - if [ -z "${python_version}" ] || (! command -v $python >/dev/null 2>&1);then - continue - fi - if [ -z $origin_version ] - then - origin_version=$python_version - export PYTHON_COMMAND=$python - continue - fi - if [[ "$origin_version" < "$python_version" ]]; then - origin_version=$python_version - export PYTHON_COMMAND=$python + if [ -z $PYTHON_COMMAND ]; then + for cmd in python python3 python2; do + if command -v $cmd >/dev/null 2>&1; then + PYTHON_COMMAND=$cmd + break fi done + + if [ -z $PYTHON_COMMAND ]; then + FindHighestVersionedExecutable python + fi + fi + + if command -v $PYTHON_COMMAND >/dev/null 2>&1; then + echo "PYTHON command ($PYTHON_COMMAND) works!" + PYTHON_MAJOR_VER=`$PYTHON_COMMAND --version 2>&1 | cut -d" " -f2 | cut -d. -f1` + if [ $PYTHON_MAJOR_VER -eq 3 ]; then + PYTHON3_ENABLE=TRUE + echo PYTHON is PYTHON3 + fi return 0 + else + echo $PYTHON_COMMAND Cannot be used to build or execute the python tools. + return 1 fi - - SetupPython3 } function SourceEnv() -- 2.11.0 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#43816): https://edk2.groups.io/g/devel/message/43816 Mute This Topic: https://groups.io/mt/32495132/21656 Group Owner: devel+ow...@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com] -=-=-=-=-=-=-=-=-=-=-=-