[tor-commits] [chutney/master] Rewrite Python detection in Chutney shell script

nickm at torproject.org nickm at torproject.org
Tue Feb 2 04:11:28 UTC 2016


commit 89a255017412447f915ab5f9ddbdb55fc1000786
Author: cypherpunks <cypherpunks at torproject.org>
Date:   Fri Jan 29 17:36:00 2016 +0100

    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.
---
 chutney | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 50 insertions(+), 12 deletions(-)

diff --git a/chutney b/chutney
index cbbc673..2076185 100755
--- a/chutney
+++ b/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 "${@}"



More information about the tor-commits mailing list