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, get_new_comments, has_new_comment
8 @group read/write: get_or_set*, set_files_id
9 @group writeonly: add_new_comment, delete_new_comments
11 @contact: Debian FTP Master <ftpmaster@debian.org>
12 @copyright: 2000, 2001, 2002, 2003, 2004, 2006 James Troup <james@nocrew.org>
13 @copyright: 2009 Joerg Jaspert <joerg@debian.org>
14 @license: GNU General Public License version 2 or later
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
27 # You should have received a copy of the GNU General Public License
28 # along with this program; if not, write to the Free Software
29 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 ################################################################################
39 ################################################################################
41 Cnf = None #: Configuration, apt_pkg.Configuration
42 projectB = None #: database connection, pgobject
43 suite_id_cache = {} #: cache for suites
44 section_id_cache = {} #: cache for sections
45 priority_id_cache = {} #: cache for priorities
46 override_type_id_cache = {} #: cache for overrides
47 architecture_id_cache = {} #: cache for architectures
48 archive_id_cache = {} #: cache for archives
49 component_id_cache = {} #: cache for components
50 location_id_cache = {} #: cache for locations
51 maintainer_id_cache = {} #: cache for maintainers
52 keyring_id_cache = {} #: cache for keyrings
53 source_id_cache = {} #: cache for sources
55 files_id_cache = {} #: cache for files
56 maintainer_cache = {} #: cache for maintainer names
57 fingerprint_id_cache = {} #: cache for fingerprints
58 queue_id_cache = {} #: cache for queues
59 uid_id_cache = {} #: cache for uids
60 suite_version_cache = {} #: cache for suite_versions (packages)
61 suite_bin_version_cache = {}
62 cache_preloaded = False
64 ################################################################################
66 def init (config, sql):
70 @type config: apt_pkg.Configuration
71 @param config: apt config, see U{http://apt.alioth.debian.org/python-apt-doc/apt_pkg/cache.html#Configuration}
74 @param sql: database connection
82 ################################################################################
84 def get_suite_id (suite):
86 Returns database id for given C{suite}.
87 Results are kept in a cache during runtime to minimize database queries.
90 @param suite: The name of the suite
93 @return: the database id for the given suite
98 if suite_id_cache.has_key(suite):
99 return suite_id_cache[suite]
101 q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
107 suite_id_cache[suite] = suite_id
111 def get_section_id (section):
113 Returns database id for given C{section}.
114 Results are kept in a cache during runtime to minimize database queries.
116 @type section: string
117 @param section: The name of the section
120 @return: the database id for the given section
123 global section_id_cache
125 if section_id_cache.has_key(section):
126 return section_id_cache[section]
128 q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
133 section_id = ql[0][0]
134 section_id_cache[section] = section_id
138 def get_priority_id (priority):
140 Returns database id for given C{priority}.
141 Results are kept in a cache during runtime to minimize database queries.
143 @type priority: string
144 @param priority: The name of the priority
147 @return: the database id for the given priority
150 global priority_id_cache
152 if priority_id_cache.has_key(priority):
153 return priority_id_cache[priority]
155 q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
160 priority_id = ql[0][0]
161 priority_id_cache[priority] = priority_id
165 def get_override_type_id (type):
167 Returns database id for given override C{type}.
168 Results are kept in a cache during runtime to minimize database queries.
171 @param type: The name of the override type
174 @return: the database id for the given override type
177 global override_type_id_cache
179 if override_type_id_cache.has_key(type):
180 return override_type_id_cache[type]
182 q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type))
187 override_type_id = ql[0][0]
188 override_type_id_cache[type] = override_type_id
190 return override_type_id
192 def get_architecture_id (architecture):
194 Returns database id for given C{architecture}.
195 Results are kept in a cache during runtime to minimize database queries.
197 @type architecture: string
198 @param architecture: The name of the override type
201 @return: the database id for the given architecture
204 global architecture_id_cache
206 if architecture_id_cache.has_key(architecture):
207 return architecture_id_cache[architecture]
209 q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
214 architecture_id = ql[0][0]
215 architecture_id_cache[architecture] = architecture_id
217 return architecture_id
219 def get_archive_id (archive):
221 Returns database id for given C{archive}.
222 Results are kept in a cache during runtime to minimize database queries.
224 @type archive: string
225 @param archive: The name of the override type
228 @return: the database id for the given archive
231 global archive_id_cache
233 archive = archive.lower()
235 if archive_id_cache.has_key(archive):
236 return archive_id_cache[archive]
238 q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive))
243 archive_id = ql[0][0]
244 archive_id_cache[archive] = archive_id
248 def get_component_id (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 component
257 @return: the database id for the given component
260 global component_id_cache
262 component = component.lower()
264 if component_id_cache.has_key(component):
265 return component_id_cache[component]
267 q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
272 component_id = ql[0][0]
273 component_id_cache[component] = component_id
277 def get_location_id (location, component, archive):
279 Returns database id for the location behind the given combination of
280 - B{location} - the path of the location, eg. I{/srv/ftp.debian.org/ftp/pool/}
281 - B{component} - the id of the component as returned by L{get_component_id}
282 - B{archive} - the id of the archive as returned by L{get_archive_id}
283 Results are kept in a cache during runtime to minimize database queries.
285 @type location: string
286 @param location: the path of the location
289 @param component: the id of the component
292 @param archive: the id of the archive
295 @return: the database id for the location
298 global location_id_cache
300 cache_key = location + '_' + component + '_' + location
301 if location_id_cache.has_key(cache_key):
302 return location_id_cache[cache_key]
304 archive_id = get_archive_id (archive)
306 component_id = get_component_id (component)
307 if component_id != -1:
308 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
310 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
315 location_id = ql[0][0]
316 location_id_cache[cache_key] = location_id
320 def get_source_id (source, version):
322 Returns database id for the combination of C{source} and C{version}
323 - B{source} - source package name, eg. I{mailfilter}, I{bbdb}, I{glibc}
325 Results are kept in a cache during runtime to minimize database queries.
328 @param source: source package name
330 @type version: string
331 @param version: the source version
334 @return: the database id for the source
337 global source_id_cache
339 cache_key = source + '_' + version + '_'
340 if source_id_cache.has_key(cache_key):
341 return source_id_cache[cache_key]
343 q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
345 if not q.getresult():
348 source_id = q.getresult()[0][0]
349 source_id_cache[cache_key] = source_id
353 def get_suite_version(source, suite):
355 Returns database id for a combination of C{source} and C{suite}.
357 - B{source} - source package name, eg. I{mailfilter}, I{bbdb}, I{glibc}
358 - B{suite} - a suite name, eg. I{unstable}
360 Results are kept in a cache during runtime to minimize database queries.
363 @param source: source package name
366 @param suite: the suite name
369 @return: the version for I{source} in I{suite}
373 global suite_version_cache
374 cache_key = "%s_%s" % (source, suite)
376 if suite_version_cache.has_key(cache_key):
377 return suite_version_cache[cache_key]
379 q = projectB.query("""
380 SELECT s.version FROM source s, suite su, src_associations sa
383 AND su.suite_name='%s'
387 if not q.getresult():
390 version = q.getresult()[0][0]
391 suite_version_cache[cache_key] = version
395 def get_latest_binary_version_id(binary, section, suite, arch):
396 global suite_bin_version_cache
397 cache_key = "%s_%s_%s_%s" % (binary, section, suite, arch)
398 cache_key_all = "%s_%s_%s_%s" % (binary, section, suite, get_architecture_id("all"))
400 # Check for the cache hit for its arch, then arch all
401 if suite_bin_version_cache.has_key(cache_key):
402 return suite_bin_version_cache[cache_key]
403 if suite_bin_version_cache.has_key(cache_key_all):
404 return suite_bin_version_cache[cache_key_all]
405 if cache_preloaded == True:
406 return # package does not exist
408 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)))
410 if not q.getresult():
413 highest_bid = q.getresult()[0][0]
415 suite_bin_version_cache[cache_key] = highest_bid
418 def preload_binary_id_cache():
419 global suite_bin_version_cache, cache_preloaded
422 q = projectB.query("SELECT id FROM suite")
423 suites = q.getresult()
426 q = projectB.query("SELECT id FROM architecture")
427 arches = q.getresult()
431 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])))
433 for bi in q.getresult():
434 cache_key = "%s_%s_%s_%s" % (bi[1], bi[2], suite[0], arch[0])
435 suite_bin_version_cache[cache_key] = int(bi[0])
437 cache_preloaded = True
439 def get_suite_architectures(suite):
441 Returns list of architectures for C{suite}.
443 @type suite: string, int
444 @param suite: the suite name or the suite_id
447 @return: the list of architectures for I{suite}
451 if type(suite) == str:
452 suite_id = get_suite_id(suite)
453 elif type(suite) == int:
458 sql = """ SELECT a.arch_string FROM suite_architectures sa
459 JOIN architecture a ON (a.id = sa.architecture)
460 WHERE suite='%s' """ % (suite_id)
462 q = projectB.query(sql)
463 return map(lambda x: x[0], q.getresult())
465 def get_suite_untouchable(suite):
467 Returns true if the C{suite} is untouchable, otherwise false.
469 @type suite: string, int
470 @param suite: the suite name or the suite_id
473 @return: status of suite
477 if type(suite) == str:
478 suite_id = get_suite_id(suite.lower())
479 elif type(suite) == int:
484 sql = """ SELECT untouchable FROM suite WHERE id='%s' """ % (suite_id)
486 q = projectB.query(sql)
487 if q.getresult()[0][0] == "f":
492 ################################################################################
494 def get_or_set_maintainer_id (maintainer):
496 If C{maintainer} does not have an entry in the maintainer table yet, create one
497 and return the new id.
498 If C{maintainer} already has an entry, simply return the existing id.
500 Results are kept in a cache during runtime to minimize database queries.
502 @type maintainer: string
503 @param maintainer: the maintainer name
506 @return: the database id for the maintainer
509 global maintainer_id_cache
511 if maintainer_id_cache.has_key(maintainer):
512 return maintainer_id_cache[maintainer]
514 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
515 if not q.getresult():
516 projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
517 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
518 maintainer_id = q.getresult()[0][0]
519 maintainer_id_cache[maintainer] = maintainer_id
523 ################################################################################
525 def get_or_set_keyring_id (keyring):
527 If C{keyring} does not have an entry in the C{keyrings} table yet, create one
528 and return the new id.
529 If C{keyring} already has an entry, simply return the existing id.
531 Results are kept in a cache during runtime to minimize database queries.
533 @type keyring: string
534 @param keyring: the keyring name
537 @return: the database id for the keyring
540 global keyring_id_cache
542 if keyring_id_cache.has_key(keyring):
543 return keyring_id_cache[keyring]
545 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
546 if not q.getresult():
547 projectB.query("INSERT INTO keyrings (name) VALUES ('%s')" % (keyring))
548 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
549 keyring_id = q.getresult()[0][0]
550 keyring_id_cache[keyring] = keyring_id
554 ################################################################################
556 def get_or_set_uid_id (uid):
558 If C{uid} does not have an entry in the uid table yet, create one
559 and return the new id.
560 If C{uid} already has an entry, simply return the existing id.
562 Results are kept in a cache during runtime to minimize database queries.
568 @return: the database id for the uid
574 if uid_id_cache.has_key(uid):
575 return uid_id_cache[uid]
577 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
578 if not q.getresult():
579 projectB.query("INSERT INTO uid (uid) VALUES ('%s')" % (uid))
580 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
581 uid_id = q.getresult()[0][0]
582 uid_id_cache[uid] = uid_id
586 ################################################################################
588 def get_or_set_fingerprint_id (fingerprint):
590 If C{fingerprint} does not have an entry in the fingerprint table yet, create one
591 and return the new id.
592 If C{fingerprint} already has an entry, simply return the existing id.
594 Results are kept in a cache during runtime to minimize database queries.
596 @type fingerprint: string
597 @param fingerprint: the fingerprint
600 @return: the database id for the fingerprint
603 global fingerprint_id_cache
605 if fingerprint_id_cache.has_key(fingerprint):
606 return fingerprint_id_cache[fingerprint]
608 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
609 if not q.getresult():
610 projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
611 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
612 fingerprint_id = q.getresult()[0][0]
613 fingerprint_id_cache[fingerprint] = fingerprint_id
615 return fingerprint_id
617 ################################################################################
619 def get_files_id (filename, size, md5sum, location_id):
621 Returns -1, -2 or the file_id for filename, if its C{size} and C{md5sum} match an
624 The database is queried using the C{filename} and C{location_id}. If a file does exist
625 at that location, the existing size and md5sum are checked against the provided
626 parameters. A size or checksum mismatch returns -2. If more than one entry is
627 found within the database, a -1 is returned, no result returns None, otherwise
630 Results are kept in a cache during runtime to minimize database queries.
632 @type filename: string
633 @param filename: the filename of the file to check against the DB
636 @param size: the size of the file to check against the DB
639 @param md5sum: the md5sum of the file to check against the DB
641 @type location_id: int
642 @param location_id: the id of the location as returned by L{get_location_id}
645 @return: Various return values are possible:
646 - -2: size/checksum error
647 - -1: more than one file found in database
648 - None: no file found in database
652 global files_id_cache
654 cache_key = "%s_%d" % (filename, location_id)
656 if files_id_cache.has_key(cache_key):
657 return files_id_cache[cache_key]
660 q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id))
666 orig_size = int(ql[1])
668 if orig_size != size or orig_md5sum != md5sum:
670 files_id_cache[cache_key] = ql[0]
671 return files_id_cache[cache_key]
675 ################################################################################
677 def get_or_set_queue_id (queue):
679 If C{queue} does not have an entry in the queue table yet, create one
680 and return the new id.
681 If C{queue} already has an entry, simply return the existing id.
683 Results are kept in a cache during runtime to minimize database queries.
686 @param queue: the queue name (no full path)
689 @return: the database id for the queue
692 global queue_id_cache
694 if queue_id_cache.has_key(queue):
695 return queue_id_cache[queue]
697 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
698 if not q.getresult():
699 projectB.query("INSERT INTO queue (queue_name) VALUES ('%s')" % (queue))
700 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
701 queue_id = q.getresult()[0][0]
702 queue_id_cache[queue] = queue_id
706 ################################################################################
708 def set_files_id (filename, size, md5sum, sha1sum, sha256sum, location_id):
710 Insert a new entry into the files table and return its id.
712 @type filename: string
713 @param filename: the filename
716 @param size: the size in bytes
719 @param md5sum: md5sum of the file
721 @type sha1sum: string
722 @param sha1sum: sha1sum of the file
724 @type sha256sum: string
725 @param sha256sum: sha256sum of the file
727 @type location_id: int
728 @param location_id: the id of the location as returned by L{get_location_id}
731 @return: the database id for the new file
734 global files_id_cache
736 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))
738 return get_files_id (filename, size, md5sum, location_id)
740 ### currval has issues with postgresql 7.1.3 when the table is big
741 ### it was taking ~3 seconds to return on auric which is very Not
744 ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')")
745 ##ql = q.getresult()[0]
746 ##cache_key = "%s_%d" % (filename, location_id)
747 ##files_id_cache[cache_key] = ql[0]
748 ##return files_id_cache[cache_key]
750 ################################################################################
752 def get_maintainer (maintainer_id):
754 Return the name of the maintainer behind C{maintainer_id}.
756 Results are kept in a cache during runtime to minimize database queries.
758 @type maintainer_id: int
759 @param maintainer_id: the id of the maintainer, eg. from L{get_or_set_maintainer_id}
762 @return: the name of the maintainer
765 global maintainer_cache
767 if not maintainer_cache.has_key(maintainer_id):
768 q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id))
769 maintainer_cache[maintainer_id] = q.getresult()[0][0]
771 return maintainer_cache[maintainer_id]
773 ################################################################################
775 def get_suites(pkgname, src=False):
777 Return the suites in which C{pkgname} can be found. If C{src} is True query for source
778 package, else binary package.
780 @type pkgname: string
781 @param pkgname: name of the package
784 @param src: if True look for source packages, false (default) looks for binary.
787 @return: list of suites, or empty list if no match
796 WHERE source.id = src_associations.source
797 AND source.source = '%s'
798 AND src_associations.suite = suite.id
806 WHERE binaries.id = bin_associations.bin
808 AND bin_associations.suite = suite.id
811 q = projectB.query(sql)
812 return map(lambda x: x[0], q.getresult())
815 ################################################################################
817 def get_new_comments(package):
819 Returns all the possible comments attached to C{package} in NEW. All versions.
821 @type package: string
822 @param package: name of the package
825 @return: list of strings containing comments for all versions from all authors for package
829 query = projectB.query(""" SELECT version, comment, author, notedate
835 for row in query.getresult():
836 comments.append("\nAuthor: %s\nVersion: %s\nTimestamp: %s\n\n%s\n" % (row[2], row[0], row[3], row[1]))
837 comments.append("-"*72)
841 def has_new_comment(package, version, ignore_trainee=False):
843 Returns true if the given combination of C{package}, C{version} has a comment.
844 If C{ignore_trainee} is true, comments from a trainee are ignored.
846 @type package: string
847 @param package: name of the package
849 @type version: string
850 @param version: package version
852 @type ignore_trainee: boolean
853 @param ignore_trainee: ignore trainee comments
861 trainee='AND trainee=false'
863 exists = projectB.query("""SELECT 1 FROM new_comments
868 % (package, version, trainee) ).getresult()
875 def add_new_comment(package, version, comment, author, trainee=False):
877 Add a new comment for C{package}, C{version} written by C{author}
879 @type package: string
880 @param package: name of the package
882 @type version: string
883 @param version: package version
885 @type comment: string
886 @param comment: the comment
889 @param author: the authorname
891 @type trainee: boolean
892 @param trainee: trainee comment
895 projectB.query(""" INSERT INTO new_comments (package, version, comment, author, trainee)
896 VALUES ('%s', '%s', '%s', '%s', '%s')
897 """ % (package, version, pg.escape_string(comment), pg.escape_string(author), trainee))
901 def delete_new_comments(package, version):
903 Delete a comment for C{package}, C{version}, if one exists
906 projectB.query(""" DELETE FROM new_comments
907 WHERE package = '%s' AND version = '%s'
908 """ % (package, version))
911 def delete_all_new_comments(package):
913 Delete all comments for C{package}, if they exist
916 projectB.query(""" DELETE FROM new_comments
921 ################################################################################
922 def copy_temporary_contents(package, version, arch, deb, reject):
924 copy the previously stored contents from the temp table to the permanant one
926 during process-unchecked, the deb should have been scanned and the
927 contents stored in pending_content_associations
930 # first see if contents exist:
932 arch_id = get_architecture_id (arch)
934 exists = projectB.query("""SELECT 1 FROM pending_content_associations
937 AND architecture=%d LIMIT 1"""
938 % (package, version, arch_id) ).getresult()
941 # This should NOT happen. We should have added contents
942 # during process-unchecked. if it did, log an error, and send
945 "__PACKAGE__": package,
946 "__VERSION__": version,
948 "__TO_ADDRESS__": Cnf["Dinstall::MyAdminAddress"],
949 "__DAK_ADDRESS__": Cnf["Dinstall::MyEmailAddress"] }
951 message = utils.TemplateSubst(subst, Cnf["Dir::Templates"]+"/missing-contents")
952 utils.send_mail( message )
954 exists = Binary(deb, reject).scan_package()
957 sql = """INSERT INTO content_associations(binary_pkg,filepath,filename)
958 SELECT currval('binaries_id_seq'), filepath, filename FROM pending_content_associations
961 AND architecture=%d""" % (package, version, arch_id)
963 projectB.query("""DELETE from pending_content_associations
966 AND architecture=%d""" % (package, version, arch_id))