Browse Source

Rewrite Python detection in Chutney shell script

Fixes Chutney on systems which have python2 in their PATH but not
python.

Portability of the script has improved by avoiding `command` and instead
walking through PATH. This code is based on the program detection in
Autoconf.

Usability has improved by displaying a message when no compatible Python
version has been found which advises the user to check their Python
installation and PATH. When a compatible Python version has been found,
the script displays the Python version before initializing Chutney.

Fixes ticket 17631.
cypherpunks 8 years ago
parent
commit
89a2550174
1 changed files with 50 additions and 12 deletions
  1. 50 12
      chutney

+ 50 - 12
chutney

@@ -1,12 +1,50 @@
-#!/bin/sh
-
-export PYTHONPATH="`dirname $0`/lib:${PYTHONPATH}"
-# Use python2, python, python3 in that order
-[ -n "$PYTHON" ] || {
-    command -v python2 >/dev/null 2>&1 && PYTHON=python2 || \
-    command -v python >/dev/null 2>&1 && PYTHON=python # || \
-#   Not yet supported 
-#   command -v python3 >/dev/null 2>&1 && PYTHON=python3
-}
-# Use python2 if the checks that use "command" fail
-${PYTHON:=python2} -m chutney.TorNet "$@"
+#!/usr/bin/env sh
+
+set -o errexit
+set -o nounset
+
+export PYTHONPATH="$(dirname "${0}")/lib:${PYTHONPATH-}"
+
+binaries="python2 python"
+
+if ! test "${PYTHON+y}"
+then
+    saved_IFS="${IFS}"
+    for binary in ${binaries}
+    do
+        IFS=":"
+        for directory in ${PATH}
+        do
+            case "${directory}" in
+                "") directory="./"
+                    ;;
+                */)
+                    ;;
+                *) directory="${directory}/"
+                    ;;
+            esac
+            abs_path="${directory}${binary}"
+            if test -f "${abs_path}" && test -x "${abs_path}"
+            then
+                PYTHON="${abs_path}"
+                break
+            fi
+        done
+
+        if test "${PYTHON+y}"
+        then
+            break
+        fi
+    done
+    IFS="${saved_IFS}"
+fi
+
+if ! test "${PYTHON+y}"
+then
+    printf "No compatible Python version found.\n" >&2
+    printf "Is Python installed and in your PATH?\n" >&2
+    exit 1
+fi
+
+printf "Using %s\n" "$("${PYTHON}" --version 2>&1)"
+"${PYTHON}" -m chutney.TorNet "${@}"