Skip to content

Commit 7f0c107

Browse files
committed
Add option to manage venv if it's not detected.
1 parent f36a1cb commit 7f0c107

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

install.sh

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ DATESTAMP=`date "+%Y-%m-%d-%H-%M-%S"`
55
CONFIG_BACKUP=false
66
APT_HAS_UPDATED=false
77
RESOURCES_TOP_DIR=$HOME/Pimoroni
8+
PY_VENV_DIR=$RESOURCES_TOP_DIR/venv
89
WD=`pwd`
910
USAGE="./install.sh (--unstable)"
1011
POSITIONAL_ARGS=()
@@ -13,14 +14,6 @@ UNSTABLE=false
1314
PYTHON="python"
1415

1516

16-
venv_check() {
17-
PYTHON_BIN=`which $PYTHON`
18-
if [[ $VIRTUAL_ENV == "" ]] || [[ $PYTHON_BIN != $VIRTUAL_ENV* ]]; then
19-
printf "This script should be run in a virtual Python environment.\n"
20-
exit 1
21-
fi
22-
}
23-
2417
user_check() {
2518
if [ $(id -u) -eq 0 ]; then
2619
printf "Script should not be run as root. Try './install.sh'\n"
@@ -62,6 +55,28 @@ warning() {
6255
echo -e "$(tput setaf 1)$1$(tput sgr0)"
6356
}
6457

58+
venv_check() {
59+
PYTHON_BIN=`which $PYTHON`
60+
if [[ $VIRTUAL_ENV == "" ]] || [[ $PYTHON_BIN != $VIRTUAL_ENV* ]]; then
61+
printf "This script should be run in a virtual Python environment.\n"
62+
if confirm "Would you like us to create one for you?"; then
63+
if [ ! -f $PY_VENV_DIR/bin/activate ]; then
64+
inform "Creating virtual Python environment in $PY_VENV_DIR, please wait...\n"
65+
mkdir -p $PY_VENV_DIR
66+
/usr/bin/python3 -m venv $PY_VENV_DIR --system-site-packages
67+
else
68+
inform "Found existing virtual Python environment in $PY_VENV_DIR\n"
69+
fi
70+
inform "Activating virtual Python environment in $PY_VENV_DIR..."
71+
inform "source $PY_VENV_DIR/bin/activate\n"
72+
source $PY_VENV_DIR/bin/activate
73+
74+
else
75+
exit 1
76+
fi
77+
fi
78+
}
79+
6580
function do_config_backup {
6681
if [ ! $CONFIG_BACKUP == true ]; then
6782
CONFIG_BACKUP=true
@@ -136,7 +151,7 @@ done
136151
user_check
137152
venv_check
138153

139-
if [ ! -f "$PYTHON" ]; then
154+
if [ ! -f `which $PYTHON` ]; then
140155
printf "Python path $PYTHON not found!\n"
141156
exit 1
142157
fi

0 commit comments

Comments
 (0)