Modernize Python package installation using virtual environments for Debian 12+

Co-authored-by: ShaYmez <76499782+ShaYmez@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2025-12-13 22:06:53 +00:00
parent 3a7c783d31
commit 36c5e38e35

View File

@ -76,7 +76,12 @@ install_docker_and_dependencies() {
# Install base dependencies # Install base dependencies
apt-get update apt-get update
apt-get install -y $DEP # For Debian 12+, add python3-venv to dependencies (PEP 668 compliance)
if [ $version -ge 12 ]; then
apt-get install -y $DEP python3-venv
else
apt-get install -y $DEP
fi
sleep 2 sleep 2
# Remove old Docker versions if present # Remove old Docker versions if present
@ -234,20 +239,18 @@ sleep 2
pip_install() { pip_install() {
local args="$@" local args="$@"
if [ $VERSION -ge 12 ]; then if [ $VERSION -ge 12 ]; then
# For Debian 12+, try with --break-system-packages flag first # For Debian 12+, use virtual environment (PEP 668 compliant)
echo "Installing Python packages for Debian $VERSION: $args" echo "Installing Python packages for Debian $VERSION: $args"
if pip3 install --break-system-packages $args; then if [ -z "$VIRTUAL_ENV" ]; then
echo "ERROR: Virtual environment not activated"
return 1
fi
if pip3 install $args; then
echo "Successfully installed: $args" echo "Successfully installed: $args"
return 0 return 0
else else
echo "Warning: Installation with --break-system-packages failed, trying standard installation..." echo "ERROR: Failed to install: $args"
if pip3 install $args; then return 1
echo "Successfully installed: $args"
return 0
else
echo "ERROR: Failed to install: $args"
return 1
fi
fi fi
else else
# For Debian 10-11, use standard pip installation # For Debian 10-11, use standard pip installation
@ -265,6 +268,39 @@ pip_install() {
echo "Installing Python dependencies..." echo "Installing Python dependencies..."
cd $HBMONDIR cd $HBMONDIR
# For Debian 12+, create and use a virtual environment (modern PEP 668 compliant approach)
if [ $VERSION -ge 12 ]; then
echo "Creating Python virtual environment for Debian $VERSION..."
# Install python3-venv if not already installed
if ! dpkg -l | grep -q python3-venv; then
echo "Installing python3-venv package..."
apt-get install -y python3-venv
fi
# Create virtual environment
if [ ! -d "$HBMONDIR/venv" ]; then
python3 -m venv "$HBMONDIR/venv"
if [ $? -ne 0 ]; then
echo "ERROR: Failed to create virtual environment"
exit 1
fi
echo "Virtual environment created successfully at $HBMONDIR/venv"
else
echo "Virtual environment already exists at $HBMONDIR/venv"
fi
# Activate virtual environment
source "$HBMONDIR/venv/bin/activate"
if [ $? -ne 0 ]; then
echo "ERROR: Failed to activate virtual environment"
exit 1
fi
echo "Virtual environment activated"
# Upgrade pip in the virtual environment
pip3 install --upgrade pip
fi
# Install setuptools and wheel first # Install setuptools and wheel first
if ! pip_install setuptools wheel; then if ! pip_install setuptools wheel; then
echo "ERROR: Failed to install setuptools and wheel" echo "ERROR: Failed to install setuptools and wheel"
@ -343,6 +379,18 @@ LOG_NAME = 'hbmon.log'
EOF EOF
cp utils/hbmon.service /lib/systemd/system/ cp utils/hbmon.service /lib/systemd/system/
# For Debian 12+, update the service file to use virtual environment
if [ $VERSION -ge 12 ]; then
echo "Updating hbmon.service to use virtual environment..."
# Update ExecStart to use venv Python
sed -i "s|ExecStart=/usr/bin/python3|ExecStart=$HBMONDIR/venv/bin/python3|g" /lib/systemd/system/hbmon.service
sed -i "s|ExecStart=python3|ExecStart=$HBMONDIR/venv/bin/python3|g" /lib/systemd/system/hbmon.service
# Also handle cases where the path might be relative
sed -i "s|ExecStart=\([^/]\)|ExecStart=$HBMONDIR/venv/bin/python3 \1|g" /lib/systemd/system/hbmon.service
echo "Service file updated to use virtual environment"
fi
cp utils/lastheard /etc/cron.daily/ cp utils/lastheard /etc/cron.daily/
chmod +x /etc/cron.daily/lastheard chmod +x /etc/cron.daily/lastheard
echo "" echo ""