]> git.decadent.org.uk Git - dak.git/blob - scripts/debian/insert_missing_changedby.py
Fix path
[dak.git] / scripts / debian / insert_missing_changedby.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
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.
8
9 # Copyright (C) 2008  Christoph Berg <myon@debian.org>
10 # Copyright (C) 2008  Bernd Zeimetz <bzed@debian.org>
11
12
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.
17
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.
22
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
26
27 ###############################################################################
28
29 #    /Everybody stand back/
30 #
31 #    I know regular expressions
32
33 ###############################################################################
34
35 import errno, fcntl, os, sys, time, re
36 import apt_pkg
37 import daklib.database
38 import daklib.queue
39 import daklib.utils
40 from pysqlite2 import dbapi2 as sqlite
41 import pysqlite2.dbapi2
42 import psycopg2
43
44 projectB = None
45 projectBdb = None
46 DBNAME = "uploads-queue.db"
47 sqliteConn = None
48 maintainer_id_cache={}
49
50 ###############################################################################
51
52 def get_or_set_maintainer_id (maintainer):
53     global maintainer_id_cache
54
55     if maintainer_id_cache.has_key(maintainer):
56         return maintainer_id_cache[maintainer]
57
58     if isinstance(maintainer, basestring):
59         if not isinstance(maintainer, unicode):
60             try:
61                 maintainer = unicode(maintainer, 'utf-8')
62             except:
63                 maintainer = unicode(maintainer, 'iso8859-15')
64     maintainer = maintainer.encode('utf-8')
65
66     print "%s" % maintainer
67     cursor = projectBdb.cursor()
68     cursor.execute("SELECT id FROM maintainer WHERE name=%s", (maintainer, ))
69     row = cursor.fetchone()
70     if not row:
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
76     cursor.close()
77
78     return maintainer_id
79
80
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))
84     res = cur.fetchone()
85     cur.close()
86     return res
87
88 def insert ():
89     print "Adding missing changedby fields."
90
91     listcursor = projectBdb.cursor()
92     listcursor.execute("SELECT id, source, version FROM source WHERE changedby IS NULL")
93     row = listcursor.fetchone()
94
95     while row:
96         print repr(row)
97         try:
98             res = __get_changedby__(row[1], row[2])
99         except:
100             sqliteConn.text_factory = str
101             try:
102                 res = __get_changedby__(row[1], row[2])
103             except:
104                 print 'FAILED SQLITE'
105                 res=None
106             sqliteConn.text_factory = unicode
107         if res:
108             changedby_id = get_or_set_maintainer_id(res[0])
109
110             cur = projectBdb.cursor()
111             cur.execute("UPDATE source SET changedby=%s WHERE id=%s" % (changedby_id, row[0]))
112             cur.close()
113             print changedby_id, "(%d)" % row[0]
114
115         else:
116             print "nothing found"
117
118         row = listcursor.fetchone()
119     listcursor.close()
120
121 ###############################################################################
122
123
124 def main():
125     global projectB, sqliteConn, projectBdb
126
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"])
131
132     sqliteConn = sqlite.connect(DBNAME)
133
134     insert()
135
136     projectBdb.commit()
137     projectBdb.close()
138
139 ###############################################################################
140
141 if __name__ == '__main__':
142     main()