From 604818c4e08fcac138d8a46e709213cb8dd2bb93 Mon Sep 17 00:00:00 2001 From: Faik Uygur Date: Fri, 22 Feb 2008 09:46:31 +0000 Subject: [PATCH] pisi hs --takeback Takes back the system to the state after the given operation finished. Taking back to a snapshot and taking real action (installing and removing packages) is not done yet. But we are there already. :) --- pisi/api.py | 12 ++++++++ pisi/cli/history.py | 9 +++++- pisi/history.py | 2 +- pisi/operations/history.py | 57 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 pisi/operations/history.py diff --git a/pisi/api.py b/pisi/api.py index cf511daf..bca1dd66 100644 --- a/pisi/api.py +++ b/pisi/api.py @@ -39,6 +39,7 @@ import pisi.operations.delta import pisi.operations.remove import pisi.operations.upgrade import pisi.operations.install +import pisi.operations.history import pisi.operations.helper import pisi.operations.emerge import pisi.operations.build @@ -282,6 +283,17 @@ def install(packages, reinstall=False, ignore_file_conflicts=False): else: return pisi.operations.install.install_pkg_names(packages, reinstall) +def takeback(operation): + """ + Takes the system back to the state after the given operation no from the pisi history. + @param operation: operation number -> integer + """ + + historydb = pisi.db.historydb.HistoryDB() + historydb.create_history("takeback") + + pisi.operations.history.takeback(operation) + def snapshot(): """ Takes snapshot of the system packages. The snapshot is only a record of which packages are currently diff --git a/pisi/cli/history.py b/pisi/cli/history.py index bd7b9f38..4cda07a6 100644 --- a/pisi/cli/history.py +++ b/pisi/cli/history.py @@ -48,10 +48,15 @@ Lists previous operations.""" help=_("Output only the last n operations")) group.add_option("-s", "--snapshot", action="store_true", default=False, help=_("Take snapshot of the current system")) + group.add_option("-t", "--takeback", action="store", type="int", default=1, + help=_("Takeback to the state after the given operation finished")) def take_snapshot(self): pisi.api.snapshot() + def takeback(self, operation): + pisi.api.takeback(operation) + def print_history(self): for operation in self.historydb.get_last(ctx.get_option('last')): print _("Operation #%d: %s") % (operation.no, operation.type) @@ -62,7 +67,7 @@ Lists previous operations.""" print _(" * There are %d packages in this snapshot.") % len(operation.packages) else: for pkg in operation.packages: - print " *", pkg + print " *", pkg print def run(self): @@ -70,5 +75,7 @@ Lists previous operations.""" if ctx.get_option('snapshot'): self.take_snapshot() + elif ctx.get_option('takeback'): + self.takeback(ctx.get_option('takeback')) else: self.print_history() diff --git a/pisi/history.py b/pisi/history.py index b69c3366..c43fbd14 100644 --- a/pisi/history.py +++ b/pisi/history.py @@ -76,7 +76,7 @@ class History(xmlfile.XmlFile): def create(self, operation): - if operation not in ["upgrade", "remove", "emerge", "install", "snapshot"]: + if operation not in ["upgrade", "remove", "emerge", "install", "snapshot", "takeback"]: raise Exception("Unknown package operation") self.histfile = "%s_%s.xml" % (self._get_latest(), operation) diff --git a/pisi/operations/history.py b/pisi/operations/history.py new file mode 100644 index 00000000..7c2cd717 --- /dev/null +++ b/pisi/operations/history.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2008, TUBITAK/UEKAE +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free +# Software Foundation; either version 2 of the License, or (at your option) +# any later version. +# +# Please read the COPYING file. + +import gettext +__trans = gettext.translation("pisi", fallback=True) +_ = __trans.ugettext + +import pisi.context as ctx +import pisi.util +import pisi.db + +def __listactions(actions): + beinstalled = [] + beremoved = [] + + for pkg in actions: + action, version = actions[pkg] + if action == "install": + beinstalled.append("%s-%s" % (pkg, version)) + else: + beremoved.append("%s" % pkg) + + return beinstalled, beremoved + +def takeback(operation): + + historydb = pisi.db.historydb.HistoryDB() + actions = {} + + for operation in historydb.get_till_operation(operation): + if operation == "snapshot": + pass + + for pkg in operation.packages: + if pkg.operation in ["upgrade", "downgrade", "remove"]: + actions[pkg.name] = ("install", pkg.before) + if pkg.operation == "install": + actions[pkg.name] = ("remove", None) + + beinstalled, beremoved = __listactions(actions) + + if beinstalled: + ctx.ui.info(_("Following packages will be installed:\n") + pisi.util.strlist(beinstalled)) + + if beremoved: + ctx.ui.info(_("Following packages will be removed:\n") + pisi.util.strlist(beremoved)) + + if (beremoved or beinstalled) and not ctx.ui.confirm(_('Do you want to continue?')): + return