#!/usr/bin/env python # This script copies the validation databases from the server where # they live to the localhost. This is meant # to allow developers to run the tests against local databases, # which is much faster than running them against a remote database. # # Note that whenever the validation database are modified, developers # will have to run this script again to get the new database. This # means developers will have to stay in reasonable contact with # each other regarding the status of tests involving the databases. # # This script uses mysqldump to fetch entire test databases at # once. It does not do incremental database changes, like DBSync does. # It blows away your old test databases (if they exist) and replaces # them with new ones # # author: T. Paul # # version: $Id$ import sys import string import os if len(sys.argv) != 1: print 'Usage: python CopyTestDB.py' sys.exit() # Put the names of all the test database you want # to use in the databases list below. databases = ['validate_Atm_Aerosol_0_A', 'validate_Atm_Cloud_1_A', 'validate_Atm_Lidar_1_A', 'validate_Atm_Molecular_0_A', 'validate_Atm_Quality_0_A', 'validate_FD_Calib_0_A', 'validate_Atm_GOES_0_A'] remote_host = 'westeros.cgca.uwm.edu' dump_filename = 'validation_db_dump.sql' u_p = '--user=Mocca --password=Sibyll' full_dump_string = '' print 'Dumping contents of valdation databases located on remote host ' + remote_host for db in databases: drop_prefix = 'drop database if exists ' + db + ';\n' create_prefix = 'create database ' + db + ';\n' grant_prefix = 'grant select, insert, delete, create, drop on ' + db + '.* to Mocca@localhost identified by \'Sibyll\' ;\n' grant_prefix += 'grant select on ' + db + '.* to Mocca@\'%\' identified by \'Sibyll\' ;\n' grant_prefix += 'grant select on ' + db + '.* to \'\'@localhost ;\n' use_prefix = 'use ' + db + ';\n' dump_cmd = 'mysqldump ' + u_p + ' --host=' + remote_host + ' --lock_tables=FALSE ' + db tmp_file_name = 'dump_temp.sql' print 'dumping database : ' + db res = os.system(dump_cmd + ' > ' + tmp_file_name) if res != 0 : print 'Failed to dump a local copy of the remote database. Giving up.' sys.exit() in_file = open(tmp_file_name, 'r') total_string = drop_prefix + create_prefix + grant_prefix + use_prefix + in_file.read() + '\n' full_dump_string += total_string # add 'test' database (used by Framework/Detector/testDBConnection) full_dump_string += "create database if not exists test;\n" full_dump_string += "grant select, insert, delete, create, drop on test.* to Mocca@localhost identified by 'Sibyll';" full_dump_string += "grant select on test.* to Mocca@'%' identified by 'Sibyll' ;\n" full_dump_string += "grant select on test.* to ''@localhost ;\n" os.system('rm ' + tmp_file_name) f = open(dump_filename, 'w') f.write(full_dump_string) f.close() # import the validation database dump print '\n' print 'Validation database dumps written to file.' print 'To load them to your local database server, I need your mysql root password, please..' print '\n' mysql_cmd = 'mysql -u root -p < ' + dump_filename res = os.system(mysql_cmd) if res != 0 : print 'Failed to load the database. Giving up.' sys.exit() msg = 'Would you like to delete validation database dump file? (y/n) ' y_n = raw_input(msg) if y_n == "y": os.system('rm ' + dump_filename)