Exemple #1
0
    def testPipeline2(self):
        arena = test_lib.MakeArena('testPipeline')
        ex = test_lib.InitExecutor(arena=arena)

        Banner('ls | cut -d . -f 1 | head')
        p = process.Pipeline()
        p.Add(_ExtProc(['ls']))
        p.Add(_ExtProc(['cut', '-d', '.', '-f', '1']))

        node = _CommandNode('head', arena)
        p.AddLast((ex, node))

        fd_state = process.FdState()
        print(p.Run(_WAITER, _FD_STATE))

        # Simulating subshell for each command
        node1 = _CommandNode('ls', arena)
        node2 = _CommandNode('head', arena)
        node3 = _CommandNode('sort --reverse', arena)

        p = process.Pipeline()
        p.Add(Process(process.SubProgramThunk(ex, node1)))
        p.Add(Process(process.SubProgramThunk(ex, node2)))
        p.Add(Process(process.SubProgramThunk(ex, node3)))

        last_thunk = (ex, _CommandNode('cat', arena))
        p.AddLast(last_thunk)

        print(p.Run(_WAITER, _FD_STATE))
Exemple #2
0
  def testPipeline2(self):
    cmd_ev = test_lib.InitCommandEvaluator(arena=_ARENA, ext_prog=_EXT_PROG)

    Banner('ls | cut -d . -f 1 | head')
    p = process.Pipeline()
    p.Add(_ExtProc(['ls']))
    p.Add(_ExtProc(['cut', '-d', '.', '-f', '1']))

    node = _CommandNode('head', _ARENA)
    p.AddLast((cmd_ev, node))

    fd_state = process.FdState(_ERRFMT, _JOB_STATE)
    print(p.Run(_WAITER, _FD_STATE))

    # Simulating subshell for each command
    node1 = _CommandNode('ls', _ARENA)
    node2 = _CommandNode('head', _ARENA)
    node3 = _CommandNode('sort --reverse', _ARENA)

    p = process.Pipeline()
    p.Add(Process(process.SubProgramThunk(cmd_ev, node1), _JOB_STATE))
    p.Add(Process(process.SubProgramThunk(cmd_ev, node2), _JOB_STATE))
    p.Add(Process(process.SubProgramThunk(cmd_ev, node3), _JOB_STATE))

    last_thunk = (cmd_ev, _CommandNode('cat', _ARENA))
    p.AddLast(last_thunk)

    print(p.Run(_WAITER, _FD_STATE))
Exemple #3
0
    def testPipeline2(self):
        Banner('ls | cut -d . -f 1 | head')
        p = process.Pipeline()
        p.Add(_ExtProc(['ls']))
        p.Add(_ExtProc(['cut', '-d', '.', '-f', '1']))
        p.Add(_ExtProc(['head']))

        print(p.Run(_WAITER))

        ex = InitExecutor()

        # Simulating subshell for each command
        w1 = ast.CompoundWord()
        w1.parts.append(ast.LiteralPart(ast.token(Id.Lit_Chars, 'ls')))
        node1 = ast.SimpleCommand()
        node1.words = [w1]

        w2 = ast.CompoundWord()
        w2.parts.append(ast.LiteralPart(ast.token(Id.Lit_Chars, 'head')))
        node2 = ast.SimpleCommand()
        node2.words = [w2]

        w3 = ast.CompoundWord()
        w3.parts.append(ast.LiteralPart(ast.token(Id.Lit_Chars, 'sort')))
        w4 = ast.CompoundWord()
        w4.parts.append(ast.LiteralPart(ast.token(Id.Lit_Chars, '--reverse')))
        node3 = ast.SimpleCommand()
        node3.words = [w3, w4]

        p = process.Pipeline()
        p.Add(Process(process.SubProgramThunk(ex, node1)))
        p.Add(Process(process.SubProgramThunk(ex, node2)))
        p.Add(Process(process.SubProgramThunk(ex, node3)))

        print(p.Run(_WAITER))
