From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=pass header.i=@linaro.org header.s=google header.b=ZdtypGrW; spf=pass (domain: linaro.org, ip: 209.85.128.68, mailfrom: leif.lindholm@linaro.org) Received: from mail-wm1-f68.google.com (mail-wm1-f68.google.com [209.85.128.68]) by groups.io with SMTP; Tue, 16 Jul 2019 12:08:00 -0700 Received: by mail-wm1-f68.google.com with SMTP id l2so19740397wmg.0 for ; Tue, 16 Jul 2019 12:08:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=/GGA/WdYJaslqo7O+rWKm3vYDtUCPqw4nfIN23p3F9s=; b=ZdtypGrW6NHXWTg0+TGCdqMzf7gaoInt8Z3OrCskAq2PORBkgY0+jF0M4oekwGvi1c 1bk046KmtgfQnhDyQp0OHxl8L3FxwSM3CUz1vMTsOhIKlBlIFtPbLotYGg9AnNzqdBMQ KkQrKg6eeOHSU+ixcalc8KxF0N6aET0TghuPPa5Y1szC8GreiojxmwI0hVxsxjGAhtci bDbK+nugIyXNIbZgotATEvcoq1iMQN/H2fYjzsnNJpcK6BKrcZi70IvySLlGnzLBxdDp IX9MYV15AbeKEfbIwkF/k5W9ePbxm22UuV9ev2tETJfMit2qN8gNPJ7TLKvZSvj4L3Kq kGZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=/GGA/WdYJaslqo7O+rWKm3vYDtUCPqw4nfIN23p3F9s=; b=gEsixDQs+oCvllhXBVsOpH5oekIjLTRjqKx6SP9DqCinm6Tlk0zt7UP4jXOc/gC6g+ SETKt0Ri5aJ6z3EE8M19YTBXA8g4m4qSyfthjwqkWmfZt9Qd96G2Itt1+t7BG/EOVlz4 gMd/9tFf80Oe8ssql2aVohyHGd17WgXnybfbgYE1Me5Pws776BNtXgt1wlGTCNa1gVrJ StHt4zcmvQsa6huUotzA7nPGF38QdNZ9TIbeNaN0P48B7OXwQmREi3UiB+biGxAI37C9 LvlrFiTIwNMEg11uBBA0rtem2MSElIbUxAfFZc74l3zv+lE3a1XJB/6PU7LkWkhoG3Ov Q3Jg== X-Gm-Message-State: APjAAAVTev22YGsSC3YWDrWLGVuVcGLhFbx72Vpt3b0/DFg+Yh9ayFmL DndGPCS8HIT3sghMaFFMNnNNWKyNHzc= X-Google-Smtp-Source: APXvYqwwI9EIkkt/5h0ktKfz17vx7krBrfwb+JY2v6rnsLE7wjlkuIKk/arWkMyHAjTC2mR55tYKWQ== X-Received: by 2002:a7b:c104:: with SMTP id w4mr33173863wmi.42.1563304078592; Tue, 16 Jul 2019 12:07:58 -0700 (PDT) Return-Path: Received: from urvogel.hemma.eciton.net (cpc92302-cmbg19-2-0-cust304.5-4.cable.virginm.net. [82.1.209.49]) by smtp.gmail.com with ESMTPSA id o185sm20585646wmo.45.2019.07.16.12.07.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Jul 2019 12:07:57 -0700 (PDT) From: "Leif Lindholm" To: devel@edk2.groups.io Cc: Rebecca Cran , lersek@redhat.com, bob.c.feng@intel.com, liming.gao@intel.com, leif.lindholm@linaro.org, michael.d.kinney@intel.com, afish@apple.com Subject: [PATCH 1/1] edksetup.sh: rework python executable scanning Date: Tue, 16 Jul 2019 20:07:54 +0100 Message-Id: <20190716190754.25412-1-leif.lindholm@linaro.org> X-Mailer: git-send-email 2.11.0 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 --- 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.
-# Copyright (c) 2016, Linaro Ltd. All rights reserved.
+# Copyright (c) 2016-2018, Linaro Ltd. All rights reserved.
# 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