Example #1
0
    def __init__(self, cmd='', prompt=None, rx=None):
        if not cmd: cmd = '{} {}'.format(dist, mimic_bin)
        if not prompt: prompt = 'Enter word or sentence (EXIT to break): '

        if isinstance(cmd, str):
            self._repl = REPLWrapper(cmd, prompt, None)

        else:
            self._repl = cmd
        self.nodes = []
        self.rx = rx if rx else '\s+(?P<Word>\S+)\t+.*'
Example #2
0
 def __init__(
     self,
     cmd_or_spawn,
     extra_init_cmd=None,
 ):
     REPLWrapper.__init__(
         self,
         cmd_or_spawn,
         u"(^|\r\n)> ",
         None,
         continuation_prompt=u"^  ",
         extra_init_cmd=extra_init_cmd,
     )
def test_job_attach_tty(helper: Helper) -> None:
    job_id = helper.run_job_and_wait_state(UBUNTU_IMAGE_NAME, "sh", tty=True)

    status = helper.job_info(job_id)
    assert status.container.tty

    expect = helper.pexpect(["job", "attach", job_id])
    expect.expect("========== Job is running in terminal mode =========")
    expect.sendline("\n")  # prompt may be missing after the connection.
    repl = REPLWrapper(expect, "# ", None)
    ret = repl.run_command("echo abc\n")
    assert ret.strip() == "echo abc\r\r\nabc"

    helper.kill_job(job_id)
    def __init__(self,
                 gdb='riscv64-unknown-elf-gdb',
                 openocd='openocd',
                 openocd_config_filename='./testing/targets/ssith_gfe.cfg',
                 timeout=60):
        print_and_log("Starting GDB session...")
        if not which(gdb):
            raise GdbError('Executable {} not found'.format(gdb))
        if not which(openocd):
            raise GdbError('Executable {} not found'.format(openocd))
        xlen = 32  # Default
        try:
            run_and_log(print_and_log("Starting openocd"), run([openocd, '-f', openocd_config_filename], \
                        timeout=0.5, stdout=PIPE, stderr=PIPE))
        except TimeoutExpired as exc:
            log = str(exc.stderr, encoding='utf-8')
            match = re.search('XLEN=(32|64)', log)
            if match:
                xlen = int(match.group(1))
            else:
                raise GdbError('XLEN not found by OpenOCD')

        # Create temporary files for gdb and openocd.  The `NamedTemporaryFile`
        # objects are stored in `self` so the files stay around as long as this
        # `GdbSession` is in use.  We open both in text mode, instead of the
        # default 'w+b'.
        self.openocd_log = tempfile.NamedTemporaryFile(mode='w',
                                                       prefix='openocd.',
                                                       suffix='.log')
        self.gdb_log = tempfile.NamedTemporaryFile(mode='w',
                                                   prefix='gdb.',
                                                   suffix='.log')

        init_cmds = '\n'.join([
            'set confirm off', 'set pagination off'
            'set width 0', 'set height 0', 'set print entry-values no',
            'set remotetimeout {}'.format(timeout),
            'set arch riscv:rv{}'.format(xlen),
            'target remote | {} -c "gdb_port pipe; log_output {}" -f {}'.
            format(openocd, self.openocd_log.name, openocd_config_filename)
        ])

        self.pty = spawn(gdb,
                         encoding='utf-8',
                         logfile=self.gdb_log,
                         timeout=timeout)
        self.repl = REPLWrapper(self.pty,
                                '(gdb) ',
                                None,
                                extra_init_cmd=init_cmds)
Example #5
0
 def new_session(settings, command=None):
     """Returns a new REPLWrapper"""
     return REPLWrapper(
         command or settings.shell['command'],
         settings.shell['prompt'],
         None,
         continuation_prompt=settings.shell['continuation'])
Example #6
0
class Distance():
    # Word2Vec distance wrapper by default

    def __init__(self, cmd='', prompt=None, rx=None):
        if not cmd: cmd = '{} {}'.format(dist, mimic_bin)
        if not prompt: prompt = 'Enter word or sentence (EXIT to break): '

        if isinstance(cmd, str):
            self._repl = REPLWrapper(cmd, prompt, None)

        else:
            self._repl = cmd
        self.nodes = []
        self.rx = rx if rx else '\s+(?P<Word>\S+)\t+.*'

    def find(self, words, num, max_levels=1, unique=True, level=0):
        first = self._repl.run_command(words, None).split('\n')[5:num + 5]
        first = [re.match(self.rx, line).group('Word') for line in first]

        if level < max_levels:
            rest = [
                Distance(self._repl).find(first[idx],
                                          num,
                                          max_levels,
                                          level=level + 1)
                for idx in range(len(first))
            ]
            first.extend(rest)
            first = [
                word for sublist in first if isinstance(sublist, list)
                for word in sublist
            ]
            if unique: first = list(set(first))
        return first
