diff --git a/src/controllers/Package/Apt.py b/src/controllers/Package/Apt.py index 9631fc6..53c6502 100644 --- a/src/controllers/Package/Apt.py +++ b/src/controllers/Package/Apt.py @@ -7,6 +7,8 @@ import os import re import sys +import time +import fcntl from colorama import Fore, Style from pathlib import Path @@ -146,21 +148,34 @@ def get_available_packages(self, dist_upgrade: bool = False): # #----------------------------------------------------------------------------------------------- def wait_for_dpkg_lock(self, timeout: int = 60): - import fcntl - from time import sleep + lock_files = [ + '/var/lib/dpkg/lock', + '/var/lib/dpkg/lock-frontend', + '/var/cache/apt/archives/lock', + '/var/lib/apt/lists/lock' + ] - while timeout > 0: - with open('/var/lib/dpkg/lock', 'w') as handle: - try: - fcntl.lockf(handle, fcntl.LOCK_EX | fcntl.LOCK_NB) - return - except IOError: - pass + start_time = time.time() + + while time.time() - start_time < timeout: + locks_held = False + for lock_file in lock_files: + if os.path.exists(lock_file): + try: + with open(lock_file, 'w') as handle: + fcntl.lockf(handle, fcntl.LOCK_EX | fcntl.LOCK_NB) + except IOError: + locks_held = True + break + + if not locks_held: + return + + print(' Waiting for dpkg lock to be released...') + time.sleep(5) - timeout -= 1 - sleep(1) + raise Exception(f'Could not acquire dpkg lock within {timeout} seconds') - raise Exception('could not acquire dpkg lock (timeout ' + str(timeout) + 's)') #----------------------------------------------------------------------------------------------- # @@ -330,7 +345,7 @@ def update(self, packagesList, exit_on_package_update_error: bool = True, dry_ru raise Exception('Could not retrieve current version of package ' + pkg['name'] + ': ' + str(e)) # Define the command to update the package - cmd = '/usr/bin/apt-get install ' + pkg['name'] + '=' + pkg['target_version'] + ' -y' + cmd = 'DEBIAN_FRONTEND=noninteractive /usr/bin/apt-get install ' + pkg['name'] + '=' + pkg['target_version'] + ' -y' # If --keep-oldconf is True, then keep the old configuration files if self.keep_oldconf: