public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Laszlo Ersek" <lersek@redhat.com>
To: Rebecca Cran <rebecca@bsdio.com>,
	devel@edk2.groups.io, bob.c.feng@intel.com, liming.gao@intel.com,
	leif.lindholm@linaro.org, michael.d.kinney@intel.com,
	afish@apple.com
Subject: Re: [PATCH 5/6] edksetup.sh: Simplify SetupPython3 and SetupPython functions.
Date: Tue, 16 Jul 2019 04:16:55 +0200	[thread overview]
Message-ID: <191b2bc0-62c5-e31b-a62a-ca92fa3253cf@redhat.com> (raw)
In-Reply-To: <20190715222516.53254-5-rebecca@bsdio.com>

On 07/16/19 00:25, Rebecca Cran wrote:
> On Linux, "whereis" matches python3, python3.7, as well as
> man pages, libs etc.  While on macOS it only matches the specified
> name, and so misses python3.7. Improve this by looping over
> potential version numbers and seeing if such a binary exists and
> can be executed.
> 
> Signed-off-by: Rebecca Cran <rebecca@bsdio.com>
> ---
>  edksetup.sh | 48 +++++++++---------------------------------------
>  1 file changed, 9 insertions(+), 39 deletions(-)
> 
> diff --git a/edksetup.sh b/edksetup.sh
> index 06d2f041e6..e2f116f8bc 100755
> --- a/edksetup.sh
> +++ b/edksetup.sh
> @@ -107,24 +107,10 @@ function SetupEnv()
>  
>  function SetupPython3()
>  {
> -  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
> +  for python in $(seq -f "python3.%g" 15 -1 1) python3; do
> +    if command -v $python >/dev/null 2>&1; then
> +      export PYTHON_COMMAND=$(which $python)
> +      break
>      fi
>    done
>    return 0
> @@ -146,27 +132,11 @@ function SetupPython()
>      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 [ -n "$PYTHON3_ENABLE" ] && [ "$PYTHON3_ENABLE" != "TRUE" ]; then
> +    for python in $(seq -f "python2.%g" 10 -1 1) python2; do
> +      if command -v $python >/dev/null 2>&1; then
> +        export PYTHON_COMMAND=$(which $python)
> +        break
>        fi
>      done
>      return 0
> 

Two things I'm not a fan of:
- seq for generating candidate version numbers (floating point)
- using "which" after having used "command -v"

For example, we could do:

for ((pyver=10; pyver>=1; --pyver)); do
  if python=$(command -v python2.$pyver); then
    export PYTHON_COMMAND=$python
    break
  fi
done

(It doesn't check for just "python2", but that's always a symlink
anyway, isn't it?)

The trick is that the exit status of just

  X=$(cmd)

is the exit status of "cmd":

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01

"If there is no command name, but the command contained a command
substitution, the command shall complete with the exit status of the
last command substitution performed"

That said, if you wouldn't like to rework the patch, I can give my R-b
as-is (please confirm).

Thanks
Laszlo

  reply	other threads:[~2019-07-16  2:16 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-15 22:25 [PATCH 1/6] edksetup.sh: Use bash variable $PWD instead of executing pwd command rebecca
2019-07-15 22:25 ` [PATCH 2/6] edksetup.sh: Use $SCRIPTNAME consistently instead of 'edksetup.sh' rebecca
2019-07-16  1:39   ` Laszlo Ersek
2019-07-15 22:25 ` [PATCH 3/6] edksetup.sh: when executing arithmetic commands, $ isn't needed rebecca
2019-07-16  1:40   ` Laszlo Ersek
2019-07-15 22:25 ` [PATCH 4/6] edksetup.sh: remove redundant -?, -h and --help in options parsing rebecca
2019-07-16  1:40   ` Laszlo Ersek
2019-07-15 22:25 ` [PATCH 5/6] edksetup.sh: Simplify SetupPython3 and SetupPython functions rebecca
2019-07-16  2:16   ` Laszlo Ersek [this message]
2019-07-16  2:27     ` rebecca
2019-07-16 10:38       ` Laszlo Ersek
2019-07-15 22:25 ` [PATCH 6/6] edksetup.sh: Add quotes and explicit checks in test statements rebecca
2019-07-16  1:47   ` Laszlo Ersek
2019-07-16  1:53     ` Laszlo Ersek
2019-07-16  2:20       ` [edk2-devel] " rebecca
2019-07-16 10:34         ` Laszlo Ersek
2019-07-16  1:37 ` [PATCH 1/6] edksetup.sh: Use bash variable $PWD instead of executing pwd command Laszlo Ersek
2019-07-16  2:13   ` rebecca
2019-07-16 10:32     ` Laszlo Ersek
2019-07-16 10:36       ` Laszlo Ersek
2019-07-16  9:16 ` Leif Lindholm
2019-07-16 11:40   ` Leif Lindholm

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=191b2bc0-62c5-e31b-a62a-ca92fa3253cf@redhat.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox