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