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, apt_pkg
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 suite_bin_version_cache = {}
46 content_path_id_cache = {}
47 content_file_id_cache = {}
49 ################################################################################
51 def init (config, sql):
59 sys.stderr.write("query: \"%s\" ... " % (q))
62 time_diff = time.time()-before
63 sys.stderr.write("took %.3f seconds.\n" % (time_diff))
65 sys.stderr.write("int result: %s\n" % (r))
66 elif type(r) is types.NoneType:
67 sys.stderr.write("result: None\n")
69 sys.stderr.write("pgresult: %s\n" % (r.getresult()))
72 ################################################################################
74 def get_suite_id (suite):
77 if suite_id_cache.has_key(suite):
78 return suite_id_cache[suite]
80 q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
86 suite_id_cache[suite] = suite_id
90 def get_section_id (section):
91 global section_id_cache
93 if section_id_cache.has_key(section):
94 return section_id_cache[section]
96 q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
101 section_id = ql[0][0]
102 section_id_cache[section] = section_id
106 def get_priority_id (priority):
107 global priority_id_cache
109 if priority_id_cache.has_key(priority):
110 return priority_id_cache[priority]
112 q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
117 priority_id = ql[0][0]
118 priority_id_cache[priority] = priority_id
122 def get_override_type_id (type):
123 global override_type_id_cache
125 if override_type_id_cache.has_key(type):
126 return override_type_id_cache[type]
128 q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type))
133 override_type_id = ql[0][0]
134 override_type_id_cache[type] = override_type_id
136 return override_type_id
138 def get_architecture_id (architecture):
139 global architecture_id_cache
141 if architecture_id_cache.has_key(architecture):
142 return architecture_id_cache[architecture]
144 q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
149 architecture_id = ql[0][0]
150 architecture_id_cache[architecture] = architecture_id
152 return architecture_id
154 def get_archive_id (archive):
155 global archive_id_cache
157 archive = archive.lower()
159 if archive_id_cache.has_key(archive):
160 return archive_id_cache[archive]
162 q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive))
167 archive_id = ql[0][0]
168 archive_id_cache[archive] = archive_id
172 def get_component_id (component):
173 global component_id_cache
175 component = component.lower()
177 if component_id_cache.has_key(component):
178 return component_id_cache[component]
180 q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
185 component_id = ql[0][0]
186 component_id_cache[component] = component_id
190 def get_location_id (location, component, archive):
191 global location_id_cache
193 cache_key = location + '_' + component + '_' + location
194 if location_id_cache.has_key(cache_key):
195 return location_id_cache[cache_key]
197 archive_id = get_archive_id (archive)
199 component_id = get_component_id (component)
200 if component_id != -1:
201 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
203 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
208 location_id = ql[0][0]
209 location_id_cache[cache_key] = location_id
213 def get_source_id (source, version):
214 global source_id_cache
216 cache_key = source + '_' + version + '_'
217 if source_id_cache.has_key(cache_key):
218 return source_id_cache[cache_key]
220 q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
222 if not q.getresult():
225 source_id = q.getresult()[0][0]
226 source_id_cache[cache_key] = source_id
230 def get_suite_version(source, suite, arch):
231 global suite_version_cache
232 cache_key = "%s_%s" % (source, suite)
234 if suite_version_cache.has_key(cache_key):
235 return suite_version_cache[cache_key]
237 q = projectB.query("""
238 SELECT s.version FROM source s, suite su, src_associations sa
241 AND su.suite_name='%s'
245 if not q.getresult():
248 version = q.getresult()[0][0]
249 suite_version_cache[cache_key] = version
253 def get_latest_binary_version_id(binary, suite, arch):
254 global suite_bin_version_cache
255 cache_key = "%s_%s" % (binary, suite)
257 if suite_bin_version_cache.has_key(cache_key):
258 return suite_bin_version_cache[cache_key]
260 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 b.architecture = '%d' AND ba.suite = '%d'" % (binary, int(arch), int(suite)))
262 highest_bid, highest_version = None, None
264 for bi in q.getresult():
265 if highest_version == None or apt_pkg.VersionCompare(bi[1], highest_version) == 1:
267 highest_version = bi[1]
271 ################################################################################
273 def get_or_set_maintainer_id (maintainer):
274 global maintainer_id_cache
276 if maintainer_id_cache.has_key(maintainer):
277 return maintainer_id_cache[maintainer]
279 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
280 if not q.getresult():
281 projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
282 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
283 maintainer_id = q.getresult()[0][0]
284 maintainer_id_cache[maintainer] = maintainer_id
288 ################################################################################
290 def get_or_set_keyring_id (keyring):
291 global keyring_id_cache
293 if keyring_id_cache.has_key(keyring):
294 return keyring_id_cache[keyring]
296 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
297 if not q.getresult():
298 projectB.query("INSERT INTO keyrings (name) VALUES ('%s')" % (keyring))
299 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
300 keyring_id = q.getresult()[0][0]
301 keyring_id_cache[keyring] = keyring_id
305 ################################################################################
307 def get_or_set_uid_id (uid):
310 if uid_id_cache.has_key(uid):
311 return uid_id_cache[uid]
313 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
314 if not q.getresult():
315 projectB.query("INSERT INTO uid (uid) VALUES ('%s')" % (uid))
316 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
317 uid_id = q.getresult()[0][0]
318 uid_id_cache[uid] = uid_id
322 ################################################################################
324 def get_or_set_fingerprint_id (fingerprint):
325 global fingerprint_id_cache
327 if fingerprint_id_cache.has_key(fingerprint):
328 return fingerprint_id_cache[fingerprint]
330 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
331 if not q.getresult():
332 projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
333 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
334 fingerprint_id = q.getresult()[0][0]
335 fingerprint_id_cache[fingerprint] = fingerprint_id
337 return fingerprint_id
339 ################################################################################
341 def get_files_id (filename, size, md5sum, location_id):
342 global files_id_cache
344 cache_key = "%s_%d" % (filename, location_id)
346 if files_id_cache.has_key(cache_key):
347 return files_id_cache[cache_key]
350 q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id))
356 orig_size = int(ql[1])
358 if orig_size != size or orig_md5sum != md5sum:
360 files_id_cache[cache_key] = ql[0]
361 return files_id_cache[cache_key]
365 ################################################################################
367 def get_or_set_queue_id (queue):
368 global queue_id_cache
370 if queue_id_cache.has_key(queue):
371 return queue_id_cache[queue]
373 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
374 if not q.getresult():
375 projectB.query("INSERT INTO queue (queue_name) VALUES ('%s')" % (queue))
376 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
377 queue_id = q.getresult()[0][0]
378 queue_id_cache[queue] = queue_id
382 ################################################################################
384 def set_files_id (filename, size, md5sum, sha1sum, sha256sum, location_id):
385 global files_id_cache
387 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 return get_files_id (filename, size, md5sum, location_id)
391 ### currval has issues with postgresql 7.1.3 when the table is big
392 ### it was taking ~3 seconds to return on auric which is very Not
395 ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')")
396 ##ql = q.getresult()[0]
397 ##cache_key = "%s_%d" % (filename, location_id)
398 ##files_id_cache[cache_key] = ql[0]
399 ##return files_id_cache[cache_key]
401 ################################################################################
403 def get_maintainer (maintainer_id):
404 global maintainer_cache
406 if not maintainer_cache.has_key(maintainer_id):
407 q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id))
408 maintainer_cache[maintainer_id] = q.getresult()[0][0]
410 return maintainer_cache[maintainer_id]
412 ################################################################################
414 def get_suites(pkgname, src=False):
416 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 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
419 q = projectB.query(sql)
420 return map(lambda x: x[0], q.getresult())
422 ################################################################################
424 def get_or_set_contents_file_id(file):
425 global content_file_id_cache
427 if not content_file_id_cache.has_key(file):
428 sql_select = "SELECT id FROM content_file_names WHERE file = '%s'" % file
429 q = projectB.query(sql_select)
430 if not q.getresult():
431 # since this can be called within a transaction, we can't use currval
432 q = projectB.query("SELECT nextval('content_file_names_id_seq')")
433 file_id = int(q.getresult()[0][0])
434 projectB.query("INSERT INTO content_file_names VALUES ('%d', '%s')" % (file_id, file))
435 content_file_id_cache[file] = file_id
437 content_file_id_cache[file] = int(q.getresult()[0][0])
438 return content_file_id_cache[file]
440 ################################################################################
442 def get_or_set_contents_path_id(path):
443 global content_path_id_cache
445 if not content_path_id_cache.has_key(path):
446 sql_select = "SELECT id FROM content_file_paths WHERE path = '%s'" % path
447 q = projectB.query(sql_select)
448 if not q.getresult():
449 # since this can be called within a transaction, we can't use currval
450 q = projectB.query("SELECT nextval('content_file_names_id_seq')")
451 path_id = int(q.getresult()[0][0])
452 projectB.query("INSERT INTO content_file_paths VALUES ('%d', '%s')" % ( path_id, path))
453 content_path_id_cache[path] = path_id
455 content_path_id_cache[path] = int(q.getresult()[0][0])
457 return content_path_id_cache[path]
459 ################################################################################
461 def insert_content_path(bin_id, fullpath):
462 # split the path into basename, and pathname
463 (path, file) = os.path.split(fullpath)
465 # Get the necessary IDs ...
466 file_id = get_or_set_contents_file_id(file)
467 path_id = get_or_set_contents_path_id(path)
469 # Put them into content_assiocations
470 projectB.query("INSERT INTO content_associations VALUES (DEFAULT, '%d', '%d', '%d')" % (bin_id, path_id, file_id))