def start_application(self, applicationName, args="", binaryDirectoryOnTarget=None, nameExtension="", env={}, dltAppID=None): #ensure binary is there if binaryDirectoryOnTarget: binaryPathOnTarget = binaryDirectoryOnTarget + '/' + applicationName else: binaryPathOnTarget = applicationName (_, _, resultTest) = self.execute_on_target("type " + binaryPathOnTarget) #test -e cannot be used as it does not work for applications in system path if resultTest != 0: log.error("Error: executable '{0}' could not be found (path: '{1}')".format(applicationName, binaryPathOnTarget)) return Application(None, None, None, applicationName, binaryDirectoryOnTarget, nameExtension) prefix = helper.get_env_var_setting_string(self._get_merged_env(env)) #execute application if binaryDirectoryOnTarget: command = "cd {}; {} ./{} {}".format(binaryDirectoryOnTarget, prefix, applicationName, args) else: command = "{} {} {}".format(prefix, applicationName, args) log.info("start_application command: '{}'".format(command)) try: stdin, stdout, stderr = self.sshClient.exec_command(command) except Exception as e: log.error("Error: {0} could not be started (error message: {1})".format(applicationName, e.message)) return Application(None, None, None, applicationName, binaryDirectoryOnTarget, nameExtension) application = Application(stdin, stdout, stderr, applicationName, binaryDirectoryOnTarget, nameExtension) application.started = True return application
def execute_on_target(self, commandToExecute, block=True, env={}, cwd=None, timeout=None): prefix = helper.get_env_var_setting_string(env) command = "{} ".format(prefix) + commandToExecute if cwd: command = "cd {}; ".format(cwd) + command log.info("[{}]{}".format(self.name, command)) stdin, stdout, stderr = self.sshClient.exec_command(command) stdoutBuffer = Buffer() stdoutReader = AsynchronousPipeReader(stdout, stdoutBuffer) stderrBuffer = Buffer() stderrReader = AsynchronousPipeReader(stderr, stderrBuffer) if block: if timeout: # poll on application exit with timeout to prevent infinite blocking endTime = time.time() + timeout while time.time( ) < endTime and not stdout.channel.exit_status_ready(): time.sleep(0.1) if not stdout.channel.exit_status_ready(): return [], ["<timeout>"], 1 # get application exit status blocking. will immediately succeed when timeout was given returnCode = stdout.channel.recv_exit_status() stdoutReader.stop(withTimeout=True) stderrReader.stop(withTimeout=True) return stdoutBuffer.get_all_data(), stderrBuffer.get_all_data( ), returnCode return None
def execute_on_target(self, commandToExecute, block=True, env={}, cwd=None): prefix = helper.get_env_var_setting_string(env) command = "{} ".format(prefix) + commandToExecute if cwd: command = "cd {}; ".format(cwd) + command log.info("executing '" + command + "' on target") stdin, stdout, stderr = self.sshClient.exec_command(command) stdoutBuffer = Buffer() stdoutReader = AsynchronousPipeReader(stdout, stdoutBuffer) stderrBuffer = Buffer() stderrReader = AsynchronousPipeReader(stderr, stderrBuffer) if (block): returnCode = stdout.channel.recv_exit_status( ) #block till call is finished stdoutReader.stop(withTimeout=True) stderrReader.stop(withTimeout=True) return stdoutBuffer.get_all_data(), stderrBuffer.get_all_data( ), returnCode return None
def start_application(self, applicationName, args="", binaryDirectoryOnTarget=None, nameExtension="", env={}, dltAppID=None, prepend_unbuffer=False): #ensure binary is there if binaryDirectoryOnTarget: binaryPathOnTarget = binaryDirectoryOnTarget + '/' + applicationName else: binaryPathOnTarget = applicationName if not self._executable_exists_on_target(binaryPathOnTarget): log.error( "Error: executable '{0}' could not be found (path: '{1}')". format(applicationName, binaryPathOnTarget)) return Application(None, None, None, applicationName, binaryDirectoryOnTarget, nameExtension) prefix = helper.get_env_var_setting_string(self._get_merged_env(env)) if prepend_unbuffer: if self.supportsUnbuffer: prefix += " unbuffer" else: log.warn( "Unbuffer is not supported on this target! Will be ignored." ) #execute application if binaryDirectoryOnTarget: command = "cd {}; {} ./{} {}".format(binaryDirectoryOnTarget, prefix, applicationName, args) else: command = "{} {} {}".format(prefix, applicationName, args) log.info("start_application command: '{}'".format(command)) try: stdin, stdout, stderr = self.sshClient.exec_command(command) except Exception as e: log.error( "Error: {0} could not be started (error message: {1})".format( applicationName, e.message)) return Application(None, None, None, applicationName, binaryDirectoryOnTarget, nameExtension) application = Application(stdin, stdout, stderr, applicationName, binaryDirectoryOnTarget, nameExtension) application.started = True return application