Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
lbr38 committed Dec 18, 2024
1 parent 4bd6f1f commit 8234b61
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions src/controllers/Package/Apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import os
import re
import sys
import time
import fcntl
from colorama import Fore, Style
from pathlib import Path

Expand Down Expand Up @@ -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)')

#-----------------------------------------------------------------------------------------------
#
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 8234b61

Please sign in to comment.