Skip to main content

A subprocess wrapper which provides command pipeline functionality and easy data capture.

Project description

The sarge package provides a wrapper for subprocess which provides command
pipeline functionality.

This package leverages subprocess to provide easy-to-use cross-platform command
pipelines with a Posix flavour: you can have chains of commands using ``;``, ``&``, pipes using ``|`` and ``|&``, and redirection.

Here's a taster (example suggested by Kenneth Reitz's Envoy documentation):

>>> from sarge import capture_stdout
>>> p = capture_stdout('fortune|cowthink')
>>> p.returncode
0
>>> p.commands
[Command('fortune'), Command('cowthink')]
>>> p.returncodes
[0, 0]
>>> print(p.stdout.text)
____________________________________
( The last thing one knows in )
( constructing a work is what to put )
( first. )
( )
( -- Blaise Pascal )
------------------------------------
o ^__^
o (oo)\_______
(__)\ )\/\
||----w |
|| ||

The ``capture_stdout`` function is a convenient form of an underlying
function, ``run``. You can also use conditionals:

>>> from sarge import run
>>> p = run('false && echo foo')
>>> p.commands
[Command('false')]
>>> p.returncodes
[1]
>>> p.returncode
1
>>> p = run('false || echo foo')
foo
>>> p.commands
[Command('false'), Command('echo foo')]
>>> p.returncodes
[1, 0]
>>> p.returncode
0

The conditional logic is being done by sarge and not the shell -- which means
you can use the identical code on Windows. Here's an example of some more
involved use of pipes, which also works identically on Posix and Windows:

>>> cmd = 'echo foo | tee stdout.log 3>&1 1>&2 2>&3 | tee stderr.log > %s' % os.devnull
>>> p = run(cmd)
>>> p.commands
[Command('echo foo'), Command('tee stdout.log'), Command('tee stderr.log')]
>>> p.returncodes
[0, 0, 0]
>>>
vinay@eta-oneiric64:~/projects/sarge$ cat stdout.log
foo
vinay@eta-oneiric64:~/projects/sarge$ cat stderr.log
foo

In the above example, the first tee invocation swaps its ``stderr`` and
``stdout`` -- see `this post <http://goo.gl/Enl0c>`_ for a longer explanation
of this somewhat esoteric usage.

Documentation
-------------

Documentation is available at:

http://sarge.readthedocs.org/

Source code
-----------

The development repository for sarge can be found at:

https://bitbucket.org/vinay.sajip/sarge/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sarge-0.1.1.tar.gz (42.9 kB view details)

Uploaded Source

File details

Details for the file sarge-0.1.1.tar.gz.

File metadata

  • Download URL: sarge-0.1.1.tar.gz
  • Upload date:
  • Size: 42.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for sarge-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9ece4cbcce750312c82f45b8463298891e943deb8e12f2a07f22f18ddad25907
MD5 2a9b9d46e4ef6ae51e2a5ff7de93d9dd
BLAKE2b-256 f7c0e5f9cedeea9408ce77e5413a79ce89988962a1bd406940e210aa529a10c7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page