4 # Copyright (C) 2000, 2001, 2002, 2003, 2004, 2006 James Troup <james@nocrew.org>
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.
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.
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
20 ################################################################################
22 import os, sys, time, types
24 ################################################################################
30 priority_id_cache = {}
31 override_type_id_cache = {}
32 architecture_id_cache = {}
34 component_id_cache = {}
35 location_id_cache = {}
36 maintainer_id_cache = {}
41 fingerprint_id_cache = {}
44 suite_version_cache = {}
45 content_path_id_cache = {}
46 content_file_id_cache = {}
48 ################################################################################
50 def init (config, sql):
58 sys.stderr.write("query: \"%s\" ... " % (q))
61 time_diff = time.time()-before
62 sys.stderr.write("took %.3f seconds.\n" % (time_diff))
64 sys.stderr.write("int result: %s\n" % (r))
65 elif type(r) is types.NoneType:
66 sys.stderr.write("result: None\n")
68 sys.stderr.write("pgresult: %s\n" % (r.getresult()))
71 ################################################################################
73 def get_suite_id (suite):
76 if suite_id_cache.has_key(suite):
77 return suite_id_cache[suite]
79 q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
85 suite_id_cache[suite] = suite_id
89 def get_section_id (section):
90 global section_id_cache
92 if section_id_cache.has_key(section):
93 return section_id_cache[section]
95 q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
100 section_id = ql[0][0]
101 section_id_cache[section] = section_id
105 def get_priority_id (priority):
106 global priority_id_cache
108 if priority_id_cache.has_key(priority):
109 return priority_id_cache[priority]
111 q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
116 priority_id = ql[0][0]
117 priority_id_cache[priority] = priority_id
121 def get_override_type_id (type):
122 global override_type_id_cache
124 if override_type_id_cache.has_key(type):
125 return override_type_id_cache[type]
127 q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type))
132 override_type_id = ql[0][0]
133 override_type_id_cache[type] = override_type_id
135 return override_type_id
137 def get_architecture_id (architecture):
138 global architecture_id_cache
140 if architecture_id_cache.has_key(architecture):
141 return architecture_id_cache[architecture]
143 q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
148 architecture_id = ql[0][0]
149 architecture_id_cache[architecture] = architecture_id
151 return architecture_id
153 def get_archive_id (archive):
154 global archive_id_cache
156 archive = archive.lower()
158 if archive_id_cache.has_key(archive):
159 return archive_id_cache[archive]
161 q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive))
166 archive_id = ql[0][0]
167 archive_id_cache[archive] = archive_id
171 def get_component_id (component):
172 global component_id_cache
174 component = component.lower()
176 if component_id_cache.has_key(component):
177 return component_id_cache[component]
179 q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
184 component_id = ql[0][0]
185 component_id_cache[component] = component_id
189 def get_location_id (location, component, archive):
190 global location_id_cache
192 cache_key = location + '_' + component + '_' + location
193 if location_id_cache.has_key(cache_key):
194 return location_id_cache[cache_key]
196 archive_id = get_archive_id (archive)
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))
202 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
207 location_id = ql[0][0]
208 location_id_cache[cache_key] = location_id
212 def get_source_id (source, version):
213 global source_id_cache
215 cache_key = source + '_' + version + '_'
216 if source_id_cache.has_key(cache_key):
217 return source_id_cache[cache_key]
219 q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
221 if not q.getresult():
224 source_id = q.getresult()[0][0]
225 source_id_cache[cache_key] = source_id
229 def get_suite_version(source, suite):
230 global suite_version_cache
231 cache_key = "%s_%s" % (source, suite)
233 if suite_version_cache.has_key(cache_key):
234 return suite_version_cache[cache_key]
236 q = projectB.query("""
237 SELECT s.version FROM source s, suite su, src_associations sa
240 AND su.suite_name='%s'
244 if not q.getresult():
247 version = q.getresult()[0][0]
248 suite_version_cache[cache_key] = version
252 def get_latest_binary_version_id(binary, suite):
253 global suite_version_cache
254 cache_key = "%s_%s" % (binary, suite)
257 if suite_version_cache.has_key(cache_key):
258 return suite_version_cache[cache_key]
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)))
263 highest_bid, highest_version = None, None
265 for bi in q.getresult():
266 if highest_version == None or apt_pkg.VersionCompare(bi[1], highest_version) == 1:
268 highest_version = bi[1]
272 ################################################################################
274 def get_or_set_maintainer_id (maintainer):
275 global maintainer_id_cache
277 if maintainer_id_cache.has_key(maintainer):
278 return maintainer_id_cache[maintainer]
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
289 ################################################################################
291 def get_or_set_keyring_id (keyring):
292 global keyring_id_cache
294 if keyring_id_cache.has_key(keyring):
295 return keyring_id_cache[keyring]
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
306 ################################################################################
308 def get_or_set_uid_id (uid):
311 if uid_id_cache.has_key(uid):
312 return uid_id_cache[uid]
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
323 ################################################################################
325 def get_or_set_fingerprint_id (fingerprint):
326 global fingerprint_id_cache
328 if fingerprint_id_cache.has_key(fingerprint):
329 return fingerprint_id_cache[fingerprint]
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
338 return fingerprint_id
340 ################################################################################
342 def get_files_id (filename, size, md5sum, location_id):
343 global files_id_cache
345 cache_key = "%s_%d" % (filename, location_id)
347 if files_id_cache.has_key(cache_key):
348 return files_id_cache[cache_key]
351 q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id))
357 orig_size = int(ql[1])
359 if orig_size != size or orig_md5sum != md5sum:
361 files_id_cache[cache_key] = ql[0]
362 return files_id_cache[cache_key]
366 ################################################################################
368 def get_or_set_queue_id (queue):
369 global queue_id_cache
371 if queue_id_cache.has_key(queue):
372 return queue_id_cache[queue]
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
383 ################################################################################
385 def set_files_id (filename, size, md5sum, sha1sum, sha256sum, location_id):
386 global files_id_cache
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))
390 return get_files_id (filename, size, md5sum, location_id)
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
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]
402 ################################################################################
404 def get_maintainer (maintainer_id):
405 global maintainer_cache
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]
411 return maintainer_cache[maintainer_id]
413 ################################################################################
415 def get_suites(pkgname, src=False):
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
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())
423 ################################################################################
425 def get_or_set_contents_file_id(file):
426 global content_file_id_cache
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
438 content_file_id_cache[file] = int(q.getresult()[0][0])
439 return content_file_id_cache[file]
441 ################################################################################
443 def get_or_set_contents_path_id(path):
444 global content_path_id_cache
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
456 content_path_id_cache[path] = int(q.getresult()[0][0])
458 return content_path_id_cache[path]
460 ################################################################################
462 def insert_content_path(bin_id, fullpath):
463 # split the path into basename, and pathname
464 (path, file) = os.path.split(fullpath)
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)
470 # Put them into content_assiocations
471 projectB.query("INSERT INTO content_associations VALUES (DEFAULT, '%d', '%d', '%d')" % (bin_id, path_id, file_id))