From aa64aef50bec93f4e581bd93b8c6e3e7dcd212e3 Mon Sep 17 00:00:00 2001 From: Faik Uygur Date: Fri, 8 Feb 2008 13:27:33 +0000 Subject: [PATCH] Add pisi history command --- pisi/cli/history.py | 28 +++++++++++++++++++++++++++- pisi/db/historydb.py | 20 ++++++++++++++++---- pisi/history.py | 21 ++++++++++++++++++++- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/pisi/cli/history.py b/pisi/cli/history.py index f6632df6..733b29ea 100644 --- a/pisi/cli/history.py +++ b/pisi/cli/history.py @@ -10,8 +10,16 @@ # Please read the COPYING file. # +import optparse + +import gettext +__trans = gettext.translation('pisi', fallback=True) +_ = __trans.ugettext + import pisi import pisi.api +import pisi.db +import pisi.context as ctx import pisi.cli.command as command class History(command.Command): @@ -25,8 +33,26 @@ Lists previous operations.""" def __init__(self, args=None): super(History, self).__init__(args) + self.historydb = pisi.db.historydb.HistoryDB() name = ("history", "hs") + def options(self): + + group = optparse.OptionGroup(self.parser, _("history options")) + self.add_options(group) + self.parser.add_option_group(group) + + def add_options(self, group): + group.add_option("-l", "--last", action="store", type="int", default=0, + help=_("Output only the last n operations")) + def run(self): - pass + self.init(database = False, write = False) + for operation in self.historydb.latest_operations(ctx.get_option('last')): + print "Operation: %s" % operation.type + print "Date: %s %s" % (operation.date, operation.time) + print + for pkg in operation.packages: + print " *", pkg + print diff --git a/pisi/db/historydb.py b/pisi/db/historydb.py index ed8b2377..e509899b 100644 --- a/pisi/db/historydb.py +++ b/pisi/db/historydb.py @@ -10,22 +10,26 @@ # Please read the COPYING file. # +import os import gettext __trans = gettext.translation('pisi', fallback=True) _ = __trans.ugettext import piksemel +import pisi.context as ctx import pisi.db.lazydb as lazydb import pisi.history class HistoryDB(lazydb.LazyDB): def init(self): - pass - - def __generate_history(self, doc): - pass + self.__logs = self.__generate_history() + + def __generate_history(self): + logs = os.listdir(ctx.config.history_dir()) + logs.sort() + return logs def create(self, operation): self.history = pisi.history.History() @@ -34,3 +38,11 @@ class HistoryDB(lazydb.LazyDB): def update(self, pkgBefore=None, pkgAfter=None, operation=None): self.history.add(pkgBefore, pkgAfter, operation) self.history.update() + + def latest_operations(self, count=0): + if not count or count > len(self.__logs): + count = len(self.__logs) + + for log in self.__logs[-count:]: + hist = pisi.history.History(os.path.join(ctx.config.history_dir(), log)) + yield hist.operation diff --git a/pisi/history.py b/pisi/history.py index 77a1475b..f4b5bca1 100644 --- a/pisi/history.py +++ b/pisi/history.py @@ -29,7 +29,10 @@ class PackageInfo: a_build = [autoxml.String, autoxml.optional] def __str__(self): - return self.version + "-" + self.release + "-" + self.build + vrb = self.version + "-" + self.release + if self.build: + vrb += "-" + self.build + return vrb class Package: @@ -39,6 +42,22 @@ class Package: t_Before = [PackageInfo, autoxml.optional] t_After = [PackageInfo, autoxml.optional] + def __str__(self): + # "upgrade", "remove", "install", "reinstall", "downgrade" + operation = "" + if self.operation == "upgrade": + return "%s is upgraded from %s to %s." % (self.name, self.before, self.after) + elif self.operation == "remove": + return "%s %s is removed." % (self.name, self.before) + elif self.operation == "install": + return "%s %s is installed." % (self.name, self.after) + elif self.operation == "reinstall": + return "%s %s is reinstalled." % (self.name, self.after) + elif self.operation == "downgrade": + return "%s is downgraded from %s to %s." % (self.name, self.before, self.after) + else: + return "" + class Operation: a_type = [autoxml.String, autoxml.mandatory]