-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-python-environment.sh
More file actions
136 lines (115 loc) · 3.89 KB
/
setup-python-environment.sh
File metadata and controls
136 lines (115 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/sh -e
EXTENSION_NAME="python-environment"
BUILD_DIR="/tmp/${EXTENSION_NAME}"
# Add piCorePlayer .tcz extension dependencies without the .tcz suffix here (1 per line)
TCZ_DEPENDENCIES="
"
# Add Python dependencies here (1 per line)
PIP_DEPENDENCIES="
"
# Add absolute paths to your Python scripts to run on startup here (1 per line)
AUTOSTART_PYTHON_SCRIPTS="
"
############ END VARIABLES
### Abort, if extension is already installed
if [ -f "/etc/sysconfig/tcedir/optional/${EXTENSION_NAME}.tcz" ]; then
>&2 echo "Uninstall the ${EXTENSION_NAME} Extension and reboot, before installing it again"
>&2 echo "In Main Page > Extensions > Installed > select '${EXTENSION_NAME}.tcz' and press 'Delete'"
exit 1
fi
### Abort, if not enough free space
requiredSpaceInMB=100
availableSpaceInMB=$(/bin/df -m /dev/mmcblk0p2 | awk 'NR==2 { print $4 }')
if [ "$availableSpaceInMB" -le $requiredSpaceInMB ]; then
>&2 echo "Not enough free space"
>&2 echo "Increase SD-Card size: Main Page > Additional functions > Resize FS"
exit 1
fi
### Ensure fresh build dir exists
if [ -d $BUILD_DIR ]; then
>&2 echo "Reboot before running the script again."
exit 1
fi
mkdir -p $BUILD_DIR
### Decide for 64bit or 32bit installation
if [ "aarch64" = "$(uname -m)" ]; then
use32bit=false
else
use32bit=true
fi
# Installs a module from the piCorePlayer repository - if not already installed.
# Call like this: install_if_missing module_name
install_if_missing(){
if tce-status -u | grep -q "$1" ; then
pcp-load -il "$1"
elif ! tce-status -i | grep -q "$1" ; then
pcp-load -wil "$1"
fi
}
# Installs a module from the piCorePlayer repository, at least until the next reboot - if not already installed.
# Call like this: install_temporarily_if_missing module_name
install_temporarily_if_missing(){
if tce-status -u | grep -q "$1" ; then
pcp-load -il "$1"
elif ! tce-status -i | grep -q "$1" ; then
pcp-load -wil -t /tmp "$1" # Downloads to /tmp/optional and loads extensions temporarily
fi
}
set -v
### Install .tcz dependencies
install_if_missing python3.11
$use32bit && install_temporarily_if_missing python3.11-dev
for ext in $TCZ_DEPENDENCIES
do
echo "Installing $ext"
install_if_missing $ext
done
### Creating virtual environment
install_temporarily_if_missing git
sudo mkdir -m 775 "/usr/local/${EXTENSION_NAME}"
sudo chown root:staff "/usr/local/${EXTENSION_NAME}"
cd "/usr/local/${EXTENSION_NAME}"
python3 -m venv environment --system-site-packages
. environment/bin/activate # activate custom python environment
python3 -m pip install --upgrade pip
for package in $PIP_DEPENDENCIES
do
echo "Installing python package $package"
pip install $package
done
mkdir -p ${BUILD_DIR}/usr/local/
sudo mv "/usr/local/${EXTENSION_NAME}" ${BUILD_DIR}/usr/local/
### Creating startup script
mkdir -p ${BUILD_DIR}/usr/local/tce.installed/
cd ${BUILD_DIR}/usr/local/tce.installed/
set -- $AUTOSTART_PYTHON_SCRIPTS
if [ $# -gt 0 ]; then
echo "#!/bin/sh
sudo -u tc sh -c '
while [ ! -f /usr/local/bin/python3 ]; do sleep 1; done
source /usr/local/${EXTENSION_NAME}/environment/bin/activate
while [ ! -f $1 ]; do sleep 1; done" > "${EXTENSION_NAME}"
for script in "$@"
do
echo "python3 -u $script >> /tmp/$(basename $script).log 2>&1 &" >> "${EXTENSION_NAME}"
done
echo "' &" >> "${EXTENSION_NAME}"
chmod 775 "${EXTENSION_NAME}"
echo "Generated autostart script:"
cat "${EXTENSION_NAME}"
fi
### Creating tiny core extension
cd /tmp
install_temporarily_if_missing squashfs-tools
mksquashfs "${EXTENSION_NAME}" "${EXTENSION_NAME}.tcz"
mv -f "${EXTENSION_NAME}.tcz" /etc/sysconfig/tcedir/optional
dependencyFile="/etc/sysconfig/tcedir/optional/${EXTENSION_NAME}.tcz.dep"
echo "python3.11.tcz" > "$dependencyFile"
for ext in $TCZ_DEPENDENCIES
do
echo "${ext}.tcz" >> "$dependencyFile"
done
echo "${EXTENSION_NAME}.tcz" >> /etc/sysconfig/tcedir/onboot.lst
### Backup and reboot
pcp backup
pcp reboot