~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/ping.py

  • Committer: Vincent Ladeuil
  • Date: 2017-01-17 13:48:10 UTC
  • mfrom: (6615.3.6 merges)
  • mto: This revision was merged to the branch mainline in revision 6620.
  • Revision ID: v.ladeuil+lp@free.fr-20170117134810-j9p3lidfy6pfyfsc
Merge 2.7, resolving conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008-2012 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Ping plugin for bzr."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
from bzrlib.commands import Command
 
22
from bzrlib.lazy_import import lazy_import
 
23
 
 
24
lazy_import(globals(), """
 
25
from bzrlib import errors
 
26
from bzrlib.smart.client import _SmartClient
 
27
from bzrlib.transport import get_transport
 
28
""")
 
29
 
 
30
 
 
31
class cmd_ping(Command):
 
32
    """Pings a Bazaar smart server.
 
33
    
 
34
    This command sends a 'hello' request to the given location using the bzr
 
35
    smart protocol, and reports the response.
 
36
    """
 
37
 
 
38
    takes_args = ['location']
 
39
 
 
40
    def run(self, location):
 
41
        transport = get_transport(location)
 
42
        try:
 
43
            medium = transport.get_smart_medium()
 
44
        except errors.NoSmartMedium, e:
 
45
            raise errors.BzrCommandError(str(e))
 
46
        client = _SmartClient(medium)
 
47
        # Use call_expecting_body (even though we don't expect a body) so that
 
48
        # we can see the response headers (if any) via the handler object.
 
49
        response, handler = client.call_expecting_body('hello')
 
50
        handler.cancel_read_body()
 
51
        self.outf.write('Response: %r\n' % (response,))
 
52
        if getattr(handler, 'headers', None) is not None:
 
53
            self.outf.write('Headers: %r\n' % (handler.headers,))