#! python: #w#VIC sh.py module # # Copyright 2002, 2003 by Timothy Rue <3seas@threeseas.net> # # VIC sh.py module: version 0.5.1.python (BETA) # # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation version 2 # of the License. http://www.gnu.org/copyleft/gpl.html # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Or access http://www.gnu.org/copyleft/gpl.html ########################################################################## #August 29, 2003 made file IQ parseable 0.5.1 #July 24, 2003 initial release 0.5 # ########################################################################## #s#system modules import string import os import tempfile # ADDED THIS and see below (only for exploring # possibilities of solution directions) #s#local modules import options import state import errors import files #w#class SH class SH: "SH" #s#def getName def getName(self): return "Sh (run on shell)" #s#def parseRun def parseRun(self,state,vic,config,command,stdin): handled = 0 output = [] error = errors.ok try: args,opts = options.getopt(command,[]) if (len(args) > 0 and args[0] != 'quit' and args[0] != 'exit'): ### and args[0] == '!'): handled = 1 command = "" for i in args: #[1:len(args)]: command = command + " " + i if (os.name == 'amiga'): ### os.popen() supports only reading OR writing to the child ### so we can't read the output, without putting it into a file ### (or we can read from it, but then not write to it) try: tmp_filename = tempfile.mktemp() #os.tmpnam() except: print "cannot make temp file" ### So pipe the output to a file and read that command = command + " > "+tmp_filename # (does amiga support redirection?) #command = command + " > "+tmp_filename+" 2>&1 " ### Execute the command fout = os.popen(command,'w') if (stdin != ""): fout.write(stdin) fout.close() ### see if there's any stdout and read it while 1: #test = files.fileReadable(tmp_filename) if (files.fileReadable(tmp_filename) != 1): continue #print tmp_filename+" "+"Not Yet available" else: break try: fin = open(tmp_filename,"r") output = fin.readlines() fin.close() except: print "except" output = [] ### remove the temporary file used to catch output files.delFile(tmp_filename) else: fin,fout = os.popen4(command) if (stdin != ""): fin.write(stdin) fin.close() output = fout.readlines() fout.close() ### remove endoflines from lines for j in range(len(output)): line = output[j] line = string.replace(line,os.linesep,"",1) output[j] = line except: pass return handled,output,error