def executeClCommand(self): """Execute an arbitrary CL command""" # pattern match to handle special commands that write to task mcmd = _re_clcmd.match(self.msg) if mcmd is None: # general command i = self.msg.find("\n") if i>=0: cmd = self.msg[:i+1] self.msg = self.msg[i+1:] else: cmd = self.msg self.msg = "" if not (cmd.find(IPCOUT) >= 0): # normal case -- execute the CL script code # redirect I/O (but don't use graphics status line) pyraf.iraf.clExecute(cmd, Stdout=self.default_stdout, Stdin=self.default_stdin, Stderr=self.default_stderr) else: # # Bizzaro protocol -- redirection to file with special # name given by IPCOUT causes output to be written back # to subprocess instead of to stdout. # # I think this only occurs one place in the entire system # (in clio/clepset.x) so I'm not trying to handle it robustly. # Just raise an exception if it does not fit my preconceptions. # ll = -(len(IPCOUT)+3) if cmd[ll:] != "> %s\n" % IPCOUT: raise IrafProcessError( "Error: cannot understand IPCOUT syntax in `%s'" % (cmd,)) sys.stdout.flush() # strip the redirection off and capture output of command buffer = cStringIO.StringIO() # redirect other I/O (but don't use graphics status line) pyraf.iraf.clExecute(cmd[:ll]+"\n", Stdout=buffer, Stdin=self.default_stdin, Stderr=self.default_stderr) # send it off to the task with special flag line at end buffer.write(IPCDONEMSG) self.writeString(buffer.getvalue()) buffer.close() elif mcmd.group('stty'): # terminal window size if self.stdoutIsatty: nlines, ncols = wutil.getTermWindowSize() else: # a kluge -- if self.stdout is not a tty, assume it is a # file and give a large number for the number of lines nlines, ncols = 100000, 80 self.writeString('set ttynlines=%d\nset ttyncols=%d\n' % (nlines, ncols)) self.msg = self.msg[mcmd.end():] elif mcmd.group('curpack'): # current package request self.writeString(pyraf.iraf.curpack() + '\n') self.msg = self.msg[mcmd.end():] elif mcmd.group('sysescape'): # OS escape tmsg = mcmd.group('sys_cmd') # use my version of system command so redirection works sysstatus = pyraf.iraf.clOscmd(tmsg, Stdin=self.stdin, Stdout=self.stdout, Stderr=self.stderr) self.writeString(str(sysstatus)+"\n") self.msg = self.msg[mcmd.end():] # self.stdout.write(self.msg + "\n") else: # should never get here raise RuntimeError( "Program bug: uninterpreted message `%s'" % (self.msg,))
def executeClCommand(self): """Execute an arbitrary CL command""" # pattern match to handle special commands that write to task mcmd = _re_clcmd.match(self.msg) if mcmd is None: # general command i = self.msg.find("\n") if i >= 0: cmd = self.msg[:i + 1] self.msg = self.msg[i + 1:] else: cmd = self.msg self.msg = "" if not (cmd.find(IPCOUT) >= 0): # normal case -- execute the CL script code # redirect I/O (but don't use graphics status line) pyraf.iraf.clExecute(cmd, Stdout=self.default_stdout, Stdin=self.default_stdin, Stderr=self.default_stderr) else: # # Bizzaro protocol -- redirection to file with special # name given by IPCOUT causes output to be written back # to subprocess instead of to stdout. # # I think this only occurs one place in the entire system # (in clio/clepset.x) so I'm not trying to handle it robustly. # Just raise an exception if it does not fit my preconceptions. # ll = -(len(IPCOUT) + 3) if cmd[ll:] != "> %s\n" % IPCOUT: raise IrafProcessError( "Error: cannot understand IPCOUT syntax in `%s'" % (cmd, )) sys.stdout.flush() # strip the redirection off and capture output of command buffer = cStringIO.StringIO() # redirect other I/O (but don't use graphics status line) pyraf.iraf.clExecute(cmd[:ll] + "\n", Stdout=buffer, Stdin=self.default_stdin, Stderr=self.default_stderr) # send it off to the task with special flag line at end buffer.write(IPCDONEMSG) self.writeString(buffer.getvalue()) buffer.close() elif mcmd.group('stty'): # terminal window size if self.stdoutIsatty: nlines, ncols = wutil.getTermWindowSize() else: # a kluge -- if self.stdout is not a tty, assume it is a # file and give a large number for the number of lines nlines, ncols = 100000, 80 self.writeString('set ttynlines=%d\nset ttyncols=%d\n' % (nlines, ncols)) self.msg = self.msg[mcmd.end():] elif mcmd.group('curpack'): # current package request self.writeString(pyraf.iraf.curpack() + '\n') self.msg = self.msg[mcmd.end():] elif mcmd.group('sysescape'): # OS escape tmsg = mcmd.group('sys_cmd') # use my version of system command so redirection works sysstatus = pyraf.iraf.clOscmd(tmsg, Stdin=self.stdin, Stdout=self.stdout, Stderr=self.stderr) self.writeString(str(sysstatus) + "\n") self.msg = self.msg[mcmd.end():] # self.stdout.write(self.msg + "\n") else: # should never get here raise RuntimeError("Program bug: uninterpreted message `%s'" % (self.msg, ))
def run(self, task, pstdin=None, pstdout=None, pstderr=None): """Run the IRAF logical task (which must be in this executable) The IrafTask object must have these methods: getName(): return the name of the task getParam(param): get parameter value setParam(param,value): set parameter value getParObject(param): get parameter object """ if test_probe : sys.stdout.write( "Running IRAF task %s from %s\n"% (task, self.executable) ) self.task = task # set IO streams stdin = pstdin or sys.stdin stdout = pstdout or sys.stdout stderr = pstderr or sys.stderr self.stdin = stdin self.stdout = stdout self.stderr = stderr self.default_stdin = stdin self.default_stdout = stdout self.default_stderr = stderr # stdinIsatty flag is used in xfer to decide whether to # read inputs in blocks or not. As long as input comes # from __stdin__, consider it equivalent to a tty. self.stdinIsatty = (hasattr(stdin,'isatty') and stdin.isatty()) or \ self.stdin == sys.__stdin__ self.stdoutIsatty = hasattr(stdout,'isatty') and stdout.isatty() # stdinIsraw flag is used in xfer to decide whether to # read inputs as RAW input or not. self.stdinIsraw = False # redir_info tells task that IO has been redirected redir_info = '' if pstdin and pstdin != sys.__stdin__: redir_info = '<' if (pstdout and pstdout != sys.__stdout__) or \ (pstderr and pstderr != sys.__stderr__): redir_info = redir_info+'>' # update IRAF environment variables if necessary if self.envVarList: self.writeString(''.join(self.envVarList)) self.envVarList = [] # if stdout is a terminal, set the lines & columns sizes # this ensures that they are up-to-date at the start of the task # (which is better than the CL does) if self.stdoutIsatty: nlines, ncols = wutil.getTermWindowSize() self.writeString('set ttynlines=%d\nset ttyncols=%d\n' % (nlines, ncols)) taskname = self.task.getName() # remove leading underscore, which is just a convention for CL if taskname[:1]=='_': taskname = taskname[1:] self.writeString(taskname+redir_info+'\n') self.running = 1 try: # begin slave mode self.slave() finally: self.running = 0
def run(self, task, pstdin=None, pstdout=None, pstderr=None): """Run the IRAF logical task (which must be in this executable) The IrafTask object must have these methods: getName(): return the name of the task getParam(param): get parameter value setParam(param,value): set parameter value getParObject(param): get parameter object """ if test_probe: sys.stdout.write("Running IRAF task %s from %s\n" % (task, self.executable)) self.task = task # set IO streams stdin = pstdin or sys.stdin stdout = pstdout or sys.stdout stderr = pstderr or sys.stderr self.stdin = stdin self.stdout = stdout self.stderr = stderr self.default_stdin = stdin self.default_stdout = stdout self.default_stderr = stderr # stdinIsatty flag is used in xfer to decide whether to # read inputs in blocks or not. As long as input comes # from __stdin__, consider it equivalent to a tty. self.stdinIsatty = (hasattr(stdin,'isatty') and stdin.isatty()) or \ self.stdin == sys.__stdin__ self.stdoutIsatty = hasattr(stdout, 'isatty') and stdout.isatty() # stdinIsraw flag is used in xfer to decide whether to # read inputs as RAW input or not. self.stdinIsraw = False # redir_info tells task that IO has been redirected redir_info = '' if pstdin and pstdin != sys.__stdin__: redir_info = '<' if (pstdout and pstdout != sys.__stdout__) or \ (pstderr and pstderr != sys.__stderr__): redir_info = redir_info + '>' # update IRAF environment variables if necessary if self.envVarList: self.writeString(''.join(self.envVarList)) self.envVarList = [] # if stdout is a terminal, set the lines & columns sizes # this ensures that they are up-to-date at the start of the task # (which is better than the CL does) if self.stdoutIsatty: nlines, ncols = wutil.getTermWindowSize() self.writeString('set ttynlines=%d\nset ttyncols=%d\n' % (nlines, ncols)) taskname = self.task.getName() # remove leading underscore, which is just a convention for CL if taskname[:1] == '_': taskname = taskname[1:] self.writeString(taskname + redir_info + '\n') self.running = 1 try: # begin slave mode self.slave() finally: self.running = 0