Exemple #1
0
    def test_run(self):
        pipe_obj = Pipe(Sh("echo foo"),
                        Sh("cat; echo bar"),
                        Cmd("cat", {1: os.devnull}))

        self.assertEquals(pipe_obj.run(), 0)
        self.assertEquals(pipe_obj.returncodes, [0,0,0])
Exemple #2
0
    def _test_pipe_composable(self):
        """we should be able to compose pipes of pipes """
        Pipe(Pipe(Sh("echo foo")),
             Pipe(Sh("cat; echo bar")),
             Pipe(Cmd("cat", {1: os.devnull}))).run(),

        self.assertEquals(
            [0,0,0])


        self.assertEquals(len(JOBS), 0)
        " yes | grep no"
        yesno = Pipe(
            Pipe(Cmd('yes')),
            Pipe(Cmd(['grep', 'no']))).spawn()
        yesno.cmds[0].kill()
        self.assertEquals(yesno.cmds[-1].wait(), 1)
        self.assertEquals(yesno.wait(), 1)
        self.assertEquals(len(JOBS), 0)

        pipe_a = Pipe(Cmd('echo foo'))
        pdb.set_trace()
        ab = pipe_a._popen()

        self.assertSh(pipe_a.running_fd_objs[STDOUT].read(), 'foo')
Exemple #3
0
 def test_spawn(self):
     self.assertEquals(len(JOBS), 0)
     " yes | grep no"
     yesno = Pipe(Cmd('yes'), Cmd(['grep', 'no'])).spawn()
     yesno.cmds[0].kill()
     self.assertEquals(yesno.cmds[-1].wait(), 1)
     self.assertEquals(yesno.wait(), 1)
     self.assertEquals(len(JOBS), 0)
Exemple #4
0
def pipe(*cmds, **kwargs):
    """
  Run the pipeline with given Cmd's, then returns its stdout as a byte string.
  """
    f = Pipe(*cmds, **kwargs).capture(1).stdout
    try:
        s = f.read()
    finally:
        f.close()
    return s
Exemple #5
0
    def test_capture_timeout(self):
        ## make sure that capture doesn't return instantly
        cmd = Pipe(Cmd("/bin/sh -c 'sleep 1 && echo foo'"))
        self.assertSh(
            cmd.capture(1).stdout.read(), 'foo')

        ## make sure that timeout is honored, this process should take
        ## too long and be terminated
        cmd = Pipe(Cmd("/bin/sh -c 'sleep 5 && echo foo'"))
        self.assertSh(
            cmd.capture(1, timeout=1).stdout.read(), '')
Exemple #6
0
    def test_capture_timeout2(self):
        """ make sure that a timeout that is longer than a pipeline
        should take to execute doesn't alter the results of that
        pipeline
        """
        p = Pipe(Cmd('yes'), Cmd('head -n 10'))
        self.assertSh(
            p.capture(1).stdout.read(),
            'y\ny\ny\ny\ny\ny\ny\ny\ny\ny\n')

        p = Pipe(Cmd('yes'), Cmd('head -n 10'))
        self.assertSh(
            p.capture(1, timeout=1).stdout.read(),
            'y\ny\ny\ny\ny\ny\ny\ny\ny\ny\n')
Exemple #7
0
    def test_pipe_proc_decorator(self):
        @fork_dec
        def echoer(stdin_f, stdout_f, stderr_f):
            for line in stdin_f:
                stdout_f.write(line + "\n")
        pipe_obj = Pipe(Cmd("echo foo"),
                        echoer)
        self.assertSh(pipe_obj.capture(1).stdout.read(), 'foo')

        @fork_dec
        def doubler(stdin_f, stdout_f, stderr_f):
            for line in stdin_f:
                stdout_f.write(line+line + "\n")

        pipe_obj = Pipe(Cmd("echo foo"), doubler)
        self.assertSh(pipe_obj.capture(1).stdout.read(), 'foofoo')
Exemple #8
0
    def test_capture(self):
        self.assertSh(
            Pipe(Sh('echo foo; echo bar >&2', {2: os.devnull}),
                 Cmd('cat')).capture(1).stdout.read(),
            'foo')

        self.assertSh(
            Pipe(Sh('echo foo; echo bar >&2'),
                 Cmd('cat', {1: os.devnull})).capture(2).stderr.read(),
            'bar')

        sh_call = Pipe(Sh('echo bar >&2; echo foo; exit 1'))
        out, err, status = sh_call.capture(1, 2)
        self.assertEquals(status, 1)
        # these tests pass on OS X, not sure how they will run on
        # linux
        self.assertSh(out.read(), 'foo')
        self.assertSh(err.read(), 'bar')
Exemple #9
0
    def test_capture(self):
        sh_call = Sh('echo bar >&2; echo foo; exit 1')
        out, err, status = sh_call.capture(1, 2)
        self.assertEquals(status, 1)
        # these tests pass on OS X, not sure how they will run on
        # linux
        self.assertSh(out.read(), 'foo')
        self.assertSh(err.read(), 'bar')

        ### test Pipe impossible capture
        self.assertRaises(
            ValueError,
            lambda:pipe(Sh("echo bogus"), Cmd("cat", {1: os.devnull})))

        ### test Cmd impossible capture
        self.assertRaises(
            ValueError,
            lambda: sh("echo bogus stuff", {1: os.devnull}))

        ### test Pipe stderr capture
        pipe_ = Pipe(Sh('echo foo; sleep 0.01; echo  bar >&2'), Sh('cat >&2'))
        self.assertSh(pipe_.capture(2).stderr.read(), 'foobar')
Exemple #10
0
    def _asfd(self):
        pipe_a=Pipe(Cmd('ls /usr/local/Cellar'))

        pipe_b=Pipe(Cmd('wc -l'))
        outer_pipe = Pipe(pipe_a, pipe_b)
        self.assertSh(outer_pipe.capture(1).stdout.read(), '10')
Exemple #11
0
    def test_pipe_proc_interface(self):
        ### test Pipe ENV
        pipe_obj = Pipe(Pipe(Cmd("/bin/sh -c 'echo foo'")))

        self.assertSh(pipe_obj.capture(1).stdout.read(), 'foo')