def run_command(self, program, args): if debug_run(): print "will run this command:", program, args from PyQt4.Qt import QStringList, QProcess, QObject, SIGNAL, QDir # modified from runSim.py arguments = QStringList() if sys.platform == 'win32': program = "\"%s\"" % program # Double quotes needed by Windows. ###@@@ test this ### try it with blanks in output file name and in program name, once it works ###@@@ for arg in [program] + args: if arg: arguments.append(arg) self.simProcess = simProcess = QProcess() simProcess.setArguments(arguments) simProcess.setWorkingDirectory(QDir(self.working_directory)) # in case it writes random files if 1: # report stdout/stderr def blabout(): print "stdout:", simProcess.readStdout() ##e should also mention its existence in history, but don't copy it all there in case a lot def blaberr(): text = str(simProcess.readStderr()) # str since it's QString (i hope it can't be unicode) print "stderr:", text env.history.message(redmsg("%s stderr: " % self.plugin_name + quote_html(text))) # examples from CoNTub/bin/HJ: # stderr: BAD INPUT # stderr: Error: Indices of both tubes coincide QObject.connect(simProcess, SIGNAL("readyReadStdout()"), blabout) QObject.connect(simProcess, SIGNAL("readyReadStderr()"), blaberr) started = simProcess.start() ###k what is this code? i forget if true means ok or error if debug_run(): print "qprocess started:",started while 1: ###e need to make it abortable! from which abort button? ideally, one on the dialog; maybe cancel button?? # on exception: simProcess.kill() if simProcess.isRunning(): if debug_run(): print "still running" time.sleep(1) else: time.sleep(0.1) else: break if debug_run(): print "process done i guess: normalExit = %r, (if normal) exitStatus = %r" % \ (simProcess.normalExit(), simProcess.exitStatus()) if 1: QObject.disconnect(simProcess, SIGNAL("readyReadStdout()"), blabout) QObject.disconnect(simProcess, SIGNAL("readyReadStderr()"), blaberr) if simProcess.normalExit(): return simProcess.exitStatus() else: return -1
def _launch_pcgamess(self): """ Run PC GAMESS (Windows only). PC GAMESS creates 2 output files: - the DAT file, called "PUNCH", is written to the directory from which PC GAMESS is started. This is why we chdir to the Gamess temp directory before we run PC GAMESS. - the OUT file (aka the log file), which we name jigname.out. Returns: 0 = Success 1 = Cancelled 2 = Failed """ oldir = os.getcwd() # Save current directory jobDir = os.path.dirname(self.job_batfile) os.chdir(jobDir) # Change directory to the GAMESS temp directory. ## print "Current directory is: ", jobDir DATfile = os.path.join(jobDir, "PUNCH") if os.path.exists(DATfile): # Remove any previous DAT (PUNCH) file. print "run_pcgamess: Removing DAT file: ", DATfile os.remove(DATfile) # Hours wasted testing this undocumented tripe. Here's the deal: When using spawnv # on Windows, any args that might have spaces must be delimited by double quotes. # Mark 050530. #program = "\"" + self.job_batfile + "\"" #args = [program, ] # Here's the infuriating part. The 2nd arg to spawnv cannot have double quotes, but the # first arg in args (the program name) must have the double quotes if there is a space in # self.gms_program. #print "program = ", program #print "Spawnv args are %r" % (args,) # this %r remains (see above) #os.spawnv(os.P_WAIT, self.job_batfile, args) arg_list = ['-i', self.job_inputfile, '-o', self.job_outputfile] args = QStringList() for s in arg_list: args.append(str(s)) process = QProcess() process.start(self.server.program, args) # Blocks for n millisconds until the process has started and started() # signal is emitted. Returns true if the process was started successfullly. if not process.waitForStarted(2000): print "The process can't be started." return 2 progressDialog = self.showProgress() progressDialog.show() i = 55 pInc = True while process.state() == QProcess.Running: env.call_qApp_processEvents( ) #bruce 050908 replaced qApp.processEvents() if progressDialog.wasCanceled(): process.kill() os.chdir(oldir) return 1 # Job cancelled. progressDialog.setValue(i) if pInc: if i < 75: i += 1 else: pInc = False else: if i > 55: i -= 1 else: pInc = True # Do sth here time.sleep(0.05) if not process.state() == QProcess.Running: break progressDialog.setValue(100) progressDialog.accept() os.chdir(oldir) self.gamessJig.outputfile = self.job_outputfile return 0 # Success
def _launch_gamess(self): oldir = os.getcwd() # Save current directory ####self.outputFile = QFile(self.job_outputfile) ####self.outputFile.open( IO_WriteOnly | IO_Append ) jobInputfile = os.path.basename(self.job_inputfile) jobInputFile = jobInputfile[:-4] jobOutputfile = self.job_outputfile #os.path.basename(self.job_outputfile) ### Notes: The following way to get the 'bin' works by assuming user didn't change the ### working directory after the atom runs, otherwise it will get problem. filePath = os.path.dirname(os.path.abspath(sys.argv[0])) program = os.path.normpath(filePath + '/../bin/rungms') jobDir = os.path.dirname(self.job_batfile) os.chdir(jobDir) # Change directory to the GAMESS temp directory. # print "Current directory is: ", jobDir executableFile = self.server.program args = [program, jobInputFile, jobOutputfile, executableFile] #, '01', '1'] self.process = QProcess() for s in args: self.process.addArgument(s) ####self.process.setCommunication(QProcess.Stdout) print "The params for the QProcess run are: ", args ####self.fwThread = _FileWriting(self.outputFile) self.jobTimer = QTimer(self) self.progressDialog = JobProgressDialog(self.process, self.Calculation) self.connect(self.process, SIGNAL('processExited ()'), self.processDone) #self.connect(self.process, SIGNAL('readyReadStdout()'), self.readOutData) ####self.fwThread.start() if not self.process.start(): print "The process can't be started." return 2 self.connect(self.jobTimer, SIGNAL('timeout()'), self.processTimeout) self.stime = time.time() self.jobTimer.start(1) ret = self.progressDialog.exec_() if ret == QDialog.Accepted: retValue = 0 else: retValue = 1 ####self.fwThread.wait() ####bytes = self.process.readStdout() ####if bytes: #####self.outputFile.writeBlock(bytes) #####self.outputFile.flush() self.process = None ####self.outputFile.close() os.chdir(oldir) self.gamessJig.outputfile = self.job_outputfile return retValue