extras-buildsys/utils plague-user-migration.py, NONE, 1.1 user-manager.py, 1.3, 1.4

Daniel Williams (dcbw) fedora-extras-commits at redhat.com
Sun Nov 20 18:42:42 UTC 2005


Author: dcbw

Update of /cvs/fedora/extras-buildsys/utils
In directory cvs-int.fedora.redhat.com:/tmp/cvs-serv22895/utils

Modified Files:
	user-manager.py 
Added Files:
	plague-user-migration.py 
Log Message:
2005-11-20  Dan Williams  <dcbw at redhat.com>

    Patches from Jeff Sheltren <sheltren at cs.ucsb.edu>
    * server/User.py
      server/main.py
      server/DBManager.py
      utils/user-manager.py
        - Store users using same db engine as the job database

    * utils/plague-user-migration.py
        - Script to move user info from sqlite to postgres/mysql




--- NEW FILE plague-user-migration.py ---
#!/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 <sheltren at cs.ucsb.edu>


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> <configfile> 
       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:
            # create sql insert statement
            sql = 'INSERT INTO users (email, own_jobs, job_admin,' \
                    ' user_admin, server_admin) VALUES (' \
                    '"%s", %d, %d, %d, %d)' \
                    % (user['email'], user['own_jobs'], user['job_admin'], \
                    user['user_admin'], user['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)


Index: user-manager.py
===================================================================
RCS file: /cvs/fedora/extras-buildsys/utils/user-manager.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- user-manager.py	8 Aug 2005 16:15:18 -0000	1.3
+++ user-manager.py	20 Nov 2005 18:42:40 -0000	1.4
@@ -18,12 +18,14 @@
 
 
 import sys, os
-import sqlite
 
+sys.path.append('/usr/share/plague/server')
+import DBManager
+import Config
 
 def print_usage(prog):
     print "Usage:\n"
-    print "   %s <dbfile> <command ...>\n\n" % prog
+    print "   %s <server_config_file> <command ...>\n\n" % prog
     print "      Commands:"
     print "         add <email> [own_jobs] [job_admin] [user_admin] [server_admin]"
     print "         del <email>"
@@ -37,26 +39,25 @@
 
 
 class UserManager:
-    def __init__(self, dbfile):
-        self.dbcx = sqlite.connect(dbfile, encoding="utf-8", timeout=2)
-        self.curs = self.dbcx.cursor()
-
+    def __init__(self, db_manager):
+        self._db_manager = db_manager
+        try:
+            self.dbcx = self._db_manager.dbcx()
+            self.curs = self.dbcx.cursor()
+        except StandardError, e:
+            print "Unable to access user database: '%s'" % str(e)
+            sys.exit(1)
         # Ensure the table exists in the database
-        create = False
         try:
-            self.curs.execute('SELECT * FROM users')
-            self.dbcx.commit()
-        except sqlite._sqlite.DatabaseError, e:
-            create = True
-
-        if create:
-            self.curs.execute('CREATE TABLE users (email VARCHAR(50), ' \
-                    'own_jobs BOOLEAN, job_admin BOOLEAN, ' \
-                    'user_admin BOOLEAN, server_admin BOOLEAN)')
-            self.dbcx.commit()
+            self.curs.execute('SELECT * FROM users LIMIT 4')
+        except Exception, e:
+            print "Unable to access user table: '%s'" % str(e)
+            print "Please run plague-server once to generate needed tables"
+            sys.exit(1)
 
     def __del__(self):
-        self.dbcx.close()
+        del self.curs
+        del self.dbcx
 
     def dispatch(self, prog, command, args):
         if command == 'add':
@@ -77,7 +78,7 @@
         email = args[0]
         self.curs.execute('SELECT * FROM users WHERE email="%s"' % email)
         self.dbcx.commit()
-        item = self.curs.fetchone()
+        item = self.dbcx.fetchone(self.curs)
         if item:
             raise UserManagerException("User %s already exists." % email)
 
@@ -110,7 +111,7 @@
         email = args[0]
         self.curs.execute('SELECT * FROM users WHERE email="%s"' % email)
         self.dbcx.commit()
-        item = self.curs.fetchone()
+        item = self.dbcx.fetchone(self.curs)
         if not item:
             raise UserManagerException("User %s doesn't exist." % email)
 
@@ -139,7 +140,7 @@
         email = args[0]
         self.curs.execute('SELECT * FROM users WHERE email="%s"' % email)
         self.dbcx.commit()
-        item = self.curs.fetchone()
+        item = self.dbcx.fetchone(self.curs)
         if not item:
             raise UserManagerException("User %s doesn't exist." % email)
 
@@ -176,7 +177,7 @@
 
         self.curs.execute(sql)
         self.dbcx.commit()
-        data = self.curs.fetchall()
+        data = self.dbcx.fetchall(self.curs)
         if not len(data):
             raise UserManagerException("No matching users found.")
 
@@ -197,12 +198,16 @@
         print_usage(sys.argv[0])
         sys.exit(1)
 
-    dbfile = sys.argv[1]
-    if not os.access(dbfile, os.R_OK):
-        print "The user database '%s' does not exist or is not readable." % dbfile
+    configfile = sys.argv[1]
+    if not os.access(configfile, os.R_OK):
+        print "The config file '%s' does not exist or is not readable." % configfile
         sys.exit(1)
 
-    um = UserManager(dbfile)
+    # load database information from config file
+    cfg = Config.ServerConfig(configfile)
+    dbm = DBManager.DBManager(cfg)
+
+    um = UserManager(dbm)
     try:
         um.dispatch(sys.argv[0], sys.argv[2], sys.argv[3:])
     except UserManagerException, e:




More information about the fedora-extras-commits mailing list