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

Fix infinite blocking on os update #174

Merged
merged 3 commits into from
Jul 18, 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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies = [
"pyqt5==5.15.7",
"pyqt5-qt5==5.15.2",
"psutil>=6.0.0",
"distro>=1.9.0",
]
requires-python = ">= 3.9"

Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ debugpy==1.8.2
# via ipykernel
decorator==5.1.1
# via ipython
distro==1.9.0
# via cocktailberry
exceptiongroup==1.2.1
# via ipython
executing==2.0.1
Expand Down
2 changes: 2 additions & 0 deletions requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ click==8.1.7
# via typer
colorama==0.4.6
# via click
distro==1.9.0
# via cocktailberry
gitdb==4.0.11
# via gitpython
gitpython==3.1.43
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ piicodev
qtsass
pyqtspinner
pillow
psutil
psutil
distro
26 changes: 18 additions & 8 deletions scripts/all_in_one.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,18 @@ else
fi

# might also need to install python-venv
echo "~ Check if python3-venv is installed ~"
echo "~ Check if python3-venv and ensurepip are available ~"
python3 -m venv --help 2>&1 >/dev/null
VENV_IS_AVAILABLE=$?
if [ $VENV_IS_AVAILABLE -ne 0 ]; then
echo "Python3 venv was not found, installing it ..."
sudo apt install python3-venv
python3 -c "import ensurepip" 2>&1 >/dev/null
ENSUREPIP_IS_AVAILABLE=$?

if [ $VENV_IS_AVAILABLE -ne 0 ] || [ $ENSUREPIP_IS_AVAILABLE -ne 0 ]; then
echo "Python3 venv or ensurepip was not found, installing python3-venv ..."
PYTHON_VERSION=$(python3 -V | cut -d' ' -f2 | cut -d'.' -f1,2) # Extracts version in format X.Y
sudo apt install python${PYTHON_VERSION}-venv
else
echo "Python3 venv is already installed!"
echo "Python3 venv and ensurepip are already installed!"
fi

# also install pip if not already done
Expand All @@ -60,9 +64,15 @@ else
fi

# Warning if debian is not at least v11. Still go on because some users may use none debian
DEBIAN_VERSION=$(sed 's/\..*//' /etc/debian_version)
if [[ "$DEBIAN_VERSION" -lt "11" ]]; then
echo "WARNING: Your Debian seem not to be at least version 11. It is recommended to update to the latest Raspberry Pi OS!"
# Check if /etc/debian_version exists
if [ -f /etc/debian_version ]; then
# Extract the major version number of Debian
DEBIAN_VERSION=$(sed 's/\..*//' /etc/debian_version)
if [[ "$DEBIAN_VERSION" -lt "11" ]]; then
echo "WARNING: Your Debian seems not to be at least version 11. It is recommended to update to the latest Raspberry Pi OS!"
fi
else
echo "WARNING: /etc/debian_version not found. This script might not work properly on none debian systems."
fi

# Now gets CocktailBerry source
Expand Down
2 changes: 1 addition & 1 deletion scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ else
echo "Installing PyQt"
sudo apt-get -y install qt5-default pyqt5-dev pyqt5-dev-tools || sudo apt-get -y install python3-pyqt5 || echo "ERROR: Could not install PyQt5"
echo "Installing needed Python libraries"
pip install requests pyyaml GitPython typer pyfiglet qtawesome piicodev pyqtspinner pillow psutil
pip install requests pyyaml GitPython typer pyfiglet qtawesome piicodev pyqtspinner pillow psutil distro
# try to install mfrc522, this will probably fail on non raspberry pi devices
if is_raspberry_pi; then
pip install mfrc522 || echo "ERROR: Could not install mfrc522, are you on a Raspberry Pi?"
Expand Down
Empty file added scripts/setup_non_rpi.sh
Empty file.
1 change: 1 addition & 0 deletions src/migration/migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def make_migrations(self):
"1.30.1": [add_unit_column_to_ingredients],
"1.33.0": [_move_slow_factor_to_db],
"1.35.0": [lambda: self._install_pip_package("psutil", "1.35.0")],
"1.36.0": [lambda: self._install_pip_package("distro", "1.36.0")],
}

