From dbbb9f823b538cfdff9ec1b65c815a7c28781d8b Mon Sep 17 00:00:00 2001
From: David Trattnig <david.trattnig@o94.at>
Date: Wed, 23 Sep 2020 19:19:26 +0200
Subject: [PATCH] Dynamically use a compatible Python version. #17

---
 engine-core.py |  5 ++++-
 guru.py        |  5 ++++-
 install.sh     | 11 ++++++++++-
 run.sh         | 20 +++++++++++++++-----
 test/tests.py  |  5 ++++-
 5 files changed, 37 insertions(+), 9 deletions(-)

diff --git a/engine-core.py b/engine-core.py
index 3d0b50bf..23b74141 100755
--- a/engine-core.py
+++ b/engine-core.py
@@ -1,4 +1,7 @@
-#!/usr/bin/env python3.7
+#!/bin/sh
+''''which python3.8 >/dev/null 2>&1 && exec python3.8 "$0" "$@"     # '''
+''''which python3.7  >/dev/null 2>&1 && exec python3.7  "$0" "$@"   # '''
+''''exec echo "Error: Snaaakey Python, where are you?"                # '''
 
 #
 # Aura Engine (https://gitlab.servus.at/aura/engine)
diff --git a/guru.py b/guru.py
index 19cb9d9a..f0b8e981 100755
--- a/guru.py
+++ b/guru.py
@@ -1,4 +1,7 @@
-#!/usr/bin/env python3.7
+#!/bin/sh
+''''which python3.8 >/dev/null 2>&1 && exec python3.8 "$0" "$@"     # '''
+''''which python3.7  >/dev/null 2>&1 && exec python3.7  "$0" "$@"   # '''
+''''exec echo "Error: Snaaakey Python, where are you?"                # '''
 
 #
 # Aura Engine (https://gitlab.servus.at/aura/engine)
diff --git a/install.sh b/install.sh
index a7718e29..d654327b 100755
--- a/install.sh
+++ b/install.sh
@@ -13,6 +13,15 @@ if [ $mode == "prod" ]; then
     echo "[Installing AURA ENGINE for Production]"
 fi
 
+# Find the correct Python version (3.7 or 3.8)
+
+if hash python3.8 2>/dev/null; then
+	PYTHON_EXEC="python3.8"
+	echo "[ Using Python 3.8 ]"
+else
+	PYTHON_EXEC="python3.7"
+	echo "[ Using Python 3.7 ]"
+fi
 
 # Development and Production
 
@@ -20,7 +29,7 @@ echo "Installing OPAM Packages ..."
 bash script/install-opam-packages.sh
 
 echo "Installing Python Requirements ..."
-python3.7 $(which pip3) install -r requirements.txt
+$PYTHON_EXEC $(which pip3) install -r requirements.txt
 
 
 # Development 
diff --git a/run.sh b/run.sh
index 9dfb4ffa..94277539 100755
--- a/run.sh
+++ b/run.sh
@@ -32,6 +32,16 @@ echo "[ Run mode=$mode ]"
 echo "[ Docker=$docker ]"
 
 
+# Find the correct Python version (3.7 or 3.8)
+
+if hash python3.8 2>/dev/null; then
+	PYTHON_EXEC="python3.8"
+	echo "[ Using Python 3.8 ]"
+else
+	PYTHON_EXEC="python3.7"
+	echo "[ Using Python 3.7 ]"
+fi
+
 
 # +++ DEFAULT COMMANDS +++ #
 
@@ -48,26 +58,26 @@ if [[ $docker == "false" ]]; then
 
 	if [[ $mode == "engine" ]]; then
 		eval $(opam env)
-		/usr/bin/env python3.7 engine-core.py
+		/usr/bin/env $PYTHON_EXEC engine-core.py
 	fi
 
 	### Runs Engine Core only ###
 
 	if [[ $mode == "core" ]]; then
-		/usr/bin/env python3.7 engine-core.py --without-lqs
+		/usr/bin/env $PYTHON_EXEC engine-core.py --without-lqs
 	fi
 
 	### Runs Liquidsoap only ###
 
 	if [[ $mode == "lqs" ]]; then
-		lqs=$(/usr/bin/env python3.7 engine-core.py --get-lqs-command)
+		lqs=$(/usr/bin/env $PYTHON_EXEC engine-core.py --get-lqs-command)
 		eval "$lqs"
 	fi
 
 	### CAUTION: This deletes everything in your database ###
 
 	if [[ $mode == "recreate-database" ]]; then
-		/usr/bin/env python3.7 engine-core.py --recreate-database
+		/usr/bin/env $PYTHON_EXEC engine-core.py --recreate-database
 	fi
 
 fi
@@ -87,7 +97,7 @@ if [[ $docker == "true" ]]; then
 			--rm -d \
 			-u $UID:$GID \
 			-v "$BASE_D":/srv \
-			-v "$BASE_D/audio/source":/home/opam/audio/source:ro \
+			-v "$BASE_D/audio/source":/var/audio/source:ro \
 			-v "$BASE_D/configuration/docker":/etc/aura \
 			-v "/dev/snd":/dev/snd \
 			--privileged \
diff --git a/test/tests.py b/test/tests.py
index 51bbab63..b99c4e1b 100755
--- a/test/tests.py
+++ b/test/tests.py
@@ -1,4 +1,7 @@
-#!/usr/bin/python3.7
+#!/bin/sh
+''''which python3.8 >/dev/null 2>&1 && exec python3.8 "$0" "$@"     # '''
+''''which python3.7  >/dev/null 2>&1 && exec python3.7  "$0" "$@"   # '''
+''''exec echo "Error: Snaaakey Python, where are you?"                # '''
 
 #
 # Aura Engine (https://gitlab.servus.at/aura/engine)
-- 
GitLab