ia64/xen-unstable

changeset 14607:b13a95fc0e2f

Move the client-specific parts of xmlrpclib2 into xmlrpcclient.py. This means
that clients do not need to import swathes of server code.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Tue Mar 27 22:34:22 2007 +0100 (2007-03-27)
parents ea68ae90fc10
children 94b873fb033a
files tools/python/xen/util/xmlrpcclient.py tools/python/xen/util/xmlrpclib2.py tools/python/xen/xend/XendClient.py tools/python/xen/xm/XenAPI.py tools/python/xen/xm/main.py
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/python/xen/util/xmlrpcclient.py	Tue Mar 27 22:34:22 2007 +0100
     1.3 @@ -0,0 +1,94 @@
     1.4 +#============================================================================
     1.5 +# This library is free software; you can redistribute it and/or
     1.6 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
     1.7 +# License as published by the Free Software Foundation.
     1.8 +#
     1.9 +# This library is distributed in the hope that it will be useful,
    1.10 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.11 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.12 +# Lesser General Public License for more details.
    1.13 +#
    1.14 +# You should have received a copy of the GNU Lesser General Public
    1.15 +# License along with this library; if not, write to the Free Software
    1.16 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.17 +#============================================================================
    1.18 +# Copyright (C) 2006 Anthony Liguori <aliguori@us.ibm.com>
    1.19 +# Copyright (C) 2007 XenSource Inc.
    1.20 +#============================================================================
    1.21 +
    1.22 +
    1.23 +from httplib import FakeSocket, HTTPConnection, HTTP
    1.24 +import socket
    1.25 +import xmlrpclib
    1.26 +from types import StringTypes
    1.27 +
    1.28 +
    1.29 +try:
    1.30 +    import SSHTransport
    1.31 +    ssh_enabled = True
    1.32 +except ImportError:
    1.33 +    # SSHTransport is disabled on Python <2.4, because it uses the subprocess
    1.34 +    # package.
    1.35 +    ssh_enabled = False
    1.36 +
    1.37 +
    1.38 +
    1.39 +# A new ServerProxy that also supports httpu urls.  An http URL comes in the
    1.40 +# form:
    1.41 +#
    1.42 +# httpu:///absolute/path/to/socket.sock
    1.43 +#
    1.44 +# It assumes that the RPC handler is /RPC2.  This probably needs to be improved
    1.45 +
    1.46 +class HTTPUnixConnection(HTTPConnection):
    1.47 +    def connect(self):
    1.48 +        self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    1.49 +        self.sock.connect(self.host)
    1.50 +
    1.51 +class HTTPUnix(HTTP):
    1.52 +    _connection_class = HTTPUnixConnection
    1.53 +
    1.54 +class UnixTransport(xmlrpclib.Transport):
    1.55 +    def request(self, host, handler, request_body, verbose=0):
    1.56 +        self.__handler = handler
    1.57 +        return xmlrpclib.Transport.request(self, host, '/RPC2',
    1.58 +                                           request_body, verbose)
    1.59 +    def make_connection(self, host):
    1.60 +        return HTTPUnix(self.__handler)
    1.61 +
    1.62 +
    1.63 +# See xmlrpclib2.TCPXMLRPCServer._marshalled_dispatch.
    1.64 +def conv_string(x):
    1.65 +    if isinstance(x, StringTypes):
    1.66 +        s = string.replace(x, "'", r"\047")
    1.67 +        exec "s = '" + s + "'"
    1.68 +        return s
    1.69 +    else:
    1.70 +        return x
    1.71 +
    1.72 +
    1.73 +class ServerProxy(xmlrpclib.ServerProxy):
    1.74 +    def __init__(self, uri, transport=None, encoding=None, verbose=0,
    1.75 +                 allow_none=1):
    1.76 +        if transport == None:
    1.77 +            (protocol, rest) = uri.split(':', 1)
    1.78 +            if protocol == 'httpu':
    1.79 +                uri = 'http:' + rest
    1.80 +                transport = UnixTransport()
    1.81 +            elif protocol == 'ssh':
    1.82 +                global ssh_enabled
    1.83 +                if ssh_enabled:
    1.84 +                    (transport, uri) = SSHTransport.getHTTPURI(uri)
    1.85 +                else:
    1.86 +                    raise ValueError(
    1.87 +                        "SSH transport not supported on Python <2.4.")
    1.88 +        xmlrpclib.ServerProxy.__init__(self, uri, transport, encoding,
    1.89 +                                       verbose, allow_none)
    1.90 +
    1.91 +    def __request(self, methodname, params):
    1.92 +        response = xmlrpclib.ServerProxy.__request(self, methodname, params)
    1.93 +
    1.94 +        if isinstance(response, tuple):
    1.95 +            return tuple([conv_string(x) for x in response])
    1.96 +        else:
    1.97 +            return conv_string(response)
     2.1 --- a/tools/python/xen/util/xmlrpclib2.py	Tue Mar 27 16:52:56 2007 +0100
     2.2 +++ b/tools/python/xen/util/xmlrpclib2.py	Tue Mar 27 22:34:22 2007 +0100
     2.3 @@ -26,7 +26,6 @@ import fcntl
     2.4  from types import *
     2.5      
     2.6  
     2.7 -from httplib import HTTPConnection, HTTP
     2.8  from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
     2.9  import SocketServer
    2.10  import xmlrpclib, socket, os, stat
    2.11 @@ -36,14 +35,6 @@ import mkdir
    2.12  from xen.web import connection
    2.13  from xen.xend.XendLogging import log
    2.14  
    2.15 -try:
    2.16 -    import SSHTransport
    2.17 -    ssh_enabled = True
    2.18 -except ImportError:
    2.19 -    # SSHTransport is disabled on Python <2.4, because it uses the subprocess
    2.20 -    # package.
    2.21 -    ssh_enabled = False
    2.22 -
    2.23  #
    2.24  # Convert all integers to strings as described in the Xen API
    2.25  #
    2.26 @@ -64,13 +55,6 @@ def stringify(value):
    2.27          return value
    2.28  
    2.29  
    2.30 -# A new ServerProxy that also supports httpu urls.  An http URL comes in the
    2.31 -# form:
    2.32 -#
    2.33 -# httpu:///absolute/path/to/socket.sock
    2.34 -#
    2.35 -# It assumes that the RPC handler is /RPC2.  This probably needs to be improved
    2.36 -
    2.37  # We're forced to subclass the RequestHandler class so that we can work around
    2.38  # some bugs in Keep-Alive handling and also enabled it by default
    2.39  class XMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
    2.40 @@ -106,60 +90,6 @@ class XMLRPCRequestHandler(SimpleXMLRPCR
    2.41          if self.close_connection == 1:
    2.42              self.connection.shutdown(1)
    2.43  
    2.44 -class HTTPUnixConnection(HTTPConnection):
    2.45 -    def connect(self):
    2.46 -        self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    2.47 -        self.sock.connect(self.host)
    2.48 -
    2.49 -class HTTPUnix(HTTP):
    2.50 -    _connection_class = HTTPUnixConnection
    2.51 -
    2.52 -class UnixTransport(xmlrpclib.Transport):
    2.53 -    def request(self, host, handler, request_body, verbose=0):
    2.54 -        self.__handler = handler
    2.55 -        return xmlrpclib.Transport.request(self, host, '/RPC2',
    2.56 -                                           request_body, verbose)
    2.57 -    def make_connection(self, host):
    2.58 -        return HTTPUnix(self.__handler)
    2.59 -
    2.60 -
    2.61 -# See _marshalled_dispatch below.
    2.62 -def conv_string(x):
    2.63 -    if isinstance(x, StringTypes):
    2.64 -        s = string.replace(x, "'", r"\047")
    2.65 -        exec "s = '" + s + "'"
    2.66 -        return s
    2.67 -    else:
    2.68 -        return x
    2.69 -
    2.70 -
    2.71 -class ServerProxy(xmlrpclib.ServerProxy):
    2.72 -    def __init__(self, uri, transport=None, encoding=None, verbose=0,
    2.73 -                 allow_none=1):
    2.74 -        if transport == None:
    2.75 -            (protocol, rest) = uri.split(':', 1)
    2.76 -            if protocol == 'httpu':
    2.77 -                uri = 'http:' + rest
    2.78 -                transport = UnixTransport()
    2.79 -            elif protocol == 'ssh':
    2.80 -                global ssh_enabled
    2.81 -                if ssh_enabled:
    2.82 -                    (transport, uri) = SSHTransport.getHTTPURI(uri)
    2.83 -                else:
    2.84 -                    raise ValueError(
    2.85 -                        "SSH transport not supported on Python <2.4.")
    2.86 -        xmlrpclib.ServerProxy.__init__(self, uri, transport, encoding,
    2.87 -                                       verbose, allow_none)
    2.88 -
    2.89 -    def __request(self, methodname, params):
    2.90 -        response = xmlrpclib.ServerProxy.__request(self, methodname, params)
    2.91 -
    2.92 -        if isinstance(response, tuple):
    2.93 -            return tuple([conv_string(x) for x in response])
    2.94 -        else:
    2.95 -            return conv_string(response)
    2.96 -
    2.97 -
    2.98  # This is a base XML-RPC server for TCP.  It sets allow_reuse_address to
    2.99  # true, and has an improved marshaller that logs and serializes exceptions.
   2.100  
     3.1 --- a/tools/python/xen/xend/XendClient.py	Tue Mar 27 16:52:56 2007 +0100
     3.2 +++ b/tools/python/xen/xend/XendClient.py	Tue Mar 27 22:34:22 2007 +0100
     3.3 @@ -17,7 +17,7 @@
     3.4  # Copyright (C) 2006 Anthony Liguori <aliguori@us.ibm.com>
     3.5  #============================================================================
     3.6  
     3.7 -from xen.util.xmlrpclib2 import ServerProxy
     3.8 +from xen.util.xmlrpcclient import ServerProxy
     3.9  import os
    3.10  import sys
    3.11  
     4.1 --- a/tools/python/xen/xm/XenAPI.py	Tue Mar 27 16:52:56 2007 +0100
     4.2 +++ b/tools/python/xen/xm/XenAPI.py	Tue Mar 27 22:34:22 2007 +0100
     4.3 @@ -12,7 +12,7 @@
     4.4  # License along with this library; if not, write to the Free Software
     4.5  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     4.6  #============================================================================
     4.7 -# Copyright (C) 2006 XenSource Inc.
     4.8 +# Copyright (C) 2006-2007 XenSource Inc.
     4.9  #============================================================================
    4.10  #
    4.11  # Parts of this file are based upon xmlrpclib.py, the XML-RPC client
    4.12 @@ -47,7 +47,7 @@
    4.13  import gettext
    4.14  import xmlrpclib
    4.15  
    4.16 -import xen.util.xmlrpclib2
    4.17 +import xen.util.xmlrpcclient as xmlrpcclient
    4.18  
    4.19  
    4.20  translation = gettext.translation('xen-xm', fallback = True)
    4.21 @@ -85,7 +85,7 @@ class Failure(Exception):
    4.22  _RECONNECT_AND_RETRY = (lambda _ : ())
    4.23  
    4.24  
    4.25 -class Session(xen.util.xmlrpclib2.ServerProxy):
    4.26 +class Session(xmlrpcclient.ServerProxy):
    4.27      """A server proxy and session manager for communicating with Xend using
    4.28      the Xen-API.
    4.29  
    4.30 @@ -104,9 +104,8 @@ class Session(xen.util.xmlrpclib2.Server
    4.31  
    4.32      def __init__(self, uri, transport=None, encoding=None, verbose=0,
    4.33                   allow_none=1):
    4.34 -        xen.util.xmlrpclib2.ServerProxy.__init__(self, uri, transport,
    4.35 -                                                 encoding, verbose,
    4.36 -                                                 allow_none)
    4.37 +        xmlrpcclient.ServerProxy.__init__(self, uri, transport, encoding,
    4.38 +                                          verbose, allow_none)
    4.39          self._session = None
    4.40          self.last_login_method = None
    4.41          self.last_login_params = None
    4.42 @@ -153,7 +152,7 @@ class Session(xen.util.xmlrpclib2.Server
    4.43          elif name.startswith('login'):
    4.44              return lambda *params: self._login(name, params)
    4.45          else:
    4.46 -            return xen.util.xmlrpclib2.ServerProxy.__getattr__(self, name)
    4.47 +            return xmlrpcclient.ServerProxy.__getattr__(self, name)
    4.48  
    4.49  
    4.50  def _parse_result(result):
     5.1 --- a/tools/python/xen/xm/main.py	Tue Mar 27 16:52:56 2007 +0100
     5.2 +++ b/tools/python/xen/xm/main.py	Tue Mar 27 22:34:22 2007 +0100
     5.3 @@ -49,7 +49,7 @@ from xen.xend.XendConstants import *
     5.4  
     5.5  from xen.xm.opts import OptionError, Opts, wrap, set_true
     5.6  from xen.xm import console
     5.7 -from xen.util.xmlrpclib2 import ServerProxy
     5.8 +from xen.util.xmlrpcclient import ServerProxy
     5.9  
    5.10  import XenAPI
    5.11