Rev 2450: Add new SmartClientRequestProtocolOne.call_with_file_upload method in http://sourcefrog.net/bzr/hpss-streaming-api

Martin Pool mbp at sourcefrog.net
Tue Apr 24 11:56:19 BST 2007


At http://sourcefrog.net/bzr/hpss-streaming-api

------------------------------------------------------------
revno: 2450
revision-id: mbp at sourcefrog.net-20070424105618-dqf7jyc4yfqly80v
parent: pqm at pqm.ubuntu.com-20070424033735-s0igieajv2czb2tt
committer: Martin Pool <mbp at sourcefrog.net>
branch nick: hpss-streaming-api
timestamp: Tue 2007-04-24 20:56:18 +1000
message:
  Add new SmartClientRequestProtocolOne.call_with_file_upload method
modified:
  bzrlib/smart/protocol.py       protocol.py-20061108035435-ot0lstk2590yqhzr-1
  bzrlib/tests/test_smart_transport.py test_ssh_transport.py-20060608202016-c25gvf1ob7ypbus6-2
=== modified file 'bzrlib/smart/protocol.py'
--- a/bzrlib/smart/protocol.py	2007-04-16 06:53:00 +0000
+++ b/bzrlib/smart/protocol.py	2007-04-24 10:56:18 +0000
@@ -269,12 +269,31 @@
         self._request.accept_bytes(bytes)
         self._request.finished_writing()
 
+    def call_with_file_upload(self, args, body_file, body_length):
+        """Make a remote call whose body is the contents of a file-like object.
+        
+        :param args: List of string procedure arguments.
+        :param body_file: File or similar object supporting read() and close()
+        :param body_length: Number of bytes of body to send; the file must be at least
+            this long.
+        """
+        self._request.accept_bytes(_encode_tuple(args))
+        self._request.accept_bytes('%d\n' % body_length)
+        while body_length > 0:
+            d = body_file.read(min(body_length, 16384))
+            self._request.accept_bytes(d)
+            body_length -= len(d)
+        self._request.accept_bytes('done\n')
+        self._request.finished_writing()
+
     def call_with_body_readv_array(self, args, body):
         """Make a remote call with a readv array.
 
         The body is encoded with one line per readv offset pair. The numbers in
         each pair are separated by a comma, and no trailing \n is emitted.
         """
+        # TODO: In a future protocol, allow the arguments to contain a list of
+        # tuples and this will be unnecessary.
         bytes = _encode_tuple(args)
         self._request.accept_bytes(bytes)
         readv_bytes = self._serialise_offsets(body)

=== modified file 'bzrlib/tests/test_smart_transport.py'
--- a/bzrlib/tests/test_smart_transport.py	2007-04-20 05:11:46 +0000
+++ b/bzrlib/tests/test_smart_transport.py	2007-04-24 10:56:18 +0000
@@ -1160,6 +1160,10 @@
     in the list is a write call from the client or server respectively.
     """
 
+    # TODO: If we add multiple protocols, then we might want these tests to be
+    # parameterized per protocol, but the expected results will then also need
+    # to vary, and some protocols may support more operations.
+
     def setUp(self):
         super(TestSmartProtocol, self).setUp()
         # XXX: self.server_to_client doesn't seem to be used.  If so,
@@ -1378,6 +1382,18 @@
         smart_protocol.call_with_body_bytes(('foo', ), "abcdefg")
         self.assertEqual(expected_bytes, output.getvalue())
 
+    def test_client_write_body_as_file(self):
+        # the client can get a file-like object that sends the body contents
+        expected_bytes = "foo\n7\nabcdefgdone\n"
+        from_server = StringIO('\n')
+        to_server = StringIO()
+        client_medium = medium.SmartSimplePipesClientMedium(from_server, to_server)
+        request = client_medium.get_request()
+        smart_protocol = protocol.SmartClientRequestProtocolOne(request)
+        body_file = StringIO('abcdefg')
+        smart_protocol.call_with_file_upload(('foo',), body_file, 7)
+        self.assertEqual(expected_bytes, to_server.getvalue())
+
     def test_client_call_with_body_readv_array(self):
         # protocol.call_with_upload should encode the readv array and then
         # length-prefix the bytes onto the wire.




More information about the bazaar-commits mailing list