library to do easy shell scripting in Python

M

Michael Torrie

Recently a post that mentioned a recipe that extended subprocess to
allow killable processes caused me to do some thinking. Some of my
larger bash scripts are starting to become a bit unwieldy (hundreds of
lines of code). Yet for many things bash just works out so well because
it is so close to the file system and processes. As part of another
project, I now have need of a really good library to make it almost as
easy to do things in Python as it is in Bash. With a simple wrapper
around subprocess, I'm pretty much able to do most things. Most of my
complicated bash hackery involves using awk, sed, grep, and cut to
process text, which python does quite nicely, thank you very much. But
there's a few things to add.

To wit, I'm wanting to write a library that can deal with the following
things:

- spawn a process, feed it std in, get stdout, stderr, and err code.
This is largely already accomplished by subprocess
- spawn off processes as background daemons
- spawn multiple processes and pipe output to input.
- can do fancier things like bash does, like combine stderr/stdout,
switch stderr/stdout, redirects to and from files
- transparently allow a python function or object to be a part of
the pipeline at any stage.

Questions include, how would one design the interface for things, like
assembling pipes? Several ideas include:

pipe([prog1,args],[prog2,args],...)

or

run([prog1,args]).pipe([prog2,args]).pipe(...)

The former doesn't deal very well with re-plumbing of the pipes, nor is
there an easy way to redirect to and from a file. The second syntax is
more flexible but a bit cumbersome. Also it doesn't allow redirection
or flexible plumbing either.

Any ideas on how I could design this?
 
A

alex23

pipe([prog1,args],[prog2,args],...)
Any ideas on how I could design this?

There's a recipe on Activestate's Python Cookbook that does pretty
much this:
Allows arbitrary number of commands to be strung together with
each one feeding into the next ones input. Syntax is simple:
x=pipe("cmd1", "cmd2", "cmd3").read() is equivalent to bash
command x=`cmd1 | cmd2 | cmd3`.

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/475171

I haven't used it myself, but it might make a good place to start.

- alex23
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top