[Checkins] SVN: z3c.vcsync/trunk/src/z3c/vcsync/ Allow
import/export to and from zipfiles.
Martijn Faassen
faassen at infrae.com
Fri Jul 6 16:58:26 EDT 2007
Log message for revision 77531:
Allow import/export to and from zipfiles.
Changed:
U z3c.vcsync/trunk/src/z3c/vcsync/README.txt
U z3c.vcsync/trunk/src/z3c/vcsync/__init__.py
U z3c.vcsync/trunk/src/z3c/vcsync/importexport.py
-=-
Modified: z3c.vcsync/trunk/src/z3c/vcsync/README.txt
===================================================================
--- z3c.vcsync/trunk/src/z3c/vcsync/README.txt 2007-07-06 20:53:40 UTC (rev 77530)
+++ z3c.vcsync/trunk/src/z3c/vcsync/README.txt 2007-07-06 20:58:25 UTC (rev 77531)
@@ -733,6 +733,34 @@
>>> sub.join('qux.test').read()
'3\n'
+We can also export to a zipfile. Let's first add an empty folder to the
+content to make things more difficult::
+
+ >>> container2['empty'] = Container()
+
+Now let's do the export::
+
+ >>> ziptarget = create_test_dir()
+ >>> zipfile_path = ziptarget.join('export.zip')
+ >>> from z3c.vcsync import export_state_zip
+ >>> export_state_zip(TestState(container2), 'data', zipfile_path)
+
+Inspecting the zipfile shows us the right files::
+
+ >>> from zipfile import ZipFile
+ >>> zf = ZipFile(zipfile_path.strpath, 'r')
+ >>> sorted(zf.namelist())
+ ['data/', 'data/alpha.test', 'data/empty/', 'data/foo.test',
+ 'data/hoi.test', 'data/sub/', 'data/sub/qux.test']
+ >>> zf.read('data/alpha.test')
+ '4000\n'
+ >>> zf.read('data/foo.test')
+ '1\n'
+ >>> zf.read('data/hoi.test')
+ '3000\n'
+ >>> zf.read('data/sub/qux.test')
+ '3\n'
+
Importing
---------
@@ -759,4 +787,30 @@
>>> container3['sub']['qux'].payload
3
+We can also import from a zipfile::
+
+ >>> container4 = Container()
+ >>> container4.__name__ = 'root'
+
+ >>> from z3c.vcsync import import_state_zip
+ >>> import_state_zip(TestState(container4), 'data', zipfile_path)
+
+We expect the structure to be the same as what we exported (including the
+empty folder)::
+
+ >>> sorted(container4.keys())
+ ['alpha', 'empty', 'foo', 'hoi', 'sub']
+ >>> sorted(container4['sub'].keys())
+ ['qux']
+ >>> container4['alpha'].payload
+ 4000
+ >>> container4['foo'].payload
+ 1
+ >>> container4['hoi'].payload
+ 3000
+ >>> container4['sub']['qux'].payload
+ 3
+ >>> sorted(container4['empty'].keys())
+ []
+
Note that importing into a container with existing content isn't supported yet.
Modified: z3c.vcsync/trunk/src/z3c/vcsync/__init__.py
===================================================================
--- z3c.vcsync/trunk/src/z3c/vcsync/__init__.py 2007-07-06 20:53:40 UTC (rev 77530)
+++ z3c.vcsync/trunk/src/z3c/vcsync/__init__.py 2007-07-06 20:58:25 UTC (rev 77531)
@@ -1,2 +1,3 @@
from vc import Synchronizer
-from importexport import export_state, import_state
+from importexport import (export_state, import_state,
+ export_state_zip, import_state_zip)
Modified: z3c.vcsync/trunk/src/z3c/vcsync/importexport.py
===================================================================
--- z3c.vcsync/trunk/src/z3c/vcsync/importexport.py 2007-07-06 20:53:40 UTC (rev 77530)
+++ z3c.vcsync/trunk/src/z3c/vcsync/importexport.py 2007-07-06 20:58:25 UTC (rev 77531)
@@ -1,3 +1,6 @@
+import py
+import tempfile, zipfile
+
from zope.app.container.interfaces import IContainer
from z3c.vcsync.interfaces import IVcDump, IVcFactory
from zope.component import getUtility
@@ -11,9 +14,28 @@
if IContainer.providedBy(obj):
export_helper(obj, path.join(obj.__name__))
+def export_state_zip(state, name, zippath):
+ tmp_dir = py.path.local(tempfile.mkdtemp())
+ try:
+ save_path = tmp_dir.join(name)
+ export_state(state, save_path)
+ zf = zipfile.ZipFile(zippath.strpath, 'w')
+ export_state_zip_helper(zf, tmp_dir, save_path)
+ zf.close()
+ finally:
+ tmp_dir.remove()
+
+def export_state_zip_helper(zf, save_path, path):
+ if path.check(dir=True):
+ zf.writestr(path.relto(save_path) + '/', '')
+ for p in path.listdir():
+ export_state_zip_helper(zf, save_path, p)
+ else:
+ zf.write(path.strpath, path.relto(save_path))
+
def import_state(state, path):
import_helper(state.root, path)
-
+
def import_helper(obj, path):
for p in path.listdir():
factory = getUtility(IVcFactory, name=p.ext)
@@ -23,3 +45,24 @@
obj[name] = new_obj = factory(p)
if p.check(dir=True):
import_helper(new_obj, p)
+
+def import_state_zip(state, name, zippath):
+ tmp_dir = py.path.local(tempfile.mkdtemp())
+ try:
+ zf = zipfile.ZipFile(zippath.strpath, 'r')
+ for p in zf.namelist():
+ if p.endswith('/'):
+ p = p[:-1]
+ dir = True
+ else:
+ dir = False
+ new_path = tmp_dir.join(*p.split('/'))
+ if not dir:
+ new_path.ensure()
+ new_path.write(zf.read(p))
+ else:
+ new_path.ensure(dir=True)
+ import_state(state, tmp_dir.join(name))
+ zf.close()
+ finally:
+ tmp_dir.remove()
More information about the Checkins
mailing list