import repository from arizona
[raven.git] / lib / ravenlib / xmlrpc / client.py
1 """
2 XMLRPC support for unix-domain sockets
3 Scott Baker, smbaker@gmail.com
4 """
5
6 import httplib
7 import pickle
8 import socket
9 import sys
10 import xmlrpclib
11
12 class ServerException(Exception):
13    def __init__(self, name, str, tb):
14        self.name = name
15        self.str = str
16        self.tb = tb
17
18    def __str__(self):
19        return str("".join(self.tb)) + "\n" + str(self.name) + ": " + str(self.str)
20
21    def one_liner(self):
22        """ return a one-line description of the exception """
23        return str(self.name) + ": " + str(self.str)
24
25    def exit_one_liner(self, code):
26        """ print a one-line description and exit with the specified code """
27        print >> sys.stderr, "exit", code, ":", self.one_liner()
28        sys.exit(code)
29
30
31 class ExceptionUnmarshaller(xmlrpclib.Unmarshaller):
32     def close(self):\r
33         try:\r
34             return xmlrpclib.Unmarshaller.close(self)\r
35         except xmlrpclib.Fault, e:\r
36             dict = pickle.loads(e.faultString)\r
37             tb = dict.get("tb", "Unknown")\r
38             name = dict.get("name", "Unknown")
39             str = dict.get("str", "Unknown")
40             se = ServerException(name, str, tb)
41             raise se
42
43 class UDSConnection(httplib.HTTPConnection):
44     def __init__(self, uds_name):
45          self.uds_name = uds_name
46          httplib.HTTPConnection.__init__(self, uds_name, 0)
47
48     def _set_hostport(self, host, port):
49          self.host = host
50          self.port = port
51
52     def connect(self):
53          self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
54          self.sock.connect(self.uds_name)
55
56 class UDS(httplib.HTTP):
57     _connection_class = UDSConnection
58
59     def __init__(self, uds_name):
60         self._setup(self._connection_class(uds_name))
61
62 class UDSTransport(xmlrpclib.Transport):
63     def __init__(self, uds_name):
64         self.uds_name = uds_name
65
66     def make_connection(self, host):
67         return UDS(self.uds_name)
68
69     def getparser(self):\r
70         unmarshaller = ExceptionUnmarshaller()\r
71         parser = xmlrpclib.ExpatParser(unmarshaller)\r
72         return parser, unmarshaller\r
73 \r
74 class HTTPTransport(xmlrpclib.Transport):\r
75     def make_connection(self, host):
76         return httplib.HTTP(host, None)\r
77 \r
78     def getparser(self):\r
79         unmarshaller = ExceptionUnmarshaller()\r
80         parser = xmlrpclib.ExpatParser(unmarshaller)\r
81         return parser, unmarshaller\r
82 \r
83 class XMLRPCClientBase():\r
84     ##
85     # Create a new GeniClient object.
86     #
87     # @param url is the url of the server
88     # @param key_file = private key file of client
89     # @param cert_file = x.509 cert containing the client's public key. This
90     #      could be a GID certificate, or any x.509 cert.
91
92     def __init__(self, addr):
93        if addr.startswith("uds:"):
94            uds_name = addr[4:]
95            self.transport = UDSTransport(uds_name)
96            self.server = xmlrpclib.ServerProxy("http://fakeurl/", self.transport, allow_none=True)
97        else:
98            self.transport = HTTPTransport()
99            self.server = xmlrpclib.ServerProxy(addr, self.transport, allow_none=True)
100
101     def noop(self, anything):
102        return self.server.noop(anything)