def test_is_package_manager_busy(): """Test the behavior of `is-package-manager-busy` in both locked and unlocked states of the dpkg lock file.""" apt_pkg.init() # initialize apt_pkg module # In the locked state, the lsof command returns 0. # Hence no error is thrown. with apt_pkg.SystemLock(): superuser_run('packages', ['is-package-manager-busy']) # In the unlocked state, the lsof command returns 1. # An ActionError is raised in this case. with pytest.raises(ActionError): superuser_run('packages', ['is-package-manager-busy'])
def commit(self, fetch_progress=None, install_progress=None): # type: (AcquireProgress, InstallProgress) -> bool """Apply the marked changes to the cache. The first parameter, *fetch_progress*, refers to a FetchProgress() object as found in apt.progress, the default being apt.progress.FetchProgress(). The second parameter, *install_progress*, is a apt.progress.InstallProgress() object. """ # FIXME: # use the new acquire/pkgmanager interface here, # raise exceptions when a download or install fails # and send proper error strings to the application. # Current a failed download will just display "error" # which is less than optimal! if fetch_progress is None: fetch_progress = apt.progress.base.AcquireProgress() if install_progress is None: install_progress = apt.progress.base.InstallProgress() assert install_progress is not None with apt_pkg.SystemLock(): pm = apt_pkg.PackageManager(self._depcache) fetcher = apt_pkg.Acquire(fetch_progress) with self._archive_lock: while True: # fetch archives first res = self._fetch_archives(fetcher, pm) # then install res = self.install_archives(pm, install_progress) if res == pm.RESULT_COMPLETED: break elif res == pm.RESULT_FAILED: raise SystemError("installArchives() failed") elif res == pm.RESULT_INCOMPLETE: pass else: raise SystemError("internal-error: unknown result " "code from InstallArchives: %s" % res) # reload the fetcher for media swaping fetcher.shutdown() return (res == pm.RESULT_COMPLETED)
def wrapper(self, *args, **kwargs): """ The function wrapper. """ try: with apt_pkg.SystemLock(): return obj(self, *args, **kwargs) except SystemError as e: # Lock failed logger.error("Unable to create a SystemLock: %s" % e) # A bit convoluted here, but we should test for both # the existence of lock_failed_callback and of the callback # it references if lock_failed_callback and hasattr(self, lock_failed_callback): callback = getattr(self, lock_failed_callback) if callback: callback()
"-y", "--yes", action="store_true", default=False, help="assume yes to all questions for which yes is the default") parser.add_option( "-f", "--force", action="store_true", default=False, help="force the requested changes, don't resolve problems") opts, args = parser.parse_args() # try to grab the system lock briefly, so we're not caught without it later try: with apt_pkg.SystemLock(): pass except SystemError as exc: print exc raise SystemExit(100) # see apt-get(8) print "Loading package cache..." cache = apt.Cache() if opts.update: print "Updating package cache..." cache.update(apt.progress.TextFetchProgress()) print "Searching for installed packages that are no longer available..." for pkg in cache: if pkg.installed and not pkg.candidate.downloadable: