Skip to content

Commit

Permalink
Allow overriding of environment variables in run commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Kundert authored and Ken Kundert committed May 12, 2018
1 parent 5621057 commit 88ea9ab
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -473,7 +473,7 @@ Run subclasses Cmd. It basically constructs the process and then immediately
calls the run() method. It takes the same arguments as Cmd, but an additional
argument that allows you to specify stdin for the process::

Run(cmd[, modes][, stdin][, encoding])
Run(cmd[, modes][, stdin][, env][, encoding])

Run expect you to wait for the process to end, either by specify the 'W' mode,
or by calling wait(). For example::
Expand Down
16 changes: 10 additions & 6 deletions shlib.py
Expand Up @@ -391,8 +391,9 @@ class Cmd(object):
M,N,...: accept status codes M, N, ...
"""
# __init__ {{{3
def __init__(self, cmd, modes=None, encoding=None):
def __init__(self, cmd, modes=None, env=None, encoding=None):
self.cmd = cmd
self.env = env
self.use_shell = False
self.save_stdout = False
self.save_stderr = False
Expand Down Expand Up @@ -460,7 +461,7 @@ def run(self, stdin=None):

# run the command
process = subprocess.Popen(
cmd, shell=self.use_shell, **streams
cmd, shell=self.use_shell, env=self.env, **streams
)

# store needed information and wait for termination if desired
Expand Down Expand Up @@ -502,7 +503,7 @@ def start(self, stdin=None):

# run the command
process = subprocess.Popen(
cmd, shell=self.use_shell, **streams
cmd, shell=self.use_shell, env=self.env, **streams
)

# store needed information and wait for termination if desired
Expand Down Expand Up @@ -562,14 +563,15 @@ def __str__(self):
# Run class {{{2
class Run(Cmd):
"Run a command immediately."
def __init__(self, cmd, modes=None, stdin=None, encoding=None):
def __init__(self, cmd, modes=None, stdin=None, env=None, encoding=None):
self.cmd = cmd
self.stdin = None
self.use_shell = False
self.save_stdout = False
self.save_stderr = False
self.wait_for_termination = True
self.accept = (0,)
self.env = env
self.encoding = DEFAULT_ENCODING if not encoding else encoding
self._interpret_modes(modes)
self._sanity_check()
Expand All @@ -578,13 +580,14 @@ def __init__(self, cmd, modes=None, stdin=None, encoding=None):
# Sh class (deprecated) {{{2
class Sh(Cmd):
"Run a command immediately in the shell."
def __init__(self, cmd, modes=None, stdin=None, encoding=None):
def __init__(self, cmd, modes=None, stdin=None, env=None, encoding=None):
self.cmd = cmd
self.stdin = None
self.use_shell = True
self.save_stdout = False
self.save_stderr = False
self.wait_for_termination = True
self.env = env
self.encoding = DEFAULT_ENCODING if not encoding else encoding
self._interpret_modes(modes)
self._sanity_check()
Expand All @@ -594,14 +597,15 @@ def __init__(self, cmd, modes=None, stdin=None, encoding=None):
# Start class {{{2
class Start(Cmd):
"Run a command immediately, don't wait for it to exit"
def __init__(self, cmd, modes=None, stdin=None, encoding=None):
def __init__(self, cmd, modes=None, stdin=None, env=None, encoding=None):
self.cmd = cmd
self.stdin = None
self.use_shell = False
self.save_stdout = False
self.save_stderr = False
self.wait_for_termination = True
self.accept = (0,)
self.env = env
self.encoding = DEFAULT_ENCODING if not encoding else encoding
self._interpret_modes(modes)
self._sanity_check()
Expand Down

0 comments on commit 88ea9ab

Please sign in to comment.