Skip to content

Commit 735e029

Browse files
authored
Merge pull request #7 from pimoroni/patch-install-bullseye
New dedicated install script for Bullseye
2 parents 842b5e7 + 91c925b commit 735e029

File tree

2 files changed

+286
-23
lines changed

2 files changed

+286
-23
lines changed

install-legacy.sh

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
#!/bin/bash
2+
3+
CONFIG=/boot/config.txt
4+
DATESTAMP=`date "+%Y-%m-%d-%H-%M-%S"`
5+
CONFIG_BACKUP=false
6+
APT_HAS_UPDATED=false
7+
USER_HOME=/home/$SUDO_USER
8+
RESOURCES_TOP_DIR=$USER_HOME/Pimoroni
9+
WD=`pwd`
10+
USAGE="sudo ./install.sh (--unstable)"
11+
POSITIONAL_ARGS=()
12+
FORCE=false
13+
UNSTABLE=false
14+
CODENAME=`lsb_release -sc`
15+
16+
if [[ $CODENAME == "bullseye" ]]; then
17+
bash ./install.sh $@
18+
exit $?
19+
fi
20+
21+
user_check() {
22+
if [ $(id -u) -ne 0 ]; then
23+
printf "Script must be run as root. Try 'sudo ./install.sh'\n"
24+
exit 1
25+
fi
26+
}
27+
28+
confirm() {
29+
if $FORCE; then
30+
true
31+
else
32+
read -r -p "$1 [y/N] " response < /dev/tty
33+
if [[ $response =~ ^(yes|y|Y)$ ]]; then
34+
true
35+
else
36+
false
37+
fi
38+
fi
39+
}
40+
41+
prompt() {
42+
read -r -p "$1 [y/N] " response < /dev/tty
43+
if [[ $response =~ ^(yes|y|Y)$ ]]; then
44+
true
45+
else
46+
false
47+
fi
48+
}
49+
50+
success() {
51+
echo -e "$(tput setaf 2)$1$(tput sgr0)"
52+
}
53+
54+
inform() {
55+
echo -e "$(tput setaf 6)$1$(tput sgr0)"
56+
}
57+
58+
warning() {
59+
echo -e "$(tput setaf 1)$1$(tput sgr0)"
60+
}
61+
62+
function do_config_backup {
63+
if [ ! $CONFIG_BACKUP == true ]; then
64+
CONFIG_BACKUP=true
65+
FILENAME="config.preinstall-$LIBRARY_NAME-$DATESTAMP.txt"
66+
inform "Backing up $CONFIG to /boot/$FILENAME\n"
67+
cp $CONFIG /boot/$FILENAME
68+
mkdir -p $RESOURCES_TOP_DIR/config-backups/
69+
cp $CONFIG $RESOURCES_TOP_DIR/config-backups/$FILENAME
70+
if [ -f "$UNINSTALLER" ]; then
71+
echo "cp $RESOURCES_TOP_DIR/config-backups/$FILENAME $CONFIG" >> $UNINSTALLER
72+
fi
73+
fi
74+
}
75+
76+
function apt_pkg_install {
77+
PACKAGES=()
78+
PACKAGES_IN=("$@")
79+
for ((i = 0; i < ${#PACKAGES_IN[@]}; i++)); do
80+
PACKAGE="${PACKAGES_IN[$i]}"
81+
if [ "$PACKAGE" == "" ]; then continue; fi
82+
printf "Checking for $PACKAGE\n"
83+
dpkg -L $PACKAGE > /dev/null 2>&1
84+
if [ "$?" == "1" ]; then
85+
PACKAGES+=("$PACKAGE")
86+
fi
87+
done
88+
PACKAGES="${PACKAGES[@]}"
89+
if ! [ "$PACKAGES" == "" ]; then
90+
echo "Installing missing packages: $PACKAGES"
91+
if [ ! $APT_HAS_UPDATED ]; then
92+
apt update
93+
APT_HAS_UPDATED=true
94+
fi
95+
apt install -y $PACKAGES
96+
if [ -f "$UNINSTALLER" ]; then
97+
echo "apt uninstall -y $PACKAGES"
98+
fi
99+
fi
100+
}
101+
102+
while [[ $# -gt 0 ]]; do
103+
K="$1"
104+
case $K in
105+
-u|--unstable)
106+
UNSTABLE=true
107+
shift
108+
;;
109+
-f|--force)
110+
FORCE=true
111+
shift
112+
;;
113+
*)
114+
if [[ $1 == -* ]]; then
115+
printf "Unrecognised option: $1\n";
116+
printf "Usage: $USAGE\n";
117+
exit 1
118+
fi
119+
POSITIONAL_ARGS+=("$1")
120+
shift
121+
esac
122+
done
123+
124+
user_check
125+
126+
apt_pkg_install python-configparser
127+
128+
CONFIG_VARS=`python - <<EOF
129+
from configparser import ConfigParser
130+
c = ConfigParser()
131+
c.read('library/setup.cfg')
132+
p = dict(c['pimoroni'])
133+
# Convert multi-line config entries into bash arrays
134+
for k in p.keys():
135+
fmt = '"{}"'
136+
if '\n' in p[k]:
137+
p[k] = "'\n\t'".join(p[k].split('\n')[1:])
138+
fmt = "('{}')"
139+
p[k] = fmt.format(p[k])
140+
print("""
141+
LIBRARY_NAME="{name}"
142+
LIBRARY_VERSION="{version}"
143+
""".format(**c['metadata']))
144+
print("""
145+
PY3_DEPS={py3deps}
146+
PY2_DEPS={py2deps}
147+
SETUP_CMDS={commands}
148+
CONFIG_TXT={configtxt}
149+
""".format(**p))
150+
EOF`
151+
152+
if [ $? -ne 0 ]; then
153+
warning "Error parsing configuration...\n"
154+
exit 1
155+
fi
156+
157+
eval $CONFIG_VARS
158+
159+
RESOURCES_DIR=$RESOURCES_TOP_DIR/$LIBRARY_NAME
160+
UNINSTALLER=$RESOURCES_DIR/uninstall.sh
161+
162+
mkdir -p $RESOURCES_DIR
163+
164+
cat << EOF > $UNINSTALLER
165+
printf "It's recommended you run these steps manually.\n"
166+
printf "If you want to run the full script, open it in\n"
167+
printf "an editor and remove 'exit 1' from below.\n"
168+
exit 1
169+
EOF
170+
171+
printf "$LIBRARY_NAME $LIBRARY_VERSION Python Library: Installer\n\n"
172+
173+
if $UNSTABLE; then
174+
warning "Installing unstable library from source.\n\n"
175+
else
176+
printf "Installing stable library from pypi.\n\n"
177+
fi
178+
179+
cd library
180+
181+
printf "Installing for Python 2..\n"
182+
apt_pkg_install "${PY2_DEPS[@]}"
183+
if $UNSTABLE; then
184+
python setup.py install > /dev/null
185+
else
186+
pip install --upgrade $LIBRARY_NAME
187+
fi
188+
if [ $? -eq 0 ]; then
189+
success "Done!\n"
190+
echo "pip uninstall $LIBRARY_NAME" >> $UNINSTALLER
191+
fi
192+
193+
if [ -f "/usr/bin/python3" ]; then
194+
printf "Installing for Python 3..\n"
195+
apt_pkg_install "${PY3_DEPS[@]}"
196+
if $UNSTABLE; then
197+
python3 setup.py install > /dev/null
198+
else
199+
pip3 install --upgrade $LIBRARY_NAME
200+
fi
201+
if [ $? -eq 0 ]; then
202+
success "Done!\n"
203+
echo "pip3 uninstall $LIBRARY_NAME" >> $UNINSTALLER
204+
fi
205+
fi
206+
207+
cd $WD
208+
209+
for ((i = 0; i < ${#SETUP_CMDS[@]}; i++)); do
210+
CMD="${SETUP_CMDS[$i]}"
211+
# Attempt to catch anything that touches /boot/config.txt and trigger a backup
212+
if [[ "$CMD" == *"raspi-config"* ]] || [[ "$CMD" == *"$CONFIG"* ]] || [[ "$CMD" == *"\$CONFIG"* ]]; then
213+
do_config_backup
214+
fi
215+
eval $CMD
216+
done
217+
218+
for ((i = 0; i < ${#CONFIG_TXT[@]}; i++)); do
219+
CONFIG_LINE="${CONFIG_TXT[$i]}"
220+
if ! [ "$CONFIG_LINE" == "" ]; then
221+
do_config_backup
222+
inform "Adding $CONFIG_LINE to $CONFIG\n"
223+
sed -i "s/^#$CONFIG_LINE/$CONFIG_LINE/" $CONFIG
224+
if ! grep -q "^$CONFIG_LINE" $CONFIG; then
225+
printf "$CONFIG_LINE\n" >> $CONFIG
226+
fi
227+
fi
228+
done
229+
230+
if [ -d "examples" ]; then
231+
if confirm "Would you like to copy examples to $RESOURCES_DIR?"; then
232+
inform "Copying examples to $RESOURCES_DIR"
233+
cp -r examples/ $RESOURCES_DIR
234+
echo "rm -r $RESOURCES_DIR" >> $UNINSTALLER
235+
success "Done!"
236+
fi
237+
fi
238+
239+
printf "\n"
240+
241+
if [ -f "/usr/bin/pydoc" ]; then
242+
printf "Generating documentation.\n"
243+
pydoc -w $LIBRARY_NAME > /dev/null
244+
if [ -f "$LIBRARY_NAME.html" ]; then
245+
cp $LIBRARY_NAME.html $RESOURCES_DIR/docs.html
246+
rm -f $LIBRARY_NAME.html
247+
inform "Documentation saved to $RESOURCES_DIR/docs.html"
248+
success "Done!"
249+
else
250+
warning "Error: Failed to generate documentation."
251+
fi
252+
fi
253+
254+
success "\nAll done!"
255+
inform "If this is your first time installing you should reboot for hardware changes to take effect.\n"
256+
inform "Find uninstall steps in $UNINSTALLER\n"

install.sh

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/bin/bash
2-
32
CONFIG=/boot/config.txt
43
DATESTAMP=`date "+%Y-%m-%d-%H-%M-%S"`
54
CONFIG_BACKUP=false
@@ -9,7 +8,10 @@ RESOURCES_TOP_DIR=$USER_HOME/Pimoroni
98
WD=`pwd`
109
USAGE="sudo ./install.sh (--unstable)"
1110
POSITIONAL_ARGS=()
11+
FORCE=false
1212
UNSTABLE=false
13+
PYTHON="/usr/bin/python3"
14+
1315

1416
user_check() {
1517
if [ $(id -u) -ne 0 ]; then
@@ -19,7 +21,7 @@ user_check() {
1921
}
2022

2123
confirm() {
22-
if [ "$FORCE" == '-y' ]; then
24+
if $FORCE; then
2325
true
2426
else
2527
read -r -p "$1 [y/N] " response < /dev/tty
@@ -99,6 +101,15 @@ while [[ $# -gt 0 ]]; do
99101
UNSTABLE=true
100102
shift
101103
;;
104+
-f|--force)
105+
FORCE=true
106+
shift
107+
;;
108+
-p|--python)
109+
PYTHON=$2
110+
shift
111+
shift
112+
;;
102113
*)
103114
if [[ $1 == -* ]]; then
104115
printf "Unrecognised option: $1\n";
@@ -110,11 +121,21 @@ while [[ $# -gt 0 ]]; do
110121
esac
111122
done
112123

124+
distro_check
113125
user_check
114126

115-
apt_pkg_install python-configparser
127+
if [ ! -f "$PYTHON" ]; then
128+
printf "Python path $PYTHON not found!\n"
129+
exit 1
130+
fi
116131

117-
CONFIG_VARS=`python - <<EOF
132+
PYTHON_VER=`$PYTHON --version`
133+
134+
inform "Installing. Please wait..."
135+
136+
$PYTHON -m pip install --upgrade configparser
137+
138+
CONFIG_VARS=`$PYTHON - <<EOF
118139
from configparser import ConfigParser
119140
c = ConfigParser()
120141
c.read('library/setup.cfg')
@@ -167,30 +188,16 @@ fi
167188
168189
cd library
169190
170-
printf "Installing for Python 2..\n"
171-
apt_pkg_install "${PY2_DEPS[@]}"
191+
printf "Installing for $PYTHON_VER...\n"
192+
apt_pkg_install "${PY3_DEPS[@]}"
172193
if $UNSTABLE; then
173-
python setup.py install > /dev/null
194+
$PYTHON setup.py install > /dev/null
174195
else
175-
pip install --upgrade $LIBRARY_NAME
196+
$PYTHON -m pip install --upgrade $LIBRARY_NAME
176197
fi
177198
if [ $? -eq 0 ]; then
178199
success "Done!\n"
179-
echo "pip uninstall $LIBRARY_NAME" >> $UNINSTALLER
180-
fi
181-
182-
if [ -f "/usr/bin/python3" ]; then
183-
printf "Installing for Python 3..\n"
184-
apt_pkg_install "${PY3_DEPS[@]}"
185-
if $UNSTABLE; then
186-
python3 setup.py install > /dev/null
187-
else
188-
pip3 install --upgrade $LIBRARY_NAME
189-
fi
190-
if [ $? -eq 0 ]; then
191-
success "Done!\n"
192-
echo "pip3 uninstall $LIBRARY_NAME" >> $UNINSTALLER
193-
fi
200+
echo "$PYTHON -m pip uninstall $LIBRARY_NAME" >> $UNINSTALLER
194201
fi
195202
196203
cd $WD

0 commit comments

Comments
 (0)