5 @contact: Debian FTPMaster <ftpmaster@debian.org>
6 @copyright: 2000, 2001, 2002, 2003, 2004, 2006 James Troup <james@nocrew.org>
7 @copyright: 2008-2009 Mark Hymers <mhy@debian.org>
8 @copyright: 2009 Joerg Jaspert <joerg@debian.org>
9 @copyright: 2009 Mike O'Connor <stew@debian.org>
10 @license: GNU General Public License version 2 or later
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 ################################################################################
29 # < mhy> I need a funny comment
30 # < sgran> two peanuts were walking down a dark street
31 # < sgran> one was a-salted
32 # * mhy looks up the definition of "funny"
34 ################################################################################
40 from singleton import Singleton
41 from config import Config
43 ################################################################################
46 def __init__(self, hashfunc=None):
48 self.hashfunc = hashfunc
50 self.hashfunc = lambda x: x['value']
54 def SetValue(self, keys, value):
55 self.data[self.hashfunc(keys)] = value
57 def GetValue(self, keys):
58 return self.data.get(self.hashfunc(keys))
60 ################################################################################
62 class DBConn(Singleton):
66 def __init__(self, *args, **kwargs):
67 super(DBConn, self).__init__(*args, **kwargs)
69 def _startup(self, *args, **kwargs):
73 ## Connection functions
74 def __createconn(self):
76 connstr = "dbname=%s" % "projectbstew" #cnf["DB::Name"]
77 print( "connstr: %s "% connstr)
79 connstr += " host=%s" % cnf["DB::Host"]
80 if cnf["DB::Port"] and cnf["DB::Port"] != "-1":
81 connstr += " port=%s" % cnf["DB::Port"]
83 self.db_con = psycopg2.connect(connstr)
88 except psycopg2.InterfaceError:
95 def __init_caches(self):
96 self.caches = {'suite': Cache(),
99 'override_type': Cache(),
100 'architecture': Cache(),
102 'component': Cache(),
103 'content_path_names': Cache(),
104 'content_file_names': Cache(),
105 'location': Cache(lambda x: '%s_%s_%s' % (x['location'], x['component'], x['location'])),
106 'maintainer': {}, # TODO
107 'keyring': {}, # TODO
108 'source': Cache(lambda x: '%s_%s_' % (x['source'], x['version'])),
109 'files': Cache(lambda x: '%s_%s_' % (x['filename'], x['location'])),
110 'maintainer': {}, # TODO
111 'fingerprint': {}, # TODO
114 'suite_version': Cache(lambda x: '%s_%s' % (x['source'], x['suite'])),
117 self.prepared_statements = {}
119 def prepare(self,name,statement):
120 if not self.prepared_statements.has_key(name):
123 self.prepared_statements[name] = statement
125 def clear_caches(self):
128 ## Functions to pass through to the database connector
130 return self.db_con.cursor()
133 return self.db_con.commit()
136 def __get_single_id(self, query, values, cachename=None):
137 # This is a bit of a hack but it's an internal function only
138 if cachename is not None:
139 res = self.caches[cachename].GetValue(values)
143 c = self.db_con.cursor()
144 c.execute(query, values)
149 res = c.fetchone()[0]
151 if cachename is not None:
152 self.caches[cachename].SetValue(values, res)
156 def __get_id(self, retfield, table, qfield, value):
157 query = "SELECT %s FROM %s WHERE %s = %%(value)s" % (retfield, table, qfield)
158 return self.__get_single_id(query, {'value': value}, cachename=table)
160 def get_suite_id(self, suite):
162 Returns database id for given C{suite}.
163 Results are kept in a cache during runtime to minimize database queries.
166 @param suite: The name of the suite
169 @return: the database id for the given suite
172 suiteid = self.__get_id('id', 'suite', 'suite_name', suite)
178 def get_section_id(self, section):
180 Returns database id for given C{section}.
181 Results are kept in a cache during runtime to minimize database queries.
183 @type section: string
184 @param section: The name of the section
187 @return: the database id for the given section
190 return self.__get_id('id', 'section', 'section', section)
192 def get_priority_id(self, priority):
194 Returns database id for given C{priority}.
195 Results are kept in a cache during runtime to minimize database queries.
197 @type priority: string
198 @param priority: The name of the priority
201 @return: the database id for the given priority
204 return self.__get_id('id', 'priority', 'priority', priority)
206 def get_override_type_id(self, override_type):
208 Returns database id for given override C{type}.
209 Results are kept in a cache during runtime to minimize database queries.
211 @type override_type: string
212 @param override_type: The name of the override type
215 @return: the database id for the given override type
218 return self.__get_id('id', 'override_type', 'type', override_type)
220 def get_architecture_id(self, architecture):
222 Returns database id for given C{architecture}.
223 Results are kept in a cache during runtime to minimize database queries.
225 @type architecture: string
226 @param architecture: The name of the override type
229 @return: the database id for the given architecture
232 return self.__get_id('id', 'architecture', 'arch_string', architecture)
234 def get_archive_id(self, archive):
236 returns database id for given c{archive}.
237 results are kept in a cache during runtime to minimize database queries.
239 @type archive: string
240 @param archive: the name of the override type
243 @return: the database id for the given archive
246 return self.__get_id('id', 'archive', 'lower(name)', archive)
248 def get_component_id(self, component):
250 Returns database id for given C{component}.
251 Results are kept in a cache during runtime to minimize database queries.
253 @type component: string
254 @param component: The name of the override type
257 @return: the database id for the given component
260 return self.__get_id('id', 'component', 'lower(name)', component)
262 def get_location_id(self, location, component, archive):
264 Returns database id for the location behind the given combination of
265 - B{location} - the path of the location, eg. I{/srv/ftp.debian.org/ftp/pool/}
266 - B{component} - the id of the component as returned by L{get_component_id}
267 - B{archive} - the id of the archive as returned by L{get_archive_id}
268 Results are kept in a cache during runtime to minimize database queries.
270 @type location: string
271 @param location: the path of the location
274 @param component: the id of the component
277 @param archive: the id of the archive
280 @return: the database id for the location
284 archive_id = self.get_archive_id(archive)
292 component_id = self.get_component_id(component)
294 res = self.__get_single_id("SELECT id FROM location WHERE path=%(location)s AND component=%(component)s AND archive=%(archive)s",
295 {'location': location,
296 'archive': int(archive_id),
297 'component': component_id}, cachename='location')
299 res = self.__get_single_id("SELECT id FROM location WHERE path=%(location)s AND archive=%(archive)d",
300 {'location': location, 'archive': archive_id, 'component': ''}, cachename='location')
304 def get_source_id(self, source, version):
306 Returns database id for the combination of C{source} and C{version}
307 - B{source} - source package name, eg. I{mailfilter}, I{bbdb}, I{glibc}
309 Results are kept in a cache during runtime to minimize database queries.
312 @param source: source package name
314 @type version: string
315 @param version: the source version
318 @return: the database id for the source
321 return self.__get_single_id("SELECT id FROM source s WHERE s.source=%(source)s AND s.version=%(version)s",
322 {'source': source, 'version': version}, cachename='source')
324 def get_suite_version(self, source, suite):
326 Returns database id for a combination of C{source} and C{suite}.
328 - B{source} - source package name, eg. I{mailfilter}, I{bbdb}, I{glibc}
329 - B{suite} - a suite name, eg. I{unstable}
331 Results are kept in a cache during runtime to minimize database queries.
334 @param source: source package name
337 @param suite: the suite name
340 @return: the version for I{source} in I{suite}
343 return self.__get_single_id("""
344 SELECT s.version FROM source s, suite su, src_associations sa
347 AND su.suite_name=%(suite)s
348 AND s.source=%(source)""", {'suite': suite, 'source': source}, cachename='suite_version')
351 def get_files_id (self, filename, size, md5sum, location_id):
353 Returns -1, -2 or the file_id for filename, if its C{size} and C{md5sum} match an
356 The database is queried using the C{filename} and C{location_id}. If a file does exist
357 at that location, the existing size and md5sum are checked against the provided
358 parameters. A size or checksum mismatch returns -2. If more than one entry is
359 found within the database, a -1 is returned, no result returns None, otherwise
362 Results are kept in a cache during runtime to minimize database queries.
364 @type filename: string
365 @param filename: the filename of the file to check against the DB
368 @param size: the size of the file to check against the DB
371 @param md5sum: the md5sum of the file to check against the DB
373 @type location_id: int
374 @param location_id: the id of the location as returned by L{get_location_id}
377 @return: Various return values are possible:
378 - -2: size/checksum error
379 - -1: more than one file found in database
380 - None: no file found in database
384 values = {'filename' : filename,
385 'location' : location_id}
387 res = self.caches['files'].GetValue( values )
390 query = """SELECT id, size, md5sum
392 WHERE filename = %(filename)s AND location = %(location)s"""
394 cursor = self.db_con.cursor()
395 cursor.execute( query, values )
397 if cursor.rowcount == 0:
400 elif cursor.rowcount != 1:
404 row = cursor.fetchone()
406 if row[1] != int(size) or row[2] != md5sum:
410 self.caches['files'].SetValue(values, row[0])
416 def get_or_set_contents_file_id(self, filename):
418 Returns database id for given filename.
420 Results are kept in a cache during runtime to minimize database queries.
421 If no matching file is found, a row is inserted.
423 @type filename: string
424 @param filename: The filename
427 @return: the database id for the given component
430 values={'value': filename}
431 query = "SELECT id FROM content_file_names WHERE file = %(value)s"
432 id = self.__get_single_id(query, values, cachename='content_file_names')
434 c = self.db_con.cursor()
435 c.execute( "INSERT INTO content_file_names VALUES (DEFAULT, %(value)s) RETURNING id",
439 self.caches['content_file_names'].SetValue(values, id)
443 traceback.print_exc()
446 def get_or_set_contents_path_id(self, path):
448 Returns database id for given path.
450 Results are kept in a cache during runtime to minimize database queries.
451 If no matching file is found, a row is inserted.
454 @param path: The filename
457 @return: the database id for the given component
460 values={'value': path}
461 query = "SELECT id FROM content_file_paths WHERE path = %(value)s"
462 id = self.__get_single_id(query, values, cachename='content_path_names')
464 c = self.db_con.cursor()
465 c.execute( "INSERT INTO content_file_paths VALUES (DEFAULT, %(value)s) RETURNING id",
469 self.caches['content_path_names'].SetValue(values, id)
473 traceback.print_exc()
476 def get_suite_architectures(self, suite):
478 Returns list of architectures for C{suite}.
480 @type suite: string, int
481 @param suite: the suite name or the suite_id
484 @return: the list of architectures for I{suite}
488 if type(suite) == str:
489 suite_id = self.get_suite_id(suite)
490 elif type(suite) == int:
495 c = self.db_con.cursor()
496 c.execute( """SELECT a.arch_string FROM suite_architectures sa
497 JOIN architecture a ON (a.id = sa.architecture)
498 WHERE suite='%s'""" % suite_id )
500 return map(lambda x: x[0], c.fetchall())
502 def insert_content_paths(self, bin_id, fullpaths):
504 Make sure given path is associated with given binary id
507 @param bin_id: the id of the binary
508 @type fullpaths: list
509 @param fullpaths: the list of paths of the file being associated with the binary
511 @return: True upon success
514 c = self.db_con.cursor()
516 c.execute("BEGIN WORK")
519 for fullpath in fullpaths:
520 (path, file) = os.path.split(fullpath)
522 if path.startswith( "./" ):
524 # Get the necessary IDs ...
525 file_id = self.get_or_set_contents_file_id(file)
526 path_id = self.get_or_set_contents_path_id(path)
528 c.execute("""INSERT INTO deb_contents
530 (binary_pkg, filepath, filename)
531 VALUES ( '%d', '%d', '%d')""" % (bin_id, path_id, file_id) )
536 traceback.print_exc()
537 c.execute("ROLLBACK")
540 def insert_pending_content_paths(self, package, fullpaths):
542 Make sure given paths are temporarily associated with given
546 @param package: the package to associate with should have been read in from the binary control file
547 @type fullpaths: list
548 @param fullpaths: the list of paths of the file being associated with the binary
550 @return: True upon success
553 c = self.db_con.cursor()
555 c.execute("BEGIN WORK")
557 arch_id = self.get_architecture_id(package['Architecture'])
559 # Remove any already existing recorded files for this package
560 c.execute("""DELETE FROM pending_content_associations
561 WHERE package=%(Package)s
562 AND version=%(Version)s
563 AND architecture=%(ArchID)s""", {'Package': package['Package'],
564 'Version': package['Version'],
567 for fullpath in fullpaths:
568 (path, file) = os.path.split(fullpath)
570 if path.startswith( "./" ):
572 # Get the necessary IDs ...
573 file_id = self.get_or_set_contents_file_id(file)
574 path_id = self.get_or_set_contents_path_id(path)
576 c.execute("""INSERT INTO pending_content_associations
577 (package, version, architecture, filepath, filename)
578 VALUES (%%(Package)s, %%(Version)s, '%d', '%d', '%d')"""
579 % (arch_id, path_id, file_id), package )
584 traceback.print_exc()
585 c.execute("ROLLBACK")