def quote(string, safe=''): if isinstance(string, util.utype): string = string.encode('utf-8') return util.urlquote(string, safe)
def main(): usage = '%prog [options] <source> <target>' parser = optparse.OptionParser(usage=usage) parser.add_option('--continuous', action='store_true', dest='continuous', help='trigger continuous replication in cochdb') parser.add_option('--compact', action='store_true', dest='compact', help='compact target database after replication') options, args = parser.parse_args() if len(args) != 2: raise parser.error('need source and target arguments') # set up server objects src, tgt = args sbase, spath = findpath(parser, src) source = client.Server(sbase) tbase, tpath = findpath(parser, tgt) target = client.Server(tbase) # check database name specs if '*' in tpath: raise parser.error('invalid target path: must be single db or empty') all = sorted(i for i in source if i[0] != '_') # Skip reserved names. if not spath: raise parser.error('source database must be specified') sources = [i for i in all if fnmatch.fnmatchcase(i, spath)] if not sources: raise parser.error("no source databases match glob '%s'" % spath) if len(sources) > 1 and tpath: raise parser.error('target path must be empty with multiple sources') elif len(sources) == 1: databases = [(sources[0], tpath)] else: databases = [(i, i) for i in sources] # do the actual replication for sdb, tdb in databases: start = time.time() print(sdb, '->', tdb) sys.stdout.flush() if tdb not in target: target.create(tdb) sys.stdout.write("created") sys.stdout.flush() sdb = '%s%s' % (sbase, util.urlquote(sdb, '')) if options.continuous: target.replicate(sdb, tdb, continuous=options.continuous) else: target.replicate(sdb, tdb) print('%.1fs' % (time.time() - start)) sys.stdout.flush() if options.compact: for (sdb, tdb) in databases: print('compact', tdb) target[tdb].compact()