def execute(self, cmdline): logfile = self.interpreter.filePool.create_file(suffix='.log') result = pyalps.executeCommandLogged(cmdline, logfile.name) self.setResult('log_file', logfile) if result <> 0: if platform.system() == 'Darwin': pyalps.executeCommand(['open', logfile.name]) else: print "Error log in ", logfile.name print "COmmand was: ", cmdline raise ModuleError(self, 'Execution failed')
def compute(self): description = self.getInputFromPort('plot') outputname = None term=None if self.hasInputFromPort('filename'): outputname = self.getInputFromPort('filename') if self.hasInputFromPort('terminal'): term = self.getInputFromPort('terminal') res = convert_to_gnuplot(desc=description, outfile=outputname, terminal=term) o = self.interpreter.filePool.create_file() f = file(o.name,'w') f.write(res) f.close() self.setResult('value_as_string',res) self.setResult('file',o) if term == 'x11': executeCommand(['nohup','gnuplot', '-persist' , o.name,'&'])
def startRunScript(batch_run_script, batch_cmd_prefix=None, loc=None): # Format string if loc != None: locals().update(loc) batch_run_script = format_string(batch_run_script, loc) return startRunScript(batch_run_script, batch_cmd_prefix=batch_cmd_prefix) command = [] if batch_cmd_prefix != None: command += batch_cmd_prefix.split() command += ['./' + batch_run_script] return pyalps.executeCommand(command)
def createTest(script, inputs=None, outputs=None, prefix=None, refdir='./ref'): """ Create reference data, .testin.xml file and execute_test.py inputs are: ----------- script: computes results to be tested inputs: Optional list of input files if the application(s) called in 'script' rely on them and the input files are in the same directory as 'script'. If you specified relative paths to another directory, it won't work. outputs or prefix: outputs of script can either be specified with a complete list of output files or as a prefix creates a script called apptest_name_of_script.py, which can be used to execute the test """ if outputs is not None and prefix is not None: raise Exception("Cannot both define outputs and prefix") elif outputs is None and prefix is None: raise Exception("Script output has to be specified") script = os.path.expandvars(script) scriptdir = os.path.dirname(script) if not os.path.exists(refdir): recursive_mkdir(refdir) # Copy input files to refdir to allow execution of script there if inputs is not None: for f in inputs: if not os.path.expandvars(os.path.dirname(f)) == scriptdir: print( "Input files to %s should be in the same directory as %s" % (script, script)) sys.exit(1) shutil.copy(f, refdir) # execute given script in refdir ( creates reference data ) pardir = os.getcwd() os.chdir(refdir) cmdline = [sys.executable, os.path.join(pardir, script)] pyalps.executeCommand(cmdline) if inputs is not None: for f in inputs: os.remove(f) os.chdir(pardir) if prefix is None: reffiles = [os.path.join(refdir, os.path.basename(f)) for f in outputs] else: reffiles = pyalps.getResultFiles(prefix=prefix, dirname=refdir) if not reffiles: print( "Reference files not found. (If you use 'loop' or 'dmrg', try to delete old result files.)" ) sys.exit(1) # acquire a list of all observables allobs = [] try: eigenstatedata = pyalps.loadEigenstateMeasurements(reffiles) except RuntimeError: pass else: try: allobs += [o.props['observable'] for o in eigenstatedata[0][0]] # DMRG eigenstate data has one level of nesting less except TypeError: allobs += [o.props['observable'] for o in eigenstatedata[0]] try: mcdata = pyalps.loadMeasurements(reffiles) except RuntimeError: pass else: allobs += [o.props['observable'] for o in mcdata[0]] allobs = list(set(allobs)) scriptname = os.path.basename(script) scriptname = os.path.splitext(scriptname)[0] scriptname_prefixed = 'apptest_%s.py' % scriptname # Write .xml test-input file refparms = { "TESTNAME": scriptname, "TOLERANCE": "auto", "WRITE_RESULTS_TO_FILE": "yes", "SAVE_OUT_IF_FAIL": "yes" } testinputfile = writeTestInputFile(script, inputs, refparms, reffiles, allobs) pyalps.tools.copyStylesheet(pardir) # Write .py test-start script f = open(scriptname_prefixed, 'w') f.write('#!/usr/bin/env python\n\n') f.write('import sys\n') f.write('from pyalps import apptest\n') f.write( '# Explicitly specify "compMethod=..." and "outputs=..." if needed\n') f.write( "ret = apptest.runTest( '%s', outputs='auto', compMethod='auto', pyexec='auto' )\n" % testinputfile) f.write('if not ret: sys.exit(1)\n') f.close() os.chmod(scriptname_prefixed, 0o755)
def runTest(testinputfile, outputs='auto', compMethod='auto', pyexec='auto'): """ Run the test according to testinputfile and compare its output against reference files inputs are: ----------- script: a script to be run testinputfile: file containing reference info outputs: - default is 'auto' -> assume file names are identical with reference files list of script outputs - glob pattern - explicit list of files ( order has to be the same as in the .testin.xml file ) compMethod: - default is 'auto' -> try to detect comparison Method to be used (e.g. Monte Carlo or epsilon precise data) """ tstart = strftime("%Y-%b-%d %H:%M:%S", gmtime()) testinputfile = os.path.expandvars(testinputfile) props, inputs, refFileList, whatlist = read_testprop_xml(testinputfile) script = props['SCRIPT'] tmpdir = tempfile.mkdtemp() # execute given script in tmpdir pardir = os.getcwd() shutil.copy(script, tmpdir) if inputs is not None: for f in inputs: shutil.copy(f, tmpdir) # get python executable if pyexec == 'auto': pyexec = sys.executable os.chdir(tmpdir) cmdline = [pyexec, os.path.basename(script)] pyalps.executeCommand(cmdline) if inputs is not None: for f in inputs: os.remove(f) os.chdir(pardir) # copy stylesheet pyalps.tools.copyStylesheet(pardir) # Guess outputs from reference files if outputs == 'auto': outputs = [ os.path.join(tmpdir, os.path.basename(x)) for x in refFileList ] elif type(outputs) == str: print("Using glob '%s' to find outputs" % outputs) outputs = pyalps.getResultFiles( pattern=os.path.basename(outputs), \ dirname=os.path.dirname(outputs) ) if not outputs: print("\nList of output files of %s is empty\n" % script) test_success = False else: missing = [x for x in outputs if not os.path.exists(x)] if missing: for f in missing: print("Output file '%s' does not exist" % f) test_success = False else: # Start test test_success = compareTest(testinputfile, outputs, tmpdir, tstart, compMethod=compMethod) # if something goes wrong above, tmpdir will not be removed # for the moment this is useful, later maybe use try/except shutil.rmtree(tmpdir) return test_success
def recursiveRun(cmd, cmd_lang='command_line', follow_up_script=None, end_script=None, n=None, break_if=None, break_elseif=None, write_status=None, loc=None, loc0=None, batch_submit=False, batch_cmd_prefix=None, batch_run_directory=None, batch_run_script='run.script', batch_next_run_script=None, batch_run_now=False, batch_noRun=False): ### ### Either recursively run cmd for n times, or until the break_if condition holds true. ### ### Note: ### 1. cmd : command to be recursively run (quoted as a python str) ### 2. cmd_lang : language of cmd, either "command_line" (default), or "python". ### 3. n : number of recursions ### 4. break_if : condition to break recursion loop (quoted as a python str, interpreted as python command) ### 5. break_elseif : further condition to break recursion loop (""") ### 6. follow_up_script : script to be run after command (""") ### 7. end_script : script to be run just before recursive loop ends ### 8. loc : Python dict of local variables ### # set absolute path for current path if batch_run_directory == None: batch_run_directory = os.getcwd() # Format string if loc != None: locals().update(loc) cmd = format_string(cmd, loc) if follow_up_script != None: follow_up_script = format_string(follow_up_script, loc) if end_script != None: end_script = format_string(end_script, loc) if break_if != None: break_if = format_string(break_if, loc) if break_elseif != None: break_elseif = format_string(break_elseif, loc) if write_status != None: write_status = format_string(write_status, loc) batch_run_script = format_string(batch_run_script, loc) if batch_next_run_script != None: batch_next_run_script = format_string(batch_next_run_script, loc) return recursiveRun(cmd, cmd_lang=cmd_lang, follow_up_script=follow_up_script, end_script=end_script, n=n, break_if=break_if, break_elseif=break_elseif, write_status=write_status, loc0=loc, batch_submit=batch_submit, batch_cmd_prefix=batch_cmd_prefix, batch_run_directory=batch_run_directory, batch_run_script=batch_run_script, batch_next_run_script=batch_next_run_script, batch_run_now=batch_run_now, batch_noRun=batch_noRun) if loc0 != None: locals().update(loc0) # preparing batch run script if batch_submit: if not batch_run_now: batch_cmd = '' batch_cmd += 'cd ' + str(batch_run_directory) + '\n\n' batch_cmd += 'python <<@@\n' batch_cmd += 'import pyalps;\n' batch_cmd += 'import pyalps.dwa\n\n' batch_cmd += 'pyalps.dwa.recursiveRun(' + str_quote(cmd) if cmd_lang != None: batch_cmd += ', cmd_lang = ' + str_quote(cmd_lang) if follow_up_script != None: batch_cmd += ', \n\tfollow_up_script = ' + str_quote( follow_up_script) if end_script != None: batch_cmd += ', \n\tend_script = ' + str_quote(end_script) if n != None: batch_cmd += ', \n\tn = ' + str(n) if break_if != None: batch_cmd += ', \n\tbreak_if = ' + str_quote(break_if) if break_elseif != None: batch_cmd += ', \n\tbreak_elseif = ' + str_quote(break_elseif) if write_status != None: batch_cmd += ', \n\twrite_status = ' + str_quote(write_status) batch_cmd += ', \n\tbatch_submit = ' + str(batch_submit) if batch_cmd_prefix != None: batch_cmd += ', \n\tbatch_cmd_prefix = ' + str_quote( batch_cmd_prefix) batch_cmd += ', \n\tbatch_run_directory = ' + str_quote( batch_run_directory) batch_cmd += ', \n\tbatch_run_script = ' + str_quote( batch_run_script) if batch_next_run_script != None: batch_cmd += ', \n\tbatch_next_run_script = ' + str_quote( batch_next_run_script) batch_cmd += ', \n\tbatch_run_now = True' batch_cmd += ');\n\n' batch_cmd += '@@' pyalps.executeCommand(['echo', batch_cmd, '>', batch_run_script]) pyalps.executeCommand(['chmod', '755', batch_run_script]) if batch_noRun: return command = [] if batch_cmd_prefix != None: command += batch_cmd_prefix.split() command += ['./' + batch_run_script] return pyalps.executeCommand(command) if cmd_lang == 'command_line': pyalps.executeCommand(cmd.split()) elif cmd_lang == 'python': eval(cmd) else: print( "Error: The options for cmd_lang are 1) 'command_line' (default), or 2) 'python'." ) return if follow_up_script != None: exec(follow_up_script) if write_status != None: eval(write_status) if n != None: # if n exists if isinstance(n, int): # if n is a python integer if n <= 1: if end_script != None: eval(end_script) if batch_next_run_script != None: command = [] if batch_cmd_prefix != None: command += batch_cmd_prefix.split() command += ['./' + batch_next_run_script] return pyalps.executeCommand(command) else: return else: return recursiveRun( cmd, cmd_lang=cmd_lang, follow_up_script=follow_up_script, end_script=end_script, n=n - 1, write_status=write_status, loc0=loc0, batch_submit=batch_submit, batch_cmd_prefix=batch_cmd_prefix, batch_run_directory=batch_run_directory, batch_run_script=batch_run_script, batch_next_run_script=batch_next_run_script, batch_run_now=False) elif break_if != None: # otherwise, if break_if exists if eval(break_if): if end_script != None: eval(end_script) if batch_next_run_script != None: command = [] if batch_cmd_prefix != None: command += batch_cmd_prefix.split() command += ['./' + batch_next_run_script] return pyalps.executeCommand(command) else: return else: if break_elseif != None: # otherotherwise, if break_elseif exists if eval(break_elseif): if end_script != None: eval(end_script) if batch_next_run_script != None: command = [] if batch_cmd_prefix != None: command += batch_cmd_prefix.split() command += ['./' + batch_next_run_script] return pyalps.executeCommand(command) else: return else: return recursiveRun( cmd, cmd_lang=cmd_lang, follow_up_script=follow_up_script, end_script=end_script, break_if=break_if, break_elseif=break_elseif, write_status=write_status, loc0=loc0, batch_submit=batch_submit, batch_cmd_prefix=batch_cmd_prefix, batch_run_directory=batch_run_directory, batch_run_script=batch_run_script, batch_next_run_script=batch_next_run_script, batch_run_now=False) else: return recursiveRun( cmd, cmd_lang=cmd_lang, follow_up_script=follow_up_script, end_script=end_script, break_if=break_if, write_status=write_status, loc0=loc0, batch_submit=batch_submit, batch_cmd_prefix=batch_cmd_prefix, batch_run_script=batch_run_script, batch_run_directory=batch_run_directory, batch_next_run_script=batch_next_run_script, batch_run_now=False) else: # otherwise, recursiveRun only runs once if end_script != None: eval(end_script) if batch_next_run_script != None: command = [] if batch_cmd_prefix != None: command += batch_cmd_prefix.split() command += ['./' + batch_next_run_script] return pyalps.executeCommand(command) else: return
def execute(self, cmdline): if pyalps.executeCommand(cmdline) <> 0: raise ModuleError(self, 'Execution failed')