Beispiel #1
0
    def test_lock(self):
        apt_pkg.get_lock(self.file_unicode, True)
        apt_pkg.get_lock(self.file_bytes, True)

        with apt_pkg.FileLock(self.file_unicode):
            pass
        with apt_pkg.FileLock(self.file_bytes):
            pass
Beispiel #2
0
    def test_lock(self):
        apt_pkg.get_lock(self.file_unicode, True)
        apt_pkg.get_lock(self.file_bytes, True)

        with apt_pkg.FileLock(self.file_unicode):
            pass
        with apt_pkg.FileLock(self.file_bytes):
            pass
Beispiel #3
0
 def obtain_lock(self, sender=None, conn=None):
     """ Lock the package system. """
     self._check_polkit_privilege(
         sender, conn, 'org.pop_os.transition_system.removedebs'
     )
     print('Obtaining Package manager lock')
     try:
         self.lock = apt_pkg.get_lock('/var/lib/dpkg/lock-frontend', True)
         self.apt_lock = apt_pkg.get_lock('/var/lib/apt/lists/lock', True)
         print('Lock obtained')
         return True
     except apt_pkg.Error:
         print('Could not obtain lock')
         self.lock = None
         self.apt_lock = None
         return False
    def lock(self, raise_on_fail=True):
        # type: (bool) -> bool
        """
		Get locks to prevent concurrent calls.

		:param bool raise_on_fail: Raise :py:class:`LockError` instead of returning `False`.
		:returns: `True` if all locks were are acquired, `False` otherwise.
		:rtype: bool
		:raises LockError: if the lock cannot be acquired.
		"""
        if self.is_locked():
            return True
        self.lock_fd = get_lock('univention-lib-package-manager',
                                nonblocking=True)
        return_value = self.lock_fd is not None

        if return_value is True:
            # get apt lock; taken from dist-packages/apt/cache.py
            lockfile = apt_pkg.config.find_dir("Dir::Cache::Archives") + "lock"
            self.apt_lock_fd = apt_pkg.get_lock(lockfile)
            if self.apt_lock_fd < 0:
                return_value = False

        if return_value is False:
            if raise_on_fail:
                raise LockError(_('Failed to lock'))

        return return_value
 def _dpkg_locked(self):
     fd = apt_pkg.get_lock('/var/lib/dpkg/lock')
     if fd == -1:
         return True
     else:
         os.close(fd)
         return False
Beispiel #6
0
    def update(self, fetch_progress=None, pulse_interval=0,
               raise_on_error=True):
        """Run the equivalent of apt-get update.

        The first parameter *fetch_progress* may be set to an instance of
        apt.progress.FetchProgress, the default is apt.progress.FetchProgress()
        .
        """
        lockfile = apt_pkg.config.find_dir("Dir::State::Lists") + "lock"
        lock = apt_pkg.get_lock(lockfile)

        if lock < 0:
            raise LockFailedException("Failed to lock %s" % lockfile)

        try:
            if fetch_progress is None:
                fetch_progress = apt.progress.base.AcquireProgress()
            try:
                res = self._cache.update(fetch_progress, self._list,
                                         pulse_interval)
            except SystemError, e:
                raise FetchFailedException(e)
            if not res and raise_on_error:
                raise FetchFailedException()
            else:
                return res
def get_lock(path):
    """ return a lock that can be released with release_lock on success
        and -1 on failure
    """
    try:
        import apt_pkg
        return apt_pkg.get_lock(path, False)
    except ImportError:
        # implement me on non-apt system, I wish python had this in the stdlib
        pass
Beispiel #8
0
def get_lock(path):
    """ return a lock that can be released with release_lock on success
        and -1 on failure
    """
    try:
        import apt_pkg
        return apt_pkg.get_lock(path, False)
    except ImportError:
        # implement me on non-apt system, I wish python had this in the stdlib
        pass
