diff -Nur -x '*.orig' -x '*~' packagekit-0.3.14/backends/apt/aptDBUSBackend.py packagekit-0.3.14.new/backends/apt/aptDBUSBackend.py --- packagekit-0.3.14/backends/apt/aptDBUSBackend.py 2009-04-20 15:28:31.000000000 +0200 +++ packagekit-0.3.14.new/backends/apt/aptDBUSBackend.py 2009-04-20 15:29:37.000000000 +0200 @@ -32,6 +32,7 @@ import socket import stat import string +import struct import subprocess import sys import time @@ -188,6 +189,24 @@ Enhanced version of the apt.cache.Cache class which supports some features which can only be found in the consolidate branch of python-apt """ + DPKG_LOCK = "/var/lib/dpkg/lock" + + def __init__(self, *args, **kwargs): + if self._is_dpkg_locked(): + pklog.debug("dpkg lock set, opening cache memonly") + kwargs["memonly"] = True + apt.cache.Cache.__init__(self, *args, **kwargs) + + def _is_dpkg_locked(self): + if not os.path.exists(self.DPKG_LOCK): + return False + flk=struct.pack('hhllhl',fcntl.F_WRLCK,0,0,0,0,0) + f=open(self.DPKG_LOCK) + rv = fcntl.fcntl(f, fcntl.F_GETLK, flk) + lockv = struct.unpack('hhllhl', rv)[0] + f.close() + return (lockv == fcntl.F_WRLCK) + def __iter__(self): """ Let the cache behave like a sorted list of packages