From: Joerg Jaspert Date: Sun, 27 Apr 2008 09:22:53 +0000 (+0200) Subject: Rewrite (from bzed), making this script actually work correctly if the input data... X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=48aadb2f94e3bf28471a7eefda9a775ce33ff607;p=dak Rewrite (from bzed), making this script actually work correctly if the input data is a bit weird --- diff --git a/scripts/debian/insert_missing_changedby.py b/scripts/debian/insert_missing_changedby.py index 7b817b5e..e3148301 100755 --- a/scripts/debian/insert_missing_changedby.py +++ b/scripts/debian/insert_missing_changedby.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- # Adds yet unknown changedby fields when this column is added to an existing # database. If everything goes well, it needs to be run only once. Data is @@ -6,6 +7,8 @@ # merkel:/home/filippo/upload-history/*.db. # Copyright (C) 2008 Christoph Berg +# Copyright (C) 2008 Bernd Zeimetz + # 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 @@ -35,51 +38,104 @@ import daklib.database import daklib.queue import daklib.utils from pysqlite2 import dbapi2 as sqlite +import pysqlite2.dbapi2 +import psycopg2 projectB = None -DBNAME = "uploads-ddc.db" +projectBdb = None +DBNAME = "uploads-queue.db" sqliteConn = None +maintainer_id_cache={} ############################################################################### +def get_or_set_maintainer_id (maintainer): + global maintainer_id_cache + + if maintainer_id_cache.has_key(maintainer): + return maintainer_id_cache[maintainer] + + if isinstance(maintainer, basestring): + if not isinstance(maintainer, unicode): + try: + maintainer = unicode(maintainer, 'utf-8') + except: + maintainer = unicode(maintainer, 'iso8859-15') + maintainer = maintainer.encode('utf-8') + + print "%s" % maintainer + cursor = projectBdb.cursor() + cursor.execute("SELECT id FROM maintainer WHERE name=%s", (maintainer, )) + row = cursor.fetchone() + if not row: + cursor.execute("INSERT INTO maintainer (name) VALUES (%s)" , (maintainer, )) + cursor.execute("SELECT id FROM maintainer WHERE name=%s", (maintainer, )) + row = cursor.fetchone() + maintainer_id = row[0] + maintainer_id_cache[maintainer] = maintainer_id + cursor.close() + + return maintainer_id + + +def __get_changedby__(package, version): + cur = sqliteConn.cursor() + cur.execute("SELECT changedby FROM uploads WHERE package=? AND version=? LIMIT 1", (package, version)) + res = cur.fetchone() + cur.close() + return res + def insert (): print "Adding missing changedby fields." - projectB.query("BEGIN WORK") - - q = projectB.query("SELECT id, source, version FROM source WHERE changedby IS NULL") - - for i in q.getresult(): - print i[1] + "/" + i[2] + ":", - - cur = sqliteConn.cursor() - cur.execute("SELECT changedby FROM uploads WHERE package = '%s' AND version = '%s' LIMIT 1" % (i[1], i[2])) - res = cur.fetchall() - if len(res) != 1: + listcursor = projectBdb.cursor() + listcursor.execute("SELECT id, source, version FROM source WHERE changedby IS NULL") + row = listcursor.fetchone() + + while row: + print repr(row) + try: + res = __get_changedby__(row[1], row[2]) + except: + sqliteConn.text_factory = str + try: + res = __get_changedby__(row[1], row[2]) + except: + print 'FAILED SQLITE' + res=None + sqliteConn.text_factory = unicode + if res: + changedby_id = get_or_set_maintainer_id(res[0]) + + cur = projectBdb.cursor() + cur.execute("UPDATE source SET changedby=%s WHERE id=%s" % (changedby_id, row[0])) + cur.close() + print changedby_id, "(%d)" % row[0] + + else: print "nothing found" - continue - changedby = res[0][0].replace("'", "\\'") - changedby_id = daklib.database.get_or_set_maintainer_id(changedby) - - projectB.query("UPDATE source SET changedby = %d WHERE id = %d" % (changedby_id, i[0])) - print changedby, "(%d)" % changedby_id - - projectB.query("COMMIT WORK") + row = listcursor.fetchone() + listcursor.close() ############################################################################### + def main(): - global projectB, sqliteConn + global projectB, sqliteConn, projectBdb Cnf = daklib.utils.get_conf() Upload = daklib.queue.Upload(Cnf) projectB = Upload.projectB + projectBdb = psycopg2.connect("dbname=%s" % Cnf["DB::Name"]) sqliteConn = sqlite.connect(DBNAME) insert() + projectBdb.commit() + projectBdb.close() + ############################################################################### if __name__ == '__main__':