]> git.decadent.org.uk Git - dak.git/blob - daklib/database.py
Merge commit 'ftpmaster/master' into sqlalchemy
[dak.git] / daklib / database.py
1 #!/usr/bin/env python
2
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
10
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
15 """
16
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.
21
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.
26
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
30
31 ################################################################################
32
33 import sys
34 import time
35 import types
36 import utils
37 import pg
38
39 ################################################################################
40
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
54
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
63
64 ################################################################################
65
66 def init (config, sql):
67     """
68     database module init.
69
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}
72
73     @type sql: pgobject
74     @param sql: database connection
75
76     """
77     global Cnf, projectB
78
79     Cnf = config
80     projectB = sql
81
82 ################################################################################
83
84 def get_suite_id (suite):
85     """
86     Returns database id for given C{suite}.
87     Results are kept in a cache during runtime to minimize database queries.
88
89     @type suite: string
90     @param suite: The name of the suite
91
92     @rtype: int
93     @return: the database id for the given suite
94
95     """
96     global suite_id_cache
97
98     if suite_id_cache.has_key(suite):
99         return suite_id_cache[suite]
100
101     q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
102     ql = q.getresult()
103     if not ql:
104         return -1
105
106     suite_id = ql[0][0]
107     suite_id_cache[suite] = suite_id
108
109     return suite_id
110
111 def get_section_id (section):
112     """
113     Returns database id for given C{section}.
114     Results are kept in a cache during runtime to minimize database queries.
115
116     @type section: string
117     @param section: The name of the section
118
119     @rtype: int
120     @return: the database id for the given section
121
122     """
123     global section_id_cache
124
125     if section_id_cache.has_key(section):
126         return section_id_cache[section]
127
128     q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
129     ql = q.getresult()
130     if not ql:
131         return -1
132
133     section_id = ql[0][0]
134     section_id_cache[section] = section_id
135
136     return section_id
137
138 def get_priority_id (priority):
139     """
140     Returns database id for given C{priority}.
141     Results are kept in a cache during runtime to minimize database queries.
142
143     @type priority: string
144     @param priority: The name of the priority
145
146     @rtype: int
147     @return: the database id for the given priority
148
149     """
150     global priority_id_cache
151
152     if priority_id_cache.has_key(priority):
153         return priority_id_cache[priority]
154
155     q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
156     ql = q.getresult()
157     if not ql:
158         return -1
159
160     priority_id = ql[0][0]
161     priority_id_cache[priority] = priority_id
162
163     return priority_id
164
165 def get_override_type_id (type):
166     """
167     Returns database id for given override C{type}.
168     Results are kept in a cache during runtime to minimize database queries.
169
170     @type type: string
171     @param type: The name of the override type
172
173     @rtype: int
174     @return: the database id for the given override type
175
176     """
177     global override_type_id_cache
178
179     if override_type_id_cache.has_key(type):
180         return override_type_id_cache[type]
181
182     q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type))
183     ql = q.getresult()
184     if not ql:
185         return -1
186
187     override_type_id = ql[0][0]
188     override_type_id_cache[type] = override_type_id
189
190     return override_type_id
191
192 def get_architecture_id (architecture):
193     """
194     Returns database id for given C{architecture}.
195     Results are kept in a cache during runtime to minimize database queries.
196
197     @type architecture: string
198     @param architecture: The name of the override type
199
200     @rtype: int
201     @return: the database id for the given architecture
202
203     """
204     global architecture_id_cache
205
206     if architecture_id_cache.has_key(architecture):
207         return architecture_id_cache[architecture]
208
209     q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
210     ql = q.getresult()
211     if not ql:
212         return -1
213
214     architecture_id = ql[0][0]
215     architecture_id_cache[architecture] = architecture_id
216
217     return architecture_id
218
219 def get_archive_id (archive):
220     """
221     Returns database id for given C{archive}.
222     Results are kept in a cache during runtime to minimize database queries.
223
224     @type archive: string
225     @param archive: The name of the override type
226
227     @rtype: int
228     @return: the database id for the given archive
229
230     """
231     global archive_id_cache
232
233     archive = archive.lower()
234
235     if archive_id_cache.has_key(archive):
236         return archive_id_cache[archive]
237
238     q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive))
239     ql = q.getresult()
240     if not ql:
241         return -1
242
243     archive_id = ql[0][0]
244     archive_id_cache[archive] = archive_id
245
246     return archive_id
247
248 def get_component_id (component):
249     """
250     Returns database id for given C{component}.
251     Results are kept in a cache during runtime to minimize database queries.
252
253     @type component: string
254     @param component: The name of the component
255
256     @rtype: int
257     @return: the database id for the given component
258
259     """
260     global component_id_cache
261
262     component = component.lower()
263
264     if component_id_cache.has_key(component):
265         return component_id_cache[component]
266
267     q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
268     ql = q.getresult()
269     if not ql:
270         return -1
271
272     component_id = ql[0][0]
273     component_id_cache[component] = component_id
274
275     return component_id
276
277 def get_location_id (location, component, archive):
278     """
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.
284
285     @type location: string
286     @param location: the path of the location
287
288     @type component: int
289     @param component: the id of the component
290
291     @type archive: int
292     @param archive: the id of the archive
293
294     @rtype: int
295     @return: the database id for the location
296
297     """
298     global location_id_cache
299
300     cache_key = location + '_' + component + '_' + location
301     if location_id_cache.has_key(cache_key):
302         return location_id_cache[cache_key]
303
304     archive_id = get_archive_id (archive)
305     if component != "":
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))
309     else:
310         q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
311     ql = q.getresult()
312     if not ql:
313         return -1
314
315     location_id = ql[0][0]
316     location_id_cache[cache_key] = location_id
317
318     return location_id
319
320 def get_source_id (source, version):
321     """
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}
324       - B{version}
325     Results are kept in a cache during runtime to minimize database queries.
326
327     @type source: string
328     @param source: source package name
329
330     @type version: string
331     @param version: the source version
332
333     @rtype: int
334     @return: the database id for the source
335
336     """
337     global source_id_cache
338
339     cache_key = source + '_' + version + '_'
340     if source_id_cache.has_key(cache_key):
341         return source_id_cache[cache_key]
342
343     q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
344
345     if not q.getresult():
346         return None
347
348     source_id = q.getresult()[0][0]
349     source_id_cache[cache_key] = source_id
350
351     return source_id
352
353 def get_suite_version(source, suite):
354     """
355     Returns database id for a combination of C{source} and C{suite}.
356
357       - B{source} - source package name, eg. I{mailfilter}, I{bbdb}, I{glibc}
358       - B{suite} - a suite name, eg. I{unstable}
359
360     Results are kept in a cache during runtime to minimize database queries.
361
362     @type source: string
363     @param source: source package name
364
365     @type suite: string
366     @param suite: the suite name
367
368     @rtype: string
369     @return: the version for I{source} in I{suite}
370
371     """
372
373     global suite_version_cache
374     cache_key = "%s_%s" % (source, suite)
375
376     if suite_version_cache.has_key(cache_key):
377         return suite_version_cache[cache_key]
378
379     q = projectB.query("""
380     SELECT s.version FROM source s, suite su, src_associations sa
381     WHERE sa.source=s.id
382       AND sa.suite=su.id
383       AND su.suite_name='%s'
384       AND s.source='%s'"""
385                               % (suite, source))
386
387     if not q.getresult():
388         return None
389
390     version = q.getresult()[0][0]
391     suite_version_cache[cache_key] = version
392
393     return version
394
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"))
399
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
407
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)))
409
410     if not q.getresult():
411         return False
412
413     highest_bid = q.getresult()[0][0]
414
415     suite_bin_version_cache[cache_key] = highest_bid
416     return highest_bid
417
418 def preload_binary_id_cache():
419     global suite_bin_version_cache, cache_preloaded
420
421     # Get suite info
422     q = projectB.query("SELECT id FROM suite")
423     suites = q.getresult()
424
425     # Get arch mappings
426     q = projectB.query("SELECT id FROM architecture")
427     arches = q.getresult()
428
429     for suite in suites:
430         for arch in arches:
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])))
432
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])
436
437     cache_preloaded = True
438
439 def get_suite_architectures(suite):
440     """
441     Returns list of architectures for C{suite}.
442
443     @type suite: string, int
444     @param suite: the suite name or the suite_id
445
446     @rtype: list
447     @return: the list of architectures for I{suite}
448     """
449
450     suite_id = None
451     if type(suite) == str:
452         suite_id = get_suite_id(suite)
453     elif type(suite) == int:
454         suite_id = suite
455     else:
456         return None
457
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)
461
462     q = projectB.query(sql)
463     return map(lambda x: x[0], q.getresult())
464
465 def get_suite_untouchable(suite):
466     """
467     Returns true if the C{suite} is untouchable, otherwise false.
468
469     @type suite: string, int
470     @param suite: the suite name or the suite_id
471
472     @rtype: boolean
473     @return: status of suite
474     """
475
476     suite_id = None
477     if type(suite) == str:
478         suite_id = get_suite_id(suite.lower())
479     elif type(suite) == int:
480         suite_id = suite
481     else:
482         return None
483
484     sql = """ SELECT untouchable FROM suite WHERE id='%s' """ % (suite_id)
485
486     q = projectB.query(sql)
487     if q.getresult()[0][0] == "f":
488         return False
489     else:
490         return True
491
492 ################################################################################
493
494 def get_or_set_maintainer_id (maintainer):
495     """
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.
499
500     Results are kept in a cache during runtime to minimize database queries.
501
502     @type maintainer: string
503     @param maintainer: the maintainer name
504
505     @rtype: int
506     @return: the database id for the maintainer
507
508     """
509     global maintainer_id_cache
510
511     if maintainer_id_cache.has_key(maintainer):
512         return maintainer_id_cache[maintainer]
513
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
520
521     return maintainer_id
522
523 ################################################################################
524
525 def get_or_set_keyring_id (keyring):
526     """
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.
530
531     Results are kept in a cache during runtime to minimize database queries.
532
533     @type keyring: string
534     @param keyring: the keyring name
535
536     @rtype: int
537     @return: the database id for the keyring
538
539     """
540     global keyring_id_cache
541
542     if keyring_id_cache.has_key(keyring):
543         return keyring_id_cache[keyring]
544
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
551
552     return keyring_id
553
554 ################################################################################
555
556 def get_or_set_uid_id (uid):
557     """
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.
561
562     Results are kept in a cache during runtime to minimize database queries.
563
564     @type uid: string
565     @param uid: the uid.
566
567     @rtype: int
568     @return: the database id for the uid
569
570     """
571
572     global uid_id_cache
573
574     if uid_id_cache.has_key(uid):
575         return uid_id_cache[uid]
576
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
583
584     return uid_id
585
586 ################################################################################
587
588 def get_or_set_fingerprint_id (fingerprint):
589     """
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.
593
594     Results are kept in a cache during runtime to minimize database queries.
595
596     @type fingerprint: string
597     @param fingerprint: the fingerprint
598
599     @rtype: int
600     @return: the database id for the fingerprint
601
602     """
603     global fingerprint_id_cache
604
605     if fingerprint_id_cache.has_key(fingerprint):
606         return fingerprint_id_cache[fingerprint]
607
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
614
615     return fingerprint_id
616
617 ################################################################################
618
619 def get_files_id (filename, size, md5sum, location_id):
620     """
621     Returns -1, -2 or the file_id for filename, if its C{size} and C{md5sum} match an
622     existing copy.
623
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
628     the file id.
629
630     Results are kept in a cache during runtime to minimize database queries.
631
632     @type filename: string
633     @param filename: the filename of the file to check against the DB
634
635     @type size: int
636     @param size: the size of the file to check against the DB
637
638     @type md5sum: string
639     @param md5sum: the md5sum of the file to check against the DB
640
641     @type location_id: int
642     @param location_id: the id of the location as returned by L{get_location_id}
643
644     @rtype: int / None
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
649                - int: file id
650
651     """
652     global files_id_cache
653
654     cache_key = "%s_%d" % (filename, location_id)
655
656     if files_id_cache.has_key(cache_key):
657         return files_id_cache[cache_key]
658
659     size = int(size)
660     q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id))
661     ql = q.getresult()
662     if ql:
663         if len(ql) != 1:
664             return -1
665         ql = ql[0]
666         orig_size = int(ql[1])
667         orig_md5sum = ql[2]
668         if orig_size != size or orig_md5sum != md5sum:
669             return -2
670         files_id_cache[cache_key] = ql[0]
671         return files_id_cache[cache_key]
672     else:
673         return None
674
675 ################################################################################
676
677 def get_or_set_queue_id (queue):
678     """
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.
682
683     Results are kept in a cache during runtime to minimize database queries.
684
685     @type queue: string
686     @param queue: the queue name (no full path)
687
688     @rtype: int
689     @return: the database id for the queue
690
691     """
692     global queue_id_cache
693
694     if queue_id_cache.has_key(queue):
695         return queue_id_cache[queue]
696
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
703
704     return queue_id
705
706 ################################################################################
707
708 def set_files_id (filename, size, md5sum, sha1sum, sha256sum, location_id):
709     """
710     Insert a new entry into the files table and return its id.
711
712     @type filename: string
713     @param filename: the filename
714
715     @type size: int
716     @param size: the size in bytes
717
718     @type md5sum: string
719     @param md5sum: md5sum of the file
720
721     @type sha1sum: string
722     @param sha1sum: sha1sum of the file
723
724     @type sha256sum: string
725     @param sha256sum: sha256sum of the file
726
727     @type location_id: int
728     @param location_id: the id of the location as returned by L{get_location_id}
729
730     @rtype: int
731     @return: the database id for the new file
732
733     """
734     global files_id_cache
735
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))
737
738     return get_files_id (filename, size, md5sum, location_id)
739
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
742     ### Cool(tm).
743     ##
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]
749
750 ################################################################################
751
752 def get_maintainer (maintainer_id):
753     """
754     Return the name of the maintainer behind C{maintainer_id}.
755
756     Results are kept in a cache during runtime to minimize database queries.
757
758     @type maintainer_id: int
759     @param maintainer_id: the id of the maintainer, eg. from L{get_or_set_maintainer_id}
760
761     @rtype: string
762     @return: the name of the maintainer
763
764     """
765     global maintainer_cache
766
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]
770
771     return maintainer_cache[maintainer_id]
772
773 ################################################################################
774
775 def get_suites(pkgname, src=False):
776     """
777     Return the suites in which C{pkgname} can be found. If C{src} is True query for source
778     package, else binary package.
779
780     @type pkgname: string
781     @param pkgname: name of the package
782
783     @type src: bool
784     @param src: if True look for source packages, false (default) looks for binary.
785
786     @rtype: list
787     @return: list of suites, or empty list if no match
788
789     """
790     if src:
791         sql = """
792         SELECT suite_name
793         FROM source,
794              src_associations,
795              suite
796         WHERE source.id = src_associations.source
797         AND   source.source = '%s'
798         AND   src_associations.suite = suite.id
799         """ % (pkgname)
800     else:
801         sql = """
802         SELECT suite_name
803         FROM binaries,
804              bin_associations,
805              suite
806         WHERE binaries.id = bin_associations.bin
807         AND   package = '%s'
808         AND   bin_associations.suite = suite.id
809         """ % (pkgname)
810
811     q = projectB.query(sql)
812     return map(lambda x: x[0], q.getresult())
813
814
815 ################################################################################
816
817 def get_new_comments(package):
818     """
819     Returns all the possible comments attached to C{package} in NEW. All versions.
820
821     @type package: string
822     @param package: name of the package
823
824     @rtype: list
825     @return: list of strings containing comments for all versions from all authors for package
826     """
827
828     comments = []
829     query = projectB.query(""" SELECT version, comment, author, notedate
830                                FROM new_comments
831                                WHERE package = '%s'
832                                ORDER BY notedate
833                            """ % (package))
834
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)
838
839     return comments
840
841 def has_new_comment(package, version, ignore_trainee=False):
842     """
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.
845
846     @type package: string
847     @param package: name of the package
848
849     @type version: string
850     @param version: package version
851
852     @type ignore_trainee: boolean
853     @param ignore_trainee: ignore trainee comments
854
855     @rtype: boolean
856     @return: true/false
857     """
858
859     trainee=""
860     if ignore_trainee:
861         trainee='AND trainee=false'
862
863     exists = projectB.query("""SELECT 1 FROM new_comments
864                                WHERE package='%s'
865                                AND version='%s'
866                                %s
867                                LIMIT 1"""
868                             % (package, version, trainee) ).getresult()
869
870     if not exists:
871         return False
872     else:
873         return True
874
875 def add_new_comment(package, version, comment, author, trainee=False):
876     """
877     Add a new comment for C{package}, C{version} written by C{author}
878
879     @type package: string
880     @param package: name of the package
881
882     @type version: string
883     @param version: package version
884
885     @type comment: string
886     @param comment: the comment
887
888     @type author: string
889     @param author: the authorname
890
891     @type trainee: boolean
892     @param trainee: trainee comment
893     """
894
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))
898
899     return
900
901 def delete_new_comments(package, version):
902     """
903     Delete a comment for C{package}, C{version}, if one exists
904     """
905
906     projectB.query(""" DELETE FROM new_comments
907                        WHERE package = '%s' AND version = '%s'
908     """ % (package, version))
909     return
910
911 def delete_all_new_comments(package):
912     """
913     Delete all comments for C{package}, if they exist
914     """
915
916     projectB.query(""" DELETE FROM new_comments
917                        WHERE package = '%s'
918     """ % (package))
919     return
920
921 ################################################################################
922 def copy_temporary_contents(package, version, arch, deb, reject):
923     """
924     copy the previously stored contents from the temp table to the permanant one
925
926     during process-unchecked, the deb should have been scanned and the
927     contents stored in pending_content_associations
928     """
929
930     # first see if contents exist:
931
932     arch_id = get_architecture_id (arch)
933
934     exists = projectB.query("""SELECT 1 FROM pending_content_associations
935                                WHERE package='%s'
936                                AND version='%s'
937                                AND architecture=%d LIMIT 1"""
938                             % (package, version, arch_id) ).getresult()
939
940     if not exists:
941         # This should NOT happen.  We should have added contents
942         # during process-unchecked.  if it did, log an error, and send
943         # an email.
944         subst = {
945             "__PACKAGE__": package,
946             "__VERSION__": version,
947             "__ARCH__": arch,
948             "__TO_ADDRESS__": Cnf["Dinstall::MyAdminAddress"],
949             "__DAK_ADDRESS__": Cnf["Dinstall::MyEmailAddress"] }
950
951         message = utils.TemplateSubst(subst, Cnf["Dir::Templates"]+"/missing-contents")
952         utils.send_mail( message )
953
954         exists = Binary(deb, reject).scan_package()
955
956     if exists:
957         sql = """INSERT INTO content_associations(binary_pkg,filepath,filename)
958                  SELECT currval('binaries_id_seq'), filepath, filename FROM pending_content_associations
959                  WHERE package='%s'
960                      AND version='%s'
961                      AND architecture=%d""" % (package, version, arch_id)
962         projectB.query(sql)
963         projectB.query("""DELETE from pending_content_associations
964                           WHERE package='%s'
965                             AND version='%s'
966                             AND architecture=%d""" % (package, version, arch_id))
967
968     return exists