def _start_idl(self): # Signal handlers are inherited by forked processes, and we can't easily # reset it from the subprocess. Since kernelapp ignores SIGINT except in # message handlers, we need to temporarily reset the SIGINT handler here # so that IDL and its children are interruptible. sig = signal.signal(signal.SIGINT, signal.SIG_DFL) try: self._executable = find_executable("idl") self._child = spawn(self._executable, timeout=300, encoding='utf-8') self.idlwrapper = replwrap.REPLWrapper(self._child, u"IDL> ", None) except: self._executable = find_executable("gdl") self._child = spawn(self._executable, timeout=300, encoding='utf-8') self.idlwrapper = replwrap.REPLWrapper(self._child, u"GDL> ", None) finally: signal.signal(signal.SIGINT, sig) self.idlwrapper.run_command( "!quiet=1 & defsysv,'!inline',0 & !more=0".rstrip(), timeout=None) # Compile IDL routines/functions dirname = os.path.dirname(os.path.abspath(__file__)) self.idlwrapper.run_command(".compile " + dirname + "/snapshot.pro", timeout=None)
def test_repl_load(container): """Load REPL with commands.""" print(f"Testing REPL on {container.name} via repl container and Python pexpect.") # Remove any existing repl containers if they exist for repl_container in client.containers.list(all=True, filters={"name":f"repl\d.{args.network}"}): print(f"removing {repl_container.name}") repl_container.remove(force=True, v=True) # Run repl loader with repetitions and repl commands defined in args for i in [0]: # Add more later by var in args if wanted to increase load diversity try: repl_node = {} repl_node[i] = {} repl_node[i]['name'] = f"repl{i}.{args.network}" repl_node[i]['volume'] = client.volumes.create() cmd = (f"sudo docker run --rm -it -v {repl_node[i]['volume'].name}:{args.rnode_directory} " f"--cpuset-cpus={args.cpuset_cpus} --memory={args.memory} --name {repl_node[i]['name']} " f"--network {args.network} {args.image} " f"--grpc-host {container.name} -r") print(f"docker repl cmd: {cmd}") repl_cmds = args.repl_cmds conn = replwrap.REPLWrapper(cmd, args.prompt, None) for i in range(args.repl_load_repetitions): for repl_cmd in repl_cmds: result = conn.run_command(repl_cmd) print(f"repetition: {i} output: {result}") i += 1 return 0 except Exception as e: print(e) return 1
def test_existing_spawn(self): child = pexpect.spawnu("bash") repl = replwrap.REPLWrapper(child, replwrap.u("$ "), "PS1='{0}'; PS2='{1}'") res = repl.run_command("echo $HOME") assert res.startswith('/'), res
def _start_gap(self): # Signal handlers are inherited by forked processes, and we can't easily # reset it from the subprocess. Since kernelapp ignores SIGINT except in # message handlers, we need to temporarily reset the SIGINT handler here # so that gap and its children are interruptible. sig = signal.signal(signal.SIGINT, signal.SIG_DFL) try: # setup.g contains functions needed for Jupyter interfacing setupg = path.dirname(path.abspath(__file__)) gap_run_command = getenv(self._env_executable) if gap_run_command is None: raise NameError( "Please set %s in your environment to a valid gap executable" % (self._env_executable)) gap_extra_options = getenv(self._env_options, "") self._loghack("starting GAP: %s" % (gap_run_command)) self.gapwrapper = replwrap.REPLWrapper( gap_run_command + ' -n -b -T %s %s/gap/setup.g' % (gap_extra_options, setupg), u'gap|| ', None, None, continuation_prompt=u'|| ') self.gapwrapper.run_command("\n") # Try to force GAP to not format the output. This doesn't work from # setup.g and god alone knows why self.gapwrapper.run_command( "SetPrintFormattingStatus(\"*stdout*\", false);\n") finally: signal.signal(signal.SIGINT, sig)
def setUp(self): """Sets up tests by executing main program programmatically.""" command = 'python' if platform == 'linux' or platform == 'darwin': command += '3' self.child = replwrap.REPLWrapper(f'{command} employee_database.py', '<< ', None)
def __init__(self, port, dbname): port = port dbname = dbname prompt = "postgres=# " # TODO: consider normal user psql_cmd = 'psql -p ' + port + ' -d ' + dbname self.psql = replwrap.REPLWrapper(psql_cmd, prompt, None)
def _start_rascal(self): sig = signal.signal(signal.SIGINT, signal.SIG_DFL) try: self.rascal_wrapper = replwrap.REPLWrapper( "{} -jar {}".format(self._JAVA_COMMAND, self._RASCAL_LOCATION), "rascal>", None) finally: signal.signal(signal.SIGINT, sig)
def test_existing_spawn(self): child = pexpect.spawn("bash", timeout=5, encoding='utf-8') repl = replwrap.REPLWrapper(child, re.compile('[$#]'), "PS1='{0}' PS2='{1}' " "PROMPT_COMMAND=''") res = repl.run_command("echo $HOME") assert res.startswith('/'), res
def _start_geoscript_groovy(self): # Signal handlers are inherited by forked processes, and we can't easily # reset it from the subprocess. Since kernelapp ignores SIGINT except in # message handlers, we need to temporarily reset the SIGINT handler here # so that bash and its children are interruptible. sig = signal.signal(signal.SIGINT, signal.SIG_DFL) try: self.geoscriptgroovywrapper = replwrap.REPLWrapper( "geoscript-groovysh --terminal=none", u"groovy:", None) finally: signal.signal(signal.SIGINT, sig) # Create a Groovy display function that can display images init_script = """ // Set XY coordinate ordering System.setProperty("org.geotools.referencing.forceXY", "true") // Turn off logging java.util.logging.LogManager.getLogManager().reset() def globalLogger = java.util.logging.Logger.getLogger(java.util.logging.Logger.GLOBAL_LOGGER_NAME) globalLogger.setLevel(java.util.logging.Level.OFF) display = { obj -> if (obj instanceof java.awt.Image) { def out = new ByteArrayOutputStream() javax.imageio.ImageIO.write(obj, "png", out) String str = javax.xml.bind.DatatypeConverter.printBase64Binary(out.toByteArray()) "image/png;base64,${str}" } else if (obj instanceof geoscript.geom.Geometry) { ByteArrayOutputStream out = new ByteArrayOutputStream() geoscript.render.Draw.draw(obj as geoscript.geom.Geometry, out:out, imageType: "base64", size:[200,200]) byte[] bytes = org.apache.commons.codec.binary.Base64.encodeBase64(out.toByteArray()) String str = new String(bytes, "UTF-8") "image/png;base64,${str}" } else if (obj instanceof geoscript.feature.Feature) { ByteArrayOutputStream out = new ByteArrayOutputStream() geoscript.render.Draw.draw(obj as geoscript.feature.Feature, out:out, imageType: "base64", size:[200,200]) byte[] bytes = org.apache.commons.codec.binary.Base64.encodeBase64(out.toByteArray()) String str = new String(bytes, "UTF-8") "image/png;base64,${str}" } else if (obj instanceof geoscript.layer.Layer) { ByteArrayOutputStream out = new ByteArrayOutputStream() geoscript.render.Draw.draw(obj as geoscript.layer.Layer, out:out, imageType: "base64", size:[200,200]) byte[] bytes = org.apache.commons.codec.binary.Base64.encodeBase64(out.toByteArray()) String str = new String(bytes, "UTF-8") "image/png;base64,${str}" } else { obj } } """ self.geoscriptgroovywrapper.run_command(init_script)
def ferret_wrapper(command="pyferret", args="-nojnl -nodisplay -server", orig_prompt=u'yes?'): ''' Start a pyferret shell and retrun a :class:`REPLWrapper` object. ''' return replwrap.REPLWrapper(" ".join((command, args)), orig_prompt=orig_prompt, prompt_change=None)
def _start_idl(self): # Signal handlers are inherited by forked processes, and we can't easily # reset it from the subprocess. Since kernelapp ignores SIGINT except in # message handlers, we need to temporarily reset the SIGINT handler here # so that IDL and its children are interruptible. sig = signal.signal(signal.SIGINT, signal.SIG_DFL) try: self._executable = find_executable("idl") self._child = spawn(self._executable,timeout = 300) self.idlwrapper = replwrap.REPLWrapper(self._child,u"IDL> ",None) except: self._executable = find_executable("gdl") self._child = spawn(self._executable,timeout = 300) self.idlwrapper = replwrap.REPLWrapper(self._child,u"GDL> ",None) finally: signal.signal(signal.SIGINT, sig) self.idlwrapper.run_command("!quiet=1 & defsysv,'!inline',0 & defsysv, '!inline_8objs',ptr_new(!NULL) & !more=0".rstrip(), timeout=None)
def _start_irb(self): sig = signal.signal(signal.SIGINT, signal.SIG_DFL) output = subprocess.getoutput("irb -v") try: if output[:7] == 'irb 1.2': self.irbwrapper = replwrap.REPLWrapper( "irb --simple-prompt --nocolorize --nomultiline", ">> ", None, continuation_prompt="?> ") else: self.irbwrapper = replwrap.REPLWrapper( "irb --simple-prompt", ">> ", None, continuation_prompt="?> ") finally: signal.signal(signal.SIGINT, sig)
def _start_java_repl(self): sig = signal.signal(signal.SIGINT, signal.SIG_DFL) try: self.javawrapper = replwrap.REPLWrapper( "{} -jar {}".format(self._JAVA_COMMAND, self._KULLA_LOCATION), u'jshell> ', None, continuation_prompt=u' ...> ') finally: signal.signal(signal.SIGINT, sig)
def _start_java_repl(self): sig = signal.signal(signal.SIGINT, signal.SIG_DFL) try: self.javawrapper = replwrap.REPLWrapper( self._JAVA_COMMAND, u'\n-> ', None, continuation_prompt=u'\n>> ' ) finally: signal.signal(signal.SIGINT, sig)
def test_no_change_prompt(self): if platform.python_implementation() == 'PyPy': raise unittest.SkipTest("This test fails on PyPy because of REPL differences") child = pexpect.spawnu('python', echo=False, timeout=5) # prompt_change=None should mean no prompt change py = replwrap.REPLWrapper(child, replwrap.u(">>> "), prompt_change=None, continuation_prompt=replwrap.u("... ")) assert py.prompt == ">>> " res = py.run_command("for a in range(3): print(a)\n") assert res.strip().splitlines() == ['0', '1', '2']
def _start_egison(self): # Signal handlers are inherited by forked processes, and we can't easily # reset it from the subprocess. Since kernelapp ignores SIGINT except in # message handlers, we need to temporarily reset the SIGINT handler here # so that Egison is interruptible. sig = signal.signal(signal.SIGINT, signal.SIG_DFL) prompt = uuid.uuid4().hex + ">" try: self.egisonwrapper = replwrap.REPLWrapper( "egison -M latex --prompt " + prompt, text(prompt), None) finally: signal.signal(signal.SIGINT, sig)
def _start_topaz(self): # Signal handlers are inherited by forked processes, and we can't easily # reset it from the subprocess. Since kernelapp ignores SIGINT except in # message handlers, we need to temporarily reset the SIGINT handler here # so that topaz and its children are interruptible. sig = signal.signal(signal.SIGINT, signal.SIG_DFL) try: child = spawn(command="topaz", args=['-il'], timeout=1, echo=True, encoding='utf-8') self.topazwrapper = replwrap.REPLWrapper(child, orig_prompt='topaz> ', prompt_change='set user DataCurator pass swordfish gems gs64stone\nlogin', new_prompt='topaz 1> ') finally: signal.signal(signal.SIGINT, sig)
def _start_magma(self): # Signal handlers are inherited by forked processes, and we can't easily # reset it from the subprocess. Since kernelapp ignores SIGINT except in # message handlers, we need to temporarily reset the SIGINT handler here # so that bash and its children are interruptible. sig = signal.signal(signal.SIGINT, signal.SIG_DFL) try: magma = spawn('magma', echo=False, encoding='utf-8') magma.expect(u'> ') magma.sendline(u'SetLineEditor(false);') magma.expect(u'> ') magma.sendline(u'') self.magmawrapper = replwrap.REPLWrapper(magma, u'> ', u'SetPrompt("{}");') finally: signal.signal(signal.SIGINT, sig)
def test_no_change_prompt(self): if platform.python_implementation() == 'PyPy': raise unittest.SkipTest(skip_pypy) child = pexpect.spawn('python', echo=False, timeout=5, encoding='utf-8') # prompt_change=None should mean no prompt change py = replwrap.REPLWrapper(child, u">>> ", prompt_change=None, continuation_prompt=u"... ") assert py.prompt == ">>> " res = py.run_command("for a in range(3): print(a)\n") assert res.strip().splitlines() == ['0', '1', '2']
def execute_interactive_code(elem, doc): """Executes code blocks for a python shell. Parses the code in `elem.text` into blocks and executes them. Args: elem The AST element. doc The document. Return: The code with inline results. """ code_lines = [l[4:] for l in elem.text.split('\n')] code_blocks = [[code_lines[0]]] for line in code_lines[1:]: if line.startswith(' ') or line == '': code_blocks[-1].append(line) else: code_blocks.append([line]) final_code = [] try: child = replwrap.REPLWrapper("python", ">>> ", None) except NameError: pf.debug('Can not run interactive session. No output produced ' + '(Code was:\n{!s}\n)' .format(elem)) pf.debug('Please pip install pexpect.') return '' for code_block in code_blocks: result = child.run_command('\n'.join(code_block) + '\n').rstrip('\r\n') final_code += [('>>> ' if i == 0 else '... ') + l for i, l in enumerate(code_block)] if result: final_code += [r for r in result.split('\n') if r.strip() not in code_block] return '\n'.join(final_code)
def _start_acl2(self): sig = signal.signal(signal.SIGINT, signal.SIG_DFL) try: prompt_change_cmd = ''' (encapsulate () (set-state-ok t) (defun jupyter-prompt (channel state) (declare (xargs :mode :program)) (fmt1 "JPY-ACL2>" '() 0 channel state nil))) (set-ld-prompt 'jupyter-prompt state) (reset-prehistory)''' self.acl2wrapper = replwrap.REPLWrapper(os.environ['ACL2'], 'ACL2 !>', prompt_change_cmd, 'JPY-ACL2>') self.acl2wrapper.run_command( ';', timeout=None) # This discards the output of progn. self.acl2wrapper.run_command( ';', timeout=None) # This discards the output of reset-prehistory. finally: signal.signal(signal.SIGINT, sig)
def _start_gdl(self): # Signal handlers are inherited by forked processes, and we can't easily # reset it from the subprocess. Since kernelapp ignores SIGINT except in # message handlers, we need to temporarily reset the SIGINT handler here # so that GDL and its children are interruptible. sig = signal.signal(signal.SIGINT, signal.SIG_DFL) try: self._executable = find_executable("gdl") self._child = spawn(self._executable, timeout=300, encoding='utf-8') self.gdlwrapper = replwrap.REPLWrapper(self._child, u"GDL> ", None) finally: signal.signal(signal.SIGINT, sig) self.gdlwrapper.run_command( "!quiet=1 & defsysv,'!inline',0 & !more=0".rstrip(), timeout=None) self.gdlwrapper.run_command("SET_PLOT,'Z'".rstrip(), timeout=None) self.gdlwrapper.run_command( "DEVICE, SET_PIXEL_DEPTH=24, DECOMPOSED=1, SET_RESOLUTION=[800,600]" .rstrip(), timeout=None)
def _start_lisp(self): # Signal handlers are inherited by forked processes, and we can't easily # reset it from the subprocess. Since kernelapp ignores SIGINT except in # message handlers, we need to temporarily reset the SIGINT handler here # so that the child and its children are interruptible. #syslog.openlog('kernel') sig = signal.signal(signal.SIGINT, signal.SIG_DFL) try: # Note: the next few lines mirror functionality in the # bash() function of pexpect/replwrap.py. Look at the # source code there for comments and context for # understanding the code here. self.child = pexpect.spawn('lisp', [ '--eval', '(spocq.si::main-repl)', '--lispinit', '/opt/spocq/init.sxp' ], echo=False, encoding='utf-8', codec_errors='replace') self.lispwrapper = replwrap.REPLWrapper(self.child, u'\*', None, '', '') self.child.expect_exact('* ', -1) finally: signal.signal(signal.SIGINT, sig)
def _start_gap(self): # Signal handlers are inherited by forked processes, and we can't easily # reset it from the subprocess. Since kernelapp ignores SIGINT except in # message handlers, we need to temporarily reset the SIGINT handler here # so that gap and its children are interruptible. sig = signal.signal(signal.SIGINT, signal.SIG_DFL) try: # setup.g contains functions needed for Jupyter interfacing setupg = path.dirname(path.abspath(__file__)) if which('gap') != None: gap_run_command = which('gap') elif which('gap.sh') != None: gap_run_command = which('gap.sh') else: raise NameError('gap executable not found') self.gapwrapper = replwrap.REPLWrapper( gap_run_command + ' -n -b -T %s/gap/setup.g' % (setupg), u'gap|| ', None, None, continuation_prompt=u'|| ') self.gapwrapper.run_command("\n") finally: signal.signal(signal.SIGINT, sig)
def _start_smlnj(self): sig = signal.signal(signal.SIGINT, signal.SIG_DFL) try: self.smlnjwrapper = replwrap.REPLWrapper("sml", "\n- ", None) finally: signal.signal(signal.SIGINT, sig)
# Pre clean-up subprocess.call( ["docker", "container", "rm", "-f", "rnode-repl.{}".format(args.network)], stderr=subprocess.DEVNULL) volume = client.volumes.create() # Delete rnode-repl on network if it exists # result = subprocess.run(["docker", "container", "ls", "--all", "--format", "{{.Names}}"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) # containers = result.stdout.decode('ascii').splitlines() # for container in containers: # if container.endswith(args.network) and "rnode-repl" in container: # print("removing {}".format(container)) # result = subprocess.run([ "docker", "container", "rm", "-f", container ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) # Run rnode repl loader with replwrap cmd = "sudo docker run --rm -it -v {}:/var/lib/rnode --cpus=.5 --memory=1024m --name rnode-repl.{} --network {} coop.rchain/rnode --grpc-host node0.testnet1.rchain repl".format( volume.name, args.network, args.network) repl_cmds = ['5', '@"stdout"!("foo")'] conn = replwrap.REPLWrapper(cmd, args.prompt, None) for i in range(args.repeat): for repl_cmd in repl_cmds: result = conn.run_command(repl_cmd) print("repetition: {} output: {}".format(i, result)) # Post clean-up subprocess.call( ["docker", "container", "rm", "-f", "rnode-repl.{}".format(args.network)]) client.volumes.prune()
def _start_giac(self): self.giacwrapper = replwrap.REPLWrapper('giac', u'>> ', None)
def setUp(self): command = 'python' if platform == 'linux' or platform == 'darwin': command += '3' self.child = replwrap.REPLWrapper(f'{command} security_system.py', 'Input: ', None)
def _start_io(self): sig = signal.signal(signal.SIGINT, signal.SIG_DFL) try: self.iowrapper = replwrap.REPLWrapper("io", "Io> ", None) finally: signal.signal(signal.SIGINT, sig)
def setUp(self): self.wrapper = replwrap.REPLWrapper(cmd, prompt, None)