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