for version, actions in version_actions.items():
Expand Down
6 changes: 3 additions & 3 deletions src/ui/setup_option_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from src.ui.setup_wifi_window import WiFiWindow
from src.ui_elements import Ui_Optionwindow
from src.updater import Updater
from src.utils import get_platform_data, has_connection, time_print
from src.utils import get_platform_data, has_connection, time_print, update_os

if TYPE_CHECKING:
from src.ui.setup_mainwindow import MainScreen
Expand All @@ -35,15 +35,15 @@


class _Worker(QObject):
"""Worker to install qtsass on a thread."""
"""Worker to get full system update on a thread."""

done = pyqtSignal()

def __init__(self, parent=None):
super().__init__(parent)

def run(self):
os.system("sudo apt-get update && sudo apt-get -y full-upgrade")
update_os()
self.done.emit()


Expand Down
44 changes: 38 additions & 6 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
from dataclasses import dataclass
from typing import Literal

import distro
import psutil

from src.filepath import CUSTOM_STYLE_FILE, CUSTOM_STYLE_SCSS, ROOT_PATH, STYLE_FOLDER
from src.logger_handler import LogFiles, LoggerHandler

EXECUTABLE = ROOT_PATH / "runme.py"
_logger = LoggerHandler("utils")
logger = LoggerHandler("utils")


def has_connection() -> bool:
Expand Down Expand Up @@ -61,7 +62,7 @@ def set_system_time(time_string: str):
p_data = get_platform_data()
# checking system, currently only setting on Linux (RPi), bc. its only one supported
supported_os = ["Linux"]
_logger.log_event("INFO", f"Setting time to: {time_string}")
logger.log_event("INFO", f"Setting time to: {time_string}")
if p_data.system in supported_os:
# need first disable timesyncd, otherwise you cannot set time
time_commands = [
Expand All @@ -73,15 +74,15 @@ def set_system_time(time_string: str):
# Use subprocess.run to capture the command's output and error
for time_command in time_commands:
subprocess.run(time_command, shell=True, check=True, capture_output=True, text=True)
_logger.log_event("INFO", "Time set successfully")
logger.log_event("INFO", "Time set successfully")

except subprocess.CalledProcessError as err:
# Log any exceptions that occurred during command execution
err_msg = err.stderr.replace("\n", " ")
_logger.log_event("ERROR", f"Could not set time, error: {err_msg}")
_logger.log_exception(err)
logger.log_event("ERROR", f"Could not set time, error: {err_msg}")
logger.log_exception(err)
else:
_logger.log_event(
logger.log_event(
"WARNING", f"Could not set time, your OS is: {p_data.system}. Currently supported OS are: {supported_os}"
)

Expand Down Expand Up @@ -132,3 +133,34 @@ def start_resource_tracker():
"""Start a thread that tracks the system resources."""
log_thread = threading.Thread(target=_resource_logger_thread, args=(15,), daemon=True)
log_thread.start()


def update_os():
distribution = distro.id().lower()

if distribution in ["raspbian", "debian", "ubuntu"]:
command = (
"sudo DEBIAN_FRONTEND=noninteractive apt-get update && "
"sudo DEBIAN_FRONTEND=noninteractive apt-get -y "
'-o Dpkg::Options::="--force-confdef" '
'-o Dpkg::Options::="--force-confold" full-upgrade'
)
elif distribution in ["fedora", "centos", "rhel"]:
if subprocess.call(["command", "-v", "dnf"], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) == 0:
command = "sudo dnf -y update"
else:
command = "sudo yum -y update"
elif distribution in ["opensuse"]:
command = "sudo zypper -n update"
elif distribution in ["arch"]:
command = "sudo pacman -Syu --noconfirm"
else:
logger.error(f"Unsupported Linux distribution: {distribution}")
return

try:
subprocess.run(command, shell=True, check=True)
logger.info("System updated successfully.")
except subprocess.CalledProcessError as e:
logger.error("Could not update system, see debug log for more information.")
logger.log_exception(e)