-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvenv-run
53 lines (47 loc) · 2.37 KB
/
venv-run
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
#!/usr/bin/with-contenv bashio
# ==============================================================================
# Start Home Assistant service with venv
# ==============================================================================
# Compute VENV path from installed package list hash and architecture
pyenv_hash=$(uv pip list | sha256sum | cut -d " " -f 1 )
arch=$(uname -m)
venv_path="/venv/$pyenv_hash/$arch"
curr_pyenv_file="/venv/CURRENT_PYENV_HASH"
prev_pyenv_file="/venv/PREVIOUS_PYENV_HASH"
# Clear this env var that's equivalent to running --system on all uv calls. This can interfere with venv operation.
unset UV_SYSTEM_PYTHON
# Ensure /venv/ directory exists.
mkdir -p /venv/
# If there is a CURRENT_PYENV_HASH marker present, read it and move it to PREVIOUS if it doesn't match the current pyenv_hash.
if [[ -f $curr_pyenv_file ]]; then
IFS= read -r prev_pyenv_hash < $curr_pyenv_file
if [[ "$prev_pyenv_hash" != "$pyenv_hash" ]]; then
# If the PREVIOUS_PYENV_HASH marker is present, read it and delete the environments referenced in it.
if [[ -f $prev_pyenv_file ]]; then
# Cleanup the n-2 python virtualenv.
IFS= read -r prev_prev_pyenv_hash < $prev_pyenv_file
if [[ -d "/venv/$prev_prev_pyenv_hash" ]]; then
echo "Cleaning up old environment /venv/$prev_prev_pyenv_hash"
rm -r /venv/$prev_prev_pyenv_hash;
fi
fi
cp $curr_pyenv_file $prev_pyenv_file
fi
fi
# Create venv if required.
uv venv --system-site-packages --allow-existing --prompt "${pyenv_hash:0:8}-$arch" $venv_path
# Activate the venv
source $venv_path/bin/activate
ln -Tfs $venv_path /venv/current_venv
# Install uv into the venv if required. This is needed for home-assistant to properly invoke uv to install additional deps.
uv pip freeze --system | grep ^uv= | xargs uv pip install
# Log the current hash of the system python environment.
echo $pyenv_hash > $curr_pyenv_file
## EVERYTHING BELOW THIS LINE IS FROM https://github.com/home-assistant/core/blob/dev/rootfs/etc/services.d/home-assistant/run ##
cd /config || bashio::exit.nok "Can't find config folder!"
# Enable mimalloc for Home Assistant Core, unless disabled
if [[ -z "${DISABLE_JEMALLOC+x}" ]]; then
export LD_PRELOAD="/usr/local/lib/libjemalloc.so.2"
export MALLOC_CONF="background_thread:true,metadata_thp:auto,dirty_decay_ms:20000,muzzy_decay_ms:20000"
fi
exec python3 -m homeassistant --config /config