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