"Fossies" - the Fresh Open Source Software Archive

Member "pyzor-1.0.0/scripts/pyzor-migrate" (10 Dec 2014, 3191 Bytes) of package /linux/privat/pyzor-1.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "pyzor-migrate": 0.9.0_vs_1.0.0.

    1 #! /usr/bin/env python
    2 
    3 """This scripts allows migration of records between pyzor engines types."""
    4 
    5 from __future__ import print_function
    6 
    7 import sys
    8 import logging
    9 import optparse
   10 
   11 import pyzor
   12 import pyzor.engines
   13 
   14 
   15 def get_engine(engine, dsn, mode='c'):
   16     engine_class = pyzor.engines.database_classes[engine].single_threaded
   17     engine_instance = engine_class(dsn, mode)
   18     return engine_instance
   19 
   20 
   21 def migrate(options):
   22     ok_count = 0
   23     fail_count = 0
   24     print_interval = 100000
   25 
   26     source_engine = get_engine(options.source_engine, options.source_dsn,
   27                                mode='r')
   28     destination_engine = get_engine(options.destination_engine,
   29                                     options.destination_dsn)
   30 
   31     it = source_engine.iteritems()
   32     while True:
   33         try:
   34             key, record = it.next()
   35             destination_engine[key] = record
   36             if options.delete:
   37                 del source_engine[key]
   38             ok_count += 1
   39             if ok_count % print_interval == 0:
   40                 print("%s records transferred..." % ok_count)
   41         except StopIteration:
   42             break
   43         except Exception as e:
   44             fail_count += 1
   45             print("Record %s failed: %s" % (key, str(e)))
   46 
   47     print("Migration complete, %s records transferred successfully, %s "
   48           "records failed" % (ok_count, fail_count))
   49 
   50 if __name__ == '__main__':
   51     """Parse command-line arguments and execute the script."""
   52     description = """This scripts allows migrating pyzor records between 
   53 different engine types. It's arguments are pyzor's DSN, which differ according
   54 to the engine type. See pyzor documentation for more details.   
   55     """
   56     logging.basicConfig()
   57     parser = optparse.OptionParser(description=description)
   58     parser.add_option("--se", "--source-engine", action="store", default=None,
   59                       dest="source_engine",
   60                       help="select source database backend")
   61     parser.add_option("--sd", "--source-dsn", action="store", default=None,
   62                       dest="source_dsn", help="data source DSN")
   63     parser.add_option("--de", "--destination-engine", action="store",
   64                       default=None, dest="destination_engine", help="select "
   65                       "destination database backend")
   66     parser.add_option("--dd", "--destination-dsn", action="store",
   67                       default=None, dest="destination_dsn",
   68                       help="destination DSN")
   69     parser.add_option("--delete", action="store_true", dest="delete",
   70                       default=False, help="delete old records")
   71 
   72     opts, args = parser.parse_args()
   73 
   74     if not (opts.source_engine and opts.source_dsn and opts.destination_engine
   75             and opts.destination_dsn):
   76         print("options --se/--sd/--de/--dd are required")
   77         sys.exit(1)
   78 
   79     if opts.source_engine not in pyzor.engines.database_classes:
   80         print("Unsupported source engine: %s" % opts.source_engine)
   81         sys.exit(1)
   82 
   83     if opts.destination_engine not in pyzor.engines.database_classes:
   84         print("Unsupported destination engine: %s" % opts.destination_engine)
   85         sys.exit(1)
   86 
   87     migrate(opts)