Hello fellow Python users

I've always used Python as shell scripting language to automate tasks in various
projects, especially when doing complex work where Bash gets messy.

While Python is a much more powerful language than Bash, I've always
found the builtin subprocess.Popen APIs a bit inconvenient to invoke external
commands, especially for creating pipelines and doing redirection. Surely one
can use shell=True and Popen will invoke the shell, but this is not without
drawbacks since one has to deal with shell-specific syntax particularities,
such as argument quoting.

To make shell scripting in Python more convenient, I've created a new module:
https://github.com/tarruda/python-ush. The README and tests contains more
detailed examples but here's an idea of how it looks like:

  import ush
  sh = ush.Shell()

  ls, sort = sh('ls', 'sort')

  # by default, wait for command to exit and return status code for each in
  # pipeline. stdin, stdout and stderr are inherited.
  ls_exit_code, sort_exit_code = (ls | sort)()

  # iterate output, line by line
  for f in ls | sort:
    print(f)

  # collect all output into a string
  str(ls('-la') | sort('--reverse'))

  # redirect stdout

  ls | sort | 'output.txt'

  # append output to a file

  (ls | sort | '+output.txt')()

  # redirect stdin

  ('input.txt' | sort)()

  # filename expansion

  ls('*.py', glob=True)

The module is compatible with python 2 and 3, and should work on any Unix or
Windows. Also, since it is implemented in a single file (~650 LOC) without
dependencies, it should be easy to download and incorporate in a
another project.

Any feedback is appreciated
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to