def mySimulator(X): # 1. Create input file infile = "input_template.txt" outfile = "input.txt" tokens = ["@X0", "@X1", "@X2"] ct.replace(infile, outfile, tokens, X) # 2. Compute program = sys.executable + " external_program.py" cmd = program + " " + outfile ct.execute(cmd) # 3. Parse output file Y = ct.get("output.txt", tokens=["Y0=", "Y1="]) return Y
def _call(self): """Execute code on the shell and return the runtime Returns ------- runtime : float Total runtime (wall time and not cpu time) """ time_start = time.time() otct.execute(self.executable, shell=True) time_stop = time.time() return time_stop - time_start
def check_execute(): print(("=== " + sys._getframe().f_code.co_name)) # ensure previous print is print before following command output sys.stdout.flush() if sys.platform.startswith('win'): ct.execute('cmd.exe /c echo hello') else: ct.execute('/bin/echo hello') ct.execute('echo hi', shell=True) if sys.platform.startswith('win'): ct.execute('echo hi', shell=True, hide_win=False) else: ct.execute('echo hi', shell=True, executable='/bin/bash') cp = ct.execute('echo hello', shell=True, capture_output=True) if cp.returncode != 0 or not cp.stdout.decode().startswith('hello'): raise Exception("ct.execute error!") cp = ct.execute('echo hello', shell=True, capture_output=True) if cp.returncode != 0 or not cp.stdout.decode().startswith('hello') or len( cp.stderr) > 0: raise Exception("ct.execute error!") # we expect a subclass of CalledProcessError with the error stream in the exception message try: cp = ct.execute(sys.executable + ' zebuebceb745az4f801m', shell=True, capture_output=True) raise Exception("should throw CalledProcessError") except subprocess.CalledProcessError: pass print("execute ok")
def _run(self): coupling_tools.execute(self.executable)
def check_execute(): print(("=== " + sys._getframe().f_code.co_name)) # ensure previous print is print before following command output sys.stdout.flush() if sys.platform.startswith('win'): coupling_tools.execute('cmd.exe /c echo hello') else: coupling_tools.execute('/bin/echo hello') coupling_tools.execute('echo hi', is_shell=True) if sys.platform.startswith('win'): coupling_tools.execute('echo hi', is_shell=True, hide_win=False) else: coupling_tools.execute('echo hi', is_shell=True, shell_exe='/bin/bash') ret, stdout = coupling_tools.execute('echo hello', is_shell=True, get_stdout=True) if ret != 0 or not stdout.decode().startswith('hello'): raise Exception("coupling_tools.execute error!") ret, stdout, stderr = coupling_tools.execute('echo hello', is_shell=True, get_stdout=True, get_stderr=True) if ret != 0 or not stdout.decode().startswith('hello') or len(stderr)>0: raise Exception("coupling_tools.execute error!") print("execute ok")
def check_execute(): print(("=== " + sys._getframe().f_code.co_name)) # ensure previous print is print before following command output sys.stdout.flush() # Care with -darwin systems if ('darwin' in sys.platform) or ('win' not in sys.platform): coupling_tools.execute('/bin/ls /bin/kill') coupling_tools.execute('echo "hi"', is_shell=True) coupling_tools.execute('echo "hi"', is_shell=True, shell_exe='/bin/bash') ret, stdout = coupling_tools.execute('/bin/ls /bin/kill', get_stdout=True) if stdout != b'/bin/kill\n': raise Exception("coupling_tools.execute error!") ret, stdout, stderr = coupling_tools.execute('/bin/ls /bin/kill', get_stdout=True, get_stderr=True) if stdout != b'/bin/kill\n' and stderr != b'': raise Exception("coupling_tools.execute error!") ret, stderr = coupling_tools.execute('/bin/ls /bin/kill 1>&2', is_shell=True, get_stderr=True) if stderr != b'/bin/kill\n': raise Exception("coupling_tools.execute error!") else: coupling_tools.execute('cmd.exe /c echo /bin/kill') exec_in_wine = os.path.exists('/boot') if exec_in_wine: # command 'echo' do not work in python on wine for an unknown # reason print('hi') print('hi') else: # native windows coupling_tools.execute('echo hi', is_shell=True) coupling_tools.execute('echo hi', is_shell=True, hide_win=False) ret, stdout = coupling_tools.execute('echo hello', is_shell=True, get_stdout=True) if ret != 0 or not str(stdout).startswith('hello'): raise Exception("coupling_tools.execute error!") print("execute ok")