<s> from __future__ import print_function , unicode_literals <EOL> import sys <EOL> import time <EOL> import cPickle as pickle <EOL> import logging . config <EOL> import weblab . admin . bot . wl_process as WebLabProcess <EOL> import weblab . admin . bot . data as Data <EOL> import voodoo . mapper as mapper <EOL> class BotLauncher ( object ) : <EOL> def __init__ ( self , configuration , host , pickle_file_name , logging_cfg_file_name , scenario , iterations , options , verbose ) : <EOL> super ( BotLauncher , self ) . __init__ ( ) <EOL> self . verbose = verbose <EOL> self . options = options <EOL> if isinstance ( configuration , basestring ) : <EOL> self . launch_files = [ configuration ] <EOL> else : <EOL> self . launch_files = configuration <EOL> self . host = host <EOL> self . pickle_file_name = pickle_file_name <EOL> self . logging_cfg_file_name = logging_cfg_file_name <EOL> self . scenario = scenario <EOL> self . iterations = iterations <EOL> self . results = [ ] <EOL> self . _set_logging_cfg ( ) <EOL> self . _set_users ( ) <EOL> def _set_logging_cfg ( self ) : <EOL> logging . config . fileConfig ( self . logging_cfg_file_name ) <EOL> def _set_users ( self ) : <EOL> self . users = len ( self . scenario ) <EOL> def _add_exception ( self , exceptions_dict , ( exception , trace ) ) : <EOL> """<STR_LIT>""" <EOL> if exception . __class__ . __name__ in exceptions_dict : <EOL> exceptions_dict [ exception . __class__ . __name__ ] . add_instance ( ( exception , trace ) ) <EOL> else : <EOL> exceptions_dict [ exception . __class__ . __name__ ] = Data . BotError ( ( exception , trace ) ) <EOL> def _print ( self , message ) : <EOL> sys . stdout . write ( message ) <EOL> sys . stdout . flush ( ) <EOL> def _dump_results ( self ) : <EOL> results = self . get_results ( ) <EOL> pickle_file = open ( self . pickle_file_name , '<STR_LIT:w>' ) <EOL> pickle . dump ( results , pickle_file ) <EOL> def _load_results ( self ) : <EOL> pickle . load ( open ( self . pickle_file_name , '<STR_LIT:r>' ) ) <EOL> def start ( self ) : <EOL> print ( "<STR_LIT>" % self . iterations ) <EOL> self . bot_trial = self . _launch_trial ( ) <EOL> print ( "<STR_LIT>" , time . asctime ( ) ) <EOL> self . bot_trial = mapper . remove_unpickables ( self . bot_trial ) <EOL> if self . pickle_file_name is not None : <EOL> try : <EOL> print ( "<STR_LIT>" , time . asctime ( ) ) <EOL> self . _dump_results ( ) <EOL> print ( "<STR_LIT>" , time . asctime ( ) ) <EOL> except Exception as e : <EOL> print ( "<STR_LIT>" % ( self . pickle_file_name , e ) ) <EOL> def _launch_trial ( self ) : <EOL> iterations = [ ] <EOL> for i in range ( self . iterations ) : <EOL> self . _print ( "<STR_LIT>" % i ) <EOL> bot_iteration = self . _launch_iteration ( ) <EOL> self . _print ( "<STR_LIT>" % len ( bot_iteration . exceptions ) ) <EOL> iterations . append ( bot_iteration ) <EOL> self . _print ( "<STR_LIT:\n>" ) <EOL> return Data . BotTrial ( iterations ) <EOL> def _start_processes ( self ) : <EOL> if self . options [ '<STR_LIT>' ] : <EOL> time . sleep ( <NUM_LIT:10> ) <EOL> return <EOL> started_processes = [ ] <EOL> try : <EOL> for launch_file in self . launch_files : <EOL> if self . verbose : <EOL> print ( "<STR_LIT>" % launch_file ) <EOL> weblab_process = WebLabProcess . WebLabProcess ( launch_file , self . host , self . options , verbose = self . verbose ) <EOL> weblab_process . start ( ) <EOL> if self . verbose : <EOL> print ( "<STR_LIT>" % launch_file ) <EOL> started_processes . append ( weblab_process ) <EOL> if len ( started_processes ) > <NUM_LIT:1> : <EOL> started_processes [ <NUM_LIT:0> ] . step_wait ( ) <EOL> for weblab_process in started_processes : <EOL> weblab_process . wait_for_process_started ( ) <EOL> if len ( started_processes ) > <NUM_LIT:1> : <EOL> started_processes [ <NUM_LIT:0> ] . step_started_wait ( ) <EOL> except : <EOL> for started_process in started_processes : <EOL> if self . verbose : <EOL> print ( "<STR_LIT>" % started_process ) <EOL> started_process . shutdown ( ) <EOL> raise <EOL> return started_processes <EOL> def _stop_processes ( self , started_processes ) : <EOL> if self . options [ '<STR_LIT>' ] : <EOL> return '<STR_LIT>' , '<STR_LIT>' <EOL> complete_out = '<STR_LIT>' <EOL> complete_err = '<STR_LIT>' <EOL> for started_process in started_processes : <EOL> started_process . shutdown ( ) <EOL> complete_out += started_process . out <EOL> complete_err += started_process . err <EOL> return complete_out , complete_err <EOL> def _launch_iteration ( self ) : <EOL> started_processes = self . _start_processes ( ) <EOL> if self . verbose : <EOL> print ( "<STR_LIT>" ) <EOL> try : <EOL> botusers = [ ] <EOL> for botuser_creator_name , botuser_creator in self . scenario : <EOL> botusers . append ( botuser_creator ( ) ) <EOL> begin_time = time . time ( ) <EOL> for botuser in botusers : <EOL> botuser . start ( ) <EOL> waiting_botusers = botusers [ : ] <EOL> while len ( waiting_botusers ) > <NUM_LIT:0> : <EOL> number_before = len ( waiting_botusers ) <EOL> waiting_botusers = [ botuser for botuser in waiting_botusers if botuser . isAlive ( ) ] <EOL> for _ in range ( number_before - len ( waiting_botusers ) ) : <EOL> self . _print ( "<STR_LIT:.>" ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> iteration_time = time . time ( ) - begin_time <EOL> finally : <EOL> complete_out , complete_err = self . _stop_processes ( started_processes ) <EOL> botuser_routes = [ botuser . route for botuser in botusers ] <EOL> routes = { } <EOL> for route in botuser_routes : <EOL> if route in routes : <EOL> routes [ route ] = routes [ route ] + <NUM_LIT:1> <EOL> else : <EOL> routes [ route ] = <NUM_LIT:1> <EOL> self . _print ( "<STR_LIT>" % routes ) <EOL> exceptions = { } <EOL> for botuser in botusers : <EOL> for exception , trace in botuser . get_exceptions ( ) : <EOL> self . _add_exception ( exceptions , ( exception , trace ) ) <EOL> return Data . BotIteration ( iteration_time , exceptions , botusers , complete_out , complete_err ) <EOL> def len ( self ) : <EOL> return self . users / self . step <EOL> def get_results ( self ) : <EOL> return self . bot_trial </s>
