<s> import os <EOL> import signal <EOL> import shutil <EOL> import subprocess <EOL> import logging <EOL> import re <EOL> from wlauto import settings , Instrument , Parameter , ResourceGetter , GetterPriority , File <EOL> from wlauto . exceptions import InstrumentError , DeviceError , ResourceError <EOL> from wlauto . utils . misc import ensure_file_directory_exists as _f , which <EOL> from wlauto . utils . types import boolean <EOL> from wlauto . utils . log import StreamLogger , LogWriter , LineLogWriter <EOL> SESSION_TEXT_TEMPLATE = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> VERSION_REGEX = re . compile ( r'<STR_LIT>' ) <EOL> class StreamlineResourceGetter ( ResourceGetter ) : <EOL> name = '<STR_LIT>' <EOL> resource_type = '<STR_LIT:file>' <EOL> priority = GetterPriority . environment + <NUM_LIT:1> <EOL> dependencies_directory = os . path . join ( settings . dependencies_directory , '<STR_LIT>' ) <EOL> old_dependencies_directory = os . path . join ( settings . environment_root , '<STR_LIT>' ) <EOL> def get ( self , resource , ** kwargs ) : <EOL> if resource . owner . name != '<STR_LIT>' : <EOL> return None <EOL> test_path = _f ( os . path . join ( self . dependencies_directory , resource . path ) ) <EOL> if os . path . isfile ( test_path ) : <EOL> return test_path <EOL> test_path = _f ( os . path . join ( self . old_dependencies_directory , resource . path ) ) <EOL> if os . path . isfile ( test_path ) : <EOL> return test_path <EOL> def _instantiate ( resolver ) : <EOL> return StreamlineResourceGetter ( resolver ) <EOL> class StreamlineInstrument ( Instrument ) : <EOL> name = '<STR_LIT>' <EOL> description = """<STR_LIT>""" <EOL> parameters = [ <EOL> Parameter ( '<STR_LIT:port>' , default = '<STR_LIT>' , <EOL> description = '<STR_LIT>' ) , <EOL> Parameter ( '<STR_LIT>' , default = None , <EOL> description = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> Parameter ( '<STR_LIT>' , kind = boolean , default = False , global_alias = '<STR_LIT>' , <EOL> description = '<STR_LIT>' ) , <EOL> Parameter ( '<STR_LIT>' , kind = str , default = '<STR_LIT>' , <EOL> description = '<STR_LIT>' ) , <EOL> ] <EOL> daemon = '<STR_LIT>' <EOL> driver = '<STR_LIT>' <EOL> configuration_file_name = '<STR_LIT>' <EOL> def __init__ ( self , device , ** kwargs ) : <EOL> super ( StreamlineInstrument , self ) . __init__ ( device , ** kwargs ) <EOL> self . streamline = None <EOL> self . session_file = None <EOL> self . capture_file = None <EOL> self . analysis_file = None <EOL> self . report_file = None <EOL> self . configuration_file = None <EOL> self . on_device_config = None <EOL> self . daemon_process = None <EOL> self . resource_getter = None <EOL> self . host_daemon_file = None <EOL> self . host_driver_file = None <EOL> self . device_driver_file = None <EOL> self . _check_has_valid_display ( ) <EOL> def validate ( self ) : <EOL> if not which ( '<STR_LIT>' ) : <EOL> raise InstrumentError ( '<STR_LIT>' ) <EOL> p = subprocess . Popen ( '<STR_LIT>' , stdout = subprocess . PIPE , shell = True ) <EOL> out , _ = p . communicate ( ) <EOL> match = VERSION_REGEX . search ( out ) <EOL> if not match : <EOL> raise InstrumentError ( '<STR_LIT>' ) <EOL> version_tuple = tuple ( map ( int , match . group ( <NUM_LIT:1> ) . split ( '<STR_LIT:.>' ) ) ) <EOL> if version_tuple < ( <NUM_LIT:5> , <NUM_LIT> ) : <EOL> raise InstrumentError ( '<STR_LIT>' . format ( match . group ( <NUM_LIT:1> ) ) ) <EOL> def initialize ( self , context ) : <EOL> self . resource_getter = _instantiate ( context . resolver ) <EOL> self . resource_getter . register ( ) <EOL> try : <EOL> self . host_daemon_file = context . resolver . get ( File ( self , self . daemon ) ) <EOL> self . logger . debug ( '<STR_LIT>' . format ( self . host_daemon_file ) ) <EOL> self . device . killall ( self . daemon ) <EOL> self . device . install ( self . host_daemon_file ) <EOL> except ResourceError : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> try : <EOL> self . host_driver_file = context . resolver . get ( File ( self , self . driver ) ) <EOL> self . logger . debug ( '<STR_LIT>' . format ( self . host_driver_file ) ) <EOL> self . device_driver_file = self . device . install ( self . host_driver_file ) <EOL> except ResourceError : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> try : <EOL> self . configuration_file = ( os . path . expanduser ( self . configxml or '<STR_LIT>' ) or <EOL> context . resolver . get ( File ( self , self . configuration_file_name ) ) ) <EOL> self . logger . debug ( '<STR_LIT>' . format ( self . configuration_file ) ) <EOL> self . on_device_config = self . device . path . join ( self . device . working_directory , '<STR_LIT>' ) <EOL> shutil . copy ( self . configuration_file , settings . meta_directory ) <EOL> except ResourceError : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> caiman_path = subprocess . check_output ( '<STR_LIT>' , shell = True ) . strip ( ) <EOL> self . session_file = os . path . join ( context . host_working_directory , '<STR_LIT>' ) <EOL> with open ( self . session_file , '<STR_LIT:w>' ) as wfh : <EOL> if self . device . platform == "<STR_LIT>" : <EOL> wfh . write ( SESSION_TEXT_TEMPLATE . format ( '<STR_LIT:127.0.0.1>' , self . port , caiman_path ) ) <EOL> else : <EOL> wfh . write ( SESSION_TEXT_TEMPLATE . format ( self . device . host , self . port , caiman_path ) ) <EOL> if self . configuration_file : <EOL> self . device . push_file ( self . configuration_file , self . on_device_config ) <EOL> self . _initialize_daemon ( ) <EOL> def setup ( self , context ) : <EOL> self . capture_file = _f ( os . path . join ( context . output_directory , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . report_file = _f ( os . path . join ( context . output_directory , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def start ( self , context ) : <EOL> command = [ '<STR_LIT>' , '<STR_LIT>' , self . session_file , '<STR_LIT>' , self . capture_file ] <EOL> self . streamline = subprocess . Popen ( command , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE , <EOL> stdin = subprocess . PIPE , <EOL> preexec_fn = os . setpgrp ) <EOL> outlogger = StreamLogger ( '<STR_LIT>' , self . streamline . stdout , klass = LineLogWriter ) <EOL> errlogger = StreamLogger ( '<STR_LIT>' , self . streamline . stderr , klass = LineLogWriter ) <EOL> outlogger . start ( ) <EOL> errlogger . start ( ) <EOL> def stop ( self , context ) : <EOL> os . killpg ( self . streamline . pid , signal . SIGTERM ) <EOL> def update_result ( self , context ) : <EOL> if self . report : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> command = [ '<STR_LIT>' , '<STR_LIT>' , self . capture_file , '<STR_LIT>' , self . report_file ] <EOL> command += self . report_options . split ( ) <EOL> _run_streamline_command ( command ) <EOL> context . add_artifact ( '<STR_LIT>' , self . report_file , '<STR_LIT:data>' ) <EOL> def teardown ( self , context ) : <EOL> self . _kill_daemon ( ) <EOL> self . device . delete_file ( self . on_device_config ) <EOL> def _check_has_valid_display ( self ) : <EOL> reason = None <EOL> if os . name == '<STR_LIT>' and not os . getenv ( '<STR_LIT>' ) : <EOL> reason = '<STR_LIT>' <EOL> else : <EOL> p = subprocess . Popen ( '<STR_LIT>' , stdout = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> _ , error = p . communicate ( ) <EOL> if p . returncode : <EOL> reason = '<STR_LIT>' . format ( error . strip ( ) ) <EOL> if reason : <EOL> raise InstrumentError ( '<STR_LIT>' . format ( reason ) ) <EOL> def _initialize_daemon ( self ) : <EOL> if self . device_driver_file : <EOL> try : <EOL> self . device . execute ( '<STR_LIT>' . format ( self . device_driver_file ) ) <EOL> except DeviceError , e : <EOL> if '<STR_LIT>' not in e . message : <EOL> raise <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> self . _start_daemon ( ) <EOL> if self . device . platform == "<STR_LIT>" : <EOL> port_spec = '<STR_LIT>' . format ( self . port ) <EOL> self . device . forward_port ( port_spec , port_spec ) <EOL> def _start_daemon ( self ) : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> self . device . killall ( '<STR_LIT>' , as_root = True ) <EOL> if self . configuration_file : <EOL> command = '<STR_LIT>' . format ( self . daemon , self . on_device_config ) <EOL> else : <EOL> command = '<STR_LIT:{}>' . format ( self . daemon ) <EOL> self . daemon_process = self . device . execute ( command , as_root = True , background = True ) <EOL> outlogger = StreamLogger ( '<STR_LIT>' , self . daemon_process . stdout ) <EOL> errlogger = StreamLogger ( '<STR_LIT>' , self . daemon_process . stderr , logging . ERROR ) <EOL> outlogger . start ( ) <EOL> errlogger . start ( ) <EOL> if self . daemon_process . poll ( ) is not None : <EOL> raise InstrumentError ( '<STR_LIT>' ) <EOL> def _kill_daemon ( self ) : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> self . daemon_process . kill ( ) <EOL> def _run_streamline_command ( command ) : <EOL> streamline = subprocess . Popen ( command , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE , <EOL> stdin = subprocess . PIPE ) <EOL> output , error = streamline . communicate ( ) <EOL> LogWriter ( '<STR_LIT>' ) . write ( output ) . close ( ) <EOL> LogWriter ( '<STR_LIT>' ) . write ( error ) . close ( ) </s>
