insert_missing_changedby.py 4.3 KB
Newer Older
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3 4 5 6 7 8 9

# 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
# extracted from Filippo Giunchedi's upload-history project, get the file at
# merkel:/home/filippo/upload-history/*.db.

# Copyright (C) 2008  Christoph Berg <myon@debian.org>
10 11
# Copyright (C) 2008  Bernd Zeimetz <bzed@debian.org>

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

# 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

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

#    /Everybody stand back/
#
#    I know regular expressions

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

import errno, fcntl, os, sys, time, re
import apt_pkg
import daklib.database
import daklib.queue
import daklib.utils
from pysqlite2 import dbapi2 as sqlite
41 42
import pysqlite2.dbapi2
import psycopg2
43 44

projectB = None
45 46
projectBdb = None
DBNAME = "uploads-queue.db"
47
sqliteConn = None
48
maintainer_id_cache={}
49 50 51

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

52 53 54
def get_or_set_maintainer_id (maintainer):
    global maintainer_id_cache

55
    if maintainer in maintainer_id_cache:
56 57 58 59
        return maintainer_id_cache[maintainer]

    if isinstance(maintainer, basestring):
        if not isinstance(maintainer, unicode):
60
            try:
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
                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

88 89 90
def insert ():
    print "Adding missing changedby fields."

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    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])
109

110 111 112 113 114 115
            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:
116 117
            print "nothing found"

118 119
        row = listcursor.fetchone()
    listcursor.close()
120 121 122

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

123

124
def main():
125
    global projectB, sqliteConn, projectBdb
126 127 128 129

    Cnf = daklib.utils.get_conf()
    Upload = daklib.queue.Upload(Cnf)
    projectB = Upload.projectB
130
    projectBdb = psycopg2.connect("dbname=%s" % Cnf["DB::Name"])
131 132 133 134 135

    sqliteConn = sqlite.connect(DBNAME)

    insert()

136 137 138
    projectBdb.commit()
    projectBdb.close()

139 140 141 142
###############################################################################

if __name__ == '__main__':
    main()