From 8127a83e045c230d26d56d6a774ae9258167e57c Mon Sep 17 00:00:00 2001 From: Faik Uygur Date: Mon, 15 Jun 2009 11:16:37 +0000 Subject: [PATCH] Add groupdb --- pisi/api.py | 1 + pisi/db/__init__.py | 8 ++++-- pisi/db/groupdb.py | 68 +++++++++++++++++++++++++++++++++++++++++++++ pisi/group.py | 2 +- pisi/index.py | 6 ++-- 5 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 pisi/db/groupdb.py diff --git a/pisi/api.py b/pisi/api.py index a2558a12..b1b14040 100644 --- a/pisi/api.py +++ b/pisi/api.py @@ -32,6 +32,7 @@ import pisi.db.installdb import pisi.db.historydb import pisi.db.sourcedb import pisi.db.componentdb +import pisi.db.groupdb import pisi.index import pisi.config import pisi.metadata diff --git a/pisi/db/__init__.py b/pisi/db/__init__.py index 4956a8de..f4a90329 100644 --- a/pisi/db/__init__.py +++ b/pisi/db/__init__.py @@ -13,17 +13,19 @@ def invalidate_caches(): # Invalidates pisi caches in use and forces to re-fill caches from disk when needed for db in [packagedb.PackageDB(), sourcedb.SourceDB(), componentdb.ComponentDB(), - installdb.InstallDB(), historydb.HistoryDB()]: + installdb.InstallDB(), historydb.HistoryDB(), groupdb.GroupDB()]: db.invalidate() def flush_caches(): # Invalidate and flush caches to re-generate them when needed - for db in [packagedb.PackageDB(), sourcedb.SourceDB(), componentdb.ComponentDB()]: + for db in [packagedb.PackageDB(), sourcedb.SourceDB(), + componentdb.ComponentDB(), groupdb.GroupDB()]: db.invalidate() db.cache_flush() def update_caches(): # Updates ondisk caches - for db in [packagedb.PackageDB(), sourcedb.SourceDB(), componentdb.ComponentDB(), installdb.InstallDB()]: + for db in [packagedb.PackageDB(), sourcedb.SourceDB(), componentdb.ComponentDB(), + installdb.InstallDB(), groupdb.GroupDB()]: if db.is_initialized(): db.cache_save() diff --git a/pisi/db/groupdb.py b/pisi/db/groupdb.py new file mode 100644 index 00000000..26778ef1 --- /dev/null +++ b/pisi/db/groupdb.py @@ -0,0 +1,68 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2009, 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 +import pisi.db.repodb +import pisi.db.itembyrepo +import pisi.group +import pisi.db.lazydb as lazydb + +class GroupDB(lazydb.LazyDB): + + def __init__(self): + lazydb.LazyDB.__init__(self, cacheable=True) + + def init(self): + group_nodes = {} + group_components = {} + + self.repodb = pisi.db.repodb.RepoDB() + + for repo in self.repodb.list_repos(): + doc = self.repodb.get_repo_doc(repo) + group_nodes[repo] = self.__generate_groups(doc) + group_components[repo] = self.__generate_components(doc) + + self.gdb = pisi.db.itembyrepo.ItemByRepo(group_nodes) + self.gcdb = pisi.db.itembyrepo.ItemByRepo(group_components) + + def __generate_components(self, doc): + groups = {} + for c in doc.tags("Component"): + groups.setdefault(c.getTagData("Group"), []).append(c.getTagData("Name")) + return groups + + def __generate_groups(self, doc): + return dict(map(lambda x: (x.getTagData("Name"), x.toString()), doc.tags("Group"))) + + def has_group(self, name, repo = None): + return self.gdb.has_item(name, repo) + + def list_groups(self, repo=None): + return self.gdb.get_item_keys(repo) + + def get_group(self, name, repo = None): + + if not self.has_group(name, repo): + raise Exception(_('Group %s not found') % name) + + group = pisi.group.Group() + group.parse(self.gdb.get_item(name, repo)) + + return group + + def get_group_components(self, name, repo=None): + return self.gcdb.get_item(name, repo) diff --git a/pisi/group.py b/pisi/group.py index e8b07547..992b2905 100644 --- a/pisi/group.py +++ b/pisi/group.py @@ -23,7 +23,7 @@ class Error(pisi.Error): __metaclass__ = autoxml.autoxml -class Group: +class Group(xmlfile.XmlFile): "representation for group declarations" __metaclass__ = autoxml.autoxml diff --git a/pisi/index.py b/pisi/index.py index 66145b48..a9013a00 100644 --- a/pisi/index.py +++ b/pisi/index.py @@ -29,6 +29,7 @@ import pisi.pxml.xmlfile as xmlfile import pisi.file import pisi.pxml.autoxml as autoxml import pisi.component as component +import pisi.group as group class Error(pisi.Error): pass @@ -43,6 +44,7 @@ class Index(xmlfile.XmlFile): t_Packages = [ [metadata.Package], autoxml.optional, "Package"] #t_Metadatas = [ [metadata.MetaData], autoxml.optional, "MetaData"] t_Components = [ [component.Component], autoxml.optional, "Component"] + t_Groups = [ [group.Group], autoxml.optional, "Group"] def name(self): return self.distribution.name + self.distribution.repositoryname @@ -153,8 +155,8 @@ class Index(xmlfile.XmlFile): ctx.ui.info("Adding groups.xml to index...") groups_xml = group.Groups() groups_xml.read(path) - for group in groups_xml.groups: - self.groups.append(group) + for grp in groups_xml.groups: + self.groups.append(grp) def add_components(self, path): ctx.ui.info("Adding components.xml to index...")