]> git.decadent.org.uk Git - dak.git/blob - daklib/database.py
Merge branch 'master' into content_generation, make changes based on Joerg's review
[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
8 @group read/write: get_or_set*, set_files_id
9
10 @contact: Debian FTP Master <ftpmaster@debian.org>
11 @copyright: 2000, 2001, 2002, 2003, 2004, 2006  James Troup <james@nocrew.org>
12 @copyright: 2009  Joerg Jaspert <joerg@debian.org>
13 @license: GNU General Public License version 2 or later
14 """
15
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29
30 ################################################################################
31
32 import sys
33 import time
34 import types
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 def do_query(query):
81     """
82     Executes a database query. Writes statistics / timing to stderr.
83
84     @type query: string
85     @param query: database query string, passed unmodified
86
87     @return: db result
88
89     @warning: The query is passed B{unmodified}, so be careful what you use this for.
90     """
91     sys.stderr.write("query: \"%s\" ... " % (query))
92     before = time.time()
93     r = projectB.query(query)
94     time_diff = time.time()-before
95     sys.stderr.write("took %.3f seconds.\n" % (time_diff))
96     if type(r) is int:
97         sys.stderr.write("int result: %s\n" % (r))
98     elif type(r) is types.NoneType:
99         sys.stderr.write("result: None\n")
100     else:
101         sys.stderr.write("pgresult: %s\n" % (r.getresult()))
102     return r
103
104 ################################################################################
105
106 def get_suite_id (suite):
107     """
108     Returns database id for given C{suite}.
109     Results are kept in a cache during runtime to minimize database queries.
110
111     @type suite: string
112     @param suite: The name of the suite
113
114     @rtype: int
115     @return: the database id for the given suite
116
117     """
118     global suite_id_cache
119
120     if suite_id_cache.has_key(suite):
121         return suite_id_cache[suite]
122
123     q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
124     ql = q.getresult()
125     if not ql:
126         return -1
127
128     suite_id = ql[0][0]
129     suite_id_cache[suite] = suite_id
130
131     return suite_id
132
133 def get_section_id (section):
134     """
135     Returns database id for given C{section}.
136     Results are kept in a cache during runtime to minimize database queries.
137
138     @type section: string
139     @param section: The name of the section
140
141     @rtype: int
142     @return: the database id for the given section
143
144     """
145     global section_id_cache
146
147     if section_id_cache.has_key(section):
148         return section_id_cache[section]
149
150     q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
151     ql = q.getresult()
152     if not ql:
153         return -1
154
155     section_id = ql[0][0]
156     section_id_cache[section] = section_id
157
158     return section_id
159
160 def get_priority_id (priority):
161     """
162     Returns database id for given C{priority}.
163     Results are kept in a cache during runtime to minimize database queries.
164
165     @type priority: string
166     @param priority: The name of the priority
167
168     @rtype: int
169     @return: the database id for the given priority
170
171     """
172     global priority_id_cache
173
174     if priority_id_cache.has_key(priority):
175         return priority_id_cache[priority]
176
177     q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
178     ql = q.getresult()
179     if not ql:
180         return -1
181
182     priority_id = ql[0][0]
183     priority_id_cache[priority] = priority_id
184
185     return priority_id
186
187 def get_override_type_id (type):
188     """
189     Returns database id for given override C{type}.
190     Results are kept in a cache during runtime to minimize database queries.
191
192     @type type: string
193     @param type: The name of the override type
194
195     @rtype: int
196     @return: the database id for the given override type
197
198     """
199     global override_type_id_cache
200
201     if override_type_id_cache.has_key(type):
202         return override_type_id_cache[type]
203
204     q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type))
205     ql = q.getresult()
206     if not ql:
207         return -1
208
209     override_type_id = ql[0][0]
210     override_type_id_cache[type] = override_type_id
211
212     return override_type_id
213
214 def get_architecture_id (architecture):
215     """
216     Returns database id for given C{architecture}.
217     Results are kept in a cache during runtime to minimize database queries.
218
219     @type architecture: string
220     @param architecture: The name of the override type
221
222     @rtype: int
223     @return: the database id for the given architecture
224
225     """
226     global architecture_id_cache
227
228     if architecture_id_cache.has_key(architecture):
229         return architecture_id_cache[architecture]
230
231     q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
232     ql = q.getresult()
233     if not ql:
234         return -1
235
236     architecture_id = ql[0][0]
237     architecture_id_cache[architecture] = architecture_id
238
239     return architecture_id
240
241 def get_archive_id (archive):
242     """
243     Returns database id for given C{archive}.
244     Results are kept in a cache during runtime to minimize database queries.
245
246     @type archive: string
247     @param archive: The name of the override type
248
249     @rtype: int
250     @return: the database id for the given archive
251
252     """
253     global archive_id_cache
254
255     archive = archive.lower()
256
257     if archive_id_cache.has_key(archive):
258         return archive_id_cache[archive]
259
260     q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive))
261     ql = q.getresult()
262     if not ql:
263         return -1
264
265     archive_id = ql[0][0]
266     archive_id_cache[archive] = archive_id
267
268     return archive_id
269
270 def get_component_id (component):
271     """
272     Returns database id for given C{component}.
273     Results are kept in a cache during runtime to minimize database queries.
274
275     @type component: string
276     @param component: The name of the component
277
278     @rtype: int
279     @return: the database id for the given component
280
281     """
282     global component_id_cache
283
284     component = component.lower()
285
286     if component_id_cache.has_key(component):
287         return component_id_cache[component]
288
289     q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
290     ql = q.getresult()
291     if not ql:
292         return -1
293
294     component_id = ql[0][0]
295     component_id_cache[component] = component_id
296
297     return component_id
298
299 def get_location_id (location, component, archive):
300     """
301     Returns database id for the location behind the given combination of
302       - B{location} - the path of the location, eg. I{/srv/ftp.debian.org/ftp/pool/}
303       - B{component} - the id of the component as returned by L{get_component_id}
304       - B{archive} - the id of the archive as returned by L{get_archive_id}
305     Results are kept in a cache during runtime to minimize database queries.
306
307     @type location: string
308     @param location: the path of the location
309
310     @type component: int
311     @param component: the id of the component
312
313     @type archive: int
314     @param archive: the id of the archive
315
316     @rtype: int
317     @return: the database id for the location
318
319     """
320     global location_id_cache
321
322     cache_key = location + '_' + component + '_' + location
323     if location_id_cache.has_key(cache_key):
324         return location_id_cache[cache_key]
325
326     archive_id = get_archive_id (archive)
327     if component != "":
328         component_id = get_component_id (component)
329         if component_id != -1:
330             q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
331     else:
332         q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
333     ql = q.getresult()
334     if not ql:
335         return -1
336
337     location_id = ql[0][0]
338     location_id_cache[cache_key] = location_id
339
340     return location_id
341
342 def get_source_id (source, version):
343     """
344     Returns database id for the combination of C{source} and C{version}
345       - B{source} - source package name, eg. I{mailfilter}, I{bbdb}, I{glibc}
346       - B{version}
347     Results are kept in a cache during runtime to minimize database queries.
348
349     @type source: string
350     @param source: source package name
351
352     @type version: string
353     @param version: the source version
354
355     @rtype: int
356     @return: the database id for the source
357
358     """
359     global source_id_cache
360
361     cache_key = source + '_' + version + '_'
362     if source_id_cache.has_key(cache_key):
363         return source_id_cache[cache_key]
364
365     q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
366
367     if not q.getresult():
368         return None
369
370     source_id = q.getresult()[0][0]
371     source_id_cache[cache_key] = source_id
372
373     return source_id
374
375 def get_suite_version(source, suite):
376     """
377     Returns database id for a combination of C{source} and C{suite}.
378
379       - B{source} - source package name, eg. I{mailfilter}, I{bbdb}, I{glibc}
380       - B{suite} - a suite name, eg. I{unstable}
381
382     Results are kept in a cache during runtime to minimize database queries.
383
384     @type source: string
385     @param source: source package name
386
387     @type suite: string
388     @param suite: the suite name
389
390     @rtype: string
391     @return: the version for I{source} in I{suite}
392
393     """
394
395     global suite_version_cache
396     cache_key = "%s_%s" % (source, suite)
397
398     if suite_version_cache.has_key(cache_key):
399         return suite_version_cache[cache_key]
400
401     q = projectB.query("""
402     SELECT s.version FROM source s, suite su, src_associations sa
403     WHERE sa.source=s.id
404       AND sa.suite=su.id
405       AND su.suite_name='%s'
406       AND s.source='%s'"""
407                               % (suite, source))
408
409     if not q.getresult():
410         return None
411
412     version = q.getresult()[0][0]
413     suite_version_cache[cache_key] = version
414
415     return version
416
417 def get_latest_binary_version_id(binary, section, suite, arch):
418     global suite_bin_version_cache
419     cache_key = "%s_%s_%s_%s" % (binary, section, suite, arch)
420     cache_key_all = "%s_%s_%s_%s" % (binary, section, suite, get_architecture_id("all"))
421
422     # Check for the cache hit for its arch, then arch all
423     if suite_bin_version_cache.has_key(cache_key):
424         return suite_bin_version_cache[cache_key]
425     if suite_bin_version_cache.has_key(cache_key_all):
426         return suite_bin_version_cache[cache_key_all]
427     if cache_preloaded == True:
428         return # package does not exist
429
430     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)))
431
432     if not q.getresult():
433         return False
434
435     highest_bid = q.getresult()[0][0]
436
437     suite_bin_version_cache[cache_key] = highest_bid
438     return highest_bid
439
440 def preload_binary_id_cache():
441     global suite_bin_version_cache, cache_preloaded
442
443     # Get suite info
444     q = projectB.query("SELECT id FROM suite")
445     suites = q.getresult()
446
447     # Get arch mappings
448     q = projectB.query("SELECT id FROM architecture")
449     arches = q.getresult()
450
451     for suite in suites:
452         for arch in arches:
453             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])))
454
455             for bi in q.getresult():
456                 cache_key = "%s_%s_%s_%s" % (bi[1], bi[2], suite[0], arch[0])
457                 suite_bin_version_cache[cache_key] = int(bi[0])
458
459     cache_preloaded = True
460
461 def get_suite_architectures(suite):
462     """
463     Returns list of architectures for C{suite}.
464
465     @type suite: string, int
466     @param suite: the suite name or the suite_id
467
468     @rtype: list
469     @return: the list of architectures for I{suite}
470     """
471
472     suite_id = None
473     if type(suite) == str:
474         suite_id = get_suite_id(suite)
475     elif type(suite) == int:
476         suite_id = suite
477     else:
478         return None
479
480     sql = """ SELECT a.arch_string FROM suite_architectures sa
481               JOIN architecture a ON (a.id = sa.architecture)
482               WHERE suite='%s' """ % (suite_id)
483
484     q = projectB.query(sql)
485     return map(lambda x: x[0], q.getresult())
486
487 ################################################################################
488
489 def get_or_set_maintainer_id (maintainer):
490     """
491     If C{maintainer} does not have an entry in the maintainer table yet, create one
492     and return the new id.
493     If C{maintainer} already has an entry, simply return the existing id.
494
495     Results are kept in a cache during runtime to minimize database queries.
496
497     @type maintainer: string
498     @param maintainer: the maintainer name
499
500     @rtype: int
501     @return: the database id for the maintainer
502
503     """
504     global maintainer_id_cache
505
506     if maintainer_id_cache.has_key(maintainer):
507         return maintainer_id_cache[maintainer]
508
509     q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
510     if not q.getresult():
511         projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
512         q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
513     maintainer_id = q.getresult()[0][0]
514     maintainer_id_cache[maintainer] = maintainer_id
515
516     return maintainer_id
517
518 ################################################################################
519
520 def get_or_set_keyring_id (keyring):
521     """
522     If C{keyring} does not have an entry in the C{keyrings} table yet, create one
523     and return the new id.
524     If C{keyring} 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 keyring: string
529     @param keyring: the keyring name
530
531     @rtype: int
532     @return: the database id for the keyring
533
534     """
535     global keyring_id_cache
536
537     if keyring_id_cache.has_key(keyring):
538         return keyring_id_cache[keyring]
539
540     q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
541     if not q.getresult():
542         projectB.query("INSERT INTO keyrings (name) VALUES ('%s')" % (keyring))
543         q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
544     keyring_id = q.getresult()[0][0]
545     keyring_id_cache[keyring] = keyring_id
546
547     return keyring_id
548
549 ################################################################################
550
551 def get_or_set_uid_id (uid):
552     """
553     If C{uid} does not have an entry in the uid table yet, create one
554     and return the new id.
555     If C{uid} 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 uid: string
560     @param uid: the uid.
561
562     @rtype: int
563     @return: the database id for the uid
564
565     """
566
567     global uid_id_cache
568
569     if uid_id_cache.has_key(uid):
570         return uid_id_cache[uid]
571
572     q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
573     if not q.getresult():
574         projectB.query("INSERT INTO uid (uid) VALUES ('%s')" % (uid))
575         q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
576     uid_id = q.getresult()[0][0]
577     uid_id_cache[uid] = uid_id
578
579     return uid_id
580
581 ################################################################################
582
583 def get_or_set_fingerprint_id (fingerprint):
584     """
585     If C{fingerprint} does not have an entry in the fingerprint table yet, create one
586     and return the new id.
587     If C{fingerprint} already has an entry, simply return the existing id.
588
589     Results are kept in a cache during runtime to minimize database queries.
590
591     @type fingerprint: string
592     @param fingerprint: the fingerprint
593
594     @rtype: int
595     @return: the database id for the fingerprint
596
597     """
598     global fingerprint_id_cache
599
600     if fingerprint_id_cache.has_key(fingerprint):
601         return fingerprint_id_cache[fingerprint]
602
603     q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
604     if not q.getresult():
605         projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
606         q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
607     fingerprint_id = q.getresult()[0][0]
608     fingerprint_id_cache[fingerprint] = fingerprint_id
609
610     return fingerprint_id
611
612 ################################################################################
613
614 def get_files_id (filename, size, md5sum, location_id):
615     """
616     Returns -1, -2 or the file_id for filename, if its C{size} and C{md5sum} match an
617     existing copy.
618
619     The database is queried using the C{filename} and C{location_id}. If a file does exist
620     at that location, the existing size and md5sum are checked against the provided
621     parameters. A size or checksum mismatch returns -2. If more than one entry is
622     found within the database, a -1 is returned, no result returns None, otherwise
623     the file id.
624
625     Results are kept in a cache during runtime to minimize database queries.
626
627     @type filename: string
628     @param filename: the filename of the file to check against the DB
629
630     @type size: int
631     @param size: the size of the file to check against the DB
632
633     @type md5sum: string
634     @param md5sum: the md5sum of the file to check against the DB
635
636     @type location_id: int
637     @param location_id: the id of the location as returned by L{get_location_id}
638
639     @rtype: int / None
640     @return: Various return values are possible:
641                - -2: size/checksum error
642                - -1: more than one file found in database
643                - None: no file found in database
644                - int: file id
645
646     """
647     global files_id_cache
648
649     cache_key = "%s_%d" % (filename, location_id)
650
651     if files_id_cache.has_key(cache_key):
652         return files_id_cache[cache_key]
653
654     size = int(size)
655     q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id))
656     ql = q.getresult()
657     if ql:
658         if len(ql) != 1:
659             return -1
660         ql = ql[0]
661         orig_size = int(ql[1])
662         orig_md5sum = ql[2]
663         if orig_size != size or orig_md5sum != md5sum:
664             return -2
665         files_id_cache[cache_key] = ql[0]
666         return files_id_cache[cache_key]
667     else:
668         return None
669
670 ################################################################################
671
672 def get_or_set_queue_id (queue):
673     """
674     If C{queue} does not have an entry in the queue table yet, create one
675     and return the new id.
676     If C{queue} already has an entry, simply return the existing id.
677
678     Results are kept in a cache during runtime to minimize database queries.
679
680     @type queue: string
681     @param queue: the queue name (no full path)
682
683     @rtype: int
684     @return: the database id for the queue
685
686     """
687     global queue_id_cache
688
689     if queue_id_cache.has_key(queue):
690         return queue_id_cache[queue]
691
692     q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
693     if not q.getresult():
694         projectB.query("INSERT INTO queue (queue_name) VALUES ('%s')" % (queue))
695         q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
696     queue_id = q.getresult()[0][0]
697     queue_id_cache[queue] = queue_id
698
699     return queue_id
700
701 ################################################################################
702
703 def set_files_id (filename, size, md5sum, sha1sum, sha256sum, location_id):
704     """
705     Insert a new entry into the files table and return its id.
706
707     @type filename: string
708     @param filename: the filename
709
710     @type size: int
711     @param size: the size in bytes
712
713     @type md5sum: string
714     @param md5sum: md5sum of the file
715
716     @type sha1sum: string
717     @param sha1sum: sha1sum of the file
718
719     @type sha256sum: string
720     @param sha256sum: sha256sum of the file
721
722     @type location_id: int
723     @param location_id: the id of the location as returned by L{get_location_id}
724
725     @rtype: int
726     @return: the database id for the new file
727
728     """
729     global files_id_cache
730
731     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))
732
733     return get_files_id (filename, size, md5sum, location_id)
734
735     ### currval has issues with postgresql 7.1.3 when the table is big
736     ### it was taking ~3 seconds to return on auric which is very Not
737     ### Cool(tm).
738     ##
739     ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')")
740     ##ql = q.getresult()[0]
741     ##cache_key = "%s_%d" % (filename, location_id)
742     ##files_id_cache[cache_key] = ql[0]
743     ##return files_id_cache[cache_key]
744
745 ################################################################################
746
747 def get_maintainer (maintainer_id):
748     """
749     Return the name of the maintainer behind C{maintainer_id}.
750
751     Results are kept in a cache during runtime to minimize database queries.
752
753     @type maintainer_id: int
754     @param maintainer_id: the id of the maintainer, eg. from L{get_or_set_maintainer_id}
755
756     @rtype: string
757     @return: the name of the maintainer
758
759     """
760     global maintainer_cache
761
762     if not maintainer_cache.has_key(maintainer_id):
763         q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id))
764         maintainer_cache[maintainer_id] = q.getresult()[0][0]
765
766     return maintainer_cache[maintainer_id]
767
768 ################################################################################
769
770 def get_suites(pkgname, src=False):
771     """
772     Return the suites in which C{pkgname} can be found. If C{src} is True query for source
773     package, else binary package.
774
775     @type pkgname: string
776     @param pkgname: name of the package
777
778     @type src: bool
779     @param src: if True look for source packages, false (default) looks for binary.
780
781     @rtype: list
782     @return: list of suites, or empty list if no match
783
784     """
785     if src:
786         sql = """
787         SELECT suite_name
788         FROM source,
789              src_associations,
790              suite
791         WHERE source.id = src_associations.source
792         AND   source.source = '%s'
793         AND   src_associations.suite = suite.id
794         """ % (pkgname)
795     else:
796         sql = """
797         SELECT suite_name
798         FROM binaries,
799              bin_associations,
800              suite
801         WHERE binaries.id = bin_associations.bin
802         AND   package = '%s'
803         AND   bin_associations.suite = suite.id
804         """ % (pkgname)
805
806     q = projectB.query(sql)
807     return map(lambda x: x[0], q.getresult())
808
809
810 ################################################################################
811
812 def copy_temporary_contents(package, version, deb):
813     """
814     copy the previously stored contents from the temp table to the permanant one
815
816     during process-unchecked, the deb should have been scanned and the
817     contents stored in temp_content_associations
818     """
819
820     # first see if contents exist:
821
822     exists = projectB.query("""SELECT 1 FROM temp_content_associations
823                                WHERE package='%s' LIMIT 1""" % package ).getresult()
824
825     if not exists:
826         # This should NOT happen.  We should have added contents
827         # during process-unchecked.  if it did, log an error, and send
828         # an email.
829         subst = {
830             "__PACKAGE__": package,
831             "__VERSION__": version,
832             "__TO_ADDRESS__": Cnf["Dinstall::MyAdminAddress"]
833             "__DAK_ADDRESS__": Cnf["Dinstall::MyEmailAddress"]
834             }
835
836         message = utils.TemplateSubst(Subst, Cnf["Dir::Templates"]+"/missing-contents")
837         utils.send_mail( message )
838
839         exists = DBConn().insert_content_path(package, version, deb)
840
841     if exists:
842         sql = """INSERT INTO content_associations(binary_pkg,filepath,filename)
843                  SELECT currval('binaries_id_seq'), filepath, filename FROM temp_content_associations
844                  WHERE package='%s'
845                      AND version='%s'""" % (package, version)
846         projectB.query(sql)
847         projectB.query("""DELETE from temp_content_associations
848                           WHERE package='%s'
849                             AND version='%s'""" % (package, version))
850
851     return exists