Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement use of python virtual environment #1816

Merged
merged 7 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion Classes/WebServer/rest_PluginUpgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import os
import subprocess # nosec
from pathlib import Path
import sys

import distro
import z4d_certified_devices
Expand All @@ -35,10 +36,14 @@ def rest_plugin_upgrade(self, verb, data, parameters):
pluginFolder = Path(self.pluginParameters["HomeFolder"])
upgrade_script = str( pluginFolder / PLUGIN_UPGRADE_SCRIPT)

# Identify the current Python version
python_version = f"{sys.version_info.major}.{sys.version_info.minor}"
self.logging("Log", f"Current Python version: {python_version}")

self.logging("Log", "Plugin Upgrade starting: %s" %(upgrade_script))

process = subprocess.run(
upgrade_script ,
f"{upgrade_script} {python_version}",
cwd=self.pluginParameters["HomeFolder"],
universal_newlines=True,
text=True,
Expand Down
19 changes: 0 additions & 19 deletions Tools/big-clean-git.sh

This file was deleted.

6 changes: 0 additions & 6 deletions Tools/clean-git.sh

This file was deleted.

50 changes: 50 additions & 0 deletions Tools/cleaning-repository.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash

# This script performs a cleanup in the local repository to allow a git pull without issues.

# Ensure the script is run from the correct directory
if [ ! -d ".git" ]; then
echo "This script must be run from the root of the git repository."
exit 1
fi

# Warning message
echo "WARNING: This script will remove all local changes and reset the repository."
echo "If you have made any local updates, they will be removed."
read -p "Do you want to continue? (YES/no): " choice

if [ "$choice" != "YES" ]; then
echo "Operation cancelled."
exit 0
fi

echo ""
echo "Removing directories tracked by git, except Data, Conf, and OTAFirmware..."
# Remove directories tracked by git, except Data and Conf
for dir in $(git ls-tree -d --name-only HEAD); do
if [ "$dir" != "Data" ] && [ "$dir" != "Conf" ] && [ "$dir" != "OTAFirmware" ]; then
echo "Removing $dir..."
rm -rf "$dir"
fi
done

echo "Resetting the repository..."
git reset --hard

echo "Pulling the latest changes from the repository..."
git pull

echo ""
echo "Cleanup and update process completed."

echo ""
echo "Repository status:"
git status

echo ""
echo "Current branch:"
git branch --show-current

echo ""
echo "Latest commits:"
git log -n 5 --oneline
227 changes: 150 additions & 77 deletions Tools/plugin-auto-upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,85 +7,158 @@ exec 2>&1
echo "Starting Zigbee for Domoticz plugin Upgrade process."
echo "----------------------------------------------------"


if [ -z ${HOME} ]; then
export HOME=$(pwd)
fi

env
echo " "

/usr/bin/id
echo " "

/usr/bin/whoami
echo " "

PIP_OPTIONS="--no-input install -r requirements.txt --ignore-requires-python --upgrade"

if command -v lsb_release &> /dev/null; then
DISTRIB_ID=$(lsb_release -is)
DISTRIB_RELEASE=$(lsb_release -rs)
if [ "$DISTRIB_ID" = "Debian" ] && [ "$DISTRIB_RELEASE" = "12" ]; then
PIP_OPTIONS="$PIP_OPTIONS --break-system-packages"
# Function to set HOME environment variable if not set
set_home() {
if [ -z ${HOME} ]; then
export HOME=$(pwd)
fi
fi
echo "PIP Options: $PIP_OPTIONS"



echo "Current version : $(cat .hidden/VERSION)"
echo "latest git commit: $(git log --pretty=oneline -1)"
echo ""

echo "(1) git config --global --add safe.directory"
git config --global --add safe.directory $(pwd)
git config --global --add safe.directory $(pwd)/external/zigpy
git config --global --add safe.directory $(pwd)/external/zigpy-znp
git config --global --add safe.directory $(pwd)/external/zigpy-zigate
git config --global --add safe.directory $(pwd)/external/zigpy-deconz
git config --global --add safe.directory $(pwd)/external/bellows

echo " "
echo "(2) updating Zigbee for Domoticz plugin"
echo ""
echo "Setup submodule.recurse $(git config --add submodule.recurse true)"
echo ""
git pull --recurse-submodules
#git pull --recurse-submodules && git submodule update --recursive
ret="$?"
if [ "$ret" != "0" ] ; then
echo "ERROR while running command 'git pull --recurse-submodules'."
echo "Git Status: $(git status)"
exit -1
fi

echo " "
echo "(3) update python3 modules if needed"
echo ""
if [ "$(whoami)" == "root" ]; then
# Si l'utilisateur est root
python3 -m pip $PIP_OPTIONS
else
# Si l'utilisateur n'est pas root, utilisez sudo
sudo python3 -m pip $PIP_OPTIONS
fi
ret="$?"
if [ "$ret" != "0" ] ; then
echo "ERROR while running command 'sudo python3 -m pip --no-input install -r requirements.txt --ignore-requires-python --upgrade'."
echo "Is sudo available for this user without password ?"
exit -2
fi

echo " "
echo "(4) git config --global --unset safe.directory"
git config --global --unset-all safe.directory $(pwd)/external/bellows
git config --global --unset-all safe.directory $(pwd)/external/zigpy-deconz
git config --global --unset-all safe.directory $(pwd)/external/zigpy-zigate
git config --global --unset-all safe.directory $(pwd)/external/zigpy-znp
git config --global --unset-all safe.directory $(pwd)/external/zigpy
git config --global --unset-all safe.directory $(pwd)
}

# Function to print environment details
print_env_details() {
env
echo " "
/usr/bin/id
echo " "
/usr/bin/whoami
echo " "
}

# Function to set PIP options based on the distribution
set_pip_options() {
PIP_OPTIONS="--no-input install -r requirements.txt --ignore-requires-python --upgrade"
if command -v lsb_release &> /dev/null; then
DISTRIB_ID=$(lsb_release -is)
DISTRIB_RELEASE=$(lsb_release -rs)
if [ "$DISTRIB_ID" = "Debian" ] && [ "$DISTRIB_RELEASE" = "12" ]; then
PIP_OPTIONS="$PIP_OPTIONS --break-system-packages"
fi
fi
echo "PIP Options: $PIP_OPTIONS"
}

# Function to check if pip is installed in the virtual environment
check_pip_in_venv() {
if [ ! -f "$VENV_PATH/bin/$PYTHON_VERSION" ]; then
echo "pip is not installed in the virtual environment. Installing pip..."
install_pip
$PYTHON_VERSION -m venv $VENV_PATH
fi
}

# Function to install pip
install_pip() {
if command -v lsb_release &> /dev/null && [ "$(lsb_release -is)" = "Debian" ] || [ "$(lsb_release -is)" = "Ubuntu" ]; then
echo "We are expecting the user to properly install python3-pip package. if not yet done !!"
else
$PYTHON_VERSION -m ensurepip
$PYTHON_VERSION -m pip install --upgrade pip virtualenv -t $VENV_PATH
fi
}

# Function to activate virtual environment
activate_venv() {
echo "Using virtual environment at: $VENV_PATH"
source $VENV_PATH/bin/activate
}

# Function to check and activate virtual environment
check_and_activate_venv() {
if [ -n "$PYTHONPATH" ]; then
echo "PYTHONPATH is set to: $PYTHONPATH"
VENV_PATH=$(echo $PYTHONPATH | cut -d':' -f1)
if [ -d "$VENV_PATH/bin" ]; then
check_pip_in_venv
else
echo "Virtual environment path $VENV_PATH does not exist"
echo "pip is not installed in the virtual environment. Installing pip..."
install_pip
$PYTHON_VERSION -m venv $VENV_PATH
fi
VENV_ACTIVATED=true
activate_venv
else
echo "PYTHONPATH is not set"
VENV_ACTIVATED=false
fi
}

# Function to install python3-pip on Debian if necessary
install_pip_on_debian() {
if command -v lsb_release &> /dev/null; then
DISTRIB_ID=$(lsb_release -is)
DISTRIB_RELEASE=$(lsb_release -rs)
if [ "$DISTRIB_ID" = "Debian" ] && [ "$DISTRIB_RELEASE" = "12" ]; then
if ! command -v pip3 &> /dev/null; then
echo "pip3 is not installed. Installing python3-pip..."
sudo apt-get update
sudo apt-get install -y python3-pip
fi
fi
fi
}

# Function to update git configuration
update_git_config() {
echo "(1) git config --global --add safe.directory"
git config --global --add safe.directory $(pwd)
}

# Function to update python modules
update_python_modules() {
echo " "
echo "(2) update $PYTHON_VERSION modules if needed"
echo ""
if [ "$VENV_ACTIVATED" = true ]; then
$VENV_PATH/bin/python3 -m pip $PIP_OPTIONS -t $VENV_PATH
else
if [ "$(whoami)" == "root" ]; then
$PYTHON_VERSION -m pip $PIP_OPTIONS
else
sudo $PYTHON_VERSION -m pip $PIP_OPTIONS
fi
fi
ret="$?"
if [ "$ret" != "0" ] ; then
echo "ERROR while running command '$PYTHON_VERSION -m pip $PIP_OPTIONS'."
echo "Is sudo available for this user without password ?"
exit -2
fi
}

# Function to print current version and latest git commit
print_version_info() {
echo "Current version : $(cat .hidden/VERSION)"
echo "latest git commit: $(git log --pretty=oneline -1)"
echo ""
}

# Main script execution
PYTHON_VERSION="python${1:-3}"
PIP_VERSION="python${1:-3}"

set_home
print_env_details
set_pip_options
#install_pip_on_debian
check_and_activate_venv
print_version_info
update_git_config
update_python_modules

echo " "
echo "Plugin Upgrade process completed without errors."
exit 0

# Documentation:
# This script automates the upgrade process for the Zigbee for Domoticz plugin.
# It performs the following steps:
# 1. Sets the HOME environment variable if not already set.
# 2. Prints environment details for debugging purposes.
# 3. Sets PIP options based on the distribution.
# 4. Checks if PYTHONPATH is set and activates the virtual environment if available.
# 5. Installs python3-pip on Debian if necessary.
# 6. Updates the git configuration to add the current directory as a safe directory.
# 7. Updates Python modules using pip.
# 8. Prints the current version and latest git commit of the plugin.
# 9. Completes the upgrade process and exits.
Loading
Loading