def parse_pipeline(self): if self.look() is None: raise ValueError,"Expected at least one argument to exec" commands = [Command([],[])] while 1: arg = self.lex() if arg is None: break elif arg == '|': commands.append(Command([],[])) elif arg == '|&': # Write this as a redirect of stderr; it must come first because # stdout may have already been redirected. commands[-1].redirects.insert(0, (('>&',2),'1')) commands.append(Command([],[])) elif arg[:4] in TclExecCommand.kRedirectPrefixes4: commands[-1].redirects.append(self.parse_redirect(arg, 4)) elif arg[:3] in TclExecCommand.kRedirectPrefixes3: commands[-1].redirects.append(self.parse_redirect(arg, 3)) elif arg[:2] in TclExecCommand.kRedirectPrefixes2: commands[-1].redirects.append(self.parse_redirect(arg, 2)) elif arg[:1] in TclExecCommand.kRedirectPrefixes1: commands[-1].redirects.append(self.parse_redirect(arg, 1)) else: commands[-1].args.append(arg) return Pipeline(commands, False, pipe_err=True)
def test_basic(self): self.assertEqual(self.parse('echo hello'), Pipeline([Command(['echo', 'hello'], [])], False)) self.assertEqual(self.parse('echo ""'), Pipeline([Command(['echo', ''], [])], False)) self.assertEqual(self.parse("""echo -DFOO='a'"""), Pipeline([Command(['echo', '-DFOO=a'], [])], False)) self.assertEqual(self.parse('echo -DFOO="a"'), Pipeline([Command(['echo', '-DFOO=a'], [])], False))
def test_redirection(self): self.assertEqual(self.parse('echo hello > c'), Pipeline([Command(['echo', 'hello'], [((('>'),), 'c')])], False)) self.assertEqual(self.parse('echo hello > c >> d'), Pipeline([Command(['echo', 'hello'], [(('>',), 'c'), (('>>',), 'd')])], False)) self.assertEqual(self.parse('a 2>&1'), Pipeline([Command(['a'], [(('>&',2), '1')])], False))
def test_basic(self): self.assertEqual(self.parse('echo hello'), (False, False, Pipeline([Command(['echo', 'hello'], [])], False, True))) self.assertEqual(self.parse('echo hello | grep hello'), (False, False, Pipeline([Command(['echo', 'hello'], []), Command(['grep', 'hello'], [])], False, True)))
def test_redirect(self): self.assertEqual(self.parse('echo hello > a >b >>c 2> d |& e'), (False, False, Pipeline([Command(['echo', 'hello'], [(('>&',2),'1'), (('>',),'a'), (('>',),'b'), (('>>',),'c'), (('>',2),'d')]), Command(['e'], [])], False, True)))
def test_pipeline(self): self.assertEqual( self.parse('a | b'), Pipeline( [Command(['a'], []), Command(['b'], [])], False)) self.assertEqual( self.parse('a | b | c'), Pipeline( [Command(['a'], []), Command(['b'], []), Command(['c'], [])], False))
def parse_command(self): tok = self.lex() if not tok: raise ValueError, "empty command!" if isinstance(tok, tuple): raise ValueError, "syntax error near unexpected token %r" % tok[0] args = [tok] redirects = [] while 1: tok = self.look() # EOF? if tok is None: break # If this is an argument, just add it to the current command. if isinstance(tok, str): args.append(self.lex()) continue # Otherwise see if it is a terminator. assert isinstance(tok, tuple) if tok[0] in ('|', ';', '&', '||', '&&'): break # Otherwise it must be a redirection. op = self.lex() arg = self.lex() if not arg: raise ValueError, "syntax error near token %r" % op[0] redirects.append((op, arg)) return Command(args, redirects)
def test_list(self): self.assertEqual( self.parse('a ; b'), Seq(Pipeline([Command(['a'], [])], False), ';', Pipeline([Command(['b'], [])], False))) self.assertEqual( self.parse('a & b'), Seq(Pipeline([Command(['a'], [])], False), '&', Pipeline([Command(['b'], [])], False))) self.assertEqual( self.parse('a && b'), Seq(Pipeline([Command(['a'], [])], False), '&&', Pipeline([Command(['b'], [])], False))) self.assertEqual( self.parse('a || b'), Seq(Pipeline([Command(['a'], [])], False), '||', Pipeline([Command(['b'], [])], False))) self.assertEqual( self.parse('a && b || c'), Seq( Seq(Pipeline([Command(['a'], [])], False), '&&', Pipeline([Command(['b'], [])], False)), '||', Pipeline([Command(['c'], [])], False))) self.assertEqual( self.parse('a; b'), Seq(Pipeline([Command(['a'], [])], False), ';', Pipeline([Command(['b'], [])], False)))
def test_basic(self): self.assertEqual(self.parse('echo hello'), Pipeline([Command(['echo', 'hello'], [])], False)) self.assertEqual(self.parse('echo ""'), Pipeline([Command(['echo', ''], [])], False))