<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import time <EOL> import select <EOL> import ctypes <EOL> class BlobReturnCodes ( object ) : <EOL> """<STR_LIT>""" <EOL> SUCCESS = <NUM_LIT:0> <EOL> class HpIloInitialError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class HpIloReadError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class HpIloSendReceiveError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class HpIloChifPacketExchangeError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class HpIlo ( object ) : <EOL> """<STR_LIT>""" <EOL> if os . name != '<STR_LIT>' : <EOL> MAX_CCB = '<STR_LIT>' <EOL> CHANNEL = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> if os . name == '<STR_LIT>' : <EOL> fhandle = ctypes . c_void_p ( ) <EOL> self . libhandle = ctypes . windll . kernel32 . LoadLibraryA ( '<STR_LIT>' ) <EOL> self . dll = ctypes . WinDLL ( '<STR_LIT>' , handle = self . libhandle ) <EOL> self . dll . ChifInitialize ( None ) <EOL> self . dll . ChifCreate . argtypes = [ ctypes . c_void_p ] <EOL> self . dll . ChifCreate . restype = ctypes . c_uint32 <EOL> try : <EOL> status = self . dll . ChifCreate ( ctypes . byref ( fhandle ) ) <EOL> if status != BlobReturnCodes . SUCCESS : <EOL> raise HpIloInitialError ( "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" % status ) <EOL> self . fhandle = fhandle <EOL> self . dll . ChifSetRecvTimeout ( self . fhandle , <NUM_LIT> ) <EOL> except Exception , excp : <EOL> self . unload ( ) <EOL> raise excp <EOL> else : <EOL> if os . path . exists ( HpIlo . MAX_CCB ) : <EOL> fhandle = open ( HpIlo . MAX_CCB , '<STR_LIT:r>' ) <EOL> for line in fhandle : <EOL> start = int ( line ) - <NUM_LIT:1> <EOL> fhandle . close ( ) <EOL> else : <EOL> start = <NUM_LIT:7> <EOL> self . file = HpIlo . CHANNEL + str ( start ) <EOL> self . cmd = None <EOL> self . svc = None <EOL> self . response = None <EOL> while True : <EOL> try : <EOL> self . fhandle = os . open ( self . file , os . O_NONBLOCK | os . O_EXCL | os . O_RDWR , <NUM_LIT:0> <NUM_LIT> ) <EOL> self . len = <NUM_LIT:0> <EOL> self . seq = <NUM_LIT:0> <EOL> return <EOL> except Exception : <EOL> start = start - <NUM_LIT:1> <EOL> self . file = HpIlo . CHANNEL + str ( start ) <EOL> if start < <NUM_LIT:0> : <EOL> raise HpIloInitialError ( "<STR_LIT>" "<STR_LIT>" ) <EOL> def write_raw ( self , data ) : <EOL> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> return os . write ( self . fhandle , data ) <EOL> def read_raw ( self , timeout = <NUM_LIT:5> ) : <EOL> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> try : <EOL> pkt = bytearray ( ) <EOL> status = select . select ( [ self . fhandle ] , [ ] , [ ] , timeout ) <EOL> if status != ( [ self . fhandle ] , [ ] , [ ] ) and timeout > <NUM_LIT:0> : <EOL> raise HpIloReadError ( "<STR_LIT>" ) <EOL> if status != ( [ self . fhandle ] , [ ] , [ ] ) and timeout == <NUM_LIT:0> : <EOL> return pkt <EOL> pkt . extend ( os . read ( self . fhandle , <NUM_LIT> ) ) <EOL> self . response = pkt [ <NUM_LIT:4> ] + <NUM_LIT> * pkt [ <NUM_LIT:5> ] <EOL> return pkt <EOL> except Exception , excp : <EOL> raise HpIloReadError ( "<STR_LIT>" % ( excp , sys . exc_info ( ) [ <NUM_LIT:0> ] ) ) <EOL> def chif_packet_exchange ( self , data , datarecv ) : <EOL> """<STR_LIT>""" <EOL> buff = "<STR_LIT>" . join ( map ( chr , data ) ) <EOL> buff = ctypes . create_string_buffer ( "<STR_LIT>" . join ( map ( chr , data ) ) ) <EOL> error = ctypes . c_uint ( ) <EOL> recbuff = ctypes . create_string_buffer ( datarecv ) <EOL> error = self . dll . ChifPacketExchange ( self . fhandle , ctypes . byref ( buff ) , ctypes . byref ( recbuff ) , datarecv ) <EOL> if error != BlobReturnCodes . SUCCESS : <EOL> self . close ( ) <EOL> self . unload ( ) <EOL> raise HpIloChifPacketExchangeError ( "<STR_LIT>" "<STR_LIT>" % error ) <EOL> pkt = bytearray ( ) <EOL> if datarecv is None : <EOL> pkt . extend ( recbuff ) <EOL> else : <EOL> pkt . extend ( recbuff [ : datarecv ] ) <EOL> return pkt <EOL> def send_receive_raw ( self , data , retries = <NUM_LIT:3> , datarecv = None ) : <EOL> """<STR_LIT>""" <EOL> tries = <NUM_LIT:0> <EOL> while tries < retries : <EOL> try : <EOL> if os . name == '<STR_LIT>' : <EOL> resp = self . chif_packet_exchange ( data , datarecv ) <EOL> else : <EOL> retlen = self . write_raw ( data ) <EOL> if retlen != len ( data ) : <EOL> raise ValueError ( ) <EOL> resp = self . read_raw ( <NUM_LIT> ) <EOL> return resp <EOL> except Exception : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> if tries == ( retries - <NUM_LIT:1> ) : <EOL> raise <EOL> tries += <NUM_LIT:1> <EOL> raise HpIloSendReceiveError ( "<STR_LIT>" ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if os . name == '<STR_LIT>' : <EOL> self . dll . ChifClose ( self . fhandle ) <EOL> else : <EOL> os . close ( self . fhandle ) <EOL> except Exception : <EOL> pass <EOL> def unload ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> del self . dll <EOL> ctypes . windll . kernel32 . FreeLibrary ( self . libhandle ) <EOL> except Exception : <EOL> pass <EOL> def __del__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . close ( ) <EOL> if os . name == '<STR_LIT>' : <EOL> self . unload ( ) </s>
