<s> __author__ = '<STR_LIT>' <EOL> from winrm . protocol import Protocol <EOL> from winrm . exceptions import WinRMTransportError <EOL> from isodate . isoduration import duration_isoformat <EOL> from datetime import timedelta <EOL> import StringIO <EOL> import traceback <EOL> import socket <EOL> import copy <EOL> import sys <EOL> import time <EOL> import re <EOL> class Winrm_Connection : <EOL> def __init__ ( self , <EOL> hostname , <EOL> username , <EOL> password , <EOL> port = <NUM_LIT> , <EOL> protocol = '<STR_LIT:http>' , <EOL> transport = '<STR_LIT>' , <EOL> default_command_timeout = <NUM_LIT> , <EOL> url = None , <EOL> debug_method = None , <EOL> verbose = True ) : <EOL> self . debug_method = debug_method <EOL> self . hostname = hostname <EOL> self . username = username <EOL> self . password = password <EOL> self . port = int ( port ) <EOL> self . protocol = protocol <EOL> self . transport = transport <EOL> self . default_command_timeout = default_command_timeout <EOL> self . url = url or str ( protocol ) + "<STR_LIT>" + str ( hostname ) + "<STR_LIT::>" + str ( port ) + "<STR_LIT>" <EOL> self . winproto = self . get_proto ( ) <EOL> self . shell_id = None <EOL> self . command_id = None <EOL> self . last_used = None <EOL> self . verbose = verbose <EOL> def get_proto ( self ) : <EOL> self . debug ( '<STR_LIT>' + str ( self . hostname ) + "<STR_LIT::>" + str ( self . port ) + "<STR_LIT>" + str ( self . username ) + '<STR_LIT>' + str ( self . password ) ) <EOL> winproto = Protocol ( endpoint = self . url , transport = self . transport , username = self . username , password = self . password ) <EOL> return winproto <EOL> def convert_iso8601_timeout ( self , timeout ) : <EOL> return duration_isoformat ( timedelta ( int ( timeout ) ) ) <EOL> def debug ( self , msg ) : <EOL> if self . debug_method : <EOL> self . debug_method ( msg ) <EOL> else : <EOL> print ( msg ) <EOL> def reset_shell ( self , timeout = None , retries = <NUM_LIT:5> ) : <EOL> retry = <NUM_LIT:0> <EOL> tb = "<STR_LIT>" <EOL> e = None <EOL> self . close_shell ( ) <EOL> timeout = timeout or self . default_command_timeout <EOL> self . winproto . transport . timeout = timeout <EOL> while retry < retries : <EOL> retry += <NUM_LIT:1> <EOL> try : <EOL> self . shell_id = self . winproto . open_shell ( ) <EOL> return self . shell_id <EOL> except WinRMTransportError , wte : <EOL> print "<STR_LIT>" + str ( retry ) + "<STR_LIT:/>" + str ( retries ) + "<STR_LIT>" + str ( wte ) <EOL> if retry < retries : <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> except Exception , e : <EOL> tb = self . get_traceback ( ) <EOL> errmsg = "<STR_LIT>" + str ( e ) <EOL> self . debug ( "<STR_LIT>" + str ( e ) ) <EOL> self . debug ( str ( tb ) ) <EOL> raise Exception ( '<STR_LIT>' + str ( self . url ) + str ( e ) ) <EOL> def cmd ( self , command , console_mode_stdin = True , skip_cmd_shell = False , timeout = None , verbose = None ) : <EOL> errmsg = "<STR_LIT>" <EOL> if verbose is None : <EOL> verbose = self . verbose <EOL> orig_cmd = copy . copy ( command ) <EOL> arguments = command . split ( '<STR_LIT:U+0020>' ) <EOL> command = arguments . pop ( <NUM_LIT:0> ) <EOL> self . command_id = None <EOL> self . reset_shell ( timeout = timeout ) <EOL> try : <EOL> self . command_id = self . winproto . run_command ( self . shell_id , <EOL> command , <EOL> arguments = arguments , <EOL> console_mode_stdin = console_mode_stdin , <EOL> skip_cmd_shell = skip_cmd_shell ) <EOL> self . debug ( '<STR_LIT>' + str ( timeout ) + '<STR_LIT>' + str ( orig_cmd ) ) <EOL> if timeout is not None : <EOL> sockdefault = socket . getdefaulttimeout ( ) <EOL> socket . setdefaulttimeout ( timeout ) <EOL> stdout , stderr , statuscode = self . get_timed_command_output ( self . shell_id , self . command_id , active_timeout = timeout ) <EOL> else : <EOL> stdout , stderr , statuscode = self . winproto . get_command_output ( self . shell_id , self . command_id ) <EOL> self . debug ( '<STR_LIT>' + str ( orig_cmd ) + '<STR_LIT>' ) <EOL> except WinRMTransportError as wte : <EOL> errmsg = str ( wte ) <EOL> except CommandTimeoutException as cte : <EOL> self . debug ( str ( cte ) ) <EOL> errmsg = '<STR_LIT>' <EOL> finally : <EOL> try : <EOL> if timeout is not None : <EOL> socket . setdefaulttimeout ( sockdefault ) <EOL> self . winproto . cleanup_command ( self . shell_id , self . command_id ) <EOL> except : pass <EOL> self . close_shell ( ) <EOL> if errmsg : <EOL> if re . search ( '<STR_LIT>' , errmsg , re . IGNORECASE ) : <EOL> raise CommandTimeoutException ( '<STR_LIT>' + <EOL> str ( self . winproto . transport . timeout ) + <EOL> '<STR_LIT>' + str ( orig_cmd ) ) <EOL> else : <EOL> raise Exception ( errmsg ) <EOL> if verbose : <EOL> self . debug ( "<STR_LIT:\n>" + str ( stdout ) + "<STR_LIT:\n>" + str ( stderr ) ) <EOL> return { '<STR_LIT>' : stdout , '<STR_LIT>' : stderr , '<STR_LIT>' : statuscode } <EOL> def get_timed_command_output ( self , shell_id , command_id , active_timeout = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> stdout_buffer , stderr_buffer = [ ] , [ ] <EOL> command_done = False <EOL> start = time . time ( ) <EOL> while not command_done : <EOL> elapsed = time . time ( ) - start <EOL> if active_timeout and ( elapsed > active_timeout ) : <EOL> raise CommandTimeoutException ( '<STR_LIT>' + str ( elapsed ) ) <EOL> stdout , stderr , return_code , command_done = self . winproto . _raw_get_command_output ( shell_id , command_id ) <EOL> stdout_buffer . append ( stdout ) <EOL> stderr_buffer . append ( stderr ) <EOL> return '<STR_LIT>' . join ( stdout_buffer ) , '<STR_LIT>' . join ( stderr_buffer ) , return_code <EOL> def close_shell ( self ) : <EOL> if self . shell_id : <EOL> self . winproto . close_shell ( self . shell_id ) <EOL> self . shell_id = None <EOL> def sys ( self , command , include_stderr = False , listformat = True , carriage_return = False , timeout = None , code = None , verbose = None ) : <EOL> ret = [ ] <EOL> if verbose is None : <EOL> verbose = self . verbose <EOL> output = self . cmd ( command , timeout = timeout , verbose = verbose ) <EOL> if code is not None and output [ '<STR_LIT>' ] != code : <EOL> raise CommandExitCodeException ( '<STR_LIT>' + str ( command ) + '<STR_LIT>' <EOL> + str ( output [ '<STR_LIT>' ] ) <EOL> + "<STR_LIT>" + str ( output [ '<STR_LIT>' ] ) <EOL> + "<STR_LIT>" + str ( output [ '<STR_LIT>' ] ) ) <EOL> ret = output [ '<STR_LIT>' ] <EOL> if ret : <EOL> if not carriage_return : <EOL> ret = ret . replace ( '<STR_LIT:\r>' , '<STR_LIT>' ) <EOL> if listformat : <EOL> ret = ret . splitlines ( ) <EOL> if include_stderr : <EOL> ret = ret . extend ( output [ '<STR_LIT>' ] . splitlines ( ) ) <EOL> return ret <EOL> @ classmethod <EOL> def get_traceback ( cls ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> out = StringIO . StringIO ( ) <EOL> traceback . print_exception ( * sys . exc_info ( ) , file = out ) <EOL> out . seek ( <NUM_LIT:0> ) <EOL> buf = out . read ( ) <EOL> except Exception , e : <EOL> buf = "<STR_LIT>" + str ( e ) <EOL> return str ( buf ) <EOL> class CommandExitCodeException ( Exception ) : <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> def __str__ ( self ) : <EOL> return repr ( self . value ) <EOL> class CommandTimeoutException ( Exception ) : <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> def __str__ ( self ) : <EOL> return repr ( self . value ) </s>
