/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/smart/medium.py

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
Media carry the bytes of the requests somehow (e.g. via TCP, wrapped in HTTP, or
23
23
over SSH), and pass them to and from the protocol logic.  See the overview in
24
 
breezy/transport/smart/__init__.py.
 
24
bzrlib/transport/smart/__init__.py.
25
25
"""
26
26
 
27
27
from __future__ import absolute_import
31
31
import sys
32
32
import time
33
33
 
34
 
import breezy
35
 
from ...lazy_import import lazy_import
 
34
import bzrlib
 
35
from bzrlib.lazy_import import lazy_import
36
36
lazy_import(globals(), """
37
37
import select
38
38
import socket
39
39
import thread
40
40
import weakref
41
41
 
42
 
from breezy import (
 
42
from bzrlib import (
43
43
    debug,
 
44
    errors,
44
45
    trace,
45
46
    transport,
46
47
    ui,
47
48
    urlutils,
48
49
    )
49
 
from breezy.i18n import gettext
50
 
from breezy.bzr.smart import client, protocol, request, signals, vfs
51
 
from breezy.transport import ssh
 
50
from bzrlib.i18n import gettext
 
51
from bzrlib.smart import client, protocol, request, signals, vfs
 
52
from bzrlib.transport import ssh
52
53
""")
53
 
from ... import (
54
 
    errors,
55
 
    osutils,
56
 
    )
 
54
from bzrlib import osutils
57
55
 
58
56
# Throughout this module buffer size parameters are either limited to be at
59
57
# most _MAX_READ_SIZE, or are ignored and _MAX_READ_SIZE is used instead.
61
59
# from non-sockets as well.
62
60
_MAX_READ_SIZE = osutils.MAX_SOCKET_CHUNK
63
61
 
64
 
 
65
 
class HpssVfsRequestNotAllowed(errors.BzrError):
66
 
 
67
 
    _fmt = ("VFS requests over the smart server are not allowed. Encountered: "
68
 
            "%(method)s, %(arguments)s.")
69
 
 
70
 
    def __init__(self, method, arguments):
71
 
        self.method = method
72
 
        self.arguments = arguments
73
 
 
74
 
 
75
62
def _get_protocol_factory_for_bytes(bytes):
76
63
    """Determine the right protocol factory for 'bytes'.
77
64
 
113
100
    :returns: a tuple of two strs: (line, excess)
114
101
    """
115
102
    newline_pos = -1
116
 
    bytes = b''
 
103
    bytes = ''
117
104
    while newline_pos == -1:
118
105
        new_bytes = read_bytes_func(1)
119
106
        bytes += new_bytes
120
 
        if new_bytes == b'':
 
107
        if new_bytes == '':
121
108
            # Ran out of bytes before receiving a complete line.
122
 
            return bytes, b''
123
 
        newline_pos = bytes.find(b'\n')
 
109
            return bytes, ''
 
110
        newline_pos = bytes.find('\n')
124
111
    line = bytes[:newline_pos+1]
125
112
    excess = bytes[newline_pos+1:]
126
113
    return line, excess
246
233
            while not self.finished:
247
234
                server_protocol = self._build_protocol()
248
235
                self._serve_one_request(server_protocol)
249
 
        except errors.ConnectionTimeout as e:
 
236
        except errors.ConnectionTimeout, e:
250
237
            trace.note('%s' % (e,))
251
238
            trace.log_exception_quietly()
252
239
            self._disconnect_client()
253
240
            # We reported it, no reason to make a big fuss.
254
241
            return
255
 
        except Exception as e:
 
242
        except Exception, e:
256
243
            stderr.write("%s terminating on exception %s\n" % (self, e))
257
244
            raise
258
245
        self._disconnect_client()
342
329
            self._serve_one_request_unguarded(protocol)
343
330
        except KeyboardInterrupt:
344
331
            raise
345
 
        except Exception as e:
 
332
        except Exception, e:
346
333
            self.terminate_due_to_error()
347
334
 
348
335
    def terminate_due_to_error(self):
669
656
            # A method we don't know about doesn't count as a VFS method.
670
657
            return
671
658
        if issubclass(request_method, vfs.VfsRequest):
672
 
            raise HpssVfsRequestNotAllowed(params.method, params.args)
 
659
            raise errors.HpssVfsRequestNotAllowed(params.method, params.args)
673
660
 
674
661
 
675
662
class _DebugCounter(object):
676
663
    """An object that counts the HPSS calls made to each client medium.
677
664
 
678
665
    When a medium is garbage-collected, or failing that when
679
 
    breezy.global_state exits, the total number of calls made on that medium
 
666
    bzrlib.global_state exits, the total number of calls made on that medium
680
667
    are reported via trace.note.
681
668
    """
682
669
 
684
671
        self.counts = weakref.WeakKeyDictionary()
685
672
        client._SmartClient.hooks.install_named_hook(
686
673
            'call', self.increment_call_count, 'hpss call counter')
687
 
        breezy.get_global_state().cleanups.add_cleanup(self.flush_all)
 
674
        bzrlib.global_state.cleanups.add_cleanup(self.flush_all)
688
675
 
689
676
    def track(self, medium):
690
677
        """Start tracking calls made to a medium.
817
804
                client_protocol = protocol.SmartClientRequestProtocolOne(medium_request)
818
805
                client_protocol.query_version()
819
806
                self._done_hello = True
820
 
            except errors.SmartProtocolError as e:
 
807
            except errors.SmartProtocolError, e:
821
808
                # Cache the error, just like we would cache a successful
822
809
                # result.
823
810
                self._protocol_version_error = e
920
907
        """See SmartClientStreamMedium.accept_bytes."""
921
908
        try:
922
909
            self._writeable_pipe.write(bytes)
923
 
        except IOError as e:
 
910
        except IOError, e:
924
911
            if e.errno in (errno.EINVAL, errno.EPIPE):
925
912
                raise errors.ConnectionReset(
926
913
                    "Error trying to write to subprocess", e)
966
953
 
967
954
        :param ssh_params: A SSHParams instance.
968
955
        :param vendor: An optional override for the ssh vendor to use. See
969
 
            breezy.transport.ssh for details on ssh vendors.
 
956
            bzrlib.transport.ssh for details on ssh vendors.
970
957
        """
971
958
        self._real_medium = None
972
959
        self._ssh_params = ssh_params
1118
1105
        try:
1119
1106
            sockaddrs = socket.getaddrinfo(self._host, port, socket.AF_UNSPEC,
1120
1107
                socket.SOCK_STREAM, 0, 0)
1121
 
        except socket.gaierror as xxx_todo_changeme:
1122
 
            (err_num, err_msg) = xxx_todo_changeme.args
 
1108
        except socket.gaierror, (err_num, err_msg):
1123
1109
            raise errors.ConnectionError("failed to lookup %s:%d: %s" %
1124
1110
                    (self._host, port, err_msg))
1125
1111
        # Initialize err in case there are no addresses returned:
1130
1116
                self._socket.setsockopt(socket.IPPROTO_TCP,
1131
1117
                                        socket.TCP_NODELAY, 1)
1132
1118
                self._socket.connect(sockaddr)
1133
 
            except socket.error as err:
 
1119
            except socket.error, err:
1134
1120
                if self._socket is not None:
1135
1121
                    self._socket.close()
1136
1122
                self._socket = None
1139
1125
        if self._socket is None:
1140
1126
            # socket errors either have a (string) or (errno, string) as their
1141
1127
            # args.
1142
 
            if isinstance(err.args, str):
 
1128
            if type(err.args) is str:
1143
1129
                err_msg = err.args
1144
1130
            else:
1145
1131
                err_msg = err.args[1]