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