Beispiel #9
0
    def update(self,
               fetch_progress=None,
               pulse_interval=0,
               raise_on_error=True,
               sources_list=None):
        # FIXME: type: (AcquireProgress, int, bool, str) -> int
        """Run the equivalent of apt-get update.

        You probably want to call open() afterwards, in order to utilise the
        new cache. Otherwise, the old cache will be used which can lead to
        strange bugs.

        The first parameter *fetch_progress* may be set to an instance of
        apt.progress.FetchProgress, the default is apt.progress.FetchProgress()
        .
        sources_list -- Update a alternative sources.list than the default.
        Note that the sources.list.d directory is ignored in this case
        """
        lockfile = apt_pkg.config.find_dir("Dir::State::Lists") + "lock"
        lock = apt_pkg.get_lock(lockfile)

        if lock < 0:
            raise LockFailedException("Failed to lock %s" % lockfile)

        if sources_list:
            old_sources_list = apt_pkg.config.find("Dir::Etc::sourcelist")
            old_sources_list_d = apt_pkg.config.find("Dir::Etc::sourceparts")
            old_cleanup = apt_pkg.config.find("APT::List-Cleanup")
            apt_pkg.config.set("Dir::Etc::sourcelist",
                               os.path.abspath(sources_list))
            apt_pkg.config.set("Dir::Etc::sourceparts", "xxx")
            apt_pkg.config.set("APT::List-Cleanup", "0")
            slist = apt_pkg.SourceList()
            slist.read_main_list()
        else:
            slist = self._list

        try:
            if fetch_progress is None:
                fetch_progress = apt.progress.base.AcquireProgress()
            try:
                res = self._cache.update(fetch_progress, slist, pulse_interval)
            except SystemError as e:
                raise FetchFailedException(e)
            if not res and raise_on_error:
                raise FetchFailedException()
            else:
                return res
        finally:
            os.close(lock)
            if sources_list:
                apt_pkg.config.set("Dir::Etc::sourcelist", old_sources_list)
                apt_pkg.config.set("Dir::Etc::sourceparts", old_sources_list_d)
                apt_pkg.config.set("APT::List-Cleanup", old_cleanup)
Beispiel #10
0
 def acquire(self):
     """Return the file descriptor of the lock file or raise
     LockFailedError if the lock cannot be obtained.
     """
     if self.fd:
         return self.fd
     fd_lock = apt_pkg.get_lock(self.path)
     if fd_lock < 0:
         process = get_locking_process_name(self.path)
         raise LockFailedError(self.path, process)
     else:
         self.fd = fd_lock
         return fd_lock
Beispiel #11
0
 def acquire(self):
     """Return the file descriptor of the lock file or raise
     LockFailedError if the lock cannot be obtained.
     """
     if self.fd:
         return self.fd
     fd_lock = apt_pkg.get_lock(self.path)
     if fd_lock < 0:
         process = get_locking_process_name(self.path)
         raise LockFailedError(self.path, process)
     else:
         self.fd = fd_lock
         return fd_lock
Beispiel #12
0
    def update(self, fetch_progress=None, pulse_interval=0,
               raise_on_error=True, sources_list=None):
        """Run the equivalent of apt-get update.

        You probably want to call open() afterwards, in order to utilise the
        new cache. Otherwise, the old cache will be used which can lead to
        strange bugs.

        The first parameter *fetch_progress* may be set to an instance of
        apt.progress.FetchProgress, the default is apt.progress.FetchProgress()
        .
        sources_list -- Update a alternative sources.list than the default.
         Note that the sources.list.d directory is ignored in this case
        """
        lockfile = apt_pkg.config.find_dir("Dir::State::Lists") + "lock"
        lock = apt_pkg.get_lock(lockfile)

        if lock < 0:
            raise LockFailedException("Failed to lock %s" % lockfile)

        if sources_list:
            old_sources_list = apt_pkg.config.find("Dir::Etc::sourcelist")
            old_sources_list_d = apt_pkg.config.find("Dir::Etc::sourceparts")
            old_cleanup = apt_pkg.config.find("APT::List-Cleanup")
            apt_pkg.config.set("Dir::Etc::sourcelist",
                               os.path.abspath(sources_list))
            apt_pkg.config.set("Dir::Etc::sourceparts", "xxx")
            apt_pkg.config.set("APT::List-Cleanup", "0")
            slist = apt_pkg.SourceList()
            slist.read_main_list()
        else:
            slist = self._list

        try:
            if fetch_progress is None:
                fetch_progress = apt.progress.base.AcquireProgress()
            try:
                res = self._cache.update(fetch_progress, slist,
                                         pulse_interval)
            except SystemError as e:
                raise FetchFailedException(e)
            if not res and raise_on_error:
                raise FetchFailedException()
            else:
                return res
        finally:
            os.close(lock)
            if sources_list:
                apt_pkg.config.set("Dir::Etc::sourcelist", old_sources_list)
                apt_pkg.config.set("Dir::Etc::sourceparts", old_sources_list_d)
                apt_pkg.config.set("APT::List-Cleanup", old_cleanup)
Beispiel #13
0
    def _fetch_archives(self, fetcher, pm):
        """ fetch the needed archives """

        # get lock
        lockfile = apt_pkg.config.find_dir("Dir::Cache::Archives") + "lock"
        lock = apt_pkg.get_lock(lockfile)
        if lock < 0:
            raise LockFailedException("Failed to lock %s" % lockfile)

        try:
            # this may as well throw a SystemError exception
            if not pm.get_archives(fetcher, self._list, self._records):
                return False
            # now run the fetcher, throw exception if something fails to be
            # fetched
            return self._run_fetcher(fetcher)
        finally:
            os.close(lock)
