#NEWLINE# def deploy_testdb( options ) : #NEWLINE# #INDENT# from weblab.admin.deploy import insert_required_initial_data , populate_weblab_tests , generate_create_database , insert_required_initial_coord_data #NEWLINE# import weblab.db.model as Model #NEWLINE# import weblab.core.coordinator.sql.model as CoordinatorModel #NEWLINE# import voodoo.sessions.db_lock_data as DbLockData #NEWLINE# import voodoo.sessions.sqlalchemy_data as SessionSqlalchemyData #NEWLINE# from sqlalchemy import create_engine #NEWLINE# try : #NEWLINE# #INDENT# import MySQLdb #NEWLINE# dbi = MySQLdb #NEWLINE# #UNINDENT# except ImportError : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# import pymysql_sa #NEWLINE# #UNINDENT# except ImportError : #NEWLINE# #INDENT# raise Exception ( "Neither MySQLdb nor pymysql_sa have been installed. First install them by running 'pip install pymysql_sa' or 'pip install python-mysql'" ) #NEWLINE# #UNINDENT# pymysql_sa . make_default_mysql_dialect ( ) #NEWLINE# #UNINDENT# t_initial = time . time ( ) #NEWLINE# db_dir = 'db' #NEWLINE# if ( not os . path . exists ( db_dir ) ) : #NEWLINE# #INDENT# os . mkdir ( db_dir ) #NEWLINE# #UNINDENT# db_engine = options . testdb_engine #NEWLINE# weblab_db_username = options . testdb_user #NEWLINE# weblab_db_password = options . testdb_passwd #NEWLINE# weblab_admin_db_username = options . testdb_admin_user #NEWLINE# if options . testdb_ask_admin_passwd : #NEWLINE# #INDENT# weblab_admin_db_password = getpass . getpass ( 'Database password:' . encode ( 'utf8' ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# weblab_admin_db_password = metadata . testdb_admin_passwd #NEWLINE# #UNINDENT# if ( db_engine == 'mysql' ) : #NEWLINE# #INDENT# weblab_test_db_str = ( 'mysql://%s:%s@localhost/WebLabTests%s' % ( weblab_db_username , weblab_db_password , '%s' ) ) #NEWLINE# weblab_coord_db_str = ( 'mysql://%s:%s@localhost/WebLabCoordination%s' % ( weblab_db_username , weblab_db_password , '%s' ) ) #NEWLINE# weblab_sessions_db_str = ( 'mysql://%s:%s@localhost/WebLabSessions' % ( weblab_db_username , weblab_db_password ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( db_engine == 'sqlite' ) : #NEWLINE# #INDENT# weblab_test_db_str = 'sqlite:///db/WebLabTests%s.db' #NEWLINE# weblab_coord_db_str = 'sqlite:///db/WebLabCoordination%s.db' #NEWLINE# weblab_sessions_db_str = 'sqlite:///db/WebLabSessions.db' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise Exception ( ( 'db engine %s not supported' % db_engine ) ) #NEWLINE# #UNINDENT# #UNINDENT# if options . testdb_create_db : #NEWLINE# #INDENT# create_database = generate_create_database ( db_engine ) #NEWLINE# if ( create_database is None ) : #NEWLINE# #INDENT# raise Exception ( ( 'db engine %s not supported for creating database' % db_engine ) ) #NEWLINE# #UNINDENT# t = time . time ( ) #NEWLINE# error_message = 'Could not create database. This may happen if the admin db credentials are wrong. Try --db-ask-admin-passwd' #NEWLINE# create_database ( error_message , weblab_admin_db_username , weblab_admin_db_password , 'WebLab' , weblab_db_username , weblab_db_password , db_dir = db_dir ) #NEWLINE# create_database ( error_message , weblab_admin_db_username , weblab_admin_db_password , 'WebLabTests' , weblab_db_username , weblab_db_password , db_dir = db_dir ) #NEWLINE# create_database ( error_message , weblab_admin_db_username , weblab_admin_db_password , 'WebLabTests2' , weblab_db_username , weblab_db_password , db_dir = db_dir ) #NEWLINE# create_database ( error_message , weblab_admin_db_username , weblab_admin_db_password , 'WebLabTests3' , weblab_db_username , weblab_db_password , db_dir = db_dir ) #NEWLINE# create_database ( error_message , weblab_admin_db_username , weblab_admin_db_password , 'WebLabIntTests1' , weblab_db_username , weblab_db_password , db_dir = db_dir ) #NEWLINE# create_database ( error_message , weblab_admin_db_username , weblab_admin_db_password , 'WebLabCoordination' , weblab_db_username , weblab_db_password , db_dir = db_dir ) #NEWLINE# create_database ( error_message , weblab_admin_db_username , weblab_admin_db_password , 'WebLabCoordination2' , weblab_db_username , weblab_db_password , db_dir = db_dir ) #NEWLINE# create_database ( error_message , weblab_admin_db_username , weblab_admin_db_password , 'WebLabCoordination3' , weblab_db_username , weblab_db_password , db_dir = db_dir ) #NEWLINE# create_database ( error_message , weblab_admin_db_username , weblab_admin_db_password , 'WebLabSessions' , weblab_db_username , weblab_db_password , db_dir = db_dir ) #NEWLINE# print ( ( 'Databases created.\t\t\t\t[done] [%1.2fs]' % ( time . time ( ) - t ) ) ) #NEWLINE# #UNINDENT# for tests in ( '' , '2' , '3' ) : #NEWLINE# #INDENT# print ( ( "Populating 'WebLabTests%s' database...  \t\t" % tests ) , end = '' ) #NEWLINE# t = time . time ( ) #NEWLINE# engine = create_engine ( ( weblab_test_db_str % tests ) , echo = False ) #NEWLINE# metadata = Model . Base . metadata #NEWLINE# metadata . drop_all ( engine ) #NEWLINE# metadata . create_all ( engine ) #NEWLINE# insert_required_initial_data ( engine ) #NEWLINE# populate_weblab_tests ( engine , tests ) #NEWLINE# print ( ( '[done] [%1.2fs]' % ( time . time ( ) - t ) ) ) #NEWLINE# #UNINDENT# for coord in ( '' , '2' , '3' ) : #NEWLINE# #INDENT# print ( ( "Populating 'WebLabCoordination%s' database...\t" % coord ) , end = '' ) #NEWLINE# t = time . time ( ) #NEWLINE# engine = create_engine ( ( weblab_coord_db_str % coord ) , echo = False ) #NEWLINE# CoordinatorModel . load ( ) #NEWLINE# metadata = CoordinatorModel . Base . metadata #NEWLINE# metadata . drop_all ( engine ) #NEWLINE# metadata . create_all ( engine ) #NEWLINE# insert_required_initial_coord_data ( engine ) #NEWLINE# print ( ( '[done] [%1.2fs]' % ( time . time ( ) - t ) ) ) #NEWLINE# #UNINDENT# print ( "Populating 'WebLabSessions' database...\t\t" , end = '' ) #NEWLINE# t = time . time ( ) #NEWLINE# engine = create_engine ( weblab_sessions_db_str , echo = False ) #NEWLINE# metadata = DbLockData . SessionLockBase . metadata #NEWLINE# metadata . drop_all ( engine ) #NEWLINE# metadata . create_all ( engine ) #NEWLINE# metadata = SessionSqlalchemyData . SessionBase . metadata #NEWLINE# metadata . drop_all ( engine ) #NEWLINE# metadata . create_all ( engine ) #NEWLINE# print ( ( '[done] [%1.2fs]' % ( time . time ( ) - t ) ) ) #NEWLINE# print ( ( 'Total database deployment: \t\t\t[done] [%1.2fs]' % ( time . time ( ) - t_initial ) ) )