public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH 1/1] edksetup.sh: rework python executable scanning
@ 2019-07-16 19:07 Leif Lindholm
  2019-07-16 20:10 ` [edk2-devel] " rebecca
  2019-07-16 20:49 ` Laszlo Ersek
  0 siblings, 2 replies; 16+ messages in thread
From: Leif Lindholm @ 2019-07-16 19:07 UTC (permalink / raw)
  To: devel
  Cc: Rebecca Cran, lersek, bob.c.feng, liming.gao, leif.lindholm,
	michael.d.kinney, afish

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.lindholm@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


^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2019-07-24 15:29 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-07-16 19:07 [PATCH 1/1] edksetup.sh: rework python executable scanning Leif Lindholm
2019-07-16 20:10 ` [edk2-devel] " rebecca
2019-07-17 14:04   ` Leif Lindholm
2019-07-16 20:49 ` Laszlo Ersek
2019-07-16 22:04   ` Leif Lindholm
2019-07-17  3:23     ` Liming Gao
2019-07-17 10:21       ` Laszlo Ersek
2019-07-17 14:32         ` Liming Gao
2019-07-17 19:43           ` Laszlo Ersek
2019-07-17 22:37       ` Leif Lindholm
2019-07-18 16:48         ` [edk2-devel] " Liming Gao
2019-07-18 17:55           ` Leif Lindholm
2019-07-19 13:07             ` Liming Gao
2019-07-23  9:44               ` Leif Lindholm
2019-07-24 15:29                 ` Liming Gao
2019-07-17 10:13     ` Laszlo Ersek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox