#!/usr/bin/python # # 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 Library 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. # # Copyright 2005 Jeff Sheltren import sys import os import sqlite sys.path.append('/usr/share/plague/server') import DBManager import Config def usage(name): print >> sys.stderr, """ Usage: %s dbfile: path to your sqlite database file containing user information configfile: path to your plague-server config This program will export user data from sqlite into the database defined in your plague-server configuration file """ % name class UserMigration: def __init__(self, dbfile, db_manager): # holder for imported data self.imported_users = [] # setup sqlite db connection (source) self.dbcx = sqlite.connect(dbfile, encoding="utf-8", timeout=2) self.curs = self.dbcx.cursor() try: self.curs.execute('SELECT * FROM users LIMIT 1') self.dbcx.commit() except sqlite._sqlite.DatabaseError, e: print >> sys.stderr, "No users found in sqlite db file: %s" % dbfile sys.exit(1) # setup destination db connection self._db_manager = db_manager try: self.dest_dbcx = self._db_manager.dbcx() self.dest_curs = self.dest_dbcx.cursor() except StandardError, e: print >> sys.stderr, "Unable to access destination user database: '%s'" % str(e) sys.exit(1) # Ensure the table exists in the database try: self.dest_curs.execute('SELECT * FROM users LIMIT 4') except Exception, e: # table should have been created by DBManager - bail out print >> sys.stderr, "Unable to access destination user table: '%s'" % str(e) sys.exit(1) def __del__(self): self.dbcx.close() del self.dest_curs del self.dest_dbcx def getUserData(self): sql = "SELECT * FROM users" self.curs.execute(sql) self.dbcx.commit() data = self.curs.fetchall() if not len(data): print >> sys.stderr, "No users found in sqlite db file: %s" % dbfile sys.exit(1) self.imported_users.extend(data) def migrateUserData(self): if not len(self.imported_users): print >> sys.stderr, "Trying to export empty data set, exiting" sys.exit(1) for user in self.imported_users: own_jobs = self.dest_dbcx.convert_boolean(user['own_jobs']) job_admin = self.dest_dbcx.convert_boolean(user['job_admin']) user_admin = self.dest_dbcx.convert_boolean(user['user_admin']) server_admin = self.dest_dbcx.convert_boolean(user['server_admin']) # create sql insert statement sql = "INSERT INTO users (email, own_jobs, job_admin," \ " user_admin, server_admin) VALUES (" \ "'%s', %s, %s, %s, %s)" \ % (user['email'], own_jobs, job_admin, \ user_admin, server_admin) self.dest_curs.execute(sql) self.dest_dbcx.commit() print "Added user: %s" % user['email'] if __name__ == '__main__': if len(sys.argv) < 3: usage(sys.argv[0]) sys.exit(1) dbfile = sys.argv[1] if not os.access(dbfile, os.R_OK): print >> sys.stderr, "Unable to read sqlite db file: %s" % dbfile sys.exit(1) configfile = sys.argv[2] if not os.access(configfile, os.R_OK): print >> sys.stderr, "Unable to read sqlite db file: %s" % configfile sys.exit(1) # load database information from config file cfg = Config.ServerConfig(configfile) # don't attempt to migrate sqlite -> sqlite engine = cfg.get_str("Database", "engine") if engine == 'sqlite': print >> sys.stderr, "Database target must be something other than sqlite." sys.exit(1) dbm = DBManager.DBManager(cfg) um = UserMigration(dbfile, dbm) # Get data out of sqlite db um.getUserData() # import data into pgsql/mysql db um.migrateUserData() print "Done!" sys.exit(0)