]> git.decadent.org.uk Git - dak.git/blob - daklib/database.py
Merge branch 'master' into content_generation
[dak.git] / daklib / database.py
1 #!/usr/bin/env python
2
3 # DB access fucntions
4 # Copyright (C) 2000, 2001, 2002, 2003, 2004, 2006  James Troup <james@nocrew.org>
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 ################################################################################
21
22 import os, sys, time, types, apt_pkg
23
24 ################################################################################
25
26 Cnf = None
27 projectB = None
28 suite_id_cache = {}
29 section_id_cache = {}
30 priority_id_cache = {}
31 override_type_id_cache = {}
32 architecture_id_cache = {}
33 archive_id_cache = {}
34 component_id_cache = {}
35 location_id_cache = {}
36 maintainer_id_cache = {}
37 keyring_id_cache = {}
38 source_id_cache = {}
39 files_id_cache = {}
40 maintainer_cache = {}
41 fingerprint_id_cache = {}
42 queue_id_cache = {}
43 uid_id_cache = {}
44 suite_version_cache = {}
45 suite_bin_version_cache = {}
46 content_path_id_cache = {}
47 content_file_id_cache = {}
48
49 ################################################################################
50
51 def init (config, sql):
52     global Cnf, projectB
53
54     Cnf = config
55     projectB = sql
56
57
58 def do_query(q):
59     sys.stderr.write("query: \"%s\" ... " % (q))
60     before = time.time()
61     r = projectB.query(q)
62     time_diff = time.time()-before
63     sys.stderr.write("took %.3f seconds.\n" % (time_diff))
64     if type(r) is int:
65         sys.stderr.write("int result: %s\n" % (r))
66     elif type(r) is types.NoneType:
67         sys.stderr.write("result: None\n")
68     else:
69         sys.stderr.write("pgresult: %s\n" % (r.getresult()))
70     return r
71
72 ################################################################################
73
74 def get_suite_id (suite):
75     global suite_id_cache
76
77     if suite_id_cache.has_key(suite):
78         return suite_id_cache[suite]
79
80     q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
81     ql = q.getresult()
82     if not ql:
83         return -1
84
85     suite_id = ql[0][0]
86     suite_id_cache[suite] = suite_id
87
88     return suite_id
89
90 def get_section_id (section):
91     global section_id_cache
92
93     if section_id_cache.has_key(section):
94         return section_id_cache[section]
95
96     q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
97     ql = q.getresult()
98     if not ql:
99         return -1
100
101     section_id = ql[0][0]
102     section_id_cache[section] = section_id
103
104     return section_id
105
106 def get_priority_id (priority):
107     global priority_id_cache
108
109     if priority_id_cache.has_key(priority):
110         return priority_id_cache[priority]
111
112     q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
113     ql = q.getresult()
114     if not ql:
115         return -1
116
117     priority_id = ql[0][0]
118     priority_id_cache[priority] = priority_id
119
120     return priority_id
121
122 def get_override_type_id (type):
123     global override_type_id_cache
124
125     if override_type_id_cache.has_key(type):
126         return override_type_id_cache[type]
127
128     q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type))
129     ql = q.getresult()
130     if not ql:
131         return -1
132
133     override_type_id = ql[0][0]
134     override_type_id_cache[type] = override_type_id
135
136     return override_type_id
137
138 def get_architecture_id (architecture):
139     global architecture_id_cache
140
141     if architecture_id_cache.has_key(architecture):
142         return architecture_id_cache[architecture]
143
144     q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
145     ql = q.getresult()
146     if not ql:
147         return -1
148
149     architecture_id = ql[0][0]
150     architecture_id_cache[architecture] = architecture_id
151
152     return architecture_id
153
154 def get_archive_id (archive):
155     global archive_id_cache
156
157     archive = archive.lower()
158
159     if archive_id_cache.has_key(archive):
160         return archive_id_cache[archive]
161
162     q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive))
163     ql = q.getresult()
164     if not ql:
165         return -1
166
167     archive_id = ql[0][0]
168     archive_id_cache[archive] = archive_id
169
170     return archive_id
171
172 def get_component_id (component):
173     global component_id_cache
174
175     component = component.lower()
176
177     if component_id_cache.has_key(component):
178         return component_id_cache[component]
179
180     q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
181     ql = q.getresult()
182     if not ql:
183         return -1
184
185     component_id = ql[0][0]
186     component_id_cache[component] = component_id
187
188     return component_id
189
190 def get_location_id (location, component, archive):
191     global location_id_cache
192
193     cache_key = location + '_' + component + '_' + location
194     if location_id_cache.has_key(cache_key):
195         return location_id_cache[cache_key]
196
197     archive_id = get_archive_id (archive)
198     if component != "":
199         component_id = get_component_id (component)
200         if component_id != -1:
201             q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
202     else:
203         q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
204     ql = q.getresult()
205     if not ql:
206         return -1
207
208     location_id = ql[0][0]
209     location_id_cache[cache_key] = location_id
210
211     return location_id
212
213 def get_source_id (source, version):
214     global source_id_cache
215
216     cache_key = source + '_' + version + '_'
217     if source_id_cache.has_key(cache_key):
218         return source_id_cache[cache_key]
219
220     q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
221
222     if not q.getresult():
223         return None
224
225     source_id = q.getresult()[0][0]
226     source_id_cache[cache_key] = source_id
227
228     return source_id
229
230 def get_suite_version(source, suite, arch):
231     global suite_version_cache
232     cache_key = "%s_%s" % (source, suite)
233
234     if suite_version_cache.has_key(cache_key):
235         return suite_version_cache[cache_key]
236
237     q = projectB.query("""
238     SELECT s.version FROM source s, suite su, src_associations sa
239     WHERE sa.source=s.id
240       AND sa.suite=su.id
241       AND su.suite_name='%s'
242       AND s.source='%s'"""
243                               % (suite, source))
244
245     if not q.getresult():
246         return None
247
248     version = q.getresult()[0][0]
249     suite_version_cache[cache_key] = version
250
251     return version
252
253 def get_latest_binary_version_id(binary, suite, arch):
254     global suite_bin_version_cache
255     cache_key = "%s_%s" % (binary, suite)
256
257     if suite_bin_version_cache.has_key(cache_key):
258         return suite_bin_version_cache[cache_key]
259
260     q = projectB.query("SELECT b.id, b.version FROM binaries b JOIN bin_associations ba ON (b.id = ba.bin) WHERE b.package = '%s' AND b.architecture = '%d' AND ba.suite = '%d'" % (binary, int(arch), int(suite)))
261
262     highest_bid, highest_version = None, None
263
264     for bi in q.getresult():
265         if highest_version == None or apt_pkg.VersionCompare(bi[1], highest_version) == 1:
266              highest_bid = bi[0]
267              highest_version = bi[1]
268
269     return highest_bid
270
271 ################################################################################
272
273 def get_or_set_maintainer_id (maintainer):
274     global maintainer_id_cache
275
276     if maintainer_id_cache.has_key(maintainer):
277         return maintainer_id_cache[maintainer]
278
279     q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
280     if not q.getresult():
281         projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
282         q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
283     maintainer_id = q.getresult()[0][0]
284     maintainer_id_cache[maintainer] = maintainer_id
285
286     return maintainer_id
287
288 ################################################################################
289
290 def get_or_set_keyring_id (keyring):
291     global keyring_id_cache
292
293     if keyring_id_cache.has_key(keyring):
294         return keyring_id_cache[keyring]
295
296     q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
297     if not q.getresult():
298         projectB.query("INSERT INTO keyrings (name) VALUES ('%s')" % (keyring))
299         q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
300     keyring_id = q.getresult()[0][0]
301     keyring_id_cache[keyring] = keyring_id
302
303     return keyring_id
304
305 ################################################################################
306
307 def get_or_set_uid_id (uid):
308     global uid_id_cache
309
310     if uid_id_cache.has_key(uid):
311         return uid_id_cache[uid]
312
313     q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
314     if not q.getresult():
315         projectB.query("INSERT INTO uid (uid) VALUES ('%s')" % (uid))
316         q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
317     uid_id = q.getresult()[0][0]
318     uid_id_cache[uid] = uid_id
319
320     return uid_id
321
322 ################################################################################
323
324 def get_or_set_fingerprint_id (fingerprint):
325     global fingerprint_id_cache
326
327     if fingerprint_id_cache.has_key(fingerprint):
328         return fingerprint_id_cache[fingerprint]
329
330     q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
331     if not q.getresult():
332         projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
333         q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
334     fingerprint_id = q.getresult()[0][0]
335     fingerprint_id_cache[fingerprint] = fingerprint_id
336
337     return fingerprint_id
338
339 ################################################################################
340
341 def get_files_id (filename, size, md5sum, location_id):
342     global files_id_cache
343
344     cache_key = "%s_%d" % (filename, location_id)
345
346     if files_id_cache.has_key(cache_key):
347         return files_id_cache[cache_key]
348
349     size = int(size)
350     q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id))
351     ql = q.getresult()
352     if ql:
353         if len(ql) != 1:
354             return -1
355         ql = ql[0]
356         orig_size = int(ql[1])
357         orig_md5sum = ql[2]
358         if orig_size != size or orig_md5sum != md5sum:
359             return -2
360         files_id_cache[cache_key] = ql[0]
361         return files_id_cache[cache_key]
362     else:
363         return None
364
365 ################################################################################
366
367 def get_or_set_queue_id (queue):
368     global queue_id_cache
369
370     if queue_id_cache.has_key(queue):
371         return queue_id_cache[queue]
372
373     q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
374     if not q.getresult():
375         projectB.query("INSERT INTO queue (queue_name) VALUES ('%s')" % (queue))
376         q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
377     queue_id = q.getresult()[0][0]
378     queue_id_cache[queue] = queue_id
379
380     return queue_id
381
382 ################################################################################
383
384 def set_files_id (filename, size, md5sum, sha1sum, sha256sum, location_id):
385     global files_id_cache
386
387     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))
388
389     return get_files_id (filename, size, md5sum, location_id)
390
391     ### currval has issues with postgresql 7.1.3 when the table is big
392     ### it was taking ~3 seconds to return on auric which is very Not
393     ### Cool(tm).
394     ##
395     ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')")
396     ##ql = q.getresult()[0]
397     ##cache_key = "%s_%d" % (filename, location_id)
398     ##files_id_cache[cache_key] = ql[0]
399     ##return files_id_cache[cache_key]
400
401 ################################################################################
402
403 def get_maintainer (maintainer_id):
404     global maintainer_cache
405
406     if not maintainer_cache.has_key(maintainer_id):
407         q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id))
408         maintainer_cache[maintainer_id] = q.getresult()[0][0]
409
410     return maintainer_cache[maintainer_id]
411
412 ################################################################################
413
414 def get_suites(pkgname, src=False):
415     if src:
416         sql = "select suite_name from source, src_associations,suite where source.id=src_associations.source and source.source='%s' and src_associations.suite = suite.id"%pkgname
417     else:
418         sql = "select suite_name from binaries, bin_associations,suite where binaries.id=bin_associations.bin and  package='%s' and bin_associations.suite = suite.id"%pkgname
419     q = projectB.query(sql)
420     return map(lambda x: x[0], q.getresult())
421
422 ################################################################################
423
424 def get_or_set_contents_file_id(file):
425     global content_file_id_cache
426
427     if not content_file_id_cache.has_key(file):
428         sql_select = "SELECT id FROM content_file_names WHERE file = '%s'" % file
429         q = projectB.query(sql_select)
430         if not q.getresult():
431             # since this can be called within a transaction, we can't use currval
432             q = projectB.query("SELECT nextval('content_file_names_id_seq')")
433             file_id = int(q.getresult()[0][0])
434             projectB.query("INSERT INTO content_file_names VALUES ('%d', '%s')" % (file_id, file))
435             content_file_id_cache[file] =  file_id
436         else:
437             content_file_id_cache[file] = int(q.getresult()[0][0])
438     return content_file_id_cache[file]
439
440 ################################################################################
441
442 def get_or_set_contents_path_id(path):
443     global content_path_id_cache
444
445     if not content_path_id_cache.has_key(path):
446         sql_select = "SELECT id FROM content_file_paths WHERE path = '%s'" % path
447         q = projectB.query(sql_select)
448         if not q.getresult():
449             # since this can be called within a transaction, we can't use currval
450             q = projectB.query("SELECT nextval('content_file_names_id_seq')")
451             path_id = int(q.getresult()[0][0])
452             projectB.query("INSERT INTO content_file_paths VALUES ('%d', '%s')" % ( path_id, path))
453             content_path_id_cache[path] = path_id
454         else:
455             content_path_id_cache[path] = int(q.getresult()[0][0])
456
457     return content_path_id_cache[path]
458
459 ################################################################################
460
461 def insert_content_path(bin_id, fullpath):
462     # split the path into basename, and pathname
463     (path, file)  = os.path.split(fullpath)
464
465     # Get the necessary IDs ...
466     file_id = get_or_set_contents_file_id(file)
467     path_id = get_or_set_contents_path_id(path)
468
469     # Put them into content_assiocations
470     projectB.query("INSERT INTO content_associations VALUES (DEFAULT, '%d', '%d', '%d')" % (bin_id, path_id, file_id))
471     return