def __init__(self, pipe = None, expect_token ="#-->", submit_split=";[ \r\n]*",submit_token = ";\n", terminal_preferred =True): """ Unless another pipe is supplied this constructor opens Maple. The expectation token is the prompt characters and should be chosen such that one would not expect it to appear in any output of any of the executed programs. The token is set in MaplePipeline.initiate_pipe(). The submit token is the token that will be send to the pipe after the command has been sent and the echo has been consumed. For a normal bash terminal this is simply \n. Remark! For now the Maple pipeline only runs through a terminal as the stdout buffer is not flushed often enough meaning that expectation tokens will not be seen and a deadlock is encountered. Hopefully a workaround for this will be found. """ if not pipe: cmd = which("maple",["/Library/Frameworks/Maple.framework/Versions/17/bin/"]) pipe = Process(cmd,["-t"], terminal_required = True) # pipe = Process(cmd,["-t"], terminal_preferred = terminal_preferred) self._submit_split = submit_split self._number_of_outlines = 1 self._result_list = [] super(MaplePipeline, self).__init__(pipe,expect_token, submit_token) self.consume_output(consume_until = self._expect_token)
def __init__(self, pipe = None, expect_token =re.compile("(>>> |\.\.\. )"), submit_split="\n",submit_token = "\n", terminal_preferred =True): if not pipe: cmd = which("python") pipe = Process(cmd,[], terminal_required = True) self._submit_split = submit_split self._number_of_outlines = 1 self._result_list = [] super(PythonTerminal, self).__init__(pipe,expect_token, submit_token) self.consume_output(consume_until = self._expect_token)
def __init__(self, pipe = None, expect_token ="#-->", submit_token = "\n", path_module = None, initiate_pipe = True, debug_level=3): if not pipe: cmd = which("bash") pipe = Process(cmd, terminal_required = True) if not path_module: self._path = os.path else: self._path = path_module self._hasher = None super(BashTerminal, self).__init__(pipe,expect_token, submit_token,initiate_pipe=initiate_pipe, debug_level=debug_level ) self.set_timeout(200) self._nodename = None
def __init__(self, pipe=None, expect_token=re.compile("(>>> |\.\.\. )"), submit_split="\n", submit_token="\n", terminal_preferred=True): if not pipe: cmd = which("python") pipe = Process(cmd, [], terminal_required=True) self._submit_split = submit_split self._number_of_outlines = 1 self._result_list = [] super(PythonTerminal, self).__init__(pipe, expect_token, submit_token) self.consume_output(consume_until=self._expect_token)
def connect(self, server, username, password, port = 22, accept_fingerprint = False, command = "ssh", port_option = "-p %d", expect_token = "#-->", submit_token="\n", additional_arguments ="",debug_level = 3): pop = port_option % int(port) cmd = which(command) pipe = Process(cmd,[pop, additional_arguments, "%s@%s" % (username, server)], terminal_required = True) super(BaseSecureTerminal, self).__init__(pipe,expect_token, submit_token, initiate_pipe = False, debug_level = debug_level) self.set_timeout(15) self.push_expect(re.compile(r"(password:|Password:|\(yes/no\)\?|\$|sftp\>|(Host key verification failed.))")) # try: out = self.expect() # except: # raise BaseSecureTerminalLoginError(self.buffer +"\nCommand executed: "+" ".join([cmd, pop, additional_arguments, "%s@%s" % (username, server)])) self.host_verification_message = None if "Host key verification failed." in out: self.host_verification_message = out raise BaseSecureTerminalHostVerification("\n"+out) # print "END OF EXPECT", out newfingerprint = "(yes/no)" in out if not accept_fingerprint and newfingerprint: raise BaseSecureTerminalLoginError("Accept fingerprint required, but not allowed by user") elif newfingerprint: self.send_command("yes") out = self.send_command(password, False) if "Password:"******"password" in out: print self.buffer raise BaseSecureTerminalLoginError("Wrong username or password.") self._path = posixpath self.pop_expect() self.set_timeout(200) self.initiate_pipe()
def restart_kernel(self, reset_vt100 = False): """ This function tries to quit Maple using 'quit', then kills it and starts a new kernel. Note the resultlist is not cleared. """ try: self.send_command("quit;") self.kill() self.remaining() finally: # self.__init__() cmd = which("maple",["/Library/Frameworks/Maple.framework/Versions/Current/bin/"]) pipe = Process(cmd,["-t"], terminal_required = True) vt100 = self.vt100interpreter if reset_vt100: vt100 = None super(MaplePipeline, self).__init__(pipe,self._expect_token,self._submit_token, vt100 = vt100) self.consume_output(consume_until = self._expect_token)
def __init__(self, pipe=None, expect_token="#-->", submit_token="\n", path_module=None, initiate_pipe=True, debug_level=3): if not pipe: cmd = which("bash") pipe = Process(cmd, terminal_required=True) if not path_module: self._path = os.path else: self._path = path_module self._hasher = None super(BashTerminal, self).__init__(pipe, expect_token, submit_token, initiate_pipe=initiate_pipe, debug_level=debug_level) self.set_timeout(200) self._nodename = None
def __init__(self): pipe = Process(which("bash"), terminal_required=True) expect_token = "#-->" submit_token = "\n" initiate_pipe = True super(BashTerminal, self).__init__(pipe, expect_token, submit_token, initiate_pipe)
from batchq.core.process import Process from batchq.core.utils import which print "Starting bash found at", which("bash") x = Process(which("bash")) # Waits until we have the prompt while x.getchar() != "$": pass # Sends a command to the terminal x.write("echo Hello world\n") # And read the response print "" print "The response is:" print x.read() print "" print "The full buffer is:" print x.buffer x.kill()
def __init__(self): pipe = Process(which("bash"), terminal_required = True) expect_token ="#-->" submit_token ="\n" initiate_pipe=True super(BashTerminal, self).__init__(pipe, expect_token, submit_token, initiate_pipe)