[Python-ideas] shutil.runret and shutil.runout

Nick Coghlan ncoghlan at gmail.com
Fri Feb 24 15:54:19 CET 2012


On Sat, Feb 25, 2012 at 12:32 AM, Paul Moore <p.f.moore at gmail.com> wrote:
> On 24 February 2012 12:41, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> Kenneth Reitz (author of "requests") has also spent some time
>> tinkering with subprocess invocation API design concepts:
>> https://github.com/kennethreitz/envoy
>
> Vinay Sanjip extended this with "sarge" (available on PyPI, IIRC). One
> key advantage of sarge for me is that it handles piping and
> redirection in a cross-platfom manner, rather than just deferring to
> the shell. (I think envoy does this too, but it's not very reliable on
> WIndows from what I recall of my brief experiments).

Ah, I knew I'd seen a more polished version of that somewhere - Vinay
posted about it a while back.

As I see it, the two complement each other fairly nicely:

shell_command is for direct access to the system shell. Appropriate
when you're writing platform specific administration scripts.

sarge is for cross platform scripting support. I'm actually not sure
what this is useful for (since the default Windows shell has different
spellings for so many basic commands and different syntax for
environment variable expansion, it seems easier to just use the
*actual* cross platform abstractions in the os module instead), but
apparently it's good for something (or Vinay wouldn't have taken the
time to write it).

Of course, since it's just a convenience wrapper around Popen,
ShellCommand does let you get pretty cute:

>>> import sys
>>> from functools import partial
>>> from shell_command import ShellCommand
>>> code = """
... def f():
...     print("Python in a subprocess, easy as!")
... f()
... """
>>> PyCmd = partial(ShellCommand, executable=sys.executable)
>>> PyCmd(code).shell_call()
Python in a subprocess, easy as!
0
>>> x = PyCmd("print('Reporting for duty!')").shell_output()
>>> x
'Reporting for duty!'

(I didn't actually do a great deal in ShellCommand to enable that -
it's just a matter of passing all the keyword args through to
subprocess.Popen)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list