<s> import socket <EOL> import gettext <EOL> import httplib <EOL> import xmlrpclib <EOL> from simplestack . common . config import config <EOL> translation = gettext . translation ( '<STR_LIT>' , fallback = True ) <EOL> API_VERSION_1_1 = '<STR_LIT>' <EOL> API_VERSION_1_2 = '<STR_LIT>' <EOL> class Failure ( Exception ) : <EOL> def __init__ ( self , details ) : <EOL> self . details = details <EOL> def __str__ ( self ) : <EOL> try : <EOL> return str ( self . details ) <EOL> except Exception , exn : <EOL> import sys <EOL> print >> sys . stderr , exn <EOL> return "<STR_LIT>" % str ( self . details ) <EOL> def _details_map ( self ) : <EOL> return dict ( [ ( str ( i ) , self . details [ i ] ) <EOL> for i in range ( len ( self . details ) ) ] ) <EOL> _RECONNECT_AND_RETRY = ( lambda _ : ( ) ) <EOL> class UDSHTTPConnection ( httplib . HTTPConnection ) : <EOL> """<STR_LIT>""" <EOL> def connect ( self ) : <EOL> path = self . host . replace ( "<STR_LIT:_>" , "<STR_LIT:/>" ) <EOL> self . sock = socket . socket ( socket . AF_UNIX , socket . SOCK_STREAM ) <EOL> self . sock . connect ( path ) <EOL> class UDSHTTP ( httplib . HTTP ) : <EOL> _connection_class = UDSHTTPConnection <EOL> class UDSTransport ( xmlrpclib . Transport ) : <EOL> def __init__ ( self , use_datetime = <NUM_LIT:0> ) : <EOL> self . _use_datetime = use_datetime <EOL> self . _extra_headers = [ ] <EOL> def add_extra_header ( self , key , value ) : <EOL> self . _extra_headers += [ ( key , value ) ] <EOL> def make_connection ( self , host ) : <EOL> return UDSHTTP ( host ) <EOL> def send_request ( self , connection , handler , request_body ) : <EOL> connection . putrequest ( "<STR_LIT:POST>" , handler ) <EOL> for key , value in self . _extra_headers : <EOL> connection . putheader ( key , value ) <EOL> class Session ( xmlrpclib . ServerProxy ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , uri , transport = None , encoding = None , verbose = <NUM_LIT:0> , <EOL> allow_none = <NUM_LIT:1> ) : <EOL> xmlrpclib . ServerProxy . __init__ ( self , uri , transport , encoding , <EOL> verbose , allow_none ) <EOL> self . transport = transport <EOL> self . _session = None <EOL> self . last_login_method = None <EOL> self . last_login_params = None <EOL> self . API_version = API_VERSION_1_1 <EOL> def xenapi_request ( self , methodname , params ) : <EOL> if methodname . startswith ( '<STR_LIT>' ) : <EOL> self . _login ( methodname , params ) <EOL> return None <EOL> elif methodname == '<STR_LIT>' or methodname == '<STR_LIT>' : <EOL> self . _logout ( ) <EOL> return None <EOL> else : <EOL> retry_count = <NUM_LIT:0> <EOL> while retry_count < <NUM_LIT:3> : <EOL> full_params = ( self . _session , ) + params <EOL> result = _parse_result ( getattr ( self , methodname ) ( * full_params ) ) <EOL> if result == _RECONNECT_AND_RETRY : <EOL> retry_count += <NUM_LIT:1> <EOL> if self . last_login_method : <EOL> self . _login ( self . last_login_method , <EOL> self . last_login_params ) <EOL> else : <EOL> raise xmlrpclib . Fault ( <NUM_LIT> , '<STR_LIT>' ) <EOL> else : <EOL> return result <EOL> raise xmlrpclib . Fault ( <EOL> <NUM_LIT> , '<STR_LIT>' ) <EOL> def _login ( self , method , params ) : <EOL> result = _parse_result ( getattr ( self , '<STR_LIT>' % method ) ( * params ) ) <EOL> if result == _RECONNECT_AND_RETRY : <EOL> raise xmlrpclib . Fault ( <EOL> <NUM_LIT> , '<STR_LIT>' ) <EOL> self . _session = result <EOL> self . last_login_method = method <EOL> self . last_login_params = params <EOL> self . API_version = self . _get_api_version ( ) <EOL> def _logout ( self ) : <EOL> try : <EOL> if self . last_login_method . startswith ( "<STR_LIT>" ) : <EOL> return _parse_result ( self . session . local_logout ( self . _session ) ) <EOL> else : <EOL> return _parse_result ( self . session . logout ( self . _session ) ) <EOL> finally : <EOL> self . _session = None <EOL> self . last_login_method = None <EOL> self . last_login_params = None <EOL> self . API_version = API_VERSION_1_1 <EOL> def _get_api_version ( self ) : <EOL> pool = self . xenapi . pool . get_all ( ) [ <NUM_LIT:0> ] <EOL> host = self . xenapi . pool . get_master ( pool ) <EOL> major = self . xenapi . host . get_API_version_major ( host ) <EOL> minor = self . xenapi . host . get_API_version_minor ( host ) <EOL> return "<STR_LIT>" % ( major , minor ) <EOL> def __getattr__ ( self , name ) : <EOL> if name == '<STR_LIT>' : <EOL> return self . _session <EOL> elif name == '<STR_LIT>' : <EOL> return _Dispatcher ( self . API_version , self . xenapi_request , None ) <EOL> elif name . startswith ( '<STR_LIT>' ) or name . startswith ( '<STR_LIT>' ) : <EOL> return lambda * params : self . _login ( name , params ) <EOL> else : <EOL> try : <EOL> socket . setdefaulttimeout ( config . getint ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> return xmlrpclib . ServerProxy . __getattr__ ( self , name ) <EOL> finally : <EOL> socket . setdefaulttimeout ( None ) <EOL> def xapi_local ( ) : <EOL> return Session ( "<STR_LIT>" , transport = UDSTransport ( ) ) <EOL> def _parse_result ( result ) : <EOL> if type ( result ) != dict or '<STR_LIT>' not in result : <EOL> raise xmlrpclib . Fault ( <EOL> <NUM_LIT> , '<STR_LIT>' + result <EOL> ) <EOL> if result [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> if '<STR_LIT>' in result : <EOL> return result [ '<STR_LIT>' ] <EOL> else : <EOL> raise xmlrpclib . Fault ( <NUM_LIT> , <EOL> '<STR_LIT>' ) <EOL> else : <EOL> if '<STR_LIT>' in result : <EOL> if result [ '<STR_LIT>' ] [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> return _RECONNECT_AND_RETRY <EOL> else : <EOL> raise Failure ( result [ '<STR_LIT>' ] ) <EOL> else : <EOL> raise xmlrpclib . Fault ( <EOL> <NUM_LIT> , '<STR_LIT>' ) <EOL> class _Dispatcher : <EOL> def __init__ ( self , API_version , send , name ) : <EOL> self . __API_version = API_version <EOL> self . __send = send <EOL> self . __name = name <EOL> def __repr__ ( self ) : <EOL> if self . __name : <EOL> return '<STR_LIT>' % self . __name <EOL> else : <EOL> return '<STR_LIT>' <EOL> def __getattr__ ( self , name ) : <EOL> if self . __name is None : <EOL> return _Dispatcher ( self . __API_version , self . __send , name ) <EOL> else : <EOL> name = "<STR_LIT>" % ( self . __name , name ) <EOL> return _Dispatcher ( self . __API_version , self . __send , name ) <EOL> def __call__ ( self , * args ) : <EOL> return self . __send ( self . __name , args ) </s>
