Skip to content

Commit

Permalink
Merge pull request pimoroni#43 from jcoetzee/master
Browse files Browse the repository at this point in the history
Add support for python virtual environments
  • Loading branch information
Gadgetoid authored Feb 5, 2020
2 parents 99151b8 + 5aa2b7e commit e17de4e
Showing 1 changed file with 39 additions and 21 deletions.
60 changes: 39 additions & 21 deletions examples/install-service.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ POSITIONAL_ARGS=()
NOLED="no"
NOBUTTON="no"
BRIGHTNESS=255
PYTHON="python3"
PIP="pip3"

ON_THRESHOLD_SET=false
OFF_THRESHOLD_SET=false
Expand All @@ -19,19 +21,7 @@ OLD_HYSTERESIS=""

SERVICE_PATH=/etc/systemd/system/pimoroni-fanshim.service

USAGE="sudo ./install-service.sh --off-threshold <n> --on-threshold <n> --delay <n> --brightness <n> (--preempt) (--noled) (--nobutton)"

if ! ( type -P python3 > /dev/null ) ; then
printf "Fan SHIM controller requires Python 3\n"
printf "You should run: 'sudo apt install python3'\n"
exit 1
fi

if ! ( type -P pip3 > /dev/null ) ; then
printf "Fan SHIM controller requires Python 3 pip\n"
printf "You should run: 'sudo apt install python3-pip'\n"
exit 1
fi
USAGE="sudo ./install-service.sh --off-threshold <n> --on-threshold <n> --delay <n> --brightness <n> --venv <python_virtual_environment> (--preempt) (--noled) (--nobutton)"

while [[ $# -gt 0 ]]; do
K="$1"
Expand Down Expand Up @@ -95,6 +85,13 @@ while [[ $# -gt 0 ]]; do
shift
shift
;;
--venv)
VENV="$(realpath ${2%/})/bin"
PYTHON="$VENV/python3"
PIP="$VENV/pip3"
shift
shift
;;
*)
if [[ $1 == -* ]]; then
printf "Unrecognised option: $1\n";
Expand All @@ -106,6 +103,27 @@ while [[ $# -gt 0 ]]; do
esac
done

if ! ( type -P "$PYTHON" > /dev/null ) ; then
if [ "$PYTHON" == "python3" ]; then
printf "Fan SHIM controller requires Python 3\n"
printf "You should run: 'sudo apt install python3'\n"
else
printf "Cannot find virtual environment.\n"
printf "Set to base of virtual environment i.e. <venv>/bin/python3.\n"
fi
exit 1
fi

if ! ( type -P "$PIP" > /dev/null ) ; then
printf "Fan SHIM controller requires Python 3 pip\n"
if [ "$PIP" == "pip3" ]; then
printf "You should run: 'sudo apt install python3-pip'\n"
else
printf "Ensure that your virtual environment has pip3 installed.\n"
fi
exit 1
fi

set -- "${POSITIONAL_ARGS[@]}"

EXTRA_ARGS=""
Expand Down Expand Up @@ -172,16 +190,16 @@ After=multi-user.target
[Service]
Type=simple
WorkingDirectory=$DIR
ExecStart=$DIR/automatic.py --on-threshold $ON_THRESHOLD --off-threshold $OFF_THRESHOLD --delay $DELAY --brightness $BRIGHTNESS $EXTRA_ARGS
WorkingDirectory=$(pwd)
ExecStart=$PYTHON $(pwd)/automatic.py --on-threshold $ON_THRESHOLD --off-threshold $OFF_THRESHOLD --delay $DELAY --brightness $BRIGHTNESS $EXTRA_ARGS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

printf "Checking for rpi.gpio>=0.7.0 (for Pi 4 support)\n"
python3 - <<EOF
$PYTHON - <<EOF
import RPi.GPIO as GPIO
from pkg_resources import parse_version
import sys
Expand All @@ -191,31 +209,31 @@ EOF

if [ $? -ne 0 ]; then
printf "Installing rpi.gpio\n"
pip3 install --upgrade "rpi.gpio>=0.7.0"
$PIP install --upgrade "rpi.gpio>=0.7.0"
else
printf "rpi.gpio >= 0.7.0 already installed\n"
fi

printf "Checking for Fan SHIM\n"
python3 - > /dev/null 2>&1 <<EOF
$PYTHON - > /dev/null 2>&1 <<EOF
import fanshim
EOF

if [ $? -ne 0 ]; then
printf "Installing Fan SHIM\n"
pip3 install fanshim
$PIP install fanshim
else
printf "Fan SHIM already installed\n"
fi

printf "Checking for psutil\n"
python3 - > /dev/null 2>&1 <<EOF
$PYTHON - > /dev/null 2>&1 <<EOF
import psutil
EOF

if [ $? -ne 0 ]; then
printf "Installing psutil\n"
pip3 install psutil fanshim
$PIP install psutil fanshim
else
printf "psutil already installed\n"
fi
Expand Down

0 comments on commit e17de4e

Please sign in to comment.