4 Class providing access to a projectb database
6 This class provides convenience functions for common queries to a
7 projectb database using psycopg2.
9 Copyright (C) 2009 Mike O'Connor <stew@vireo.org>
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ################################################################################
30 ################################################################################
32 class Projectb(object):
34 Object providing methods for accessing the projectb database
36 def __init__(self,Cnf):
37 connect_str = "dbname=%s"% (Cnf["DB::Name"])
38 if Cnf["DB::Host"] != '': connect_str += " host=%s" % (Cnf["DB::Host"])
39 if Cnf["DB::Port"] != '-1': connect_str += " port=%d" % (int(Cnf["DB::Port"]))
41 self.dbh = psycopg2.connect(connect_str)
42 self.suite_id_cache = {}
43 self.architecture_id_cache = {}
44 self.section_id_cache = {}
46 def get_suite_id(self, suite_name):
48 return the id for the given suite_name
50 @param suite_name: name of a suite such as "unsatble" or "testing"
53 @return: id of given suite or None if suite_name not matched
55 >>> Cnf = {'DB::Name' : "projectb","DB::Host":"","DB::Port":'-1' }
56 >>> pb = Projectb( Cnf )
57 >>> pb.get_suite_id("unstable")
59 >>> pb.get_suite_id("n'existe pas")
61 if not self.suite_id_cache.has_key(suite_name):
63 c.execute("SELECT id FROM suite WHERE suite_name=%(suite_name)s",
64 {'suite_name':suite_name})
67 self.suite_id_cache[suite_name] = r[0]
69 self.suite_id_cache[suite_name] = None
71 return self.suite_id_cache[suite_name]
73 def get_architecture_id(self, architecture_name):
75 return the id for the given architecture_name
77 @param architecture_name: name of a architecture such as "i386" or "source"
80 @return: id of given architecture or None if architecture_name not matched
82 >>> Cnf = {'DB::Name' : "projectb","DB::Host":"","DB::Port":'-1' }
83 >>> pb = Projectb( Cnf )
84 >>> pb.get_architecture_id("i386")
86 >>> pb.get_architecture_id("n'existe pas")
88 if not self.architecture_id_cache.has_key(architecture_name):
90 c.execute("SELECT id FROM architecture WHERE arch_string=%(architecture_name)s",
91 {'architecture_name':architecture_name})
94 self.architecture_id_cache[architecture_name] = r[0]
96 self.architecture_id_cache[architecture_name] = None
98 return self.architecture_id_cache[architecture_name]
100 def get_section_id(self, section_name):
102 return the id for the given section_name
104 @param section_name: name of a section such as "x11" or "non-free/libs"
107 @return: id of given section or None if section_name not matched
109 >>> Cnf = {'DB::Name' : "projectb","DB::Host":"","DB::Port":'-1' }
110 >>> pb = Projectb( Cnf )
111 >>> pb.get_section_id("non-free/libs")
113 >>> pb.get_section_id("n'existe pas")
115 if not self.section_id_cache.has_key(section_name):
116 c = self.dbh.cursor()
117 c.execute("SELECT id FROM section WHERE section=%(section_name)s",
118 {'section_name':section_name})
121 self.section_id_cache[section_name] = r[0]
123 self.section_id_cache[section_name] = None
125 return self.section_id_cache[section_name]
127 if __name__ == "__main__":