Skip to content
Subprocesses for Humans 2.0.
Python
Branch: master
Clone or download
Latest commit 194aa92 May 6, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
tests Initial test infrastructure, covers #57 Feb 14, 2019
.gitignore black formatting, and add .ok Sep 17, 2018
LICENSE Add LICENSE and MANIFEST.in Apr 20, 2018
MANIFEST.in Add tests to sdist Mar 8, 2019
Pipfile migrate to pipfile Jan 23, 2017
Pipfile.lock migrate to pipfile Jan 23, 2017
README.rst Update README.rst May 6, 2019
delegator.py Correctly cache Command.err Feb 14, 2019
setup.py Added tests_require Feb 14, 2019
tox.ini Initial test infrastructure, covers #57 Feb 14, 2019

README.rst

Delegator.py — Subprocesses for Humans 2.0

Delegator.py is a simple library for dealing with subprocesses, inspired by both envoy and pexpect (in fact, it depends on it!).

This module features two main functions delegator.run() and delegator.chain(). One runs commands, blocking or non-blocking, and the other runs a chain of commands, separated by the standard unix pipe operator: |.

Basic Usage

Basic run functionality:

>>> c = delegator.run('ls')
>>> print c.out
README.rst   delegator.py

>>> c = delegator.run('long-running-process', block=False)
>>> c.pid
35199
>>> c.block()
>>> c.return_code
0

Commands can be passed in as lists as well (e.g. ['ls', '-lrt']), for parameterization.

Basic chain functionality:

# Can also be called with ([['fortune'], ['cowsay']]).
# or, delegator.run('fortune').pipe('cowsay')

>>> c = delegator.chain('fortune | cowsay')
>>> print c.out
  _______________________________________
 / Our swords shall play the orators for \
 | us.                                   |
 |                                       |
 \ -- Christopher Marlowe                /
  ---------------------------------------
         \   ^__^
          \  (oo)\_______
             (__)\       )\/\
                 ||----w |
                 ||     ||

Expect functionality is built-in too, on non-blocking commands:

>>> c.expect('Password:')
>>> c.send('PASSWORD')
>>> c.block()

Other functions:

>>> c.kill()
>>> c.send('SIGTERM', signal=True)

# Only available when block=True, otherwise, use c.out.
>>> c.err
''

# Direct access to pipes.
>>> c.std_err
<open file '<fdopen>', mode 'rU' at 0x10a5351e0>

# Adjust environment variables for the command (existing will be overwritten).
>>> c = delegator.chain('env | grep NEWENV', env={'NEWENV': 'FOO_BAR'})
>>> c.out
NEWENV=FOO_BAR

Installation

$ pip install delegator.py

🍰

You can’t perform that action at this time.