Skip to content

Commit

Permalink
moved convience functions out of core extproc for better thematic co…
Browse files Browse the repository at this point in the history
…nsistency
  • Loading branch information
paddymul committed Jun 3, 2012
1 parent 380e0f2 commit dcf6bf4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 58 deletions.
58 changes: 58 additions & 0 deletions convience.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import tempfile
from extproc import Sh, Cmd, Pipe

def here(string):
"""
Make a temporary file from a string for use in redirection.
"""
t = tempfile.TemporaryFile()
t.write(string)
t.seek(0)
return t

def run(cmd, fd={}, e={}, cd=None):
"""
Perform a fork-exec-wait of a Cmd and return its exit status.
"""
return Cmd(cmd, fd=fd, e=e, cd=cd).run()

def cmd(cmd, fd={}, e={}, cd=None):
"""
Perform a fork-exec-wait of a Cmd and return the its stdout
as a byte string.
"""
f = Cmd(cmd, fd=fd, e=e, cd=cd).capture(1).stdout
try:
s = f.read()
finally:
f.close()
return s

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 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

def spawn(cmd, fd={}, e={}, cd=None, sh=False):
if sh:
return Sh(cmd, fd=fd, e=e, cd=cd).spawn()
else:
return Cmd(cmd, fd=fd, e=e, cd=cd).spawn()
56 changes: 0 additions & 56 deletions extproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,62 +458,6 @@ def capture(self, *fd):
return Capture(
self.fd[1], self.fd[2], [c.p.returncode for c in self.cmds])

def here(string):
"""
Make a temporary file from a string for use in redirection.
"""
t = tempfile.TemporaryFile()
t.write(string)
t.seek(0)
return t

def run(cmd, fd={}, e={}, cd=None):
"""
Perform a fork-exec-wait of a Cmd and return its exit status.
"""
return Cmd(cmd, fd=fd, e=e, cd=cd).run()

def cmd(cmd, fd={}, e={}, cd=None):
"""
Perform a fork-exec-wait of a Cmd and return the its stdout
as a byte string.
"""
f = Cmd(cmd, fd=fd, e=e, cd=cd).capture(1).stdout
try:
s = f.read()
finally:
f.close()
return s

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 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

def spawn(cmd, fd={}, e={}, cd=None, sh=False):
if sh:
return Sh(cmd, fd=fd, e=e, cd=cd).spawn()
else:
return Cmd(cmd, fd=fd, e=e, cd=cd).spawn()

if __name__ == '__main__':
import doctest
n = doctest.testmod().failed
Expand Down
4 changes: 2 additions & 2 deletions test_extproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import time
import os
import tempfile
from extproc import (
run, Sh, sh, Pipe, pipe, Cmd, here, JOBS, cmd)
from extproc import Sh, Pipe, Cmd, JOBS
from convience import run, sh, pipe, here, cmd
STDIN, STDOUT, STDERR = 0, 1, 2

def sh_strip(in_):
Expand Down

0 comments on commit dcf6bf4

Please sign in to comment.