Example #7
0
 def start_kotlin_shell(self):
     if platform.system() == 'Windows':
         kotlin_shell_bin = 'kotlinc-jvm.bat'
     else:
         kotlin_shell_bin = 'kotlinc-jvm'
     kotlin = REPLWrapper(kotlin_shell_bin, '>>> ', None,
                          continuation_prompt='... ')
     return kotlin
Example #8
0
class SparkKernel(Kernel):
  implementation = 'IPython'
  implementation_version = '1.0'
  banner = "Spark Kernel"
  language = 'en-us'
  language_version = '0.1'
  language_info = {'mimetype': 'text/plain', 'name': 'python'}

  def __init__(self, **kwargs):
    Kernel.__init__(self, **kwargs)
    self._start_spark()

  def _start_spark(self):
    '''Creates a pexpect.spawn, then passes to the REPLWrapper in order to allow for a larget timeout value.'''
    self.spark_spawn = spawn('spark-shell', timeout=60)
    self.sparkwrapper = REPLWrapper(self.spark_spawn, u'scala>', None)
    # spark_spawn.expect() needs implemented

  def do_execute(self, code, silent, store_history=True, user_expressions=None, allow_stdin=False):
    if match('exit.*', code):
      stream_content = {'name': 'stdout', 'text': 'To exit, just shutdown the kernel!'}
      self.send_response(self.iopub_socket, 'stream', stream_content)
      return {'status': 'ok',
              'execution_count': self.execution_count,
              'payload': [],
              'user_expressions': {},
              }
    elif match('%', code):
      stream_content = {'name': 'stdout', 'text': 'No magics :-( ....sorry.'}
      self.send_response(self.iopub_socket, 'stream', stream_content)
      return {'status': 'ok',
              'execution_count': self.execution_count,
              'payload': [],
              'user_expressions': {},
              }
    else:
      output = self.sparkwrapper.run_command(code)
      if not silent:
        try:
          stream_content = {'name': 'stdout', 'text': output}
          self.send_response(self.iopub_socket, 'stream', stream_content)
          return {'status': 'ok',
                  'execution_count': self.execution_count,
                  'payload': [],
                  'user_expressions': {},
                  }
        finally:
          print('finally')
        
        # Need some exception handling here.

  def do_shutdown(self, restart):
    '''Ensure exit command gets sent to the spark-shell.'''
    self.spark_spawn.send('exit\r\n')
    self.spark_spawn.expect(EOF)
    self.spark_spawn.close()
Example #9
0
def cypher_shell():
    if platform == 'Windows':
        cypher_shell_bin = os.path.join('cypher_kernel', 'java', 
                                        'cypher-shell.bat')
    else:
        cypher_shell_bin = 'cypher_kernel/java/cypher-shell'
    base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    cypher_shell_bin = os.path.join(base_path, cypher_shell_bin)
    cypher = REPLWrapper(f'{cypher_shell_bin} -u {test_cfg.user} -p {test_cfg.pwd} --format verbose', 'neo4j> ', None)
    return cypher
Example #10
0
    def do_execute(self,
                   code,
                   silent,
                   store_history=True,
                   user_expressions=None,
                   allow_stdin=False):

        zdict = REPLWrapper(u"zdict", u"[zDict]: ", None)
        output = zdict.run_command(code.strip())

        if not silent:
            stream_content = {'name': 'stdout', 'text': output}
            self.send_response(self.iopub_socket, 'stream', stream_content)

        return {
            'status': 'ok',
            # The base class increments the execution count
            'execution_count': self.execution_count,
            'payload': [],
            'user_expressions': {},
        }
Example #11
0
 def start_cypher_shell(self):
     if platform.system() == 'Windows':
         cypher_shell_bin = os.path.join('java', 'cypher-shell.bat')
     else:
         cypher_shell_bin = 'java/cypher-shell'
     cypher_shell_bin = os.path.join(
         os.path.dirname(os.path.abspath(__file__)), cypher_shell_bin)
     cypher = REPLWrapper(
         f'{cypher_shell_bin} -u {self.user} -p {self.pwd} --format verbose',
         'neo4j> ',
         None,
         continuation_prompt='  ...: ')
     return cypher
Example #12
0
class WCCGProcess:
    """Holds a wccg process to be reused."""
    def __init__(self):
        self.mutex = multiprocessing.Lock()
        self._new_repl()

    def _new_repl(self):
        self.repl = REPLWrapper(
            f'wccg -prompt "{PEXPECT_PROMPT}" -showsem -showall {os.environ.get("GRAMMAR_DIR", "/grammar")}',
            PEXPECT_PROMPT + ' ', None)

    def parse(self, sentence):
        """Parse a sentence by passing it to the wccg process and read the results.
        This is thread-safe, so multiple processes can try to access the process.
        """
        with self.mutex:
            return self.repl.run_command(sentence)
