Skip to content

Commit

Permalink
Make it possible to provide overrides for environment variables (issue
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Mar 4, 2015
1 parent 59a63ae commit 4776beb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
15 changes: 12 additions & 3 deletions executor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Programmer friendly subprocess wrapper.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: March 4, 2015
# Last Change: March 5, 2015
# URL: https://executor.readthedocs.org

# Standard library modules.
Expand All @@ -11,7 +11,7 @@
import subprocess

# Semi-standard module versioning.
__version__ = '1.6.2'
c__version__ = '1.7'

# Initialize a logger.
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -45,14 +45,21 @@ def execute(*command, **options):
running with ``root`` privileges the command is prefixed
with ``fakeroot``. If ``fakeroot`` is not installed we
fall back to ``sudo``.
:param encoding: In Python 3 the :py:func:`subprocess.Popen()` function
:param encoding: In Python 3 the :py:class:`subprocess.Popen()` constructor
expects its ``input`` argument to be an instance of
:py:class:`bytes`. If :py:func:`execute()` is given a
string as input it automatically encodes it. The default
encoding is UTF-8. You can change it using this argument
by passing a string containing the name of an encoding.
The encoding specified here is also used to decode the
output of the external command when ``capture=True``.
:param environment: A dictionary of environment variables for the external
command (defaults to ``None`` which means to inherit
the environment variables of the current process). You
only need to specify environment variables that differ
from those of the current process (that is to say the
environment variables of the current process are merged
with the variables that you specify here).
:returns: - If ``capture=False`` (the default) then a boolean is returned:
- ``True`` if the subprocess exited with a zero status code,
Expand Down Expand Up @@ -94,6 +101,8 @@ def execute(*command, **options):
kw['stderr'] = null_device
if options.get('capture', False):
kw['stdout'] = subprocess.PIPE
kw['env'] = os.environ.copy()
kw['env'].update(options.get('environment', {}))
shell = subprocess.Popen(['bash', '-c', command], **kw)
input = options.get('input', None)
if input is not None:
Expand Down
10 changes: 9 additions & 1 deletion executor/tests.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Automated tests for the `executor' module.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: October 18, 2014
# Last Change: March 5, 2015
# URL: https://executor.readthedocs.org

# Standard library modules.
import logging
import os
import random
import tempfile
import unittest

Expand Down Expand Up @@ -85,3 +86,10 @@ def test_sudo_option(self):
self.assertEqual(execute('stat', '--format=%a', filename, sudo=True, capture=True), '600')
finally:
self.assertTrue(execute('rm', filename, sudo=True))

def test_environment_variable_handling(self):
# Check that environment variables of the current process are passed on to subprocesses.
self.assertEqual(execute('echo $PATH', capture=True), os.environ['PATH'])
# Test that environment variable overrides can be given to external commands.
override_value = str(random.random())
self.assertEqual(execute('echo $override', capture=True, environment=dict(override=override_value)), override_value)

0 comments on commit 4776beb

Please sign in to comment.