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
52 files_id_cache = {} #: cache for files
53 maintainer_cache = {} #: cache for maintainer names
54 fingerprint_id_cache = {} #: cache for fingerprints
55 queue_id_cache = {} #: cache for queues
56 uid_id_cache = {} #: cache for uids
57 suite_version_cache = {} #: cache for suite_versions (packages)
58 suite_bin_version_cache = {}
59 cache_preloaded = False
61 ################################################################################
63 def init (config, sql):
67 @type config: apt_pkg.Configuration
68 @param config: apt config, see U{http://apt.alioth.debian.org/python-apt-doc/apt_pkg/cache.html#Configuration}
71 @param sql: database connection
82 Executes a database query. Writes statistics / timing to stderr.
85 @param query: database query string, passed unmodified
89 @warning: The query is passed B{unmodified}, so be careful what you use this for.
91 sys.stderr.write("query: \"%s\" ... " % (query))
93 r = projectB.query(query)
94 time_diff = time.time()-before
95 sys.stderr.write("took %.3f seconds.\n" % (time_diff))
97 sys.stderr.write("int result: %s\n" % (r))
98 elif type(r) is types.NoneType:
99 sys.stderr.write("result: None\n")
101 sys.stderr.write("pgresult: %s\n" % (r.getresult()))
104 ################################################################################
106 def get_suite_id (suite):
108 Returns database id for given C{suite}.
109 Results are kept in a cache during runtime to minimize database queries.
112 @param suite: The name of the suite
115 @return: the database id for the given suite
118 global suite_id_cache
120 if suite_id_cache.has_key(suite):
121 return suite_id_cache[suite]
123 q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
129 suite_id_cache[suite] = suite_id
133 def get_section_id (section):
135 Returns database id for given C{section}.
136 Results are kept in a cache during runtime to minimize database queries.
138 @type section: string
139 @param section: The name of the section
142 @return: the database id for the given section
145 global section_id_cache
147 if section_id_cache.has_key(section):
148 return section_id_cache[section]
150 q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
155 section_id = ql[0][0]
156 section_id_cache[section] = section_id
160 def get_priority_id (priority):
162 Returns database id for given C{priority}.
163 Results are kept in a cache during runtime to minimize database queries.
165 @type priority: string
166 @param priority: The name of the priority
169 @return: the database id for the given priority
172 global priority_id_cache
174 if priority_id_cache.has_key(priority):
175 return priority_id_cache[priority]
177 q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
182 priority_id = ql[0][0]
183 priority_id_cache[priority] = priority_id
187 def get_override_type_id (type):
189 Returns database id for given override C{type}.
190 Results are kept in a cache during runtime to minimize database queries.
193 @param type: The name of the override type
196 @return: the database id for the given override type
199 global override_type_id_cache
201 if override_type_id_cache.has_key(type):
202 return override_type_id_cache[type]
204 q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type))
209 override_type_id = ql[0][0]
210 override_type_id_cache[type] = override_type_id
212 return override_type_id
214 def get_architecture_id (architecture):
216 Returns database id for given C{architecture}.
217 Results are kept in a cache during runtime to minimize database queries.
219 @type architecture: string
220 @param architecture: The name of the override type
223 @return: the database id for the given architecture
226 global architecture_id_cache
228 if architecture_id_cache.has_key(architecture):
229 return architecture_id_cache[architecture]
231 q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
236 architecture_id = ql[0][0]
237 architecture_id_cache[architecture] = architecture_id
239 return architecture_id
241 def get_archive_id (archive):
243 Returns database id for given C{archive}.
244 Results are kept in a cache during runtime to minimize database queries.
246 @type archive: string
247 @param archive: The name of the override type
250 @return: the database id for the given archive
253 global archive_id_cache
255 archive = archive.lower()
257 if archive_id_cache.has_key(archive):
258 return archive_id_cache[archive]
260 q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive))
265 archive_id = ql[0][0]
266 archive_id_cache[archive] = archive_id
270 def get_component_id (component):
272 Returns database id for given C{component}.
273 Results are kept in a cache during runtime to minimize database queries.
275 @type component: string
276 @param component: The name of the component
279 @return: the database id for the given component
282 global component_id_cache
284 component = component.lower()
286 if component_id_cache.has_key(component):
287 return component_id_cache[component]
289 q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
294 component_id = ql[0][0]
295 component_id_cache[component] = component_id
299 def get_location_id (location, component, archive):
301 Returns database id for the location behind the given combination of
302 - B{location} - the path of the location, eg. I{/srv/ftp.debian.org/ftp/pool/}
303 - B{component} - the id of the component as returned by L{get_component_id}
304 - B{archive} - the id of the archive as returned by L{get_archive_id}
305 Results are kept in a cache during runtime to minimize database queries.
307 @type location: string
308 @param location: the path of the location
311 @param component: the id of the component
314 @param archive: the id of the archive
317 @return: the database id for the location
320 global location_id_cache
322 cache_key = location + '_' + component + '_' + location
323 if location_id_cache.has_key(cache_key):
324 return location_id_cache[cache_key]
326 archive_id = get_archive_id (archive)
328 component_id = get_component_id (component)
329 if component_id != -1:
330 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
332 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
337 location_id = ql[0][0]
338 location_id_cache[cache_key] = location_id
342 def get_source_id (source, version):
344 Returns database id for the combination of C{source} and C{version}
345 - B{source} - source package name, eg. I{mailfilter}, I{bbdb}, I{glibc}
347 Results are kept in a cache during runtime to minimize database queries.
350 @param source: source package name
352 @type version: string
353 @param version: the source version
356 @return: the database id for the source
359 global source_id_cache
361 cache_key = source + '_' + version + '_'
362 if source_id_cache.has_key(cache_key):
363 return source_id_cache[cache_key]
365 q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
367 if not q.getresult():
370 source_id = q.getresult()[0][0]
371 source_id_cache[cache_key] = source_id
375 def get_suite_version(source, suite):
377 Returns database id for a combination of C{source} and C{suite}.
379 - B{source} - source package name, eg. I{mailfilter}, I{bbdb}, I{glibc}
380 - B{suite} - a suite name, eg. I{unstable}
382 Results are kept in a cache during runtime to minimize database queries.
385 @param source: source package name
388 @param suite: the suite name
391 @return: the version for I{source} in I{suite}
395 global suite_version_cache
396 cache_key = "%s_%s" % (source, suite)
398 if suite_version_cache.has_key(cache_key):
399 return suite_version_cache[cache_key]
401 q = projectB.query("""
402 SELECT s.version FROM source s, suite su, src_associations sa
405 AND su.suite_name='%s'
409 if not q.getresult():
412 version = q.getresult()[0][0]
413 suite_version_cache[cache_key] = version
417 def get_latest_binary_version_id(binary, section, suite, arch):
418 global suite_bin_version_cache
419 cache_key = "%s_%s_%s_%s" % (binary, section, suite, arch)
420 cache_key_all = "%s_%s_%s_%s" % (binary, section, suite, get_architecture_id("all"))
422 # Check for the cache hit for its arch, then arch all
423 if suite_bin_version_cache.has_key(cache_key):
424 return suite_bin_version_cache[cache_key]
425 if suite_bin_version_cache.has_key(cache_key_all):
426 return suite_bin_version_cache[cache_key_all]
427 if cache_preloaded == True:
428 return # package does not exist
430 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)))
432 if not q.getresult():
435 highest_bid = q.getresult()[0][0]
437 suite_bin_version_cache[cache_key] = highest_bid
440 def preload_binary_id_cache():
441 global suite_bin_version_cache, cache_preloaded
444 q = projectB.query("SELECT id FROM suite")
445 suites = q.getresult()
448 q = projectB.query("SELECT id FROM architecture")
449 arches = q.getresult()
453 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])))
455 for bi in q.getresult():
456 cache_key = "%s_%s_%s_%s" % (bi[1], bi[2], suite[0], arch[0])
457 suite_bin_version_cache[cache_key] = int(bi[0])
459 cache_preloaded = True
461 def get_suite_architectures(suite):
463 Returns list of architectures for C{suite}.
465 @type suite: string, int
466 @param suite: the suite name or the suite_id
469 @return: the list of architectures for I{suite}
473 if type(suite) == str:
474 suite_id = get_suite_id(suite)
475 elif type(suite) == int:
480 sql = """ SELECT a.arch_string FROM suite_architectures sa
481 JOIN architecture a ON (a.id = sa.architecture)
482 WHERE suite='%s' """ % (suite_id)
484 q = projectB.query(sql)
485 return map(lambda x: x[0], q.getresult())
487 ################################################################################
489 def get_or_set_maintainer_id (maintainer):
491 If C{maintainer} does not have an entry in the maintainer table yet, create one
492 and return the new id.
493 If C{maintainer} already has an entry, simply return the existing id.
495 Results are kept in a cache during runtime to minimize database queries.
497 @type maintainer: string
498 @param maintainer: the maintainer name
501 @return: the database id for the maintainer
504 global maintainer_id_cache
506 if maintainer_id_cache.has_key(maintainer):
507 return maintainer_id_cache[maintainer]
509 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
510 if not q.getresult():
511 projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
512 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
513 maintainer_id = q.getresult()[0][0]
514 maintainer_id_cache[maintainer] = maintainer_id
518 ################################################################################
520 def get_or_set_keyring_id (keyring):
522 If C{keyring} does not have an entry in the C{keyrings} table yet, create one
523 and return the new id.
524 If C{keyring} already has an entry, simply return the existing id.
526 Results are kept in a cache during runtime to minimize database queries.
528 @type keyring: string
529 @param keyring: the keyring name
532 @return: the database id for the keyring
535 global keyring_id_cache
537 if keyring_id_cache.has_key(keyring):
538 return keyring_id_cache[keyring]
540 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
541 if not q.getresult():
542 projectB.query("INSERT INTO keyrings (name) VALUES ('%s')" % (keyring))
543 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
544 keyring_id = q.getresult()[0][0]
545 keyring_id_cache[keyring] = keyring_id
549 ################################################################################
551 def get_or_set_uid_id (uid):
553 If C{uid} does not have an entry in the uid table yet, create one
554 and return the new id.
555 If C{uid} already has an entry, simply return the existing id.
557 Results are kept in a cache during runtime to minimize database queries.
563 @return: the database id for the uid
569 if uid_id_cache.has_key(uid):
570 return uid_id_cache[uid]
572 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
573 if not q.getresult():
574 projectB.query("INSERT INTO uid (uid) VALUES ('%s')" % (uid))
575 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
576 uid_id = q.getresult()[0][0]
577 uid_id_cache[uid] = uid_id
581 ################################################################################
583 def get_or_set_fingerprint_id (fingerprint):
585 If C{fingerprint} does not have an entry in the fingerprint table yet, create one
586 and return the new id.
587 If C{fingerprint} already has an entry, simply return the existing id.
589 Results are kept in a cache during runtime to minimize database queries.
591 @type fingerprint: string
592 @param fingerprint: the fingerprint
595 @return: the database id for the fingerprint
598 global fingerprint_id_cache
600 if fingerprint_id_cache.has_key(fingerprint):
601 return fingerprint_id_cache[fingerprint]
603 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
604 if not q.getresult():
605 projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
606 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
607 fingerprint_id = q.getresult()[0][0]
608 fingerprint_id_cache[fingerprint] = fingerprint_id
610 return fingerprint_id
612 ################################################################################
614 def get_files_id (filename, size, md5sum, location_id):
616 Returns -1, -2 or the file_id for filename, if its C{size} and C{md5sum} match an
619 The database is queried using the C{filename} and C{location_id}. If a file does exist
620 at that location, the existing size and md5sum are checked against the provided
621 parameters. A size or checksum mismatch returns -2. If more than one entry is
622 found within the database, a -1 is returned, no result returns None, otherwise
625 Results are kept in a cache during runtime to minimize database queries.
627 @type filename: string
628 @param filename: the filename of the file to check against the DB
631 @param size: the size of the file to check against the DB
634 @param md5sum: the md5sum of the file to check against the DB
636 @type location_id: int
637 @param location_id: the id of the location as returned by L{get_location_id}
640 @return: Various return values are possible:
641 - -2: size/checksum error
642 - -1: more than one file found in database
643 - None: no file found in database
647 global files_id_cache
649 cache_key = "%s_%d" % (filename, location_id)
651 if files_id_cache.has_key(cache_key):
652 return files_id_cache[cache_key]
655 q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id))
661 orig_size = int(ql[1])
663 if orig_size != size or orig_md5sum != md5sum:
665 files_id_cache[cache_key] = ql[0]
666 return files_id_cache[cache_key]
670 ################################################################################
672 def get_or_set_queue_id (queue):
674 If C{queue} does not have an entry in the queue table yet, create one
675 and return the new id.
676 If C{queue} already has an entry, simply return the existing id.
678 Results are kept in a cache during runtime to minimize database queries.
681 @param queue: the queue name (no full path)
684 @return: the database id for the queue
687 global queue_id_cache
689 if queue_id_cache.has_key(queue):
690 return queue_id_cache[queue]
692 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
693 if not q.getresult():
694 projectB.query("INSERT INTO queue (queue_name) VALUES ('%s')" % (queue))
695 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
696 queue_id = q.getresult()[0][0]
697 queue_id_cache[queue] = queue_id
701 ################################################################################
703 def set_files_id (filename, size, md5sum, sha1sum, sha256sum, location_id):
705 Insert a new entry into the files table and return its id.
707 @type filename: string
708 @param filename: the filename
711 @param size: the size in bytes
714 @param md5sum: md5sum of the file
716 @type sha1sum: string
717 @param sha1sum: sha1sum of the file
719 @type sha256sum: string
720 @param sha256sum: sha256sum of the file
722 @type location_id: int
723 @param location_id: the id of the location as returned by L{get_location_id}
726 @return: the database id for the new file
729 global files_id_cache
731 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))
733 return get_files_id (filename, size, md5sum, location_id)
735 ### currval has issues with postgresql 7.1.3 when the table is big
736 ### it was taking ~3 seconds to return on auric which is very Not
739 ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')")
740 ##ql = q.getresult()[0]
741 ##cache_key = "%s_%d" % (filename, location_id)
742 ##files_id_cache[cache_key] = ql[0]
743 ##return files_id_cache[cache_key]
745 ################################################################################
747 def get_maintainer (maintainer_id):
749 Return the name of the maintainer behind C{maintainer_id}.
751 Results are kept in a cache during runtime to minimize database queries.
753 @type maintainer_id: int
754 @param maintainer_id: the id of the maintainer, eg. from L{get_or_set_maintainer_id}
757 @return: the name of the maintainer
760 global maintainer_cache
762 if not maintainer_cache.has_key(maintainer_id):
763 q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id))
764 maintainer_cache[maintainer_id] = q.getresult()[0][0]
766 return maintainer_cache[maintainer_id]
768 ################################################################################
770 def get_suites(pkgname, src=False):
772 Return the suites in which C{pkgname} can be found. If C{src} is True query for source
773 package, else binary package.
775 @type pkgname: string
776 @param pkgname: name of the package
779 @param src: if True look for source packages, false (default) looks for binary.
782 @return: list of suites, or empty list if no match
791 WHERE source.id = src_associations.source
792 AND source.source = '%s'
793 AND src_associations.suite = suite.id
801 WHERE binaries.id = bin_associations.bin
803 AND bin_associations.suite = suite.id
806 q = projectB.query(sql)
807 return map(lambda x: x[0], q.getresult())
810 ################################################################################
812 def copy_temporary_contents(package, version, deb):
814 copy the previously stored contents from the temp table to the permanant one
816 during process-unchecked, the deb should have been scanned and the
817 contents stored in pending_content_associations
820 # first see if contents exist:
822 exists = projectB.query("""SELECT 1 FROM pending_content_associations
823 WHERE package='%s' LIMIT 1""" % package ).getresult()
826 # This should NOT happen. We should have added contents
827 # during process-unchecked. if it did, log an error, and send
830 "__PACKAGE__": package,
831 "__VERSION__": version,
832 "__TO_ADDRESS__": Cnf["Dinstall::MyAdminAddress"],
833 "__DAK_ADDRESS__": Cnf["Dinstall::MyEmailAddress"] }
835 message = utils.TemplateSubst(Subst, Cnf["Dir::Templates"]+"/missing-contents")
836 utils.send_mail( message )
838 exists = DBConn().insert_content_path(package, version, deb)
841 sql = """INSERT INTO content_associations(binary_pkg,filepath,filename)
842 SELECT currval('binaries_id_seq'), filepath, filename FROM pending_content_associations
844 AND version='%s'""" % (package, version)
846 projectB.query("""DELETE from pending_content_associations
848 AND version='%s'""" % (package, version))