Exemple #4
0
    def testPipeline2(self):
        cmd_ev = test_lib.InitCommandEvaluator(arena=self.arena,
                                               ext_prog=self.ext_prog)

        Banner('ls | cut -d . -f 1 | head')
        p = process.Pipeline()
        p.Add(self._ExtProc(['ls']))
        p.Add(self._ExtProc(['cut', '-d', '.', '-f', '1']))

        node = _CommandNode('head', self.arena)
        p.AddLast((cmd_ev, node))

        print(p.Run(self.waiter, self.fd_state))

        # Simulating subshell for each command
        node1 = _CommandNode('ls', self.arena)
        node2 = _CommandNode('head', self.arena)
        node3 = _CommandNode('sort --reverse', self.arena)

        p = process.Pipeline()
        p.Add(
            Process(process.SubProgramThunk(cmd_ev, node1), self.job_state,
                    self.tracer))
        p.Add(
            Process(process.SubProgramThunk(cmd_ev, node2), self.job_state,
                    self.tracer))
        p.Add(
            Process(process.SubProgramThunk(cmd_ev, node3), self.job_state,
                    self.tracer))

        last_thunk = (cmd_ev, _CommandNode('cat', self.arena))
        p.AddLast(last_thunk)

        print(p.Run(self.waiter, self.fd_state))
Exemple #5
0
  def _MakeProcess(self, node, job_state=None, disable_errexit=False):
    """
    Assume we will run the node in another process.  Return a process.
    """
    if node.tag == command_e.ControlFlow:
      # Pipeline or subshells with control flow are invalid, e.g.:
      # - break | less
      # - continue | less
      # - ( return )
      # NOTE: This could be done at parse time too.
      e_die('Invalid control flow %r in pipeline / subshell / background',
            node.token.val, token=node.token)

    # NOTE: If ErrExit(), we could be verbose about subprogram errors?  This
    # only really matters when executing 'exit 42', because the child shell
    # inherits errexit and will be verbose.  Other notes:
    #
    # - We might want errors to fit on a single line so they don't get
    # interleaved.
    # - We could turn the `exit` builtin into a FatalRuntimeError exception and
    # get this check for "free".
    thunk = process.SubProgramThunk(self, node,
                                    disable_errexit=disable_errexit)
    p = process.Process(thunk, job_state=job_state)
    return p
Exemple #6
0
  def _GetProcessForNode(self, node):
    """
    Assume we will run the node in another process.  Return a process.
    """
    if node.tag == command_e.SimpleCommand:
      words = braces.BraceExpandWords(node.words)
      argv = self.ev.EvalWordSequence(words)
      more_env = self.mem.GetExported()
      self._EvalEnv(node.more_env, more_env)
      thunk = self._GetThunkForSimpleCommand(argv, more_env)

    elif node.tag == command_e.ControlFlow:
      # Pipeline or subshells with control flow are invalid, e.g.:
      # - break | less
      # - continue | less
      # - ( return )
      # NOTE: This could be done at parse time too.
      e_die('Invalid control flow %r in pipeline or subshell', node.token.val,
            token=node.token)

    else:
      thunk = process.SubProgramThunk(self, node)

    redirects = self._EvalRedirects(node)
    p = process.Process(thunk, fd_state=self.fd_state, redirects=redirects)
    return p
Exemple #7
0
    def _MakeProcess(self, node, parent_pipeline=None, inherit_errexit=True):
        # type: (command_t, process.Pipeline, bool) -> process.Process
        """
    Assume we will run the node in another process.  Return a process.
    """
        UP_node = node
        if node.tag_() == command_e.ControlFlow:
            node = cast(command__ControlFlow, UP_node)
            # Pipeline or subshells with control flow are invalid, e.g.:
            # - break | less
            # - continue | less
            # - ( return )
            # NOTE: This could be done at parse time too.
            if node.token.id != Id.ControlFlow_Exit:
                e_die(
                    'Invalid control flow %r in pipeline / subshell / background',
                    node.token.val,
                    token=node.token)

        # NOTE: If ErrExit(), we could be verbose about subprogram errors?  This
        # only really matters when executing 'exit 42', because the child shell
        # inherits errexit and will be verbose.  Other notes:
        #
        # - We might want errors to fit on a single line so they don't get
        # interleaved.
        # - We could turn the `exit` builtin into a FatalRuntimeError exception and
        # get this check for "free".
        thunk = process.SubProgramThunk(self.cmd_ev,
                                        node,
                                        inherit_errexit=inherit_errexit)
        p = process.Process(thunk, self.job_state)
        p.Init_ParentPipeline(parent_pipeline)
        return p