From e871a081c5f5ca76c2e36b1b317c61cc79c026ed Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 31 Jul 2011 17:43:13 +0000 Subject: [PATCH] Build cleanup actions and add support to remove packages from the db. --- pakfire/actions.py | 61 ++++++++++++++++++++++------------ pakfire/packages/base.py | 9 +++-- pakfire/packages/installed.py | 22 ++++++++++++ pakfire/repository/database.py | 22 ++++++++++++ pakfire/repository/index.py | 4 +++ pakfire/repository/local.py | 4 +++ po/pakfire.pot | 21 +++++++----- 7 files changed, 111 insertions(+), 32 deletions(-) diff --git a/pakfire/actions.py b/pakfire/actions.py index a4c77d9fa..bb048b0b9 100644 --- a/pakfire/actions.py +++ b/pakfire/actions.py @@ -42,22 +42,6 @@ class Action(object): """ return self.pakfire.repos.local - def _extract(self, message, prefix=None): - # Add package to the database. - self.local.add_package(self.pkg) - - if prefix is None: - prefix = self.pakfire.path - - self.pkg.extract(message, prefix=prefix) - - -class ActionCleanup(Action): - type = "ignore" - - def run(self): - print "XXX Cleanup: %s" % self.pkg - class ActionScript(Action): def run(self): @@ -84,14 +68,20 @@ class ActionInstall(Action): type = "install" def run(self): - self._extract(_("Installing")) + # Add package to the database. + self.local.add_package(self.pkg) + + self.pkg.extract(_("Installing"), prefix=self.pakfire.path) class ActionUpdate(Action): type = "upgrade" def run(self): - self._extract(_("Updating")) + # Add new package to the database. + self.local.add_package(self.pkg) + + self.pkg.extract(_("Updating"), prefix=self.pakfire.path) class ActionRemove(Action): @@ -107,25 +97,54 @@ class ActionRemove(Action): def run(self): self.pkg.remove(_("Removing"), prefix=self.pakfire.path) - # XXX Remove package from database + # Remove package from the database. + self.local.rem_package(self.pkg) + + +class ActionCleanup(Action): + type = "ignore" + + def __init__(self, *args, **kwargs): + Action.__init__(self, *args, **kwargs) + + # XXX This is ugly, but works for the moment. + self.pkg = self.local.index.db.get_package_from_solv(self.pkg_solv) + assert self.pkg + + def run(self): + # Cleaning up leftover files and stuff. + self.pkg.cleanup(_("Cleanup"), prefix=self.pakfire.path) + + # Remove package from the database. + self.local.rem_package(self.pkg) class ActionReinstall(Action): type = "reinstall" def run(self): - self._extract(_("Installing")) + # Remove package from the database and add it afterwards. + # Sounds weird, but fixes broken entries in the database. + self.local.rem_package(self.pkg) + self.local.add_package(self.pkg) + + self.pkg.extract(_("Installing"), prefix=self.pakfire.path) class ActionDowngrade(Action): type = "downgrade" def run(self): - self._extract(_("Downgrading")) + # Add new package to database. + self.local.add_package(self.pkg) + + self.pkg.extract(_("Downgrading"), prefix=self.pakfire.path) class ActionChange(Action): type = "change" + # XXX still need to find out what this should be doing + def run(self): print "XXX Change: %s" % self.pkg diff --git a/pakfire/packages/base.py b/pakfire/packages/base.py index 53bf55915..dafad4f28 100644 --- a/pakfire/packages/base.py +++ b/pakfire/packages/base.py @@ -354,9 +354,6 @@ class Package(object): raise NotImplementedError, "%s" % repr(self) def remove(self, message=None, prefix=None): - if prefix in ("/", None): - prefix = "" - # Make two filelists. One contains all binary files that need to be # removed, the other one contains the configuration files which are # kept. files and configfiles are disjunct. @@ -370,6 +367,12 @@ class Package(object): assert file.startswith("/") files.append(file) + self._remove_files(files, message, prefix) + + def _remove_files(self, files, message, prefix): + if prefix in ("/", None): + prefix = "" + # Load progressbar. pb = None if message: diff --git a/pakfire/packages/installed.py b/pakfire/packages/installed.py index bf044af6d..96f275425 100644 --- a/pakfire/packages/installed.py +++ b/pakfire/packages/installed.py @@ -7,6 +7,7 @@ import pakfire.downloader from base import Package from binary import BinaryPackage +import pakfire.util as util from pakfire.constants import * class DatabasePackage(Package): @@ -230,6 +231,27 @@ class DatabasePackage(Package): filename = os.path.join(cache.path, cache_filename) return BinaryPackage(self.pakfire, self.repo, filename) + def cleanup(self, message, prefix): + c = self.db.cursor() + + # Get all files, that are in this package and check for all of + # them if they need to be removed. + files = self.filelist + + # Fetch the whole filelist of the system from the database and create + # a diff. Exclude files from this package - of course. + c.execute("SELECT name FROM files WHERE pkg != ?", (self.id,)) + + for row in c: + # Check if file in filelist. + if row["name"] in files: + files.remove(row["name"]) + + c.close() + + self._remove_files(files, message, prefix) + + # XXX maybe we can remove this later? class InstalledPackage(DatabasePackage): type = "installed" diff --git a/pakfire/repository/database.py b/pakfire/repository/database.py index e6d5aa7cb..4f21b7406 100644 --- a/pakfire/repository/database.py +++ b/pakfire/repository/database.py @@ -217,6 +217,28 @@ class DatabaseLocal(Database): c.close() + def rem_package(self, pkg): + logging.debug("Removing package from database: %s" % pkg.friendly_name) + + assert pkg.uuid + + # Get the ID of the package in the database. + c = self.cursor() + c.execute("SELECT id FROM packages WHERE uuid = ? LIMIT 1", (pkg.uuid,)) + + id = None + for row in c: + id = row["id"] + break + assert id + + # First, delete all files from the database and then delete the pkg itself. + c.execute("DELETE FROM files WHERE pkg = ?", (id,)) + c.execute("DELETE FROM packages WHERE id = ?", (id,)) + + c.close() + self.commit() + @property def packages(self): c = self.cursor() diff --git a/pakfire/repository/index.py b/pakfire/repository/index.py index 57eab3802..0e9346b88 100644 --- a/pakfire/repository/index.py +++ b/pakfire/repository/index.py @@ -156,6 +156,10 @@ class Index(object): rel = self.create_relation(file) solvable.add_provides(rel) + def rem_package(self, pkg): + # XXX delete the solvable from the index. + self.db.rem_package(pkg) + def clear(self): """ Forget all packages from memory. diff --git a/pakfire/repository/local.py b/pakfire/repository/local.py index ddfb7d8a6..c7d73befb 100644 --- a/pakfire/repository/local.py +++ b/pakfire/repository/local.py @@ -183,3 +183,7 @@ class RepositoryLocal(base.RepositoryFactory): self.index.db.add_package(pkg) self.index.add_package(pkg) + + def rem_package(self, pkg): + # Remove package from the database. + self.index.rem_package(pkg) diff --git a/po/pakfire.pot b/po/pakfire.pot index f695b91dc..31ba36705 100644 --- a/po/pakfire.pot +++ b/po/pakfire.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2011-07-31 15:33+0000\n" +"POT-Creation-Date: 2011-07-31 17:38+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -17,19 +17,24 @@ msgstr "" "Content-Type: text/plain; charset=CHARSET\n" "Content-Transfer-Encoding: 8bit\n" -#: ../pakfire/actions.py:87 ../pakfire/actions.py:117 +#: ../pakfire/actions.py:74 ../pakfire/actions.py:131 msgid "Installing" msgstr "" -#: ../pakfire/actions.py:94 +#: ../pakfire/actions.py:84 msgid "Updating" msgstr "" -#: ../pakfire/actions.py:108 +#: ../pakfire/actions.py:98 msgid "Removing" msgstr "" -#: ../pakfire/actions.py:124 +#. Cleaning up leftover files and stuff. +#: ../pakfire/actions.py:116 +msgid "Cleanup" +msgstr "" + +#: ../pakfire/actions.py:141 msgid "Downgrading" msgstr "" @@ -338,19 +343,19 @@ msgstr "" msgid "Requires" msgstr "" -#: ../pakfire/repository/index.py:227 +#: ../pakfire/repository/index.py:231 #, python-format msgid "%s: package database" msgstr "" #. Create progress bar. -#: ../pakfire/repository/index.py:315 +#: ../pakfire/repository/index.py:319 #, python-format msgid "Loading from %s" msgstr "" #. Add all packages from the database to the index. -#: ../pakfire/repository/index.py:368 +#: ../pakfire/repository/index.py:372 msgid "Loading installed packages" msgstr "" -- 2.39.5