Example #13
0
def feedback_loop(phase_settings, program_path, cwd=str(Path.cwd() / '..')):
    amplifiers = [
        REPLWrapper(f'python3 ../intcomputer.py -p "> " {program_path}', '> ',
                    None) for i in 'abcde'
    ]
    # init phases
    for amplifier, phase in zip(amplifiers, phase_settings):
        try:
            amplifier.run_command(str(phase), timeout=2)
        except (pexpect.TIMEOUT, pexpect.EOF):
            print('Failed at initialization')
    signal = str(0)
    running = True
    while running:
        for c, amplifier in zip('abcde', amplifiers):
            try:
                signal = amplifier.run_command(signal,
                                               timeout=1).splitlines()[0]
            except (pexpect.EOF, OSError):
                running = False
                break
    return int(signal)
Example #14
0
 def _new_repl(self):
     self.repl = REPLWrapper(
         f'wccg -prompt "{PEXPECT_PROMPT}" -showsem -showall {os.environ.get("GRAMMAR_DIR", "/grammar")}',
         PEXPECT_PROMPT + ' ', None)
Example #15
0
 def _start_spark(self):
   '''Creates a pexpect.spawn, then passes to the REPLWrapper in order to allow for a larget timeout value.'''
   self.spark_spawn = spawn('spark-shell', timeout=60)
   self.sparkwrapper = REPLWrapper(self.spark_spawn, u'scala>', None)
Example #16
0
class GdbSession(object):
    '''Wraps a pseudo-terminal interface to GDB on the FPGA via OpenOCD.'''
    def __init__(self,
                 gdb='riscv64-unknown-elf-gdb',
                 openocd='openocd',
                 openocd_config_filename='./testing/targets/ssith_gfe.cfg',
                 timeout=60):
        print_and_log("Starting GDB session...")
        if not which(gdb):
            raise GdbError('Executable {} not found'.format(gdb))
        if not which(openocd):
            raise GdbError('Executable {} not found'.format(openocd))
        xlen = 32  # Default
        try:
            run_and_log(print_and_log("Starting openocd"), run([openocd, '-f', openocd_config_filename], \
                        timeout=0.5, stdout=PIPE, stderr=PIPE))
        except TimeoutExpired as exc:
            log = str(exc.stderr, encoding='utf-8')
            match = re.search('XLEN=(32|64)', log)
            if match:
                xlen = int(match.group(1))
            else:
                raise GdbError('XLEN not found by OpenOCD')

        # Create temporary files for gdb and openocd.  The `NamedTemporaryFile`
        # objects are stored in `self` so the files stay around as long as this
        # `GdbSession` is in use.  We open both in text mode, instead of the
        # default 'w+b'.
        self.openocd_log = tempfile.NamedTemporaryFile(mode='w',
                                                       prefix='openocd.',
                                                       suffix='.log')
        self.gdb_log = tempfile.NamedTemporaryFile(mode='w',
                                                   prefix='gdb.',
                                                   suffix='.log')

        init_cmds = '\n'.join([
            'set confirm off', 'set pagination off'
            'set width 0', 'set height 0', 'set print entry-values no',
            'set remotetimeout {}'.format(timeout),
            'set arch riscv:rv{}'.format(xlen),
            'target remote | {} -c "gdb_port pipe; log_output {}" -f {}'.
            format(openocd, self.openocd_log.name, openocd_config_filename)
        ])

        self.pty = spawn(gdb,
                         encoding='utf-8',
                         logfile=self.gdb_log,
                         timeout=timeout)
        self.repl = REPLWrapper(self.pty,
                                '(gdb) ',
                                None,
                                extra_init_cmd=init_cmds)

    def __del__(self):
        print_and_log("Closing GDB session...")
        self.pty.close()

    def cmd(self, gdb_command_string, timeout=-1):
        if not self.pty.isalive():
            raise GdbError('Dead process')
        try:
            reply = self.repl.run_command(gdb_command_string, timeout=timeout)
        except TIMEOUT as exc:
            self.pty.close()
            raise GdbError('Timeout expired') from exc
        except EOF as exc:
            self.pty.close()
            raise GdbError('Read end of file') from exc
        return reply

    def cont(self):
        if not self.pty.isalive():
            raise GdbError('Dead process')
        self.pty.send("continue\n")

    def interrupt(self):
        # send ctrl-c and wait for prompt
        return self.cmd('\003')

    def c(self, timeout):
        try:
            self.cmd('c', timeout=timeout)
        except GdbError:
            self.interrupt()

    def x(self, address, size='w'):
        output = self.cmd("x/{} {:#x}".format(size, address))
        print_and_log('Read raw output: {}'.format(output))
        if ':' in output:
            value = int(output.split(':')[1], base=0)
        else:
            raise GdbError('Failed to read from address {:#x}'.format(address))
        return value

    def read32(self, address, debug_text=None):
        value = self.x(address=address, size="1w")
        if debug_text is not None:
            print_and_log("{} Read: {:#x} from {:#x}".format(
                debug_text, value, address))
        return value