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 = {}
48 insert_contents_file_cache = {}
50 ################################################################################
52 def init (config, sql):
60 sys.stderr.write("query: \"%s\" ... " % (q))
63 time_diff = time.time()-before
64 sys.stderr.write("took %.3f seconds.\n" % (time_diff))
66 sys.stderr.write("int result: %s\n" % (r))
67 elif type(r) is types.NoneType:
68 sys.stderr.write("result: None\n")
70 sys.stderr.write("pgresult: %s\n" % (r.getresult()))
73 ################################################################################
75 def get_suite_id (suite):
78 if suite_id_cache.has_key(suite):
79 return suite_id_cache[suite]
81 q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
87 suite_id_cache[suite] = suite_id
91 def get_section_id (section):
92 global section_id_cache
94 if section_id_cache.has_key(section):
95 return section_id_cache[section]
97 q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
102 section_id = ql[0][0]
103 section_id_cache[section] = section_id
107 def get_priority_id (priority):
108 global priority_id_cache
110 if priority_id_cache.has_key(priority):
111 return priority_id_cache[priority]
113 q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
118 priority_id = ql[0][0]
119 priority_id_cache[priority] = priority_id
123 def get_override_type_id (type):
124 global override_type_id_cache
126 if override_type_id_cache.has_key(type):
127 return override_type_id_cache[type]
129 q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type))
134 override_type_id = ql[0][0]
135 override_type_id_cache[type] = override_type_id
137 return override_type_id
139 def get_architecture_id (architecture):
140 global architecture_id_cache
142 if architecture_id_cache.has_key(architecture):
143 return architecture_id_cache[architecture]
145 q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
150 architecture_id = ql[0][0]
151 architecture_id_cache[architecture] = architecture_id
153 return architecture_id
155 def get_archive_id (archive):
156 global archive_id_cache
158 archive = archive.lower()
160 if archive_id_cache.has_key(archive):
161 return archive_id_cache[archive]
163 q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive))
168 archive_id = ql[0][0]
169 archive_id_cache[archive] = archive_id
173 def get_component_id (component):
174 global component_id_cache
176 component = component.lower()
178 if component_id_cache.has_key(component):
179 return component_id_cache[component]
181 q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
186 component_id = ql[0][0]
187 component_id_cache[component] = component_id
191 def get_location_id (location, component, archive):
192 global location_id_cache
194 cache_key = location + '_' + component + '_' + location
195 if location_id_cache.has_key(cache_key):
196 return location_id_cache[cache_key]
198 archive_id = get_archive_id (archive)
200 component_id = get_component_id (component)
201 if component_id != -1:
202 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
204 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
209 location_id = ql[0][0]
210 location_id_cache[cache_key] = location_id
214 def get_source_id (source, version):
215 global source_id_cache
217 cache_key = source + '_' + version + '_'
218 if source_id_cache.has_key(cache_key):
219 return source_id_cache[cache_key]
221 q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
223 if not q.getresult():
226 source_id = q.getresult()[0][0]
227 source_id_cache[cache_key] = source_id
231 def get_suite_version(source, suite, arch):
232 global suite_version_cache
233 cache_key = "%s_%s" % (source, suite)
235 if suite_version_cache.has_key(cache_key):
236 return suite_version_cache[cache_key]
238 q = projectB.query("""
239 SELECT s.version FROM source s, suite su, src_associations sa
242 AND su.suite_name='%s'
246 if not q.getresult():
249 version = q.getresult()[0][0]
250 suite_version_cache[cache_key] = version
254 def get_latest_binary_version_id(binary, section, suite, arch):
255 global suite_bin_version_cache
256 cache_key = "%s_%s_%s_%s" % (binary, section, suite, arch)
258 if suite_bin_version_cache.has_key(cache_key):
259 return suite_bin_version_cache[cache_key]
261 q = projectB.query("SELECT b.id, b.version FROM binaries b JOIN bin_associations ba ON (b.id = ba.bin) JOIN override o ON (o.package=b.package) WHERE b.package = '%s' AND b.architecture = '%d' AND ba.suite = '%d' AND o.section = '%d'" % (binary, int(arch), int(suite), int(section)))
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]
270 suite_bin_version_cache[cache_key] = highest_bid
273 ################################################################################
275 def get_or_set_maintainer_id (maintainer):
276 global maintainer_id_cache
278 if maintainer_id_cache.has_key(maintainer):
279 return maintainer_id_cache[maintainer]
281 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
282 if not q.getresult():
283 projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
284 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
285 maintainer_id = q.getresult()[0][0]
286 maintainer_id_cache[maintainer] = maintainer_id
290 ################################################################################
292 def get_or_set_keyring_id (keyring):
293 global keyring_id_cache
295 if keyring_id_cache.has_key(keyring):
296 return keyring_id_cache[keyring]
298 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
299 if not q.getresult():
300 projectB.query("INSERT INTO keyrings (name) VALUES ('%s')" % (keyring))
301 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
302 keyring_id = q.getresult()[0][0]
303 keyring_id_cache[keyring] = keyring_id
307 ################################################################################
309 def get_or_set_uid_id (uid):
312 if uid_id_cache.has_key(uid):
313 return uid_id_cache[uid]
315 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
316 if not q.getresult():
317 projectB.query("INSERT INTO uid (uid) VALUES ('%s')" % (uid))
318 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
319 uid_id = q.getresult()[0][0]
320 uid_id_cache[uid] = uid_id
324 ################################################################################
326 def get_or_set_fingerprint_id (fingerprint):
327 global fingerprint_id_cache
329 if fingerprint_id_cache.has_key(fingerprint):
330 return fingerprint_id_cache[fingerprint]
332 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
333 if not q.getresult():
334 projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
335 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
336 fingerprint_id = q.getresult()[0][0]
337 fingerprint_id_cache[fingerprint] = fingerprint_id
339 return fingerprint_id
341 ################################################################################
343 def get_files_id (filename, size, md5sum, location_id):
344 global files_id_cache
346 cache_key = "%s_%d" % (filename, location_id)
348 if files_id_cache.has_key(cache_key):
349 return files_id_cache[cache_key]
352 q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id))
358 orig_size = int(ql[1])
360 if orig_size != size or orig_md5sum != md5sum:
362 files_id_cache[cache_key] = ql[0]
363 return files_id_cache[cache_key]
367 ################################################################################
369 def get_or_set_queue_id (queue):
370 global queue_id_cache
372 if queue_id_cache.has_key(queue):
373 return queue_id_cache[queue]
375 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
376 if not q.getresult():
377 projectB.query("INSERT INTO queue (queue_name) VALUES ('%s')" % (queue))
378 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
379 queue_id = q.getresult()[0][0]
380 queue_id_cache[queue] = queue_id
384 ################################################################################
386 def set_files_id (filename, size, md5sum, sha1sum, sha256sum, location_id):
387 global files_id_cache
389 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))
391 return get_files_id (filename, size, md5sum, location_id)
393 ### currval has issues with postgresql 7.1.3 when the table is big
394 ### it was taking ~3 seconds to return on auric which is very Not
397 ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')")
398 ##ql = q.getresult()[0]
399 ##cache_key = "%s_%d" % (filename, location_id)
400 ##files_id_cache[cache_key] = ql[0]
401 ##return files_id_cache[cache_key]
403 ################################################################################
405 def get_maintainer (maintainer_id):
406 global maintainer_cache
408 if not maintainer_cache.has_key(maintainer_id):
409 q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id))
410 maintainer_cache[maintainer_id] = q.getresult()[0][0]
412 return maintainer_cache[maintainer_id]
414 ################################################################################
416 def get_suites(pkgname, src=False):
418 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
420 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
421 q = projectB.query(sql)
422 return map(lambda x: x[0], q.getresult())
424 ################################################################################
426 def get_or_set_contents_file_id(file):
427 global content_file_id_cache
429 if not content_file_id_cache.has_key(file):
430 sql_select = "SELECT id FROM content_file_names WHERE file = '%s'" % file
431 q = projectB.query(sql_select)
432 if not q.getresult():
433 # since this can be called within a transaction, we can't use currval
434 q = projectB.query("INSERT INTO content_file_names VALUES (DEFAULT, '%s') RETURNING id" % (file))
435 content_file_id_cache[file] = int(q.getresult()[0][0])
436 return content_file_id_cache[file]
438 ################################################################################
440 def get_or_set_contents_path_id(path):
441 global content_path_id_cache
443 if not content_path_id_cache.has_key(path):
444 sql_select = "SELECT id FROM content_file_paths WHERE path = '%s'" % path
445 q = projectB.query(sql_select)
446 if not q.getresult():
447 # since this can be called within a transaction, we can't use currval
448 q = projectB.query("INSERT INTO content_file_paths VALUES (DEFAULT, '%s') RETURNING id" % (path))
449 content_path_id_cache[path] = int(q.getresult()[0][0])
450 return content_path_id_cache[path]
452 ################################################################################
454 def insert_content_path(bin_id, fullpath):
455 global insert_contents_file_cache
456 cache_key = "%s_%s" % (bin_id, fullpath)
458 # have we seen this contents before?
459 # probably only revelant during package import
460 if insert_contents_file_cache.has_key(cache_key):
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 # Determine if we're inserting a duplicate row
471 q = projectB.query("SELECT 1 FROM content_associations WHERE binary_pkg = '%d' AND filepath = '%d' AND filename = '%d'" % (int(bin_id), path_id, file_id))
473 # Yes we are, return without doing the insert
476 # Put them into content_assiocations
477 projectB.query("INSERT INTO content_associations VALUES (DEFAULT, '%d', '%d', '%d')" % (bin_id, path_id, file_id))