<s> import os <EOL> import base64 <EOL> import string <EOL> import sys <EOL> import xmlrpclib <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . path . append ( "<STR_LIT>" ) <EOL> os . environ . setdefault ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> from xos . config import Config <EOL> from core . models import Service <EOL> from services . hpc . models import HpcService <EOL> from services . requestrouter . models import RequestRouterService <EOL> from xos . logger import Logger , logging <EOL> logger = Logger ( level = logging . INFO ) <EOL> class APIHelper : <EOL> def __init__ ( self , proxy , auth , method = None ) : <EOL> self . proxy = proxy <EOL> self . auth = auth <EOL> self . method = method <EOL> def __getattr__ ( self , name ) : <EOL> if name . startswith ( "<STR_LIT:_>" ) : <EOL> return getattr ( self , name ) <EOL> else : <EOL> return APIHelper ( self . proxy , self . auth , name ) <EOL> def __call__ ( self , * args ) : <EOL> method = getattr ( self . proxy , self . method ) <EOL> return method ( self . auth , * args ) <EOL> class CmiClient : <EOL> def __init__ ( self , hostname , port = <NUM_LIT> , username = "<STR_LIT>" , password = "<STR_LIT>" ) : <EOL> self . connect_api ( hostname , port , username , password ) <EOL> def connect_api ( self , hostname , port = <NUM_LIT> , username = "<STR_LIT>" , password = "<STR_LIT>" ) : <EOL> cob = xmlrpclib . ServerProxy ( "<STR_LIT>" % ( hostname , port ) , allow_none = True ) <EOL> cob_auth = { } <EOL> cob_auth [ "<STR_LIT>" ] = username <EOL> cob_auth [ "<STR_LIT>" ] = password <EOL> cob_auth [ "<STR_LIT>" ] = "<STR_LIT:password>" <EOL> onev = xmlrpclib . ServerProxy ( "<STR_LIT>" % ( hostname , port ) , allow_none = True ) <EOL> onev_auth = { } <EOL> onev_auth [ "<STR_LIT>" ] = username <EOL> onev_auth [ "<STR_LIT>" ] = password <EOL> onev_auth [ "<STR_LIT>" ] = "<STR_LIT:password>" <EOL> self . cob = APIHelper ( cob , cob_auth ) <EOL> self . onev = APIHelper ( onev , onev_auth ) <EOL> class HpcLibrary : <EOL> def __init__ ( self ) : <EOL> self . _client = None <EOL> def make_account_name ( self , x ) : <EOL> x = x . lower ( ) <EOL> y = "<STR_LIT>" <EOL> for c in x : <EOL> if ( c in ( string . lowercase + string . digits ) ) : <EOL> y = y + c <EOL> return y [ : <NUM_LIT:20> ] <EOL> def get_hpc_service ( self ) : <EOL> hpc_service_name = getattr ( Config ( ) , "<STR_LIT>" , None ) <EOL> if hpc_service_name : <EOL> hpc_service = HpcService . objects . filter ( name = hpc_service_name ) <EOL> else : <EOL> hpc_service = HpcService . objects . all ( ) <EOL> if not hpc_service : <EOL> if hpc_service_name : <EOL> raise Exception ( "<STR_LIT>" % hpc_service_name ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> hpc_service = hpc_service [ <NUM_LIT:0> ] <EOL> return hpc_service <EOL> def get_cmi_hostname ( self , hpc_service = None ) : <EOL> if getattr ( Config ( ) , "<STR_LIT>" , None ) : <EOL> return getattr ( Config ( ) , "<STR_LIT>" ) <EOL> if ( hpc_service is None ) : <EOL> hpc_service = self . get_hpc_service ( ) <EOL> if hpc_service . cmi_hostname : <EOL> return hpc_service . cmi_hostname <EOL> try : <EOL> slices = hpc_service . slices . all ( ) <EOL> except : <EOL> slices = hpc_service . service . all ( ) <EOL> for slice in slices : <EOL> if slice . name . endswith ( "<STR_LIT>" ) : <EOL> for instance in slice . instances . all ( ) : <EOL> if instance . node : <EOL> return instance . node . name <EOL> raise Exception ( "<STR_LIT>" ) <EOL> @ property <EOL> def client ( self ) : <EOL> if self . _client is None : <EOL> self . _client = CmiClient ( self . get_cmi_hostname ( ) ) <EOL> return self . _client <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import django <EOL> django . setup ( ) <EOL> lib = HpcLibrary ( ) <EOL> print "<STR_LIT>" , lib . get_cmi_hostname ( ) <EOL> lib . client . cob . GetNewObjects ( ) <EOL> lib . client . onev . ListAll ( "<STR_LIT>" ) </s>
