]> git.decadent.org.uk Git - dak.git/blob - scripts/debian/insert_missing_changedby.py
Merge from Thomas
[dak.git] / scripts / debian / insert_missing_changedby.py
1 #!/usr/bin/env python
2
3 # Adds yet unknown changedby fields when this column is added to an existing
4 # database. If everything goes well, it needs to be run only once. Data is
5 # extracted from Filippo Giunchedi's upload-history project, get the file at
6 # merkel:/home/filippo/upload-history/*.db.
7
8 # Copyright (C) 2008  Christoph Berg <myon@debian.org>
9
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
24 ###############################################################################
25
26 #    /Everybody stand back/
27 #
28 #    I know regular expressions
29
30 ###############################################################################
31
32 import errno, fcntl, os, sys, time, re
33 import apt_pkg
34 import daklib.database
35 import daklib.queue
36 import daklib.utils
37 from pysqlite2 import dbapi2 as sqlite
38
39 projectB = None
40 DBNAME = "uploads-ddc.db"
41 sqliteConn = None
42
43 ###############################################################################
44
45 def insert ():
46     print "Adding missing changedby fields."
47
48     projectB.query("BEGIN WORK")
49
50     q = projectB.query("SELECT id, source, version FROM source WHERE changedby IS NULL")
51
52     for i in q.getresult():
53         print i[1] + "/" + i[2] + ":",
54
55         cur = sqliteConn.cursor()
56         cur.execute("SELECT changedby FROM uploads WHERE package = '%s' AND version = '%s' LIMIT 1" % (i[1], i[2]))
57         res = cur.fetchall()
58         if len(res) != 1:
59             print "nothing found"
60             continue
61
62         changedby = res[0][0].replace("'", "\\'")
63         changedby_id = daklib.database.get_or_set_maintainer_id(changedby)
64
65         projectB.query("UPDATE source SET changedby = %d WHERE id = %d" % (changedby_id, i[0]))
66         print changedby, "(%d)" % changedby_id
67
68     projectB.query("COMMIT WORK")
69
70 ###############################################################################
71
72 def main():
73     global projectB, sqliteConn
74
75     Cnf = daklib.utils.get_conf()
76     Upload = daklib.queue.Upload(Cnf)
77     projectB = Upload.projectB
78
79     sqliteConn = sqlite.connect(DBNAME)
80
81     insert()
82
83 ###############################################################################
84
85 if __name__ == '__main__':
86     main()