database.py 11.7 KB
Newer Older
1 2
#!/usr/bin/env python

J
James Troup 已提交
3
# DB access fucntions
4
# Copyright (C) 2000, 2001, 2002, 2003, 2004, 2006  James Troup <james@nocrew.org>
J
James Troup 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

J
James Troup 已提交
20
################################################################################
J
James Troup 已提交
21

22
import sys, time, types
J
James Troup 已提交
23

J
James Troup 已提交
24
################################################################################
J
sync  
James Troup 已提交
25

26 27 28 29 30 31 32 33 34 35 36
Cnf = None
projectB = None
suite_id_cache = {}
section_id_cache = {}
priority_id_cache = {}
override_type_id_cache = {}
architecture_id_cache = {}
archive_id_cache = {}
component_id_cache = {}
location_id_cache = {}
maintainer_id_cache = {}
37
keyring_id_cache = {}
38 39 40 41 42 43
source_id_cache = {}
files_id_cache = {}
maintainer_cache = {}
fingerprint_id_cache = {}
queue_id_cache = {}
uid_id_cache = {}
44
suite_version_cache = {}
J
sync  
James Troup 已提交
45

J
James Troup 已提交
46
################################################################################
J
James Troup 已提交
47 48 49

def init (config, sql):
    global Cnf, projectB
J
James Troup 已提交
50

51 52
    Cnf = config
    projectB = sql
J
James Troup 已提交
53

54 55

def do_query(q):
56 57 58 59 60
    sys.stderr.write("query: \"%s\" ... " % (q))
    before = time.time()
    r = projectB.query(q)
    time_diff = time.time()-before
    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 67
        sys.stderr.write("pgresult: %s\n" % (r.getresult()))
    return r
68

J
James Troup 已提交
69
################################################################################
J
James Troup 已提交
70 71 72 73 74 75 76 77

def get_suite_id (suite):
    global suite_id_cache

    if suite_id_cache.has_key(suite):
        return suite_id_cache[suite]

    q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
78
    ql = q.getresult()
79
    if not ql:
80
        return -1
J
James Troup 已提交
81

82
    suite_id = ql[0][0]
J
James Troup 已提交
83 84 85 86
    suite_id_cache[suite] = suite_id

    return suite_id

J
sync  
James Troup 已提交
87 88 89 90 91 92 93
def get_section_id (section):
    global section_id_cache

    if section_id_cache.has_key(section):
        return section_id_cache[section]

    q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
94
    ql = q.getresult()
95
    if not ql:
96
        return -1
J
James Troup 已提交
97

98
    section_id = ql[0][0]
J
sync  
James Troup 已提交
99 100 101 102 103 104 105 106 107 108 109
    section_id_cache[section] = section_id

    return section_id

def get_priority_id (priority):
    global priority_id_cache

    if priority_id_cache.has_key(priority):
        return priority_id_cache[priority]

    q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
110
    ql = q.getresult()
111
    if not ql:
112
        return -1
J
James Troup 已提交
113

114
    priority_id = ql[0][0]
J
sync  
James Troup 已提交
115 116 117 118 119
    priority_id_cache[priority] = priority_id

    return priority_id

def get_override_type_id (type):
120
    global override_type_id_cache
J
sync  
James Troup 已提交
121 122

    if override_type_id_cache.has_key(type):
123
        return override_type_id_cache[type]
J
sync  
James Troup 已提交
124

125 126
    q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type))
    ql = q.getresult()
127
    if not ql:
128
        return -1
J
James Troup 已提交
129

130 131
    override_type_id = ql[0][0]
    override_type_id_cache[type] = override_type_id
J
sync  
James Troup 已提交
132

133
    return override_type_id
J
sync  
James Troup 已提交
134

J
James Troup 已提交
135
def get_architecture_id (architecture):
136
    global architecture_id_cache
J
James Troup 已提交
137 138

    if architecture_id_cache.has_key(architecture):
139
        return architecture_id_cache[architecture]
J
James Troup 已提交
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
J
James Troup 已提交
145

146 147
    architecture_id = ql[0][0]
    architecture_id_cache[architecture] = architecture_id
