[Checkins] SVN: relstorage/trunk/ Added a utility for converting between storages called zodbconvert.

Shane Hathaway shane at hathawaymix.org
Thu Feb 28 03:34:44 EST 2008


Log message for revision 84359:
  Added a utility for converting between storages called zodbconvert.
  

Changed:
  U   relstorage/trunk/CHANGELOG.txt
  A   relstorage/trunk/scripts/
  A   relstorage/trunk/scripts/zodbconvert.py
  U   relstorage/trunk/setup.py

-=-
Modified: relstorage/trunk/CHANGELOG.txt
===================================================================
--- relstorage/trunk/CHANGELOG.txt	2008-02-28 06:47:36 UTC (rev 84358)
+++ relstorage/trunk/CHANGELOG.txt	2008-02-28 08:34:27 UTC (rev 84359)
@@ -1,4 +1,10 @@
 
+
+RelStorage 1.0 (not yet released)
+
+- Added a utility for converting between storages called zodbconvert.
+
+
 RelStorage 1.0c1
 
 - The previous fix for non-ASCII characters was incorrect.  Now transaction

Added: relstorage/trunk/scripts/zodbconvert.py
===================================================================
--- relstorage/trunk/scripts/zodbconvert.py	                        (rev 0)
+++ relstorage/trunk/scripts/zodbconvert.py	2008-02-28 08:34:27 UTC (rev 84359)
@@ -0,0 +1,111 @@
+#!/usr/bin/env python
+##############################################################################
+#
+# Copyright (c) 2008 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""ZODB storage conversion utility.
+
+See http://wiki.zope.org/ZODB/ZODBConvert for details.
+"""
+
+import logging
+import optparse
+from persistent.TimeStamp import TimeStamp
+from StringIO import StringIO
+import sys
+import ZConfig
+from ZODB.utils import oid_repr
+
+schema_xml = """
+<schema>
+  <import package="ZODB"/>
+  <import package="relstorage"/>
+  <section type="ZODB.storage" name="source" attribute="source"
+    required="yes" />
+  <section type="ZODB.storage" name="destination" attribute="destination"
+    required="yes" />
+</schema>
+"""
+
+
+def storage_has_data(storage):
+    i = storage.iterator()
+    try:
+        i[0]
+    except IndexError:
+        return False
+    return True
+
+
+def main():
+    parser = optparse.OptionParser(description=__doc__,
+        usage="%prog [options] config_file")
+    parser.add_option(
+        "--dry-run", dest="dry_run", action="store_true",
+        help="Attempt to open the storages, then explain what would be done")
+    parser.add_option(
+        "--clear", dest="clear", action="store_true",
+        help="Clear the contents of the destination storage before copying")
+    parser.add_option(
+        "-v", "--verbose", dest="verbose", action="store_true",
+        help="Show verbose information while copying")
+    parser.set_defaults(dry_run=False, clear=False, verbose=False)
+    options, args = parser.parse_args()
+
+    if len(args) != 1:
+        parser.error("The name of one configuration file is required.")
+
+    schema = ZConfig.loadSchemaFile(StringIO(schema_xml))
+    config, handler = ZConfig.loadConfig(schema, args[0])
+    source = config.source.open()
+    destination = config.destination.open()
+
+    print "Storages opened successfully."
+
+    if options.dry_run:
+        print "Dry run mode: not changing the destination."
+        if storage_has_data(destination):
+            print "Warning: the destination storage has data"
+        count = 0
+        for txn in source.iterator():
+            print '%s user=%s description=%s' % (
+                TimeStamp(txn.tid), txn.user, txn.description)
+            for rec in txn:
+                print '  oid=%s length=%d' % (oid_repr(rec.oid), len(rec.data))
+            count += 1
+        print "Would copy %d transactions." % count
+        sys.exit(0)
+
+    if options.clear:
+        if hasattr(destination, 'zap_all'):
+            destination.zap_all()
+        else:
+            msg = ("Error: no API is known for clearing this type of storage."
+                " Use another method.")
+            sys.exit(msg)
+
+    if storage_has_data(destination):
+        msg = "Error: the destination storage has data.  Try --clear."
+        sys.exit(msg)
+
+    destination.copyTransactionsFrom(source, verbose=options.verbose)
+
+    source.close()
+    destination.close()
+
+    print 'All transactions copied successfully.'
+
+
+if __name__ == '__main__':
+    logging.basicConfig()
+    main()
+

Modified: relstorage/trunk/setup.py
===================================================================
--- relstorage/trunk/setup.py	2008-02-28 06:47:36 UTC (rev 84358)
+++ relstorage/trunk/setup.py	2008-02-28 08:34:27 UTC (rev 84359)
@@ -69,6 +69,7 @@
     package_data={
         'relstorage': ['component.xml'],
     },
+    scripts=['scripts/zodbconvert.py'],
     license="ZPL 2.1",
     platforms=["any"],
     description=doclines[0],



More information about the Checkins mailing list