[py-svn] r36442 - in py/dist/py/test/rsession: . testing

fijal at codespeak.net fijal at codespeak.net
Wed Jan 10 23:13:29 CET 2007


Author: fijal
Date: Wed Jan 10 23:13:24 2007
New Revision: 36442

Modified:
   py/dist/py/test/rsession/rsync.py
   py/dist/py/test/rsession/rsync_remote.py
   py/dist/py/test/rsession/testing/test_rsync.py
Log:
Add support for callback which tells a bit about progress


Modified: py/dist/py/test/rsession/rsync.py
==============================================================================
--- py/dist/py/test/rsession/rsync.py	(original)
+++ py/dist/py/test/rsession/rsync.py	Wed Jan 10 23:13:24 2007
@@ -4,10 +4,11 @@
 
 class RSync(object):
 
-    def __init__(self, **options):
+    def __init__(self, callback=None, **options):
         for name in options:
-            assert name in ('delete','callback',)
+            assert name in ('delete')
         self.options = options
+        self.callback = callback
         self.channels = {}
         self.receivequeue = Queue()
         self.links = []
@@ -29,6 +30,8 @@
         self.sourcedir = os.path.dirname(os.path.join(self.sourcedir, 'x'))
         # send directory structure and file timestamps/sizes
         self._send_directory_structure(self.sourcedir)
+        self.paths = {}
+        self.to_send = {}
         # send modified file to clients
         while self.channels:
             channel, req = self.receivequeue.get()
@@ -52,12 +55,23 @@
                     if finishedcallback:
                         finishedcallback()
                 elif command == "ack":
-                    pass
+                    if self.callback:
+                        self.callback("ack", self.paths[data], channel)
+                elif command == "list_done":
+                    # sum up all to send
+                    if self.callback:
+                        s = sum([self.paths[i] for i in self.to_send[channel]])
+                        self.callback("list", s, channel)
                 elif command == "send":
                     modified_rel_path, checksum = data
                     modifiedpath = os.path.join(self.sourcedir, *modified_rel_path)
                     f = open(modifiedpath, 'rb')
                     data = f.read()
+                    modified_rel_path = "/".join(modified_rel_path)
+                    self.paths[modified_rel_path] = len(data)
+                    if channel not in self.to_send:
+                        self.to_send[channel] = []
+                    self.to_send[channel].append(modified_rel_path)
                     f.close()
                     if checksum is not None and checksum == md5.md5(data).digest():
                         data = None     # not really modified

Modified: py/dist/py/test/rsession/rsync_remote.py
==============================================================================
--- py/dist/py/test/rsession/rsync_remote.py	(original)
+++ py/dist/py/test/rsession/rsync_remote.py	Wed Jan 10 23:13:24 2007
@@ -52,10 +52,11 @@
 receive_directory_structure(destdir, [])
 
 STRICT_CHECK = False    # seems most useful this way for py.test
+channel.send(("list_done", None))
 
 for path, (time, size) in modifiedfiles:
     data = channel.receive()
-    channel.send(("ack", path))
+    channel.send(("ack", path[len(destdir) + 1:]))
     if data is not None:
         if STRICT_CHECK and len(data) != size:
             raise IOError('file modified during rsync: %r' % (path,))

Modified: py/dist/py/test/rsession/testing/test_rsync.py
==============================================================================
--- py/dist/py/test/rsession/testing/test_rsync.py	(original)
+++ py/dist/py/test/rsession/testing/test_rsync.py	Wed Jan 10 23:13:24 2007
@@ -60,3 +60,21 @@
     
     assert dest.join('rellink').readlink() == dest.join("existant")
     assert dest.join('abslink').readlink() == dest.join("existant")
+
+def test_callback():
+    base = py.test.ensuretemp('callback')
+    dest = base.join("dest")
+    source = base.join("source")
+    source.ensure("existant").write("a" * 100)
+    source.ensure("existant2").write("a" * 10)
+    total = {}
+    def callback(cmd, lgt, channel):
+        total[(cmd, lgt)] = True
+
+    rsync = RSync(callback=callback)
+    #rsync = RSync()
+    rsync.add_target(gw, dest)
+    rsync.send(source)
+
+    assert total == {("list", 110):True, ("ack", 100):True, ("ack", 10):True}
+


More information about the py-svn mailing list