def submitProtocol(script, **params): '''Launch a protocol, to a queue or executing directly. If the queue options are found, it will be launched with configuration (command and file template) found in project settings This function should be called from ProjectDir ''' #Load the config module launch = loadLaunchModule() launchFilename = script.replace('.py', '.job') # This is for make a copy of nodes files nodesFile = script.replace('.py', '.nodes') params['nodesfileBackup'] = nodesFile params['file'] = launchFilename params['XMIPP_HOME'] = os.environ['XMIPP_HOME'] #create launch file launchfile = open(launchFilename, 'w') launchfile.write(launch.FileTemplate % params) launchfile.close() command = launch.Program + " " + launch.ArgsTemplate % params from protlib_xmipp import greenStr, redStr from protlib_sql import SqliteDb print "** Submiting to queue: '%s'" % greenStr(command) ps = Popen(command, shell=True, stdout=PIPE) out = ps.communicate()[0] import re s = re.search('(\d+)', out) if s: return int(s.group(0)) else: print "** Couldn't parse %s ouput: %s" % (greenStr(launch.Program), redStr(out)) return SqliteDb.UNKNOWN_JOBID
def runJob(log, programname, params, NumberOfMpi = 1, NumberOfThreads = 1, RunInBackground=False): command = buildRunCommand(log, programname, params, NumberOfMpi, NumberOfThreads, RunInBackground) if log: from protlib_xmipp import greenStr printLog("Running command: %s" % greenStr(command),log) from subprocess import call retcode = 1000 try: retcode = call(command, shell=True, stdout=sys.stdout, stderr=sys.stderr) if log: printLog("Process returned with code %d" % retcode,log) if retcode != 0: raise Exception("Process returned with code %d, command: %s" % (retcode,command)) except OSError, e: raise Exception("Execution failed %s, command: %s" % (e, command))
def _endSingleStep(self, _connection, _cursor, stepRow, info): # Report step finish and update database msg = greenStr(" Step finished: %s" % info.stepStr) printLog(msg, self.Log, out=True, err=True) sqlCommand = ( """UPDATE %(TableSteps)s SET finish = CURRENT_TIMESTAMP WHERE step_id=%(step_id)d AND run_id=%(run_id)d""" % info.dict ) _cursor.execute(sqlCommand) _connection.commit()
def submitProgram(script, **params): ''' Same function as submitProtocol but just for single programs, not need to be inside a Project ''' #Load the config module launch = loadLaunchModule() launchFilename = script.replace('.py', '.job') nodesFile = script.replace('.py', '.nodes') params['nodesfileBackup'] = nodesFile params['file'] = launchFilename #create launch file launchfile = open(launchFilename, 'w') launchfile.write(launch.FileTemplate % params) launchfile.close() command = launch.Program + " " + launch.ArgsTemplate % params from protlib_xmipp import greenStr print "** Submiting to queue: '%s'" % greenStr(command) os.system(command)
def runProgramTests(self, program): tests = self.progDict[program] n = len(tests) outPath = join(self.fnDir, program) outDir = outPath testName = "" testNo = 1 md = MetaData(self._statistics) for test, mpi, random, preruns, postruns, changeDirectory, testfiles, owner in tests: # for test, mpi, random, preruns, postruns, changeDirectory, testfiles in tests: objid = md.addObject() if n > 1: outDir = outPath + "_%02d" % testNo testName = "(%d of %d)" % (testNo, n) # test num md.setValue(MDL_BLOCK_NUMBER, testNo, objid) # program name # print type(program) md.setValue(MDL_PROGRAM, str(program), objid) dtBegin = datetime.now() timeStr = str(dtBegin) # beginning time md.setValue(MDL_DATE, str(timeStr), objid) print "------------------------------------------------------------------------------------" print warnStr(">>> Running test %(testName)s of %(program)s (%(timeStr)s)" % locals()) print " Output dir: " print " ", outDir print " Statistics file: " print " ", self._statistics print " Timeout: " print " %d seconds" % self._timeout if exists(outDir): shutil.rmtree(outDir) os.makedirs(outDir) self.outDir = outDir self.program = program test = self.expandFormat(test) self.runCommands(preruns, "prerun") if mpi: cmd = ( "mpirun -np 3 `which %s`" % program ) ##DO NOT REPLACE SO WE CAN TEST MPI EASILY.replace("xmipp_", "xmipp_mpi_") else: cmd = program if changeDirectory: cmd = "cd %s ; " % outDir + cmd + " %s > stdout.txt 2> stderr.txt" % test else: cmd += " %s > %s/stdout.txt 2> %s/stderr.txt" % (test, outDir, outDir) print " Command: " print " ", greenStr(cmd) # run the test itself command = Command(cmd) self._command = command try: command.run(timeout=self._timeout) except KeyboardInterrupt: command.terminate() # result = os.system(cmd) # result = subprocess.call(cmd, shell=True) self.runCommands(postruns, "postrun") tdEnd = (datetime.now() - dtBegin).total_seconds() # elapsed time md.setValue(MDL_TIME, tdEnd, objid) print " Elapsed time: %d seconds" % tdEnd error = self.checkResult(testfiles, outDir, random) if len(error): self.error += error self.errorFlag = True print redStr("ERRORS:\n" + error) md.setValue(MDL_ENABLED, -1, objid) else: md.setValue(MDL_ENABLED, 1, objid) testNo += 1 md.setValue(MDL_USER, str(owner), objid) md.write(self._statistics)