def sh(cmd, fd={}, e={}, cd=None): """ Perform a fork-exec-wait of a Sh command and return its stdout as a byte string. """ f = Sh(cmd, fd=fd, e=e, cd=cd).capture(1).stdout try: s = f.read() finally: f.close() return s
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')