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