~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/remote.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-04-09 07:36:41 UTC
  • mfrom: (3297.3.7 unknown-response)
  • Revision ID: pqm@pqm.ubuntu.com-20080409073641-pvhyvdyt42fph5xf
Better infrastructure for dealing with 'bad request' responses from a
        smart server. (Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
149
149
    def open_repository(self):
150
150
        path = self._path_for_remote_call(self._client)
151
151
        verb = 'BzrDir.find_repositoryV2'
152
 
        response = self._client.call(verb, path)
153
 
        if (response == ('error', "Generic bzr smart protocol error: "
154
 
                "bad request '%s'" % verb) or
155
 
              response == ('error', "Generic bzr smart protocol error: "
156
 
                "bad request u'%s'" % verb)):
 
152
        try:
 
153
            response = self._client.call(verb, path)
 
154
        except errors.UnknownSmartMethod:
157
155
            verb = 'BzrDir.find_repository'
158
156
            response = self._client.call(verb, path)
159
157
        assert response[0] in ('ok', 'norepository'), \
634
632
        """
635
633
        import tempfile
636
634
        path = self.bzrdir._path_for_remote_call(self._client)
637
 
        response, protocol = self._client.call_expecting_body(
638
 
            'Repository.tarball', path, compression)
 
635
        try:
 
636
            response, protocol = self._client.call_expecting_body(
 
637
                'Repository.tarball', path, compression)
 
638
        except errors.UnknownSmartMethod:
 
639
            protocol.cancel_read_body()
 
640
            return None
639
641
        if response[0] == 'ok':
640
642
            # Extract the tarball and return it
641
643
            t = tempfile.NamedTemporaryFile()
643
645
            t.write(protocol.read_body_bytes())
644
646
            t.seek(0)
645
647
            return t
646
 
        if (response == ('error', "Generic bzr smart protocol error: "
647
 
                "bad request 'Repository.tarball'") or
648
 
              response == ('error', "Generic bzr smart protocol error: "
649
 
                "bad request u'Repository.tarball'")):
650
 
            protocol.cancel_read_body()
651
 
            return None
652
648
        raise errors.UnexpectedSmartServerResponse(response)
653
649
 
654
650
    def sprout(self, to_bzrdir, revision_id=None):
815
811
                100.0 * len(self._requested_parents) / len(ancestry))
816
812
        return dict((k, ancestry[k]) for k in present_keys)
817
813
 
818
 
    def _response_is_unknown_method(self, response, verb):
819
 
        """Return True if response is an unknonwn method response to verb.
820
 
        
821
 
        :param response: The response from a smart client call_expecting_body
822
 
            call.
823
 
        :param verb: The verb used in that call.
824
 
        :return: True if an unknown method was encountered.
825
 
        """
826
 
        # This might live better on
827
 
        # bzrlib.smart.protocol.SmartClientRequestProtocolOne
828
 
        if (response[0] == ('error', "Generic bzr smart protocol error: "
829
 
                "bad request '%s'" % verb) or
830
 
              response[0] == ('error', "Generic bzr smart protocol error: "
831
 
                "bad request u'%s'" % verb)):
832
 
           response[1].cancel_read_body()
833
 
           return True
834
 
        return False
835
 
 
836
814
    def _get_parent_map(self, keys):
837
815
        """Helper for get_parent_map that performs the RPC."""
838
816
        medium = self._client._medium
883
861
            assert type(key) is str
884
862
        verb = 'Repository.get_parent_map'
885
863
        args = (path,) + tuple(keys)
886
 
        response = self._client.call_with_body_bytes_expecting_body(
887
 
            verb, args, self._serialise_search_recipe(recipe))
888
 
        if self._response_is_unknown_method(response, verb):
 
864
        try:
 
865
            response = self._client.call_with_body_bytes_expecting_body(
 
866
                verb, args, self._serialise_search_recipe(recipe))
 
867
        except errors.UnknownSmartMethod:
889
868
            # Server does not support this method, so get the whole graph.
890
869
            # Worse, we have to force a disconnection, because the server now
891
870
            # doesn't realise it has a body on the wire to consume, so the
897
876
            # To avoid having to disconnect repeatedly, we keep track of the
898
877
            # fact the server doesn't understand remote methods added in 1.2.
899
878
            medium._remote_is_at_least_1_2 = False
900
 
            return self._get_revision_graph(None)
901
 
        elif response[0][0] not in ['ok']:
 
879
            return self.get_revision_graph(None)
 
880
        if response[0][0] not in ['ok']:
902
881
            response[1].cancel_read_body()
903
882
            raise errors.UnexpectedSmartServerResponse(response[0])
904
883
        if response[0][0] == 'ok':
1061
1040
        REQUEST_NAME = 'Repository.stream_revisions_chunked'
1062
1041
        path = self.bzrdir._path_for_remote_call(self._client)
1063
1042
        body = self._serialise_search_recipe(search.get_recipe())
1064
 
        response, protocol = self._client.call_with_body_bytes_expecting_body(
1065
 
            REQUEST_NAME, (path,), body)
1066
 
 
1067
 
        if self._response_is_unknown_method((response, protocol), REQUEST_NAME):
 
1043
        try:
 
1044
            result = self._client.call_with_body_bytes_expecting_body(
 
1045
                REQUEST_NAME, (path,), body)
 
1046
            response, protocol = result
 
1047
        except errors.UnknownSmartMethod:
1068
1048
            # Server does not support this method, so fall back to VFS.
1069
1049
            # Worse, we have to force a disconnection, because the server now
1070
1050
            # doesn't realise it has a body on the wire to consume, so the