library to do easy shell scripting in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Torrie

    library to do easy shell scripting in Python

    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?
  • alex23

    #2
    Re: library to do easy shell scripting in Python

    On Apr 24, 12:22 pm, Michael Torrie <torr...@gmail. comwrote:
    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").rea d() is equivalent to bash
    command x=`cmd1 | cmd2 | cmd3`.


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

    - alex23

    Comment

    Working...