Skip to main content

Programmer friendly subprocess wrapper

Project description

https://travis-ci.org/xolox/python-executor.svg?branch=master

The execute() function in the executor package/module is a simple wrapper for Python’s subprocess module that makes it very easy to handle subprocesses on UNIX systems with proper escaping of arguments and error checking. It’s currently tested on Python 2.6, 2.7 and 3.4. For usage instructions please refer to the documentation.

Examples of usage

Below are some examples of how versatile the execute() function is.

Checking status codes

The status code of the subprocess is returned as a boolean:

>>> from executor import execute
>>> execute('true')
True

If a subprocess exits with a nonzero status code an exception is raised, this makes it easy to do the right thing (i.e. check the status codes of all subprocesses without having to write a lot of repetitive code):

>>> execute('false')
Traceback (most recent call last):
  File "executor/__init__.py", line 79, in execute
    raise ExternalCommandFailed, msg % (shell.returncode, command)
executor.ExternalCommandFailed: External command failed with exit code 1! (command: false)

If you know a command is likely to exit with a nonzero status code and you want execute() to simply return a boolean you can do this:

>>> execute('false', check=False)
False

Getting output

Getting the output of subprocesses is really easy as well:

>>> execute('hostname', capture=True)
'peter-macbook'

Running commands as root

It’s also very easy to execute commands with super user privileges:

>>> execute('echo test > /etc/hostname', sudo=True)
[sudo] password for peter: **********
True
>>> execute('hostname', capture=True)
'test'

Enabling logging

If you’re wondering how prefixing the above command with sudo would end up being helpful, here’s how it works:

>>> import logging
>>> logging.basicConfig()
>>> logging.getLogger().setLevel(logging.DEBUG)
>>> execute('echo peter-macbook > /etc/hostname', sudo=True)
DEBUG:executor:Executing external command: sudo sh -c 'echo peter-macbook > /etc/hostname'

Contact

The latest version of executor is available on PyPi and GitHub (although I don’t suppose much will change, since it’s so simple). For bug reports please create an issue on GitHub. If you have questions, suggestions, etc. feel free to send me an e-mail at peter@peterodding.com.

License

This software is licensed under the MIT license.

© 2014 Peter Odding.

Project details


Release history Release notifications

History Node

20.0

History Node

19.3

History Node

19.2

History Node

19.1

History Node

19.0

History Node

18.1

History Node

18.0

History Node

17.1

History Node

17.0

History Node

16.1

History Node

16.0.1

History Node

16.0

History Node

15.1

History Node

15.0

History Node

14.1

History Node

14.0

History Node

13.0

History Node

12.0

History Node

11.0.1

History Node

11.0

History Node

10.1

History Node

10.0

History Node

9.11

History Node

9.10

History Node

9.9

History Node

9.8

History Node

9.7

History Node

9.6.1

History Node

9.6

History Node

9.5

History Node

9.3

History Node

9.0

History Node

8.4

History Node

8.3

History Node

8.2

History Node

8.1.1

History Node

8.1

History Node

8.0.1

History Node

8.0

History Node

7.7

History Node

7.6

History Node

7.2

History Node

7.1.1

History Node

7.1

History Node

7.0.1

History Node

7.0

History Node

6.2

History Node

5.0

History Node

4.9

History Node

4.8

History Node

4.7

History Node

4.6

History Node

4.5

History Node

4.4.1

History Node

4.4

History Node

4.1

History Node

4.0.1

History Node

4.0

History Node

3.6

History Node

3.5

History Node

3.4.1

History Node

3.4

History Node

3.3

History Node

3.2

History Node

3.1

History Node

3.0.2

History Node

3.0.1

History Node

3.0

History Node

2.1

History Node

2.0

History Node

1.7.1

History Node

1.6

History Node

1.4

History Node

1.3

This version
History Node

1.2

History Node

1.1

History Node

1.0

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
executor-1.2.tar.gz (5.3 kB) Copy SHA256 hash SHA256 Source None May 10, 2014

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page