J
James Troup 已提交
148

149
    return architecture_id
J
James Troup 已提交
150 151 152 153

def get_archive_id (archive):
    global archive_id_cache

154
    archive = archive.lower()
J
James Troup 已提交
155

J
James Troup 已提交
156 157 158
    if archive_id_cache.has_key(archive):
        return archive_id_cache[archive]

159 160
    q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive))
    ql = q.getresult()
161
    if not ql:
162
        return -1
163 164

    archive_id = ql[0][0]
J
James Troup 已提交
165 166 167 168 169 170 171
    archive_id_cache[archive] = archive_id

    return archive_id

def get_component_id (component):
    global component_id_cache

172
    component = component.lower()
J
James Troup 已提交
173

J
James Troup 已提交
174 175 176
    if component_id_cache.has_key(component):
        return component_id_cache[component]

J
James Troup 已提交
177
    q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
178
    ql = q.getresult()
179
    if not ql:
180
        return -1
J
James Troup 已提交
181

182
    component_id = ql[0][0]
J
James Troup 已提交
183 184 185 186 187 188 189
    component_id_cache[component] = component_id

    return component_id

def get_location_id (location, component, archive):
    global location_id_cache

190
    cache_key = location + '_' + component + '_' + location
J
James Troup 已提交
191 192 193 194 195 196 197 198 199 200
    if location_id_cache.has_key(cache_key):
        return location_id_cache[cache_key]

    archive_id = get_archive_id (archive)
    if component != "":
        component_id = get_component_id (component)
        if component_id != -1:
            q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
    else:
        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]
J
James Troup 已提交
206 207 208 209 210 211 212
    location_id_cache[cache_key] = location_id

    return location_id

def get_source_id (source, version):
    global source_id_cache

213
    cache_key = source + '_' + version + '_'
J
James Troup 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226
    if source_id_cache.has_key(cache_key):
        return source_id_cache[cache_key]

    q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))

    if not q.getresult():
        return None

    source_id = q.getresult()[0][0]
    source_id_cache[cache_key] = source_id

    return source_id

227 228 229
def get_suite_version(source, suite):
    global suite_version_cache
    cache_key = "%s_%s" % (source, suite)
230

231 232
    if suite_version_cache.has_key(cache_key):
        return suite_version_cache[cache_key]
233

234
    q = projectB.query("""
235 236 237
    SELECT s.version FROM source s, suite su, src_associations sa
    WHERE sa.source=s.id
      AND sa.suite=su.id
238
      AND su.suite_name='%s'
239
      AND s.source='%s'"""
240
                              % (suite, source))
241 242 243 244 245

    if not q.getresult():
        return None

    version = q.getresult()[0][0]
246
    suite_version_cache[cache_key] = version
247 248 249

    return version

J
James Troup 已提交
250
################################################################################
J
James Troup 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

def get_or_set_maintainer_id (maintainer):
    global maintainer_id_cache

    if maintainer_id_cache.has_key(maintainer):
        return maintainer_id_cache[maintainer]

    q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
    if not q.getresult():
        projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
        q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
    maintainer_id = q.getresult()[0][0]
    maintainer_id_cache[maintainer] = maintainer_id

    return maintainer_id

J
James Troup 已提交
267 268
################################################################################

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
def get_or_set_keyring_id (keyring):
    global keyring_id_cache

    if keyring_id_cache.has_key(keyring):
        return keyring_id_cache[keyring]

    q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
    if not q.getresult():
        projectB.query("INSERT INTO keyrings (name) VALUES ('%s')" % (keyring))
        q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
    keyring_id = q.getresult()[0][0]
    keyring_id_cache[keyring] = keyring_id

    return keyring_id

################################################################################

J
James Troup 已提交
286
def get_or_set_uid_id (uid):
287
    global uid_id_cache
J
James Troup 已提交
288 289

    if uid_id_cache.has_key(uid):
290
        return uid_id_cache[uid]
J
James Troup 已提交
291 292 293

    q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
    if not q.getresult():
