Skip to content

Commit

Permalink
Fix #76.
Browse files Browse the repository at this point in the history
  • Loading branch information
thw26 committed Jul 8, 2024
1 parent 9866753 commit 66b2f6e
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import subprocess
import sys
import threading
import time
import tkinter as tk
import zipfile
from base64 import b64encode
Expand Down Expand Up @@ -235,24 +236,34 @@ def die(message):
sys.exit(1)


def run_command(command, stdin=None, shell=False):
try:
logging.debug(f"Attempting to execute {command}")
result = subprocess.run(
command,
stdin=stdin,
check=True,
text=True,
shell=shell,
capture_output=True
)
return result.stdout
except subprocess.CalledProcessError as e:
logging.error(f"Error occurred while executing {command}: {e}")
return None
except Exception as e:
logging.error(f"An unexpected error occurred when running {command}: {e}")
return None
def run_command(command, retries=1, delay=0, stdin=None, shell=False):
if retries < 1:
retries = 1

for attempt in range(retries):
try:
logging.debug(f"Attempting to execute {command}")
result = subprocess.run(
command,
stdin=stdin,
check=True,
text=True,
shell=shell,
capture_output=True
)
return result.stdout
except subprocess.CalledProcessError as e:
logging.error(f"Error occurred while executing {command}: {e}")
if "lock" in str(e):
logging.debug(f"Database appears to be locked. Retrying in {delay} seconds…")
time.sleep(delay)
else:
raise e
except Exception as e:
logging.error(f"An unexpected error occurred when running {command}: {e}")
return None

logging.error(f"Failed to execute after {retries} attempts: '{command}'")


def reboot():
Expand Down Expand Up @@ -534,7 +545,7 @@ def download_packages(packages, elements, app=None):
command = f"{config.SUPERUSER_COMMAND} {config.PACKAGE_MANAGER_COMMAND_DOWNLOAD} {' '.join(packages)}"
logging.debug(f"download_packages cmd: {command}")
command_args = shlex.split(command)
result = run_command(command_args)
result = run_command(command_args, retries=5, delay=15)

for index, package in enumerate(packages):
status = "Downloaded" if result.returncode == 0 else "Failed"
Expand All @@ -555,7 +566,7 @@ def install_packages(packages, elements, app=None):
for index, package in enumerate(packages):
command = f"{config.SUPERUSER_COMMAND} {config.PACKAGE_MANAGER_COMMAND_INSTALL} {package}"
logging.debug(f"install_packages cmd: {command}")
result = run_command(command)
result = run_command(command, retries=5, delay=15)

if elements is not None:
elements[index] = (
Expand All @@ -579,7 +590,7 @@ def remove_packages(packages, elements, app=None):
for index, package in enumerate(packages):
command = f"{config.SUPERUSER_COMMAND} {config.PACKAGE_MANAGER_COMMAND_REMOVE} {package}"
logging.debug(f"remove_packages cmd: {command}")
result = run_command(command)
result = run_command(command, retries=5, delay=15)

if elements is not None:
elements[index] = (
Expand Down

0 comments on commit 66b2f6e

Please sign in to comment.