]> git.decadent.org.uk Git - dak.git/blob - daklib/database.py
cad427ac07c682b5994f0bf26cd12eaedbd0e9aa
[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 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
46 ################################################################################
47
48 def init (config, sql):
49     global Cnf, projectB
50
51     Cnf = config
52     projectB = sql
53
54
55 def do_query(q):
56     sys.stderr.write("query: \"%s\" ... " % (q))
57     before = time.time()
58     r = projectB.query(q)
59     time_diff = time.time()-before
60     sys.stderr.write("took %.3f seconds.\n" % (time_diff))
61     if type(r) is int:
62         sys.stderr.write("int result: %s\n" % (r))
63     elif type(r) is types.NoneType:
64         sys.stderr.write("result: None\n")
65     else:
66         sys.stderr.write("pgresult: %s\n" % (r.getresult()))
67     return r
68
69 ################################################################################
70
71 def get_suite_id (suite):
72     global suite_id_cache
73
74     if suite_id_cache.has_key(suite):
75         return suite_id_cache[suite]
76
77     q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
78     ql = q.getresult()
79     if not ql:
80         return -1
81
82     suite_id = ql[0][0]
83     suite_id_cache[suite] = suite_id
84
85     return suite_id
86
87 def get_section_id (section):
88     global section_id_cache
89
90     if section_id_cache.has_key(section):
91         return section_id_cache[section]
92
93     q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
94     ql = q.getresult()
95     if not ql:
96         return -1
97
98     section_id = ql[0][0]
99     section_id_cache[section] = section_id
100
101     return section_id
102
103 def get_priority_id (priority):
104     global priority_id_cache
105
106     if priority_id_cache.has_key(priority):
107         return priority_id_cache[priority]
108
109     q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
110     ql = q.getresult()
111     if not ql:
112         return -1
113
114     priority_id = ql[0][0]
115     priority_id_cache[priority] = priority_id
116
117     return priority_id
118
119 def get_override_type_id (type):
120     global override_type_id_cache
121
122     if override_type_id_cache.has_key(type):
123         return override_type_id_cache[type]
124
125     q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type))
126     ql = q.getresult()
127     if not ql:
128         return -1
129
130     override_type_id = ql[0][0]
131     override_type_id_cache[type] = override_type_id
132
133     return override_type_id
134
135 def get_architecture_id (architecture):
136     global architecture_id_cache
137
138     if architecture_id_cache.has_key(architecture):
139         return architecture_id_cache[architecture]
140
141     q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
142     ql = q.getresult()
143     if not ql:
144         return -1
145
146     architecture_id = ql[0][0]
147     architecture_id_cache[architecture] = architecture_id
148
149     return architecture_id
150
151 def get_archive_id (archive):
152     global archive_id_cache
153
154     archive = archive.lower()
155
156     if archive_id_cache.has_key(archive):
157         return archive_id_cache[archive]
158
159     q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive))
160     ql = q.getresult()
161     if not ql:
162         return -1
163
164     archive_id = ql[0][0]
165     archive_id_cache[archive] = archive_id
166
167     return archive_id
168
169 def get_component_id (component):
170     global component_id_cache
171
172     component = component.lower()
173
174     if component_id_cache.has_key(component):
175         return component_id_cache[component]
176
177     q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
178     ql = q.getresult()
179     if not ql:
180         return -1
181
182     component_id = ql[0][0]
183     component_id_cache[component] = component_id
184
185     return component_id
186
187 def get_location_id (location, component, archive):
188     global location_id_cache
189
190     cache_key = location + '_' + component + '_' + location
191     if location_id_cache.has_key(cache_key):
192         return location_id_cache[cache_key]
193
194     archive_id = get_archive_id (archive)
195     if component != "":
196         component_id = get_component_id (component)
197         if component_id != -1:
198             q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
199     else:
200         q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
201     ql = q.getresult()
202     if not ql:
203         return -1
204
205     location_id = ql[0][0]
206     location_id_cache[cache_key] = location_id
207
208     return location_id
209
210 def get_source_id (source, version):
211     global source_id_cache
212
213     cache_key = source + '_' + version + '_'
214     if source_id_cache.has_key(cache_key):
215         return source_id_cache[cache_key]
216
217     q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
218
219     if not q.getresult():
220         return None
221
222     source_id = q.getresult()[0][0]
223     source_id_cache[cache_key] = source_id
224
225     return source_id
226
227 def get_suite_version(source, suite):
228     global suite_version_cache
229     cache_key = "%s_%s" % (source, suite)
230
231     if suite_version_cache.has_key(cache_key):
232         return suite_version_cache[cache_key]
233
234     q = projectB.query("""
235     SELECT s.version FROM source s, suite su, src_associations sa
236     WHERE sa.source=s.id
237       AND sa.suite=su.id
238       AND su.suite_name='%s'
239       AND s.source='%s'"""
240                               % (suite, source))
241
242     if not q.getresult():
243         return None
244
245     version = q.getresult()[0][0]
246     suite_version_cache[cache_key] = version
247
248     return version
249
250 ################################################################################
251
252 def get_or_set_maintainer_id (maintainer):
253     global maintainer_id_cache
254
255     if maintainer_id_cache.has_key(maintainer):
256         return maintainer_id_cache[maintainer]
257
258     q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
259     if not q.getresult():
260         projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
261         q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
262     maintainer_id = q.getresult()[0][0]
263     maintainer_id_cache[maintainer] = maintainer_id
264
265     return maintainer_id
266
267 ################################################################################
268
269 def get_or_set_keyring_id (keyring):
270     global keyring_id_cache
271
272     if keyring_id_cache.has_key(keyring):
273         return keyring_id_cache[keyring]
274
275     q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
276     if not q.getresult():
277         projectB.query("INSERT INTO keyrings (name) VALUES ('%s')" % (keyring))
278         q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
279     keyring_id = q.getresult()[0][0]
280     keyring_id_cache[keyring] = keyring_id
281
282     return keyring_id
283
284 ################################################################################
285
286 def get_or_set_uid_id (uid):
287     global uid_id_cache
288
289     if uid_id_cache.has_key(uid):
290         return uid_id_cache[uid]
291
292     q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
293     if not q.getresult():
294         projectB.query("INSERT INTO uid (uid) VALUES ('%s')" % (uid))
295         q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
296     uid_id = q.getresult()[0][0]
297     uid_id_cache[uid] = uid_id
298
299     return uid_id
300
301 ################################################################################
302
303 def get_or_set_fingerprint_id (fingerprint):
304     global fingerprint_id_cache
305
306     if fingerprint_id_cache.has_key(fingerprint):
307         return fingerprint_id_cache[fingerprint]
308
309     q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
310     if not q.getresult():
311         projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
312         q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
313     fingerprint_id = q.getresult()[0][0]
314     fingerprint_id_cache[fingerprint] = fingerprint_id
315
316     return fingerprint_id
317
318 ################################################################################
319
320 def get_files_id (filename, size, md5sum, sha1sum, sha256sum location_id):
321     global files_id_cache
322
323     cache_key = "%s_%d" % (filename, location_id)
324
325     if files_id_cache.has_key(cache_key):
326         return files_id_cache[cache_key]
327
328     size = int(size)
329     q = projectB.query("SELECT id, size, md5sum, sha1sum, sha256sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id))
330     ql = q.getresult()
331     if ql:
332         if len(ql) != 1:
333             return -1
334         ql = ql[0]
335         orig_size = int(ql[1])
336         orig_md5sum = ql[2]
337         orig_sha1sum = ql[3]
338         orig_sha256sum = ql[4]
339         if orig_size != size or orig_md5sum != md5sum or orig_sha1sum != sha1sum or orig_sha256sum != sha256sum:
340             return -2
341         files_id_cache[cache_key] = ql[0]
342         return files_id_cache[cache_key]
343     else:
344         return None
345
346 ################################################################################
347
348 def get_or_set_queue_id (queue):
349     global queue_id_cache
350
351     if queue_id_cache.has_key(queue):
352         return queue_id_cache[queue]
353
354     q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
355     if not q.getresult():
356         projectB.query("INSERT INTO queue (queue_name) VALUES ('%s')" % (queue))
357         q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
358     queue_id = q.getresult()[0][0]
359     queue_id_cache[queue] = queue_id
360
361     return queue_id
362
363 ################################################################################
364
365 def set_files_id (filename, size, md5sum, sha1sum, sha256sum, location_id):
366     global files_id_cache
367
368     projectB.query("INSERT INTO files (filename, size, md5sum, sha1sum, sha256sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, sha1sum, sha256sum location_id))
369
370     return get_files_id (filename, size, md5sum, sha1sum, sha256sum, location_id)
371
372     ### currval has issues with postgresql 7.1.3 when the table is big
373     ### it was taking ~3 seconds to return on auric which is very Not
374     ### Cool(tm).
375     ##
376     ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')")
377     ##ql = q.getresult()[0]
378     ##cache_key = "%s_%d" % (filename, location_id)
379     ##files_id_cache[cache_key] = ql[0]
380     ##return files_id_cache[cache_key]
381
382 ################################################################################
383
384 def get_maintainer (maintainer_id):
385     global maintainer_cache
386
387     if not maintainer_cache.has_key(maintainer_id):
388         q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id))
389         maintainer_cache[maintainer_id] = q.getresult()[0][0]
390
391     return maintainer_cache[maintainer_id]
392
393 ################################################################################