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 from binary import Binary
38 ################################################################################
40 Cnf = None #: Configuration, apt_pkg.Configuration
41 projectB = None #: database connection, pgobject
42 suite_id_cache = {} #: cache for suites
43 section_id_cache = {} #: cache for sections
44 priority_id_cache = {} #: cache for priorities
45 override_type_id_cache = {} #: cache for overrides
46 architecture_id_cache = {} #: cache for architectures
47 archive_id_cache = {} #: cache for archives
48 component_id_cache = {} #: cache for components
49 location_id_cache = {} #: cache for locations
50 maintainer_id_cache = {} #: cache for maintainers
51 keyring_id_cache = {} #: cache for keyrings
52 source_id_cache = {} #: cache for sources
54 files_id_cache = {} #: cache for files
55 maintainer_cache = {} #: cache for maintainer names
56 fingerprint_id_cache = {} #: cache for fingerprints
57 queue_id_cache = {} #: cache for queues
58 uid_id_cache = {} #: cache for uids
59 suite_version_cache = {} #: cache for suite_versions (packages)
60 suite_bin_version_cache = {}
61 cache_preloaded = False
63 ################################################################################
65 def init (config, sql):
69 @type config: apt_pkg.Configuration
70 @param config: apt config, see U{http://apt.alioth.debian.org/python-apt-doc/apt_pkg/cache.html#Configuration}
73 @param sql: database connection
84 Executes a database query. Writes statistics / timing to stderr.
87 @param query: database query string, passed unmodified
91 @warning: The query is passed B{unmodified}, so be careful what you use this for.
93 sys.stderr.write("query: \"%s\" ... " % (query))
95 r = projectB.query(query)
96 time_diff = time.time()-before
97 sys.stderr.write("took %.3f seconds.\n" % (time_diff))
99 sys.stderr.write("int result: %s\n" % (r))
100 elif type(r) is types.NoneType:
101 sys.stderr.write("result: None\n")
103 sys.stderr.write("pgresult: %s\n" % (r.getresult()))
106 ################################################################################
108 def get_suite_id (suite):
110 Returns database id for given C{suite}.
111 Results are kept in a cache during runtime to minimize database queries.
114 @param suite: The name of the suite
117 @return: the database id for the given suite
120 global suite_id_cache
122 if suite_id_cache.has_key(suite):
123 return suite_id_cache[suite]
125 q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
131 suite_id_cache[suite] = suite_id
135 def get_section_id (section):
137 Returns database id for given C{section}.
138 Results are kept in a cache during runtime to minimize database queries.
140 @type section: string
141 @param section: The name of the section
144 @return: the database id for the given section
147 global section_id_cache
149 if section_id_cache.has_key(section):
150 return section_id_cache[section]
152 q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
157 section_id = ql[0][0]
158 section_id_cache[section] = section_id
162 def get_priority_id (priority):
164 Returns database id for given C{priority}.
165 Results are kept in a cache during runtime to minimize database queries.
167 @type priority: string
168 @param priority: The name of the priority
171 @return: the database id for the given priority
174 global priority_id_cache
176 if priority_id_cache.has_key(priority):
177 return priority_id_cache[priority]
179 q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
184 priority_id = ql[0][0]
185 priority_id_cache[priority] = priority_id
189 def get_override_type_id (type):
191 Returns database id for given override C{type}.
192 Results are kept in a cache during runtime to minimize database queries.
195 @param type: The name of the override type
198 @return: the database id for the given override type
201 global override_type_id_cache
203 if override_type_id_cache.has_key(type):
204 return override_type_id_cache[type]
206 q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type))
211 override_type_id = ql[0][0]
212 override_type_id_cache[type] = override_type_id
214 return override_type_id
216 def get_architecture_id (architecture):
218 Returns database id for given C{architecture}.
219 Results are kept in a cache during runtime to minimize database queries.
221 @type architecture: string
222 @param architecture: The name of the override type
225 @return: the database id for the given architecture
228 global architecture_id_cache
230 if architecture_id_cache.has_key(architecture):
231 return architecture_id_cache[architecture]
233 q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
238 architecture_id = ql[0][0]
239 architecture_id_cache[architecture] = architecture_id
241 return architecture_id
243 def get_archive_id (archive):
245 Returns database id for given C{archive}.
246 Results are kept in a cache during runtime to minimize database queries.
248 @type archive: string
249 @param archive: The name of the override type
252 @return: the database id for the given archive
255 global archive_id_cache
257 archive = archive.lower()
259 if archive_id_cache.has_key(archive):
260 return archive_id_cache[archive]
262 q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive))
267 archive_id = ql[0][0]
268 archive_id_cache[archive] = archive_id
272 def get_component_id (component):
274 Returns database id for given C{component}.
275 Results are kept in a cache during runtime to minimize database queries.
277 @type component: string
278 @param component: The name of the component
281 @return: the database id for the given component
284 global component_id_cache
286 component = component.lower()
288 if component_id_cache.has_key(component):
289 return component_id_cache[component]
291 q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
296 component_id = ql[0][0]
297 component_id_cache[component] = component_id
301 def get_location_id (location, component, archive):
303 Returns database id for the location behind the given combination of
304 - B{location} - the path of the location, eg. I{/srv/ftp.debian.org/ftp/pool/}
305 - B{component} - the id of the component as returned by L{get_component_id}
306 - B{archive} - the id of the archive as returned by L{get_archive_id}
307 Results are kept in a cache during runtime to minimize database queries.
309 @type location: string
310 @param location: the path of the location
313 @param component: the id of the component
316 @param archive: the id of the archive
319 @return: the database id for the location
322 global location_id_cache
324 cache_key = location + '_' + component + '_' + location
325 if location_id_cache.has_key(cache_key):
326 return location_id_cache[cache_key]
328 archive_id = get_archive_id (archive)
330 component_id = get_component_id (component)
331 if component_id != -1:
332 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
334 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
339 location_id = ql[0][0]
340 location_id_cache[cache_key] = location_id
344 def get_source_id (source, version):
346 Returns database id for the combination of C{source} and C{version}
347 - B{source} - source package name, eg. I{mailfilter}, I{bbdb}, I{glibc}
349 Results are kept in a cache during runtime to minimize database queries.
352 @param source: source package name
354 @type version: string
355 @param version: the source version
358 @return: the database id for the source
361 global source_id_cache
363 cache_key = source + '_' + version + '_'
364 if source_id_cache.has_key(cache_key):
365 return source_id_cache[cache_key]
367 q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
369 if not q.getresult():
372 source_id = q.getresult()[0][0]
373 source_id_cache[cache_key] = source_id
377 def get_suite_version(source, suite):
379 Returns database id for a combination of C{source} and C{suite}.
381 - B{source} - source package name, eg. I{mailfilter}, I{bbdb}, I{glibc}
382 - B{suite} - a suite name, eg. I{unstable}
384 Results are kept in a cache during runtime to minimize database queries.
387 @param source: source package name
390 @param suite: the suite name
393 @return: the version for I{source} in I{suite}
397 global suite_version_cache
398 cache_key = "%s_%s" % (source, suite)
400 if suite_version_cache.has_key(cache_key):
401 return suite_version_cache[cache_key]
403 q = projectB.query("""
404 SELECT s.version FROM source s, suite su, src_associations sa
407 AND su.suite_name='%s'
411 if not q.getresult():
414 version = q.getresult()[0][0]
415 suite_version_cache[cache_key] = version
419 def get_latest_binary_version_id(binary, section, suite, arch):
420 global suite_bin_version_cache
421 cache_key = "%s_%s_%s_%s" % (binary, section, suite, arch)
422 cache_key_all = "%s_%s_%s_%s" % (binary, section, suite, get_architecture_id("all"))
424 # Check for the cache hit for its arch, then arch all
425 if suite_bin_version_cache.has_key(cache_key):
426 return suite_bin_version_cache[cache_key]
427 if suite_bin_version_cache.has_key(cache_key_all):
428 return suite_bin_version_cache[cache_key_all]
429 if cache_preloaded == True:
430 return # package does not exist
432 q = projectB.query("SELECT DISTINCT b.id FROM binaries b JOIN bin_associations ba ON (b.id = ba.bin) JOIN override o ON (o.package=b.package) WHERE b.package = '%s' AND b.architecture = '%d' AND ba.suite = '%d' AND o.section = '%d'" % (binary, int(arch), int(suite), int(section)))
434 if not q.getresult():
437 highest_bid = q.getresult()[0][0]
439 suite_bin_version_cache[cache_key] = highest_bid
442 def preload_binary_id_cache():
443 global suite_bin_version_cache, cache_preloaded
446 q = projectB.query("SELECT id FROM suite")
447 suites = q.getresult()
450 q = projectB.query("SELECT id FROM architecture")
451 arches = q.getresult()
455 q = projectB.query("SELECT DISTINCT b.id, b.package, o.section FROM binaries b JOIN bin_associations ba ON (b.id = ba.bin) JOIN override o ON (o.package=b.package) WHERE b.architecture = '%d' AND ba.suite = '%d'" % (int(arch[0]), int(suite[0])))
457 for bi in q.getresult():
458 cache_key = "%s_%s_%s_%s" % (bi[1], bi[2], suite[0], arch[0])
459 suite_bin_version_cache[cache_key] = int(bi[0])
461 cache_preloaded = True
463 def get_suite_architectures(suite):
465 Returns list of architectures for C{suite}.
467 @type suite: string, int
468 @param suite: the suite name or the suite_id
471 @return: the list of architectures for I{suite}
475 if type(suite) == str:
476 suite_id = get_suite_id(suite)
477 elif type(suite) == int:
482 sql = """ SELECT a.arch_string FROM suite_architectures sa
483 JOIN architecture a ON (a.id = sa.architecture)
484 WHERE suite='%s' """ % (suite_id)
486 q = projectB.query(sql)
487 return map(lambda x: x[0], q.getresult())
489 ################################################################################
491 def get_or_set_maintainer_id (maintainer):
493 If C{maintainer} does not have an entry in the maintainer table yet, create one
494 and return the new id.
495 If C{maintainer} already has an entry, simply return the existing id.
497 Results are kept in a cache during runtime to minimize database queries.
499 @type maintainer: string
500 @param maintainer: the maintainer name
503 @return: the database id for the maintainer
506 global maintainer_id_cache
508 if maintainer_id_cache.has_key(maintainer):
509 return maintainer_id_cache[maintainer]
511 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
512 if not q.getresult():
513 projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
514 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
515 maintainer_id = q.getresult()[0][0]
516 maintainer_id_cache[maintainer] = maintainer_id
520 ################################################################################
522 def get_or_set_keyring_id (keyring):
524 If C{keyring} does not have an entry in the C{keyrings} table yet, create one
525 and return the new id.
526 If C{keyring} already has an entry, simply return the existing id.
528 Results are kept in a cache during runtime to minimize database queries.
530 @type keyring: string
531 @param keyring: the keyring name
534 @return: the database id for the keyring
537 global keyring_id_cache
539 if keyring_id_cache.has_key(keyring):
540 return keyring_id_cache[keyring]
542 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
543 if not q.getresult():
544 projectB.query("INSERT INTO keyrings (name) VALUES ('%s')" % (keyring))
545 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
546 keyring_id = q.getresult()[0][0]
547 keyring_id_cache[keyring] = keyring_id
551 ################################################################################
553 def get_or_set_uid_id (uid):
555 If C{uid} does not have an entry in the uid table yet, create one
556 and return the new id.
557 If C{uid} already has an entry, simply return the existing id.
559 Results are kept in a cache during runtime to minimize database queries.
565 @return: the database id for the uid
571 if uid_id_cache.has_key(uid):
572 return uid_id_cache[uid]
574 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
575 if not q.getresult():
576 projectB.query("INSERT INTO uid (uid) VALUES ('%s')" % (uid))
577 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
578 uid_id = q.getresult()[0][0]
579 uid_id_cache[uid] = uid_id
583 ################################################################################
585 def get_or_set_fingerprint_id (fingerprint):
587 If C{fingerprint} does not have an entry in the fingerprint table yet, create one
588 and return the new id.
589 If C{fingerprint} already has an entry, simply return the existing id.
591 Results are kept in a cache during runtime to minimize database queries.
593 @type fingerprint: string
594 @param fingerprint: the fingerprint
597 @return: the database id for the fingerprint
600 global fingerprint_id_cache
602 if fingerprint_id_cache.has_key(fingerprint):
603 return fingerprint_id_cache[fingerprint]
605 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
606 if not q.getresult():
607 projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
608 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
609 fingerprint_id = q.getresult()[0][0]
610 fingerprint_id_cache[fingerprint] = fingerprint_id
612 return fingerprint_id
614 ################################################################################
616 def get_files_id (filename, size, md5sum, location_id):
618 Returns -1, -2 or the file_id for filename, if its C{size} and C{md5sum} match an
621 The database is queried using the C{filename} and C{location_id}. If a file does exist
622 at that location, the existing size and md5sum are checked against the provided
623 parameters. A size or checksum mismatch returns -2. If more than one entry is
624 found within the database, a -1 is returned, no result returns None, otherwise
627 Results are kept in a cache during runtime to minimize database queries.
629 @type filename: string
630 @param filename: the filename of the file to check against the DB
633 @param size: the size of the file to check against the DB
636 @param md5sum: the md5sum of the file to check against the DB
638 @type location_id: int
639 @param location_id: the id of the location as returned by L{get_location_id}
642 @return: Various return values are possible:
643 - -2: size/checksum error
644 - -1: more than one file found in database
645 - None: no file found in database
649 global files_id_cache
651 cache_key = "%s_%d" % (filename, location_id)
653 if files_id_cache.has_key(cache_key):
654 return files_id_cache[cache_key]
657 q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id))
663 orig_size = int(ql[1])
665 if orig_size != size or orig_md5sum != md5sum:
667 files_id_cache[cache_key] = ql[0]
668 return files_id_cache[cache_key]
672 ################################################################################
674 def get_or_set_queue_id (queue):
676 If C{queue} does not have an entry in the queue table yet, create one
677 and return the new id.
678 If C{queue} already has an entry, simply return the existing id.
680 Results are kept in a cache during runtime to minimize database queries.
683 @param queue: the queue name (no full path)
686 @return: the database id for the queue
689 global queue_id_cache
691 if queue_id_cache.has_key(queue):
692 return queue_id_cache[queue]
694 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
695 if not q.getresult():
696 projectB.query("INSERT INTO queue (queue_name) VALUES ('%s')" % (queue))
697 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
698 queue_id = q.getresult()[0][0]
699 queue_id_cache[queue] = queue_id
703 ################################################################################
705 def set_files_id (filename, size, md5sum, sha1sum, sha256sum, location_id):
707 Insert a new entry into the files table and return its id.
709 @type filename: string
710 @param filename: the filename
713 @param size: the size in bytes
716 @param md5sum: md5sum of the file
718 @type sha1sum: string
719 @param sha1sum: sha1sum of the file
721 @type sha256sum: string
722 @param sha256sum: sha256sum of the file
724 @type location_id: int
725 @param location_id: the id of the location as returned by L{get_location_id}
728 @return: the database id for the new file
731 global files_id_cache
733 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))
735 return get_files_id (filename, size, md5sum, location_id)
737 ### currval has issues with postgresql 7.1.3 when the table is big
738 ### it was taking ~3 seconds to return on auric which is very Not
741 ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')")
742 ##ql = q.getresult()[0]
743 ##cache_key = "%s_%d" % (filename, location_id)
744 ##files_id_cache[cache_key] = ql[0]
745 ##return files_id_cache[cache_key]
747 ################################################################################
749 def get_maintainer (maintainer_id):
751 Return the name of the maintainer behind C{maintainer_id}.
753 Results are kept in a cache during runtime to minimize database queries.
755 @type maintainer_id: int
756 @param maintainer_id: the id of the maintainer, eg. from L{get_or_set_maintainer_id}
759 @return: the name of the maintainer
762 global maintainer_cache
764 if not maintainer_cache.has_key(maintainer_id):
765 q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id))
766 maintainer_cache[maintainer_id] = q.getresult()[0][0]
768 return maintainer_cache[maintainer_id]
770 ################################################################################
772 def get_suites(pkgname, src=False):
774 Return the suites in which C{pkgname} can be found. If C{src} is True query for source
775 package, else binary package.
777 @type pkgname: string
778 @param pkgname: name of the package
781 @param src: if True look for source packages, false (default) looks for binary.
784 @return: list of suites, or empty list if no match
793 WHERE source.id = src_associations.source
794 AND source.source = '%s'
795 AND src_associations.suite = suite.id
803 WHERE binaries.id = bin_associations.bin
805 AND bin_associations.suite = suite.id
808 q = projectB.query(sql)
809 return map(lambda x: x[0], q.getresult())
812 ################################################################################
814 def copy_temporary_contents(package, version, deb, reject):
816 copy the previously stored contents from the temp table to the permanant one
818 during process-unchecked, the deb should have been scanned and the
819 contents stored in pending_content_associations
822 # first see if contents exist:
824 exists = projectB.query("""SELECT 1 FROM pending_content_associations
825 WHERE package='%s' LIMIT 1""" % package ).getresult()
828 # This should NOT happen. We should have added contents
829 # during process-unchecked. if it did, log an error, and send
832 "__PACKAGE__": package,
833 "__VERSION__": version,
834 "__TO_ADDRESS__": Cnf["Dinstall::MyAdminAddress"],
835 "__DAK_ADDRESS__": Cnf["Dinstall::MyEmailAddress"] }
837 message = utils.TemplateSubst(subst, Cnf["Dir::Templates"]+"/missing-contents")
838 utils.send_mail( message )
840 Binary(deb, reject).scan_package()
843 sql = """INSERT INTO content_associations(binary_pkg,filepath,filename)
844 SELECT currval('binaries_id_seq'), filepath, filename FROM pending_content_associations
846 AND version='%s'""" % (package, version)
848 projectB.query("""DELETE from pending_content_associations
850 AND version='%s'""" % (package, version))