def setUp(self):
        """Setup the test harness."""
        # Setup logging with a timestamp, the module, and the log level.
        logging.basicConfig(level=logging.DEBUG,
                            format=('%(asctime)s - %(module)s -'
                                    ' %(levelname)s - %(message)s'))

        # Create a tempfile that would represent the EC UART PTY.
        self.tempfile = tempfile.NamedTemporaryFile()

        # Create the pipes that the interpreter will use.
        self.cmd_pipe_user, self.cmd_pipe_itpr = threadproc_shim.Pipe()
        self.dbg_pipe_user, self.dbg_pipe_itpr = threadproc_shim.Pipe(
            duplex=False)

        # Mock the open() function so we can inspect reads/writes to the EC.
        self.ec_uart_pty = mock.mock_open()
        with mock.patch('__builtin__.open', self.ec_uart_pty):
            # Create an interpreter.
            self.itpr = interpreter.Interpreter(self.tempfile.name,
                                                self.cmd_pipe_itpr,
                                                self.dbg_pipe_itpr,
                                                log_level=logging.DEBUG,
                                                name="EC")

        # First, check that interpreter is initialized to connected.
        self.assertTrue(self.itpr.connected,
                        ('The interpreter should be'
                         ' initialized in a connected state'))
Пример #2
0
def main(argv):
    """Kicks off the EC-3PO interactive console interface and interpreter.

  We create some pipes to communicate with an interpreter, instantiate an
  interpreter, create a PTY pair, and begin serving the console interface.

  Args:
    argv: A list of strings containing the arguments this module was called
      with.
  """
    # Set up argument parser.
    parser = argparse.ArgumentParser(
        description=('Start interactive EC console '
                     'and interpreter.'))
    parser.add_argument('ec_uart_pty',
                        help=('The full PTY name that the EC UART'
                              ' is present on. eg: /dev/pts/12'))
    parser.add_argument('--log-level',
                        default='info',
                        help='info, debug, warning, error, or critical')

    # Parse arguments.
    opts = parser.parse_args(argv)

    # Set logging level.
    opts.log_level = opts.log_level.lower()
    if opts.log_level == 'info':
        log_level = logging.INFO
    elif opts.log_level == 'debug':
        log_level = logging.DEBUG
    elif opts.log_level == 'warning':
        log_level = logging.WARNING
    elif opts.log_level == 'error':
        log_level = logging.ERROR
    elif opts.log_level == 'critical':
        log_level = logging.CRITICAL
    else:
        parser.error(
            'Invalid log level. (info, debug, warning, error, critical)')

    # Start logging with a timestamp, module, and log level shown in each log
    # entry.
    logging.basicConfig(level=log_level,
                        format=('%(asctime)s - %(module)s -'
                                ' %(levelname)s - %(message)s'))

    # Create some pipes to communicate between the interpreter and the console.
    # The command pipe is bidirectional.
    cmd_pipe_interactive, cmd_pipe_interp = threadproc_shim.Pipe()
    # The debug pipe is unidirectional from interpreter to console only.
    dbg_pipe_interactive, dbg_pipe_interp = threadproc_shim.Pipe(duplex=False)

    # Create an interpreter instance.
    itpr = interpreter.Interpreter(opts.ec_uart_pty, cmd_pipe_interp,
                                   dbg_pipe_interp, log_level)

    # Spawn an interpreter process.
    itpr_process = threadproc_shim.ThreadOrProcess(
        target=interpreter.StartLoop, args=(itpr, ))
    # Make sure to kill the interpreter when we terminate.
    itpr_process.daemon = True
    # Start the interpreter.
    itpr_process.start()

    # Open a new pseudo-terminal pair
    (master_pty, user_pty) = pty.openpty()
    # Set the permissions to 660.
    os.chmod(os.ttyname(user_pty),
             (stat.S_IRGRP | stat.S_IWGRP | stat.S_IRUSR | stat.S_IWUSR))
    # Create a console.
    console = Console(master_pty, os.ttyname(user_pty), cmd_pipe_interactive,
                      dbg_pipe_interactive)
    # Start serving the console.
    v = threadproc_shim.Value(ctypes.c_bool, False)
    StartLoop(console, v)