<s> __version__ = '<STR_LIT>' <EOL> import re <EOL> import os <EOL> import random <EOL> import time <EOL> import string <EOL> import socket <EOL> import sys <EOL> import traceback <EOL> import StringIO <EOL> import eulogger <EOL> import hashlib <EOL> import types <EOL> import operator <EOL> import fcntl <EOL> import struct <EOL> import subprocess <EOL> import termios <EOL> from functools import wraps <EOL> class TimeoutFunctionException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Eutester ( object ) : <EOL> _EUTESTER_FORCE_ANSI_ESCAPE = False <EOL> _EUTESTER_NON_STANDARD_ANSI_SUPPORT = False <EOL> def __init__ ( self , credpath = None ) : <EOL> """<STR_LIT>""" <EOL> self . credpath = credpath <EOL> self . logger = eulogger . Eulogger ( identifier = "<STR_LIT>" ) <EOL> self . debug = self . logger . log . debug <EOL> self . critical = self . logger . log . critical <EOL> self . info = self . logger . log . info <EOL> self . fail_log = [ ] <EOL> self . running_log = self . logger . log <EOL> if self . credpath is not None : <EOL> self . debug ( "<STR_LIT>" + self . credpath ) <EOL> self . aws_access_key_id = self . get_access_key ( ) <EOL> self . aws_secret_access_key = self . get_secret_key ( ) <EOL> self . account_id = self . get_account_id ( ) <EOL> self . user_id = self . get_user_id ( ) <EOL> @ property <EOL> def ec2_certpath ( self ) : <EOL> try : <EOL> return self . parse_eucarc ( '<STR_LIT>' ) <EOL> except ValueError : <EOL> return None <EOL> @ property <EOL> def ec2_cert ( self ) : <EOL> certpath = self . ec2_certpath <EOL> if certpath and os . path . exists ( certpath ) : <EOL> out = self . local ( '<STR_LIT>' . format ( certpath ) ) <EOL> return str ( "<STR_LIT:\n>" . join ( out ) ) . strip ( ) <EOL> return None <EOL> @ property <EOL> def ec2_private_key_path ( self ) : <EOL> try : <EOL> return self . parse_eucarc ( '<STR_LIT>' ) <EOL> except ValueError : <EOL> return None <EOL> @ property <EOL> def ec2_private_key ( self ) : <EOL> keypath = self . ec2_private_key_path <EOL> if keypath and os . path . exists ( keypath ) : <EOL> out = self . local ( '<STR_LIT>' . format ( keypath ) ) <EOL> return "<STR_LIT:\n>" . join ( out ) <EOL> return None <EOL> def get_access_key ( self ) : <EOL> if not self . aws_access_key_id : <EOL> """<STR_LIT>""" <EOL> self . aws_access_key_id = self . parse_eucarc ( "<STR_LIT>" ) <EOL> return self . aws_access_key_id <EOL> def get_secret_key ( self ) : <EOL> if not self . aws_secret_access_key : <EOL> """<STR_LIT>""" <EOL> self . aws_secret_access_key = self . parse_eucarc ( "<STR_LIT>" ) <EOL> return self . aws_secret_access_key <EOL> def get_account_id ( self ) : <EOL> if not self . account_id : <EOL> """<STR_LIT>""" <EOL> self . account_id = self . parse_eucarc ( "<STR_LIT>" ) <EOL> return self . account_id <EOL> def get_user_id ( self ) : <EOL> if not self . user_id : <EOL> self . user_id = self . parse_eucarc ( "<STR_LIT>" ) <EOL> """<STR_LIT>""" <EOL> return self . user_id <EOL> def get_port ( self ) : <EOL> """<STR_LIT>""" <EOL> ec2_url = self . parse_eucarc ( "<STR_LIT>" ) <EOL> return ec2_url . split ( '<STR_LIT::>' ) [ <NUM_LIT:1> ] . split ( "<STR_LIT:/>" ) [ <NUM_LIT:0> ] <EOL> def parse_eucarc ( self , field ) : <EOL> if self . credpath is None : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> cmd = '<STR_LIT>' . format ( self . credpath , field ) <EOL> out = self . local ( cmd ) <EOL> if out [ <NUM_LIT:0> ] : <EOL> return out [ <NUM_LIT:0> ] <EOL> else : <EOL> if out : <EOL> out = "<STR_LIT:\n>" . join ( out ) <EOL> self . critical ( '<STR_LIT>' <EOL> . format ( field , cmd , out ) ) <EOL> try : <EOL> catcmd = '<STR_LIT>' . format ( self . credpath , field ) <EOL> catout = self . local ( catcmd ) <EOL> catout = "<STR_LIT:\n>" . join ( catout ) <EOL> except Exception , ce : <EOL> catout = "<STR_LIT>" . format ( catcmd , ce ) <EOL> self . critical ( "<STR_LIT>" <EOL> . format ( field , catcmd , catout ) ) <EOL> raise ValueError ( "<STR_LIT>" + field + "<STR_LIT>" ) <EOL> def handle_timeout ( self , signum , frame ) : <EOL> raise TimeoutFunctionException ( ) <EOL> def local ( self , cmd , shell = True ) : <EOL> """<STR_LIT>""" <EOL> args = cmd <EOL> process = subprocess . Popen ( args , stdout = subprocess . PIPE , stderr = subprocess . STDOUT , <EOL> bufsize = <NUM_LIT> , shell = shell ) <EOL> output , unused_err = process . communicate ( ) <EOL> retcode = process . poll ( ) <EOL> if retcode : <EOL> self . debug ( '<STR_LIT>' . format ( cmd , str ( output ) ) ) <EOL> error = subprocess . CalledProcessError ( retcode , cmd ) <EOL> error . output = output <EOL> raise error <EOL> return output . split ( "<STR_LIT:\n>" ) <EOL> def found ( self , command , regex ) : <EOL> """<STR_LIT>""" <EOL> result = self . local ( command ) <EOL> for line in result : <EOL> found = re . search ( regex , line ) <EOL> if found : <EOL> return True <EOL> return False <EOL> def ping ( self , address , poll_count = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> if re . search ( "<STR_LIT>" , address ) : <EOL> self . critical ( "<STR_LIT>" ) <EOL> return False <EOL> self . debug ( "<STR_LIT>" + address ) <EOL> for x in xrange ( <NUM_LIT:0> , poll_count ) : <EOL> try : <EOL> self . local ( "<STR_LIT>" + address ) <EOL> self . debug ( "<STR_LIT>" ) <EOL> return True <EOL> except subprocess . CalledProcessError as CPE : <EOL> self . debug ( '<STR_LIT>' + str ( CPE . output ) ) <EOL> self . debug ( '<STR_LIT>' <EOL> . format ( x , poll_count , str ( CPE ) ) ) <EOL> self . sleep ( <NUM_LIT:2> ) <EOL> self . critical ( "<STR_LIT>" ) <EOL> return False <EOL> @ classmethod <EOL> def markup ( cls , text , markups = [ <NUM_LIT:1> ] , resetvalue = "<STR_LIT>" , force = None , allow_nonstandard = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( markups , list ) : <EOL> markups = [ markups ] <EOL> if force is None : <EOL> force = os . environ . get ( '<STR_LIT>' , cls . _EUTESTER_FORCE_ANSI_ESCAPE ) <EOL> if str ( force ) . upper ( ) == '<STR_LIT>' : <EOL> force = True <EOL> else : <EOL> force = False <EOL> if allow_nonstandard is None : <EOL> allow_nonstandard = os . environ . get ( '<STR_LIT>' , <EOL> cls . _EUTESTER_NON_STANDARD_ANSI_SUPPORT ) <EOL> if str ( allow_nonstandard ) . upper ( ) == '<STR_LIT>' : <EOL> allow_nonstandard = True <EOL> else : <EOL> allow_nonstandard = False <EOL> if not force : <EOL> if not ( hasattr ( sys . stdout , '<STR_LIT>' ) and sys . stdout . isatty ( ) ) : <EOL> return text <EOL> if not allow_nonstandard : <EOL> newmarkups = [ ] <EOL> for markup in markups : <EOL> if markup > <NUM_LIT> : <EOL> newmarkups . append ( markup - <NUM_LIT> ) <EOL> else : <EOL> newmarkups . append ( markup ) <EOL> markups = newmarkups <EOL> lines = [ ] <EOL> markupvalues = "<STR_LIT:;>" . join ( str ( x ) for x in markups ) <EOL> for line in text . splitlines ( ) : <EOL> lines . append ( "<STR_LIT>" . format ( markupvalues , line ) ) <EOL> buf = "<STR_LIT:\n>" . join ( lines ) <EOL> if text . endswith ( '<STR_LIT:\n>' ) and not buf . endswith ( '<STR_LIT:\n>' ) : <EOL> buf += '<STR_LIT:\n>' <EOL> return buf <EOL> def scan_port_range ( self , ip , start , stop , timeout = <NUM_LIT:1> , tcp = True ) : <EOL> '''<STR_LIT>''' <EOL> ret = [ ] <EOL> for x in xrange ( start , stop + <NUM_LIT:1> ) : <EOL> try : <EOL> sys . stdout . write ( "<STR_LIT>" + str ( '<STR_LIT>' + str ( x ) ) ) <EOL> sys . stdout . flush ( ) <EOL> self . test_port_status ( ip , x , timeout = timeout , tcp = tcp , verbose = False ) <EOL> ret . append ( x ) <EOL> except socket . error , se : <EOL> pass <EOL> return ret <EOL> def test_port_status ( self , <EOL> ip , <EOL> port , <EOL> timeout = <NUM_LIT:5> , <EOL> tcp = True , <EOL> recv_size = <NUM_LIT:0> , <EOL> send_buf = None , <EOL> verbose = True ) : <EOL> '''<STR_LIT>''' <EOL> ret_buf = "<STR_LIT>" <EOL> if verbose : <EOL> debug = self . debug <EOL> else : <EOL> debug = lambda msg : None <EOL> debug ( '<STR_LIT>' + str ( ip ) + '<STR_LIT>' + str ( port ) + '<STR_LIT>' + str ( tcp ) ) <EOL> if tcp : <EOL> s = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> else : <EOL> s = socket . socket ( socket . AF_INET , socket . SOCK_DGRAM , socket . IPPROTO_UDP ) <EOL> s . settimeout ( timeout ) <EOL> s . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> try : <EOL> if tcp : <EOL> s . connect ( ( ip , port ) ) <EOL> else : <EOL> if send_buf is None : <EOL> send_buf = "<STR_LIT>" <EOL> if send_buf is not None : <EOL> s . sendto ( send_buf , ( ip , port ) ) <EOL> if recv_size : <EOL> ret_buf = s . recv ( recv_size ) <EOL> except socket . error , se : <EOL> debug ( '<STR_LIT>' + str ( se [ <NUM_LIT:0> ] ) ) <EOL> ecode = se [ <NUM_LIT:0> ] <EOL> if ecode == socket . errno . ECONNREFUSED : <EOL> debug ( "<STR_LIT>" ) <EOL> if ecode == socket . errno . ENETUNREACH : <EOL> debug ( "<STR_LIT>" ) <EOL> if ecode == socket . errno . ETIMEDOUT or ecode == "<STR_LIT>" : <EOL> debug ( "<STR_LIT>" + str ( ip ) + "<STR_LIT::>" + str ( port ) + "<STR_LIT>" ) <EOL> raise se <EOL> except socket . timeout , st : <EOL> debug ( '<STR_LIT>' ) <EOL> raise st <EOL> finally : <EOL> s . settimeout ( None ) <EOL> s . close ( ) <EOL> debug ( '<STR_LIT>' ) <EOL> return ret_buf <EOL> def grep ( self , string , list ) : <EOL> """<STR_LIT>""" <EOL> expr = re . compile ( string ) <EOL> return filter ( expr . search , list ) <EOL> def diff ( self , list1 , list2 ) : <EOL> """<STR_LIT>""" <EOL> return list ( set ( list1 ) - set ( list2 ) ) <EOL> def fail ( self , message ) : <EOL> self . critical ( message ) <EOL> self . fail_count += <NUM_LIT:1> <EOL> if self . exit_on_fail == <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" + str ( message ) ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def clear_fail_log ( self ) : <EOL> self . fail_log = [ ] <EOL> return <EOL> def get_exectuion_time ( self ) : <EOL> """<STR_LIT>""" <EOL> return time . time ( ) - self . start_time <EOL> def clear_fail_count ( self ) : <EOL> """<STR_LIT>""" <EOL> self . fail_count = <NUM_LIT:0> <EOL> def sleep ( self , seconds = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . debug ( "<STR_LIT>" + str ( seconds ) + "<STR_LIT>" ) <EOL> time . sleep ( seconds ) <EOL> @ staticmethod <EOL> def render_file_template ( src , dest , ** kwargs ) : <EOL> import jinja2 <EOL> with open ( src ) as sfile : <EOL> templ = jinja2 . Template ( sfile . read ( ) ) <EOL> with open ( dest , '<STR_LIT:w>' ) as dfile : <EOL> dfile . write ( templ . render ( kwargs ) ) <EOL> def id_generator ( self , size = <NUM_LIT:6> , chars = string . ascii_uppercase + string . ascii_lowercase + string . digits ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . join ( random . choice ( chars ) for x in range ( size ) ) <EOL> @ staticmethod <EOL> def get_terminal_size ( ) : <EOL> '''<STR_LIT>''' <EOL> h = <NUM_LIT:30> <EOL> w = <NUM_LIT> <EOL> try : <EOL> t_h , t_w = struct . unpack ( '<STR_LIT>' , fcntl . ioctl ( sys . stdout , <EOL> termios . TIOCGWINSZ , <EOL> '<STR_LIT>' ) ) <EOL> if t_h > <NUM_LIT:1> and t_w > <NUM_LIT:1> : <EOL> h = t_h <EOL> w = t_w <EOL> except : <EOL> pass <EOL> return ( h , w ) <EOL> @ staticmethod <EOL> def get_line ( length = None ) : <EOL> line = "<STR_LIT>" <EOL> if not length : <EOL> try : <EOL> length = Eutester . get_terminal_size ( ) [ <NUM_LIT:1> ] <EOL> if length <= <NUM_LIT:1> : <EOL> length = <NUM_LIT> <EOL> except : <EOL> length = <NUM_LIT> <EOL> for x in xrange ( <NUM_LIT:0> , int ( length ) ) : <EOL> line += "<STR_LIT:->" <EOL> return "<STR_LIT:\n>" + line + "<STR_LIT:\n>" <EOL> @ classmethod <EOL> def printinfo ( cls , func ) : <EOL> '''<STR_LIT>''' <EOL> @ wraps ( func ) <EOL> def methdecor ( * func_args , ** func_kwargs ) : <EOL> _args_dict = { } <EOL> try : <EOL> defaults = func . func_defaults <EOL> kw_count = len ( defaults or [ ] ) <EOL> selfobj = None <EOL> arg_count = func . func_code . co_argcount - kw_count <EOL> var_names = func . func_code . co_varnames [ : func . func_code . co_argcount ] <EOL> arg_names = var_names [ : arg_count ] <EOL> kw_names = var_names [ arg_count : func . func_code . co_argcount ] <EOL> kw_defaults = { } <EOL> for kw_name in kw_names : <EOL> kw_defaults [ kw_name ] = defaults [ kw_names . index ( kw_name ) ] <EOL> arg_string = '<STR_LIT>' <EOL> if '<STR_LIT>' in var_names and len ( func_args ) <= <NUM_LIT:1> : <EOL> func_args_empty = True <EOL> else : <EOL> func_args_empty = False <EOL> if ( not func_args_empty or func_kwargs ) and '<STR_LIT>' in kw_names : <EOL> if not '<STR_LIT>' in func_kwargs or not func_kwargs [ '<STR_LIT>' ] : <EOL> func_kwargs [ '<STR_LIT>' ] = { '<STR_LIT:args>' : func_args , <EOL> '<STR_LIT>' : func_kwargs } <EOL> for count , arg in enumerate ( func_args ) : <EOL> if count == <NUM_LIT:0> and var_names [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> arg_string += '<STR_LIT>' <EOL> selfobj = arg <EOL> elif count >= arg_count : <EOL> kw_defaults [ var_names [ count ] ] = arg <EOL> else : <EOL> arg_string += '<STR_LIT:U+002CU+0020>' <EOL> arg_string += str ( arg_names [ count ] ) + '<STR_LIT:=>' + str ( arg ) <EOL> kw_string = "<STR_LIT>" <EOL> for kw in kw_names : <EOL> kw_string += '<STR_LIT:U+002CU+0020>' + str ( kw ) + '<STR_LIT:=>' <EOL> if kw in func_kwargs : <EOL> kw_string += str ( func_kwargs [ kw ] ) <EOL> else : <EOL> kw_string += str ( kw_defaults [ kw ] ) <EOL> debugstring = '<STR_LIT>' + str ( os . path . basename ( func . func_code . co_filename ) ) + "<STR_LIT::>" + str ( func . func_code . co_firstlineno ) + "<STR_LIT>" + str ( func . func_name ) + '<STR_LIT:(>' + arg_string + kw_string + '<STR_LIT:)>' <EOL> debugmethod = None <EOL> if selfobj and hasattr ( selfobj , '<STR_LIT>' ) : <EOL> debug = getattr ( selfobj , '<STR_LIT>' ) <EOL> if isinstance ( debug , types . MethodType ) : <EOL> debugmethod = debug <EOL> if debugmethod : <EOL> debugmethod ( debugstring ) <EOL> else : <EOL> print debugstring <EOL> except Exception , e : <EOL> print Eutester . get_traceback ( ) <EOL> print '<STR_LIT>' + str ( e ) <EOL> return func ( * func_args , ** func_kwargs ) <EOL> return methdecor <EOL> def wait_for_result ( self , <EOL> callback , <EOL> result , <EOL> timeout = <NUM_LIT> , <EOL> poll_wait = <NUM_LIT:10> , <EOL> oper = operator . eq , <EOL> allowed_exception_types = None , <EOL> ** callback_kwargs ) : <EOL> """<STR_LIT>""" <EOL> allowed_exception_types = allowed_exception_types or [ ] <EOL> self . debug ( "<STR_LIT>" + str ( callback . func_name ) + "<STR_LIT>" + str ( result ) ) <EOL> start = time . time ( ) <EOL> elapsed = <NUM_LIT:0> <EOL> current_state = callback ( ** callback_kwargs ) <EOL> while ( elapsed < timeout and not oper ( current_state , result ) ) : <EOL> self . debug ( str ( callback . func_name ) + '<STR_LIT>' + str ( current_state ) + '<STR_LIT>' <EOL> + str ( elapsed / <NUM_LIT> ) + "<STR_LIT>" + str ( elapsed % <NUM_LIT> ) + "<STR_LIT>" ) <EOL> self . sleep ( poll_wait ) <EOL> try : <EOL> current_state = callback ( ** callback_kwargs ) <EOL> except allowed_exception_types as AE : <EOL> self . debug ( '<STR_LIT>' + str ( AE ) ) <EOL> pass <EOL> elapsed = int ( time . time ( ) - start ) <EOL> self . debug ( str ( callback . func_name ) + '<STR_LIT>' + str ( current_state ) + '<STR_LIT>' <EOL> + str ( elapsed / <NUM_LIT> ) + "<STR_LIT>" + str ( elapsed % <NUM_LIT> ) + "<STR_LIT>" ) <EOL> if not oper ( current_state , result ) : <EOL> raise WaitForResultException ( str ( callback . func_name ) + "<STR_LIT>" + str ( operator . ne . __name__ ) + <EOL> "<STR_LIT:(>" + str ( result ) + "<STR_LIT>" + str ( elapsed ) ) <EOL> return current_state <EOL> def get_md5_for_file ( self , filepath , machine = None ) : <EOL> if machine : <EOL> machinename = machine . hostname <EOL> out = machine . sys ( '<STR_LIT>' . format ( filepath ) , code = <NUM_LIT:0> ) <EOL> md5string = str ( out [ <NUM_LIT:0> ] ) . split ( ) [ <NUM_LIT:0> ] <EOL> else : <EOL> machinename = '<STR_LIT>' <EOL> md5 = hashlib . md5 ( ) <EOL> length = md5 . block_size * <NUM_LIT> <EOL> with open ( filepath , "<STR_LIT:rb>" ) as f : <EOL> while True : <EOL> buf = f . read ( length ) <EOL> if not buf : <EOL> break <EOL> md5 . update ( buf ) <EOL> md5string = str ( md5 . hexdigest ( ) ) <EOL> self . debug ( '<STR_LIT>' . format ( filepath , machinename , md5string ) ) <EOL> return md5string <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> def __str__ ( self ) : <EOL> return '<STR_LIT>' <EOL> class WaitForResultException ( Exception ) : <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> def __str__ ( self ) : <EOL> return repr ( self . value ) </s>
