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 ################################################################################
38 from binary import Binary
40 ################################################################################
42 Cnf = None #: Configuration, apt_pkg.Configuration
43 projectB = None #: database connection, pgobject
44 suite_id_cache = {} #: cache for suites
45 section_id_cache = {} #: cache for sections
46 priority_id_cache = {} #: cache for priorities
47 override_type_id_cache = {} #: cache for overrides
48 architecture_id_cache = {} #: cache for architectures
49 archive_id_cache = {} #: cache for archives
50 component_id_cache = {} #: cache for components
51 location_id_cache = {} #: cache for locations
52 maintainer_id_cache = {} #: cache for maintainers
53 keyring_id_cache = {} #: cache for keyrings
54 source_id_cache = {} #: cache for sources
56 files_id_cache = {} #: cache for files
57 maintainer_cache = {} #: cache for maintainer names
58 fingerprint_id_cache = {} #: cache for fingerprints
59 queue_id_cache = {} #: cache for queues
60 uid_id_cache = {} #: cache for uids
61 suite_version_cache = {} #: cache for suite_versions (packages)
62 suite_bin_version_cache = {}
63 cache_preloaded = False
65 ################################################################################
67 def init (config, sql):
71 @type config: apt_pkg.Configuration
72 @param config: apt config, see U{http://apt.alioth.debian.org/python-apt-doc/apt_pkg/cache.html#Configuration}
75 @param sql: database connection
86 Executes a database query. Writes statistics / timing to stderr.
89 @param query: database query string, passed unmodified
93 @warning: The query is passed B{unmodified}, so be careful what you use this for.
95 sys.stderr.write("query: \"%s\" ... " % (query))
97 r = projectB.query(query)
98 time_diff = time.time()-before
99 sys.stderr.write("took %.3f seconds.\n" % (time_diff))
101 sys.stderr.write("int result: %s\n" % (r))
102 elif type(r) is types.NoneType:
103 sys.stderr.write("result: None\n")
105 sys.stderr.write("pgresult: %s\n" % (r.getresult()))
108 ################################################################################
110 def get_suite_id (suite):
112 Returns database id for given C{suite}.
113 Results are kept in a cache during runtime to minimize database queries.
116 @param suite: The name of the suite
119 @return: the database id for the given suite
122 global suite_id_cache
124 if suite_id_cache.has_key(suite):
125 return suite_id_cache[suite]
127 q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
133 suite_id_cache[suite] = suite_id
137 def get_section_id (section):
139 Returns database id for given C{section}.
140 Results are kept in a cache during runtime to minimize database queries.
142 @type section: string
143 @param section: The name of the section
146 @return: the database id for the given section
149 global section_id_cache
151 if section_id_cache.has_key(section):
152 return section_id_cache[section]
154 q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
159 section_id = ql[0][0]
160 section_id_cache[section] = section_id
164 def get_priority_id (priority):
166 Returns database id for given C{priority}.
167 Results are kept in a cache during runtime to minimize database queries.
169 @type priority: string
170 @param priority: The name of the priority
173 @return: the database id for the given priority
176 global priority_id_cache
178 if priority_id_cache.has_key(priority):
179 return priority_id_cache[priority]
181 q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
186 priority_id = ql[0][0]
187 priority_id_cache[priority] = priority_id
191 def get_override_type_id (type):
193 Returns database id for given override C{type}.
194 Results are kept in a cache during runtime to minimize database queries.
197 @param type: The name of the override type
200 @return: the database id for the given override type
203 global override_type_id_cache
205 if override_type_id_cache.has_key(type):
206 return override_type_id_cache[type]
208 q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type))
213 override_type_id = ql[0][0]
214 override_type_id_cache[type] = override_type_id
216 return override_type_id
218 def get_architecture_id (architecture):
220 Returns database id for given C{architecture}.
221 Results are kept in a cache during runtime to minimize database queries.
223 @type architecture: string
224 @param architecture: The name of the override type
227 @return: the database id for the given architecture
230 global architecture_id_cache
232 if architecture_id_cache.has_key(architecture):
233 return architecture_id_cache[architecture]
235 q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
240 architecture_id = ql[0][0]
241 architecture_id_cache[architecture] = architecture_id
243 return architecture_id
245 def get_archive_id (archive):
247 Returns database id for given C{archive}.
248 Results are kept in a cache during runtime to minimize database queries.
250 @type archive: string
251 @param archive: The name of the override type
254 @return: the database id for the given archive
257 global archive_id_cache
259 archive = archive.lower()
261 if archive_id_cache.has_key(archive):
262 return archive_id_cache[archive]
264 q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive))
269 archive_id = ql[0][0]
270 archive_id_cache[archive] = archive_id
274 def get_component_id (component):
276 Returns database id for given C{component}.
277 Results are kept in a cache during runtime to minimize database queries.
279 @type component: string
280 @param component: The name of the component
283 @return: the database id for the given component
286 global component_id_cache
288 component = component.lower()
290 if component_id_cache.has_key(component):
291 return component_id_cache[component]
293 q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
298 component_id = ql[0][0]
299 component_id_cache[component] = component_id
303 def get_location_id (location, component, archive):
305 Returns database id for the location behind the given combination of
306 - B{location} - the path of the location, eg. I{/srv/ftp.debian.org/ftp/pool/}
307 - B{component} - the id of the component as returned by L{get_component_id}
308 - B{archive} - the id of the archive as returned by L{get_archive_id}
309 Results are kept in a cache during runtime to minimize database queries.
311 @type location: string
312 @param location: the path of the location
315 @param component: the id of the component
318 @param archive: the id of the archive
321 @return: the database id for the location
324 global location_id_cache
326 cache_key = location + '_' + component + '_' + location
327 if location_id_cache.has_key(cache_key):
328 return location_id_cache[cache_key]
330 archive_id = get_archive_id (archive)
332 component_id = get_component_id (component)
333 if component_id != -1:
334 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
336 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
341 location_id = ql[0][0]
342 location_id_cache[cache_key] = location_id
346 def get_source_id (source, version):
348 Returns database id for the combination of C{source} and C{version}
349 - B{source} - source package name, eg. I{mailfilter}, I{bbdb}, I{glibc}
351 Results are kept in a cache during runtime to minimize database queries.
354 @param source: source package name
356 @type version: string
357 @param version: the source version
360 @return: the database id for the source
363 global source_id_cache
365 cache_key = source + '_' + version + '_'
366 if source_id_cache.has_key(cache_key):
367 return source_id_cache[cache_key]
369 q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
371 if not q.getresult():
374 source_id = q.getresult()[0][0]
375 source_id_cache[cache_key] = source_id
379 def get_suite_version(source, suite):
381 Returns database id for a combination of C{source} and C{suite}.
383 - B{source} - source package name, eg. I{mailfilter}, I{bbdb}, I{glibc}
384 - B{suite} - a suite name, eg. I{unstable}
386 Results are kept in a cache during runtime to minimize database queries.
389 @param source: source package name
392 @param suite: the suite name
395 @return: the version for I{source} in I{suite}
399 global suite_version_cache
400 cache_key = "%s_%s" % (source, suite)
402 if suite_version_cache.has_key(cache_key):
403 return suite_version_cache[cache_key]
405 q = projectB.query("""
406 SELECT s.version FROM source s, suite su, src_associations sa
409 AND su.suite_name='%s'
413 if not q.getresult():
416 version = q.getresult()[0][0]
417 suite_version_cache[cache_key] = version
421 def get_latest_binary_version_id(binary, section, suite, arch):
422 global suite_bin_version_cache
423 cache_key = "%s_%s_%s_%s" % (binary, section, suite, arch)
424 cache_key_all = "%s_%s_%s_%s" % (binary, section, suite, get_architecture_id("all"))
426 # Check for the cache hit for its arch, then arch all
427 if suite_bin_version_cache.has_key(cache_key):
428 return suite_bin_version_cache[cache_key]
429 if suite_bin_version_cache.has_key(cache_key_all):
430 return suite_bin_version_cache[cache_key_all]
431 if cache_preloaded == True:
432 return # package does not exist
434 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)))
436 if not q.getresult():
439 highest_bid = q.getresult()[0][0]
441 suite_bin_version_cache[cache_key] = highest_bid
444 def preload_binary_id_cache():
445 global suite_bin_version_cache, cache_preloaded
448 q = projectB.query("SELECT id FROM suite")
449 suites = q.getresult()
452 q = projectB.query("SELECT id FROM architecture")
453 arches = q.getresult()
457 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])))
459 for bi in q.getresult():
460 cache_key = "%s_%s_%s_%s" % (bi[1], bi[2], suite[0], arch[0])
461 suite_bin_version_cache[cache_key] = int(bi[0])
463 cache_preloaded = True
465 def get_suite_architectures(suite):
467 Returns list of architectures for C{suite}.
469 @type suite: string, int
470 @param suite: the suite name or the suite_id
473 @return: the list of architectures for I{suite}
477 if type(suite) == str:
478 suite_id = get_suite_id(suite)
479 elif type(suite) == int:
484 sql = """ SELECT a.arch_string FROM suite_architectures sa
485 JOIN architecture a ON (a.id = sa.architecture)
486 WHERE suite='%s' """ % (suite_id)
488 q = projectB.query(sql)
489 return map(lambda x: x[0], q.getresult())
491 def get_suite_untouchable(suite):
493 Returns true if the C{suite} is untouchable, otherwise false.
495 @type suite: string, int
496 @param suite: the suite name or the suite_id
499 @return: status of suite
503 if type(suite) == str:
504 suite_id = get_suite_id(suite.lower())
505 elif type(suite) == int:
510 sql = """ SELECT untouchable FROM suite WHERE id='%s' """ % (suite_id)
512 q = projectB.query(sql)
513 if q.getresult()[0][0] == "f":
518 ################################################################################
520 def get_or_set_maintainer_id (maintainer):
522 If C{maintainer} does not have an entry in the maintainer table yet, create one
523 and return the new id.
524 If C{maintainer} already has an entry, simply return the existing id.
526 Results are kept in a cache during runtime to minimize database queries.
528 @type maintainer: string
529 @param maintainer: the maintainer name
532 @return: the database id for the maintainer
535 global maintainer_id_cache
537 if maintainer_id_cache.has_key(maintainer):
538 return maintainer_id_cache[maintainer]
540 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
541 if not q.getresult():
542 projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
543 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
544 maintainer_id = q.getresult()[0][0]
545 maintainer_id_cache[maintainer] = maintainer_id
549 ################################################################################
551 def get_or_set_keyring_id (keyring):
553 If C{keyring} does not have an entry in the C{keyrings} table yet, create one
554 and return the new id.
555 If C{keyring} already has an entry, simply return the existing id.
557 Results are kept in a cache during runtime to minimize database queries.
559 @type keyring: string
560 @param keyring: the keyring name
563 @return: the database id for the keyring
566 global keyring_id_cache
568 if keyring_id_cache.has_key(keyring):
569 return keyring_id_cache[keyring]
571 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
572 if not q.getresult():
573 projectB.query("INSERT INTO keyrings (name) VALUES ('%s')" % (keyring))
574 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
575 keyring_id = q.getresult()[0][0]
576 keyring_id_cache[keyring] = keyring_id
580 ################################################################################
582 def get_or_set_uid_id (uid):
584 If C{uid} does not have an entry in the uid table yet, create one
585 and return the new id.
586 If C{uid} already has an entry, simply return the existing id.
588 Results are kept in a cache during runtime to minimize database queries.
594 @return: the database id for the uid
600 if uid_id_cache.has_key(uid):
601 return uid_id_cache[uid]
603 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
604 if not q.getresult():
605 projectB.query("INSERT INTO uid (uid) VALUES ('%s')" % (uid))
606 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
607 uid_id = q.getresult()[0][0]
608 uid_id_cache[uid] = uid_id
612 ################################################################################
614 def get_or_set_fingerprint_id (fingerprint):
616 If C{fingerprint} does not have an entry in the fingerprint table yet, create one
617 and return the new id.
618 If C{fingerprint} already has an entry, simply return the existing id.
620 Results are kept in a cache during runtime to minimize database queries.
622 @type fingerprint: string
623 @param fingerprint: the fingerprint
626 @return: the database id for the fingerprint
629 global fingerprint_id_cache
631 if fingerprint_id_cache.has_key(fingerprint):
632 return fingerprint_id_cache[fingerprint]
634 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
635 if not q.getresult():
636 projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
637 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
638 fingerprint_id = q.getresult()[0][0]
639 fingerprint_id_cache[fingerprint] = fingerprint_id
641 return fingerprint_id
643 ################################################################################
645 def get_files_id (filename, size, md5sum, location_id):
647 Returns -1, -2 or the file_id for filename, if its C{size} and C{md5sum} match an
650 The database is queried using the C{filename} and C{location_id}. If a file does exist
651 at that location, the existing size and md5sum are checked against the provided
652 parameters. A size or checksum mismatch returns -2. If more than one entry is
653 found within the database, a -1 is returned, no result returns None, otherwise
656 Results are kept in a cache during runtime to minimize database queries.
658 @type filename: string
659 @param filename: the filename of the file to check against the DB
662 @param size: the size of the file to check against the DB
665 @param md5sum: the md5sum of the file to check against the DB
667 @type location_id: int
668 @param location_id: the id of the location as returned by L{get_location_id}
671 @return: Various return values are possible:
672 - -2: size/checksum error
673 - -1: more than one file found in database
674 - None: no file found in database
678 global files_id_cache
680 cache_key = "%s_%d" % (filename, location_id)
682 if files_id_cache.has_key(cache_key):
683 return files_id_cache[cache_key]
686 q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id))
692 orig_size = int(ql[1])
694 if orig_size != size or orig_md5sum != md5sum:
696 files_id_cache[cache_key] = ql[0]
697 return files_id_cache[cache_key]
701 ################################################################################
703 def get_or_set_queue_id (queue):
705 If C{queue} does not have an entry in the queue table yet, create one
706 and return the new id.
707 If C{queue} already has an entry, simply return the existing id.
709 Results are kept in a cache during runtime to minimize database queries.
712 @param queue: the queue name (no full path)
715 @return: the database id for the queue
718 global queue_id_cache
720 if queue_id_cache.has_key(queue):
721 return queue_id_cache[queue]
723 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
724 if not q.getresult():
725 projectB.query("INSERT INTO queue (queue_name) VALUES ('%s')" % (queue))
726 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
727 queue_id = q.getresult()[0][0]
728 queue_id_cache[queue] = queue_id
732 ################################################################################
734 def set_files_id (filename, size, md5sum, sha1sum, sha256sum, location_id):
736 Insert a new entry into the files table and return its id.
738 @type filename: string
739 @param filename: the filename
742 @param size: the size in bytes
745 @param md5sum: md5sum of the file
747 @type sha1sum: string
748 @param sha1sum: sha1sum of the file
750 @type sha256sum: string
751 @param sha256sum: sha256sum of the file
753 @type location_id: int
754 @param location_id: the id of the location as returned by L{get_location_id}
757 @return: the database id for the new file
760 global files_id_cache
762 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))
764 return get_files_id (filename, size, md5sum, location_id)
766 ### currval has issues with postgresql 7.1.3 when the table is big
767 ### it was taking ~3 seconds to return on auric which is very Not
770 ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')")
771 ##ql = q.getresult()[0]
772 ##cache_key = "%s_%d" % (filename, location_id)
773 ##files_id_cache[cache_key] = ql[0]
774 ##return files_id_cache[cache_key]
776 ################################################################################
778 def get_maintainer (maintainer_id):
780 Return the name of the maintainer behind C{maintainer_id}.
782 Results are kept in a cache during runtime to minimize database queries.
784 @type maintainer_id: int
785 @param maintainer_id: the id of the maintainer, eg. from L{get_or_set_maintainer_id}
788 @return: the name of the maintainer
791 global maintainer_cache
793 if not maintainer_cache.has_key(maintainer_id):
794 q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id))
795 maintainer_cache[maintainer_id] = q.getresult()[0][0]
797 return maintainer_cache[maintainer_id]
799 ################################################################################
801 def get_suites(pkgname, src=False):
803 Return the suites in which C{pkgname} can be found. If C{src} is True query for source
804 package, else binary package.
806 @type pkgname: string
807 @param pkgname: name of the package
810 @param src: if True look for source packages, false (default) looks for binary.
813 @return: list of suites, or empty list if no match
822 WHERE source.id = src_associations.source
823 AND source.source = '%s'
824 AND src_associations.suite = suite.id
832 WHERE binaries.id = bin_associations.bin
834 AND bin_associations.suite = suite.id
837 q = projectB.query(sql)
838 return map(lambda x: x[0], q.getresult())
841 ################################################################################
843 def get_new_comments(package):
845 Returns all the possible comments attached to C{package} in NEW. All versions.
847 @type package: string
848 @param package: name of the package
851 @return: list of strings containing comments for all versions from all authors for package
855 query = projectB.query(""" SELECT version, comment, author, notedate
861 for row in query.getresult():
862 comments.append("\nAuthor: %s\nVersion: %s\nTimestamp: %s\n\n%s\n" % (row[2], row[0], row[3], row[1]))
863 comments.append("-"*72)
867 def has_new_comment(package, version, ignore_trainee=False):
869 Returns true if the given combination of C{package}, C{version} has a comment.
870 If C{ignore_trainee} is true, comments from a trainee are ignored.
872 @type package: string
873 @param package: name of the package
875 @type version: string
876 @param version: package version
878 @type version: boolean
879 @param version: ignore trainee comments
887 trainee='AND trainee=false'
889 exists = projectB.query("""SELECT 1 FROM new_comments
894 % (package, version, trainee) ).getresult()
901 def add_new_comment(package, version, comment, author, trainee=False):
903 Add a new comment for C{package}, C{version} written by C{author}
905 @type package: string
906 @param package: name of the package
908 @type version: string
909 @param version: package version
911 @type comment: string
912 @param comment: the comment
915 @param author: the authorname
917 @type trainee: boolean
918 @param trainee: trainee comment
921 projectB.query(""" INSERT INTO new_comments (package, version, comment, author, trainee)
922 VALUES ('%s', '%s', '%s', '%s', '%s')
923 """ % (package, version, pg.escape_string(comment), pg.escape_string(author), trainee))
927 def delete_new_comments(package, version):
929 Delete a comment for C{package}, C{version}, if one exists
932 projectB.query(""" DELETE FROM new_comments
933 WHERE package = '%s' AND version = '%s'
934 """ % (package, version))
937 ################################################################################
938 def copy_temporary_contents(package, version, arch, deb, reject):
940 copy the previously stored contents from the temp table to the permanant one
942 during process-unchecked, the deb should have been scanned and the
943 contents stored in pending_content_associations
946 # first see if contents exist:
948 arch_id = get_architecture_id (arch)
950 exists = projectB.query("""SELECT 1 FROM pending_content_associations
953 AND architecture=%d LIMIT 1"""
954 % (package, version, arch_id) ).getresult()
957 # This should NOT happen. We should have added contents
958 # during process-unchecked. if it did, log an error, and send
961 "__PACKAGE__": package,
962 "__VERSION__": version,
964 "__TO_ADDRESS__": Cnf["Dinstall::MyAdminAddress"],
965 "__DAK_ADDRESS__": Cnf["Dinstall::MyEmailAddress"] }
967 message = utils.TemplateSubst(subst, Cnf["Dir::Templates"]+"/missing-contents")
968 utils.send_mail( message )
970 exists = Binary(deb, reject).scan_package()
973 sql = """INSERT INTO content_associations(binary_pkg,filepath,filename)
974 SELECT currval('binaries_id_seq'), filepath, filename FROM pending_content_associations
977 AND architecture=%d""" % (package, version, arch_id)
979 projectB.query("""DELETE from pending_content_associations
982 AND architecture=%d""" % (package, version, arch_id))