Skip to content
This repository has been archived by the owner on Oct 30, 2019. It is now read-only.

New upgrade script for Python. #131

Merged
merged 13 commits into from
Mar 8, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/python.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Description
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add information that this is not recommended.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better now? :)

This script will upgrade Python to the latest stable version.
It will also create a new virtual environment to be used for Home Assistant.

## Upgrade
```
$ sudo hassbian-config upgrade python
```

## Additional info
If you have installed additional components directly to `/srv/homeassistant/` like `libcec` thoese will need to be reinstalled.

***
This script was originally contributed by [@Ludeeus](https://github.com/ludeeus).
With inspiration from: [blog.ceard.tech](https://blog.ceard.tech/2017/12/upgrading-python-virtual-environment.html)
88 changes: 88 additions & 0 deletions package/opt/hassbian/suites/python.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/bin/bash

function python-show-short-info {
echo "Upgrades python3 and virtual environment to the newest version."
}

function python-show-long-info {
echo "Upgrades python3 and virtual environment to the newest version."
}

function python-show-copyright-info {
echo "This script was originally contributed by Ludeeus <https://github.com/ludeeus>."
}

function python-upgrade-package {
python-show-short-info
python-show-copyright-info
PYTHONVERSION=$(curl -s https://www.python.org/downloads/source/ | grep "Latest Python 3 Release" | cut -d "<" -f 3 | awk -F ' ' '{print $NF}')

echo "Installing Python $PYTHONVERSION"
apt-get -y update
apt-get install -y build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev
apt-get install libtcmalloc-minimal4
export LD_PRELOAD="/usr/lib/libtcmalloc_minimal.so.4"
cd /tmp

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

wget https://www.python.org/ftp/python/"$PYTHONVERSION"/Python-"$PYTHONVERSION".tar.xz
tar xf Python-"$PYTHONVERSION".tar.xz
cd Python-"$PYTHONVERSION"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

./configure --enable-optimizations
make
apt -y autoremove
rm -r /tmp/Python-"$PYTHONVERSION"
rm /tmp/Python-"$PYTHONVERSION".tar.xz
echo "Done"

echo "Stopping Home Assistant."
systemctl stop home-assistant@homeassistant.service

echo "Backing up previous virutal enviorment."
mv /srv/homeassistant /srv/homeassistant_old
sudo -u homeassistant -H /bin/bash << EOF
source /srv/homeassistant/bin/activate
pip3 freeze —local > /tmp/requirements.txt
deactivate
EOF

echo "Creating new virutal enviorment using Python $PYTHONVERSION"
python3.6 -m venv /srv/homeassistant
sudo chown homeassistant:homeassistant /srv/homeassistant
sudo mv /srv/homeassistant_old/hassbian /srv/homeassistant/hassbian
sudo apt-get install python3-pip python3-dev
sudo pip3 install --upgrade virtualenv
sudo -u homeassistant -H /bin/bash << EOF
source /srv/homeassistant/bin/activate
pip3 install -r /tmp/requirements.txt
pip3 install --upgrade homeassistant
deactivate
EOF
mv /home/homeassistant/.homeassistant/deps /home/homeassistant/.homeassistant/deps_old

echo "Starting Home Assistant."
sudo curl -o /etc/systemd/system/home-assistant@homeassistant.service https://raw.githubusercontent.com/home-assistant/hassbian-scripts/dev/package/etc/systemd/system/home-assistant%40homeassistant.service
sudo systemctl enable home-assistant@homeassistant.service
sudo systemctl daemon-reload
sudo systemctl start home-assistant@homeassistant.service

echo "Checking the installation..."
validation=$(sudo -u homeassistant -H /bin/bash << EOF | grep Version | awk '{print $2}'
source /srv/homeassistant/bin/activate
python -V
EOF
)
if [ "$validation" == "Python $PYTHONVERSION" ]; then
echo
echo -e "\\e[3231mUpgrade done..\\e[0m"
echo
else
echo
echo -e "\\e[31mUpgrade failed..."
echo -e "\\e[31mReverting..."
echo -e "\\e[0mIf you have issues with this script, please say something in the #devs_hassbian channel on Discord."
echo
return 1
fi
return 0
}

[[ "$_" == "$0" ]] && echo "hassbian-config helper script; do not run directly, use hassbian-config instead"