# GLINT graphical package management
# Copyright (C) 1995 Red Hat, Inc
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

from package import *
import regex
import rpm

class RPMDatabase:

    def OpenDatabase(self, Prefix):
	self.prefix = Prefix
	self.reopen()

    def containsPackageLabel(self, label):
	return self.headers.has_key(label)

    def packageLabels(self):
	return self.headers.keys()

    def packageNames(self):
	return self.nameidx.keys()

    def getHeader(self, label):
	return self.headers[label]

    def reopen(self):
	self.db = rpm.opendb()
	self.headers = {}

	key = self.db.firstkey()
	while (key):
	    h = self.db[key]
	    name = h[rpm.RPMTAG_NAME]
	    version = h[rpm.RPMTAG_VERSION]
	    release = h[rpm.RPMTAG_RELEASE]

	    label = name + ":" + version + ":" + release
	    self.headers[label] = h

	    key = self.db.nextkey(key)

    def close(self):
	del self.db

    def __init__(self, Prefix):
	self.headers = {}
	self.OpenDatabase(Prefix)

class InstalledPackage(HeaderPackage):

    def __init__(self, label, db):
	self.db = db

	h = self.db.getHeader(label)
	HeaderPackage.__init__(self, h)

