From 508f1ddb5b6a0cc69655e39dbd59fe3466173a84 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Tue, 29 Jun 2010 11:23:31 +0200 Subject: * apt/cache.py: - add new "dpkg_journal_dirty" property that can be used to detect a interrupted dpkg (the famous "E: dpkg was interrupted, you must manually run 'dpkg --configure -a'") --- apt/cache.py | 15 +++++++++++++++ debian/changelog | 4 ++++ tests/test_apt_cache.py | 26 ++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/apt/cache.py b/apt/cache.py index 3679e4ba..3962bb4f 100644 --- a/apt/cache.py +++ b/apt/cache.py @@ -19,6 +19,7 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # USA +import fnmatch import os import weakref @@ -454,6 +455,20 @@ class Cache(object): """ return apt_pkg.ActionGroup(self._depcache) + @property + def dpkg_journal_dirty(self): + """Return True if the dpkg was interrupted + + All dpkg operations will fail until this is fixed, the action to + fix the system if dpkg got interrupted is to run + 'dpkg --configure -a' as root. + """ + dpkg_status_dir = os.path.dirname(apt_pkg.Config.find_file("Dir::State::status")) + for f in os.listdir(os.path.join(dpkg_status_dir, "updates")): + if fnmatch.fnmatch(f, "[0-9]*"): + return True + return False + @property def broken_count(self): """Return the number of packages with broken dependencies.""" diff --git a/debian/changelog b/debian/changelog index b7bb3ad0..b1c7c1b5 100644 --- a/debian/changelog +++ b/debian/changelog @@ -11,6 +11,10 @@ python-apt (0.7.96) UNRELEASED; urgency=low - do not fail on non-digits in the version number * utils/get_debian_mirrors.py: - ignore mirrors without a county + * apt/cache.py: + - add new "dpkg_journal_dirty" property that can be used to + detect a interrupted dpkg (the famous + "E: dpkg was interrupted, you must manually run 'dpkg --configure -a'") [ Martin Pitt ] * tests/test_apt_cache.py: Test accessing the record of all packages during diff --git a/tests/test_apt_cache.py b/tests/test_apt_cache.py index a00fa08b..b27ed778 100644 --- a/tests/test_apt_cache.py +++ b/tests/test_apt_cache.py @@ -9,6 +9,9 @@ import unittest import apt +import apt_pkg +import os +import tempfile class TestAptCache(unittest.TestCase): @@ -39,5 +42,28 @@ class TestAptCache(unittest.TestCase): self.assert_(len(r['Description']) > 0) self.assert_(str(r).startswith('Package: %s\n' % pkg.name)) + def test_dpkg_journal_dirty(self): + # backup old value + old_status = apt_pkg.Config.find_file("Dir::State::status") + # create tmp env + tmpdir = tempfile.mkdtemp() + dpkg_dir = os.path.join(tmpdir,"var","lib","dpkg") + os.makedirs(os.path.join(dpkg_dir,"updates")) + open(os.path.join(dpkg_dir,"status"), "w") + apt_pkg.Config.set("Dir::State::status", + os.path.join(dpkg_dir,"status")) + cache = apt.Cache() + # test empty + self.assertFalse(cache.dpkg_journal_dirty) + # that is ok, only [0-9] are dpkg jounral entries + open(os.path.join(dpkg_dir,"updates","xxx"), "w") + self.assertFalse(cache.dpkg_journal_dirty) + # that is a dirty journal + open(os.path.join(dpkg_dir,"updates","000"), "w") + self.assertTrue(cache.dpkg_journal_dirty) + # reset config value + apt_pkg.Config.set("Dir::State::status", old_status) + + if __name__ == "__main__": unittest.main() -- cgit v1.2.3