if argv is None: argv = sys.argv # parse command line options try: opts, args = getopt.getopt(argv[1:], "dh", ["debug", "help",]) except getopt.error, msg: print msg print "for help use --help" return 2 # process options skip = False check = False log = None for o, a in opts: if o in ("-h", "--help"): print __doc__ return 0 if o in ("-d", "--debug"): log = get_log(level='DEBUG') # process arguments if not log: log = get_log() log.debug("connection string %s" % args[0]) log.debug("file name %s" % args[1]) log.debug("table name %s" % args[2]) connection = get_connection(args[0]) dump(connection, args[1], args[2], log=log) if __name__ == "__main__": sys.exit(main())
def main(argv=None): """ Main function modelled on Guido's guidelines """ # Set defaults from db_refresh_config.py module (in this directory) SOURCE = None TARGET = None TABLES = None if os.path.isfile('db_refresh_config.py'): from db_refresh_config import SOURCE, TARGET, TABLES if argv is None: argv = sys.argv # parse command line options parser = argparse.ArgumentParser(description='') parser.add_argument("-d", "--debug", action="store_true", default=False) parser.add_argument("-s", "--source", help="URI for source database", default=SOURCE) parser.add_argument("-t", "--target", help="URI for target database", default=TARGET) parser.add_argument("-l", "--location", help="location of interim .csv files") parser.add_argument("-c", "--copy", help="Keep a copy of source data", action="store_true", default=False) args = parser.parse_args(argv[1:]) # process options if args.debug: set_level(LOGNAME, 'DEBUG') else: set_level(LOGNAME, 'INFO') # Set location of interim files, default to current directory csv_dir = os.getcwd() if args.location: csv_dir = args.location log.debug('csv_dir set to %s' % csv_dir) # connect to source database if args.source: source_conn = dburi.get_connection(args.source) else: raise NameError, 'Must specify a source URI' log.debug('Connected to source - %s' % args.source) # connect to target if args.target: target_conn = dburi.get_connection(args.target) else: raise NameError, 'Must specify a target URI' log.debug('Connected to target - %s' % args.target) log.info("Started clearing out target db - %s" % args.target) table_list = [] for table in TABLES: table_name = table['table_name'] log.debug("Creating %s Table object" % table_name) cur_table = Table(table_name, source_conn, target_conn, csv_dir) table_list.append(cur_table) if args.copy: log.debug("Writing target data for %s to %s" % (table_name, csv_dir)) cur_table.dump_target_to_file() log.debug("Truncating %s in target" % table_name) cur_table.truncate_target() log.info("Completed clearing out target db") # The copy is done in reverse order (reference tables first) rev_tables=table_list[:] rev_tables.reverse() for cur_table in rev_tables: table_name = cur_table.table_name log.debug("Loading %s data to target" % table_name) # Try and keep the contents in memory, may have to re-think this if args.copy: log.debug("Writing source data for %s to .csv file" % table_name) cur_table.dump_source_to_file() # TODO: If columns are defined only copy those cur_table.copy_source_to_target() log.debug("%s copied from source to target" % table_name) log.info("Completed copying data from source to target db")