2 # -*- coding: utf-8 -*-
4 # Adds yet unknown changedby fields when this column is added to an existing
5 # database. If everything goes well, it needs to be run only once. Data is
6 # extracted from Filippo Giunchedi's upload-history project, get the file at
7 # merkel:/home/filippo/upload-history/*.db.
9 # Copyright (C) 2008 Christoph Berg <myon@debian.org>
10 # Copyright (C) 2008 Bernd Zeimetz <bzed@debian.org>
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 ###############################################################################
29 # /Everybody stand back/
31 # I know regular expressions
33 ###############################################################################
35 import errno, fcntl, os, sys, time, re
37 import daklib.database
40 from pysqlite2 import dbapi2 as sqlite
41 import pysqlite2.dbapi2
46 DBNAME = "uploads-queue.db"
48 maintainer_id_cache={}
50 ###############################################################################
52 def get_or_set_maintainer_id (maintainer):
53 global maintainer_id_cache
55 if maintainer_id_cache.has_key(maintainer):
56 return maintainer_id_cache[maintainer]
58 if isinstance(maintainer, basestring):
59 if not isinstance(maintainer, unicode):
61 maintainer = unicode(maintainer, 'utf-8')
63 maintainer = unicode(maintainer, 'iso8859-15')
64 maintainer = maintainer.encode('utf-8')
66 print "%s" % maintainer
67 cursor = projectBdb.cursor()
68 cursor.execute("SELECT id FROM maintainer WHERE name=%s", (maintainer, ))
69 row = cursor.fetchone()
71 cursor.execute("INSERT INTO maintainer (name) VALUES (%s)" , (maintainer, ))
72 cursor.execute("SELECT id FROM maintainer WHERE name=%s", (maintainer, ))
73 row = cursor.fetchone()
74 maintainer_id = row[0]
75 maintainer_id_cache[maintainer] = maintainer_id
81 def __get_changedby__(package, version):
82 cur = sqliteConn.cursor()
83 cur.execute("SELECT changedby FROM uploads WHERE package=? AND version=? LIMIT 1", (package, version))
89 print "Adding missing changedby fields."
91 listcursor = projectBdb.cursor()
92 listcursor.execute("SELECT id, source, version FROM source WHERE changedby IS NULL")
93 row = listcursor.fetchone()
98 res = __get_changedby__(row[1], row[2])
100 sqliteConn.text_factory = str
102 res = __get_changedby__(row[1], row[2])
104 print 'FAILED SQLITE'
106 sqliteConn.text_factory = unicode
108 changedby_id = get_or_set_maintainer_id(res[0])
110 cur = projectBdb.cursor()
111 cur.execute("UPDATE source SET changedby=%s WHERE id=%s" % (changedby_id, row[0]))
113 print changedby_id, "(%d)" % row[0]
116 print "nothing found"
118 row = listcursor.fetchone()
121 ###############################################################################
125 global projectB, sqliteConn, projectBdb
127 Cnf = daklib.utils.get_conf()
128 Upload = daklib.queue.Upload(Cnf)
129 projectB = Upload.projectB
130 projectBdb = psycopg2.connect("dbname=%s" % Cnf["DB::Name"])
132 sqliteConn = sqlite.connect(DBNAME)
139 ###############################################################################
141 if __name__ == '__main__':