Beispiel #14
0
    def _fetch_archives(self, fetcher, pm):
        """ fetch the needed archives """

        # get lock
        lockfile = apt_pkg.config.find_dir("Dir::Cache::Archives") + "lock"
        lock = apt_pkg.get_lock(lockfile)
        if lock < 0:
            raise LockFailedException("Failed to lock %s" % lockfile)

        try:
            # this may as well throw a SystemError exception
            if not pm.get_archives(fetcher, self._list, self._records):
                return False
            # now run the fetcher, throw exception if something fails to be
            # fetched
            return self._run_fetcher(fetcher)
        finally:
            os.close(lock)
    def lock(self, raise_on_fail=True):
        if self.is_locked():
            return True
        self.lock_fd = get_lock('univention-lib-package-manager',
                                nonblocking=True)
        return_value = self.lock_fd is not None

        if return_value is True:
            # get apt lock; taken from dist-packages/apt/cache.py
            lockfile = apt_pkg.config.find_dir("Dir::Cache::Archives") + "lock"
            self.apt_lock_fd = apt_pkg.get_lock(lockfile)
            if self.apt_lock_fd < 0:
                return_value = False

        if return_value is False:
            if raise_on_fail:
                raise LockError(_('Failed to lock'))

        return return_value
Beispiel #16
0
    def _fetch_archives(self, fetcher, pm):
        # type: (apt_pkg.Acquire, apt_pkg.PackageManager) -> int
        """ fetch the needed archives """
        if self._records is None:
            raise CacheClosedException(
                "Cache object used after close() called")

        # get lock
        lockfile = apt_pkg.config.find_dir("Dir::Cache::Archives") + "lock"
        lock = apt_pkg.get_lock(lockfile)
        if lock < 0:
            raise LockFailedException("Failed to lock %s" % lockfile)

        try:
            # this may as well throw a SystemError exception
            if not pm.get_archives(fetcher, self._list, self._records):
                return False
            # now run the fetcher, throw exception if something fails to be
            # fetched
            return self._run_fetcher(fetcher)
        finally:
            os.close(lock)
Beispiel #17
0
    # system-lock
    apt_pkg.pkgsystem_lock()

    pid = os.fork()
    if pid == 0:
        try:
            apt_pkg.pkgsystem_lock()
        except SystemError as s:
            print "Can't get lock: (error text:\n%s)" % s
        sys.exit(0)

    apt_pkg.pkgsystem_unlock()

    # low-level lock
    fd = apt_pkg.get_lock(lock, True)
    print "Lockfile fd: %s" % fd

    # try to get lock without error flag
    pid = os.fork()
    if pid == 0:
        # child
        fd = apt_pkg.get_lock(lock, False)
        print "Lockfile fd (child): %s" % fd
        sys.exit(0)

    # try to get lock with error flag
    pid = os.fork()
    if pid == 0:
        # child
        fd = apt_pkg.get_lock(lock, True)
Beispiel #18
0
    # system-lock
    apt_pkg.pkgsystem_lock()

    pid = os.fork()
    if pid == 0:
        try:
            apt_pkg.pkgsystem_lock()
        except SystemError as s:
            print("Can't get lock: (error text:\n%s)" % s)
        sys.exit(0)

    apt_pkg.pkgsystem_unlock()

    # low-level lock
    fd = apt_pkg.get_lock(lock, True)
    print("Lockfile fd: %s" % fd)

    # try to get lock without error flag
    pid = os.fork()
    if pid == 0:
        # child
        fd = apt_pkg.get_lock(lock, False)
        print("Lockfile fd (child): %s" % fd)
        sys.exit(0)

    # try to get lock with error flag
    pid = os.fork()
    if pid == 0:
        # child
        fd = apt_pkg.get_lock(lock, True)
#!/usr/bin/python
#
# create a lock file so that unattended-upgrades-shutdown pauses
# on shutdown -- useful for testing

import apt_pkg
import os
import time

pid = os.fork()
if pid == 0:
    os.setsid()
    lock = apt_pkg.get_lock("/var/run/unattended-upgrades.lock")
    time.sleep(500)
#!/usr/bin/python3
#
# create a lock file so that unattended-upgrades-shutdown pauses
# on shutdown -- useful for testing

import apt_pkg
import os
import time

pid = os.fork()
if pid == 0:
    os.setsid()
    lock = apt_pkg.get_lock("/var/run/unattended-upgrades.lock")
    time.sleep(500)