294 295 296 297
        projectB.query("INSERT INTO uid (uid) VALUES ('%s')" % (uid))
        q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
    uid_id = q.getresult()[0][0]
    uid_id_cache[uid] = uid_id
J
James Troup 已提交
298

299
    return uid_id
J
James Troup 已提交
300 301

################################################################################
J
James Troup 已提交
302

J
James Troup 已提交
303
def get_or_set_fingerprint_id (fingerprint):
304
    global fingerprint_id_cache
J
James Troup 已提交
305 306 307 308

    if fingerprint_id_cache.has_key(fingerprint):
        return fingerprint_id_cache[fingerprint]

309
    q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
J
James Troup 已提交
310
    if not q.getresult():
311 312 313 314
        projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
        q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
    fingerprint_id = q.getresult()[0][0]
    fingerprint_id_cache[fingerprint] = fingerprint_id
J
James Troup 已提交
315

316
    return fingerprint_id
J
James Troup 已提交
317

J
James Troup 已提交
318
################################################################################
J
James Troup 已提交
319

M
Mark Hymers 已提交
320
def get_files_id (filename, size, md5sum, location_id):
J
James Troup 已提交
321 322
    global files_id_cache

323
    cache_key = "%s_%d" % (filename, location_id)
J
James Troup 已提交
324 325

    if files_id_cache.has_key(cache_key):
J
James Troup 已提交
326
        return files_id_cache[cache_key]
J
James Troup 已提交
327

328
    size = int(size)
M
Mark Hymers 已提交
329
    q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id))
330
    ql = q.getresult()
J
James Troup 已提交
331 332
    if ql:
        if len(ql) != 1:
333 334 335 336
            return -1
        ql = ql[0]
        orig_size = int(ql[1])
        orig_md5sum = ql[2]
M
Mark Hymers 已提交
337
        if orig_size != size or orig_md5sum != md5sum:
338 339
            return -2
        files_id_cache[cache_key] = ql[0]
J
James Troup 已提交
340 341 342 343
        return files_id_cache[cache_key]
    else:
        return None

344 345 346 347
################################################################################

def get_or_set_queue_id (queue):
    global queue_id_cache
348

349 350 351 352 353 354 355 356 357 358 359
    if queue_id_cache.has_key(queue):
        return queue_id_cache[queue]

    q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
    if not q.getresult():
        projectB.query("INSERT INTO queue (queue_name) VALUES ('%s')" % (queue))
        q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
    queue_id = q.getresult()[0][0]
    queue_id_cache[queue] = queue_id

    return queue_id
J
James Troup 已提交
360

J
James Troup 已提交
361
################################################################################
J
James Troup 已提交
362

363
def set_files_id (filename, size, md5sum, sha1sum, sha256sum, location_id):
J
James Troup 已提交
364 365
    global files_id_cache

M
Mark Hymers 已提交
366
    projectB.query("INSERT INTO files (filename, size, md5sum, sha1sum, sha256sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, sha1sum, sha256sum, location_id))
J
James Troup 已提交
367

M
Mark Hymers 已提交
368
    return get_files_id (filename, size, md5sum, location_id)
369

370
    ### currval has issues with postgresql 7.1.3 when the table is big
371 372 373
    ### it was taking ~3 seconds to return on auric which is very Not
    ### Cool(tm).
    ##
374 375
    ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')")
    ##ql = q.getresult()[0]
376
    ##cache_key = "%s_%d" % (filename, location_id)
377
    ##files_id_cache[cache_key] = ql[0]
378
    ##return files_id_cache[cache_key]
J
James Troup 已提交
379

J
James Troup 已提交
380
################################################################################
J
James Troup 已提交
381

J
James Troup 已提交
382
def get_maintainer (maintainer_id):
383
    global maintainer_cache
J
James Troup 已提交
384

J
James Troup 已提交
385
    if not maintainer_cache.has_key(maintainer_id):
386 387
        q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id))
        maintainer_cache[maintainer_id] = q.getresult()[0][0]
J
James Troup 已提交
388

389
    return maintainer_cache[maintainer_id]
J
James Troup 已提交
390

J
James Troup 已提交
391
################################################################################