3 """ DB access functions
4 @group readonly: get_suite_id, get_section_id, get_priority_id, get_override_type_id,
5 get_architecture_id, get_archive_id, get_component_id, get_location_id,
6 get_source_id, get_suite_version, get_files_id, get_maintainer, get_suites,
7 get_suite_architectures
8 @group read/write: get_or_set*, set_files_id
10 @contact: Debian FTP Master <ftpmaster@debian.org>
11 @copyright: 2000, 2001, 2002, 2003, 2004, 2006 James Troup <james@nocrew.org>
12 @copyright: 2009 Joerg Jaspert <joerg@debian.org>
13 @license: GNU General Public License version 2 or later
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 ################################################################################
36 ################################################################################
38 Cnf = None #: Configuration, apt_pkg.Configuration
39 projectB = None #: database connection, pgobject
40 suite_id_cache = {} #: cache for suites
41 section_id_cache = {} #: cache for sections
42 priority_id_cache = {} #: cache for priorities
43 override_type_id_cache = {} #: cache for overrides
44 architecture_id_cache = {} #: cache for architectures
45 archive_id_cache = {} #: cache for archives
46 component_id_cache = {} #: cache for components
47 location_id_cache = {} #: cache for locations
48 maintainer_id_cache = {} #: cache for maintainers
49 keyring_id_cache = {} #: cache for keyrings
50 source_id_cache = {} #: cache for sources
51 files_id_cache = {} #: cache for files
52 maintainer_cache = {} #: cache for maintainer names
53 fingerprint_id_cache = {} #: cache for fingerprints
54 queue_id_cache = {} #: cache for queues
55 uid_id_cache = {} #: cache for uids
56 suite_version_cache = {} #: cache for suite_versions (packages)
58 ################################################################################
60 def init (config, sql):
64 @type config: apt_pkg.Configuration
65 @param config: apt config, see U{http://apt.alioth.debian.org/python-apt-doc/apt_pkg/cache.html#Configuration}
68 @param sql: database connection
79 Executes a database query. Writes statistics / timing to stderr.
82 @param query: database query string, passed unmodified
86 @warning: The query is passed B{unmodified}, so be careful what you use this for.
88 sys.stderr.write("query: \"%s\" ... " % (query))
90 r = projectB.query(query)
91 time_diff = time.time()-before
92 sys.stderr.write("took %.3f seconds.\n" % (time_diff))
94 sys.stderr.write("int result: %s\n" % (r))
95 elif type(r) is types.NoneType:
96 sys.stderr.write("result: None\n")
98 sys.stderr.write("pgresult: %s\n" % (r.getresult()))
101 ################################################################################
103 def get_suite_id (suite):
105 Returns database id for given C{suite}.
106 Results are kept in a cache during runtime to minimize database queries.
109 @param suite: The name of the suite
112 @return: the database id for the given suite
115 global suite_id_cache
117 if suite_id_cache.has_key(suite):
118 return suite_id_cache[suite]
120 q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
126 suite_id_cache[suite] = suite_id
130 def get_section_id (section):
132 Returns database id for given C{section}.
133 Results are kept in a cache during runtime to minimize database queries.
135 @type section: string
136 @param section: The name of the section
139 @return: the database id for the given section
142 global section_id_cache
144 if section_id_cache.has_key(section):
145 return section_id_cache[section]
147 q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
152 section_id = ql[0][0]
153 section_id_cache[section] = section_id
157 def get_priority_id (priority):
159 Returns database id for given C{priority}.
160 Results are kept in a cache during runtime to minimize database queries.
162 @type priority: string
163 @param priority: The name of the priority
166 @return: the database id for the given priority
169 global priority_id_cache
171 if priority_id_cache.has_key(priority):
172 return priority_id_cache[priority]
174 q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
179 priority_id = ql[0][0]
180 priority_id_cache[priority] = priority_id
184 def get_override_type_id (type):
186 Returns database id for given override C{type}.
187 Results are kept in a cache during runtime to minimize database queries.
190 @param type: The name of the override type
193 @return: the database id for the given override type
196 global override_type_id_cache
198 if override_type_id_cache.has_key(type):
199 return override_type_id_cache[type]
201 q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type))
206 override_type_id = ql[0][0]
207 override_type_id_cache[type] = override_type_id
209 return override_type_id
211 def get_architecture_id (architecture):
213 Returns database id for given C{architecture}.
214 Results are kept in a cache during runtime to minimize database queries.
216 @type architecture: string
217 @param architecture: The name of the override type
220 @return: the database id for the given architecture
223 global architecture_id_cache
225 if architecture_id_cache.has_key(architecture):
226 return architecture_id_cache[architecture]
228 q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
233 architecture_id = ql[0][0]
234 architecture_id_cache[architecture] = architecture_id
236 return architecture_id
238 def get_archive_id (archive):
240 Returns database id for given C{archive}.
241 Results are kept in a cache during runtime to minimize database queries.
243 @type archive: string
244 @param archive: The name of the override type
247 @return: the database id for the given archive
250 global archive_id_cache
252 archive = archive.lower()
254 if archive_id_cache.has_key(archive):
255 return archive_id_cache[archive]
257 q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive))
262 archive_id = ql[0][0]
263 archive_id_cache[archive] = archive_id
267 def get_component_id (component):
269 Returns database id for given C{component}.
270 Results are kept in a cache during runtime to minimize database queries.
272 @type component: string
273 @param component: The name of the component
276 @return: the database id for the given component
279 global component_id_cache
281 component = component.lower()
283 if component_id_cache.has_key(component):
284 return component_id_cache[component]
286 q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
291 component_id = ql[0][0]
292 component_id_cache[component] = component_id
296 def get_location_id (location, component, archive):
298 Returns database id for the location behind the given combination of
299 - B{location} - the path of the location, eg. I{/srv/ftp.debian.org/ftp/pool/}
300 - B{component} - the id of the component as returned by L{get_component_id}
301 - B{archive} - the id of the archive as returned by L{get_archive_id}
302 Results are kept in a cache during runtime to minimize database queries.
304 @type location: string
305 @param location: the path of the location
308 @param component: the id of the component
311 @param archive: the id of the archive
314 @return: the database id for the location
317 global location_id_cache
319 cache_key = location + '_' + component + '_' + location
320 if location_id_cache.has_key(cache_key):
321 return location_id_cache[cache_key]
323 archive_id = get_archive_id (archive)
325 component_id = get_component_id (component)
326 if component_id != -1:
327 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
329 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
334 location_id = ql[0][0]
335 location_id_cache[cache_key] = location_id
339 def get_source_id (source, version):
341 Returns database id for the combination of C{source} and C{version}
342 - B{source} - source package name, eg. I{mailfilter}, I{bbdb}, I{glibc}
344 Results are kept in a cache during runtime to minimize database queries.
347 @param source: source package name
349 @type version: string
350 @param version: the source version
353 @return: the database id for the source
356 global source_id_cache
358 cache_key = source + '_' + version + '_'
359 if source_id_cache.has_key(cache_key):
360 return source_id_cache[cache_key]
362 q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
364 if not q.getresult():
367 source_id = q.getresult()[0][0]
368 source_id_cache[cache_key] = source_id
372 def get_suite_version(source, suite):
374 Returns database id for a combination of C{source} and C{suite}.
376 - B{source} - source package name, eg. I{mailfilter}, I{bbdb}, I{glibc}
377 - B{suite} - a suite name, eg. I{unstable}
379 Results are kept in a cache during runtime to minimize database queries.
382 @param source: source package name
385 @param suite: the suite name
388 @return: the version for I{source} in I{suite}
391 global suite_version_cache
392 cache_key = "%s_%s" % (source, suite)
394 if suite_version_cache.has_key(cache_key):
395 return suite_version_cache[cache_key]
397 q = projectB.query("""
398 SELECT s.version FROM source s, suite su, src_associations sa
401 AND su.suite_name='%s'
405 if not q.getresult():
408 version = q.getresult()[0][0]
409 suite_version_cache[cache_key] = version
413 def get_suite_architectures(suite):
415 Returns list of architectures for C{suite}.
417 @type suite: string, int
418 @param suite: the suite name or the suite_id
421 @return: the list of architectures for I{suite}
425 if type(suite) == str:
426 suite_id = get_suite_id(suite)
427 elif type(suite) == int:
432 sql = """ SELECT a.arch_string FROM suite_architectures sa
433 JOIN architecture a ON (a.id = sa.architecture)
434 WHERE suite='%s' """ % (suite_id)
436 q = projectB.query(sql)
437 return map(lambda x: x[0], q.getresult())
440 ################################################################################
442 def get_or_set_maintainer_id (maintainer):
444 If C{maintainer} does not have an entry in the maintainer table yet, create one
445 and return the new id.
446 If C{maintainer} already has an entry, simply return the existing id.
448 Results are kept in a cache during runtime to minimize database queries.
450 @type maintainer: string
451 @param maintainer: the maintainer name
454 @return: the database id for the maintainer
457 global maintainer_id_cache
459 if maintainer_id_cache.has_key(maintainer):
460 return maintainer_id_cache[maintainer]
462 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
463 if not q.getresult():
464 projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
465 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
466 maintainer_id = q.getresult()[0][0]
467 maintainer_id_cache[maintainer] = maintainer_id
471 ################################################################################
473 def get_or_set_keyring_id (keyring):
475 If C{keyring} does not have an entry in the C{keyrings} table yet, create one
476 and return the new id.
477 If C{keyring} already has an entry, simply return the existing id.
479 Results are kept in a cache during runtime to minimize database queries.
481 @type keyring: string
482 @param keyring: the keyring name
485 @return: the database id for the keyring
488 global keyring_id_cache
490 if keyring_id_cache.has_key(keyring):
491 return keyring_id_cache[keyring]
493 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
494 if not q.getresult():
495 projectB.query("INSERT INTO keyrings (name) VALUES ('%s')" % (keyring))
496 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
497 keyring_id = q.getresult()[0][0]
498 keyring_id_cache[keyring] = keyring_id
502 ################################################################################
504 def get_or_set_uid_id (uid):
506 If C{uid} does not have an entry in the uid table yet, create one
507 and return the new id.
508 If C{uid} already has an entry, simply return the existing id.
510 Results are kept in a cache during runtime to minimize database queries.
516 @return: the database id for the uid
522 if uid_id_cache.has_key(uid):
523 return uid_id_cache[uid]
525 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
526 if not q.getresult():
527 projectB.query("INSERT INTO uid (uid) VALUES ('%s')" % (uid))
528 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
529 uid_id = q.getresult()[0][0]
530 uid_id_cache[uid] = uid_id
534 ################################################################################
536 def get_or_set_fingerprint_id (fingerprint):
538 If C{fingerprint} does not have an entry in the fingerprint table yet, create one
539 and return the new id.
540 If C{fingerprint} already has an entry, simply return the existing id.
542 Results are kept in a cache during runtime to minimize database queries.
544 @type fingerprint: string
545 @param fingerprint: the fingerprint
548 @return: the database id for the fingerprint
551 global fingerprint_id_cache
553 if fingerprint_id_cache.has_key(fingerprint):
554 return fingerprint_id_cache[fingerprint]
556 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
557 if not q.getresult():
558 projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
559 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
560 fingerprint_id = q.getresult()[0][0]
561 fingerprint_id_cache[fingerprint] = fingerprint_id
563 return fingerprint_id
565 ################################################################################
567 def get_files_id (filename, size, md5sum, location_id):
569 Returns -1, -2 or the file_id for filename, if its C{size} and C{md5sum} match an
572 The database is queried using the C{filename} and C{location_id}. If a file does exist
573 at that location, the existing size and md5sum are checked against the provided
574 parameters. A size or checksum mismatch returns -2. If more than one entry is
575 found within the database, a -1 is returned, no result returns None, otherwise
578 Results are kept in a cache during runtime to minimize database queries.
580 @type filename: string
581 @param filename: the filename of the file to check against the DB
584 @param size: the size of the file to check against the DB
587 @param md5sum: the md5sum of the file to check against the DB
589 @type location_id: int
590 @param location_id: the id of the location as returned by L{get_location_id}
593 @return: Various return values are possible:
594 - -2: size/checksum error
595 - -1: more than one file found in database
596 - None: no file found in database
600 global files_id_cache
602 cache_key = "%s_%d" % (filename, location_id)
604 if files_id_cache.has_key(cache_key):
605 return files_id_cache[cache_key]
608 q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id))
614 orig_size = int(ql[1])
616 if orig_size != size or orig_md5sum != md5sum:
618 files_id_cache[cache_key] = ql[0]
619 return files_id_cache[cache_key]
623 ################################################################################
625 def get_or_set_queue_id (queue):
627 If C{queue} does not have an entry in the queue table yet, create one
628 and return the new id.
629 If C{queue} already has an entry, simply return the existing id.
631 Results are kept in a cache during runtime to minimize database queries.
634 @param queue: the queue name (no full path)
637 @return: the database id for the queue
640 global queue_id_cache
642 if queue_id_cache.has_key(queue):
643 return queue_id_cache[queue]
645 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
646 if not q.getresult():
647 projectB.query("INSERT INTO queue (queue_name) VALUES ('%s')" % (queue))
648 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
649 queue_id = q.getresult()[0][0]
650 queue_id_cache[queue] = queue_id
654 ################################################################################
656 def set_files_id (filename, size, md5sum, sha1sum, sha256sum, location_id):
658 Insert a new entry into the files table and return its id.
660 @type filename: string
661 @param filename: the filename
664 @param size: the size in bytes
667 @param md5sum: md5sum of the file
669 @type sha1sum: string
670 @param sha1sum: sha1sum of the file
672 @type sha256sum: string
673 @param sha256sum: sha256sum of the file
675 @type location_id: int
676 @param location_id: the id of the location as returned by L{get_location_id}
679 @return: the database id for the new file
682 global files_id_cache
684 projectB.query("INSERT INTO files (filename, size, md5sum, sha1sum, sha256sum, location) VALUES ('%s', %d, '%s', '%s', '%s', %d)" % (filename, long(size), md5sum, sha1sum, sha256sum, location_id))
686 return get_files_id (filename, size, md5sum, location_id)
688 ### currval has issues with postgresql 7.1.3 when the table is big
689 ### it was taking ~3 seconds to return on auric which is very Not
692 ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')")
693 ##ql = q.getresult()[0]
694 ##cache_key = "%s_%d" % (filename, location_id)
695 ##files_id_cache[cache_key] = ql[0]
696 ##return files_id_cache[cache_key]
698 ################################################################################
700 def get_maintainer (maintainer_id):
702 Return the name of the maintainer behind C{maintainer_id}.
704 Results are kept in a cache during runtime to minimize database queries.
706 @type maintainer_id: int
707 @param maintainer_id: the id of the maintainer, eg. from L{get_or_set_maintainer_id}
710 @return: the name of the maintainer
713 global maintainer_cache
715 if not maintainer_cache.has_key(maintainer_id):
716 q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id))
717 maintainer_cache[maintainer_id] = q.getresult()[0][0]
719 return maintainer_cache[maintainer_id]
721 ################################################################################
723 def get_suites(pkgname, src=False):
725 Return the suites in which C{pkgname} can be found. If C{src} is True query for source
726 package, else binary package.
728 @type pkgname: string
729 @param pkgname: name of the package
732 @param src: if True look for source packages, false (default) looks for binary.
735 @return: list of suites, or empty list if no match
744 WHERE source.id = src_associations.source
745 AND source.source = '%s'
746 AND src_associations.suite = suite.id
754 WHERE binaries.id = bin_associations.bin
756 AND bin_associations.suite = suite.id
759 q = projectB.query(sql)
760 return map(lambda x: x[0], q.getresult())