Skip to content

Commit

Permalink
Add verbose flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Istenes authored and toastdriven committed Jun 8, 2017
1 parent 4e8fe3f commit 58625cd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
31 changes: 24 additions & 7 deletions shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"""
import shlex
import subprocess
import sys


__author__ = 'Daniel Lindsley'
Expand Down Expand Up @@ -74,14 +75,19 @@ class Shell(object):
Optionally accepts a ``die`` parameter, which should be a boolean.
If set to ``True``, raises a CommandError if the command exits with a
non-zero return code. (Default: ``False``)
Optionally accepts a ``verbose`` parameter, which should be a boolean.
If set to ``True``, prints stdout to stdout and stderr to stderr as
execution happens. (Default: ``False``)
"""
def __init__(self, has_input=False, record_output=True, record_errors=True,
strip_empty=True, die=False):
strip_empty=True, die=False, verbose=False):
self.has_input = has_input
self.record_output = record_output
self.record_errors = record_errors
self.strip_empty = strip_empty
self.die = die
self.verbose = verbose

self.last_command = ''
self.line_breaks = '\n'
Expand Down Expand Up @@ -114,13 +120,19 @@ def _handle_output(self, stdout, stderr):
Records nothing if the ``record_*`` options have been set to ``False``.
"""
if self.record_output:
if stdout != None:
if stdout != None:
if self.record_output:
self._stdout += stdout
if self.verbose:
sys.stdout.write(stdout)
sys.stdout.flush()

if self.record_errors:
if stderr != None:
if stderr != None:
if self.record_errors:
self._stderr += stderr
if self.verbose:
sys.stderr.write(stderr)
sys.stderr.flush()

def _communicate(self, the_input=None):
"""
Expand Down Expand Up @@ -292,7 +304,7 @@ def errors(self, raw=False):


def shell(command, has_input=False, record_output=True, record_errors=True,
strip_empty=True, die=False):
strip_empty=True, die=False, verbose=False):
"""
A convenient shortcut for running commands.
Expand Down Expand Up @@ -320,6 +332,10 @@ def shell(command, has_input=False, record_output=True, record_errors=True,
If set to ``True``, raises a CommandError if the command exits with a
non-zero return code. (Default: ``False``)
Optionally accepts a ``verbose`` parameter, which should be a boolean.
If set to ``True``, prints stdout to stdout and stderr to stderr as
execution happens. (Default: ``False``)
Returns the ``Shell`` instance, which has been run with the given command.
Example::
Expand All @@ -335,6 +351,7 @@ def shell(command, has_input=False, record_output=True, record_errors=True,
record_output=record_output,
record_errors=record_errors,
strip_empty=strip_empty,
die=die
die=die,
verbose=verbose
)
return sh.run(command)
10 changes: 10 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,13 @@ def test_die(self):
self.assertEqual(e.code, 1)
self.assertEqual(e.stderr, no_die.errors()[0] + os.linesep)

def test_verbose(self):
sh = Shell(verbose=True)
with mock.patch('shell.sys.stdout') as mock_stdout:
sh.run('ls')
mock_stdout.write.assert_called_once_with(os.linesep.join(sh.output()) + os.linesep)
with mock.patch('shell.sys.stderr') as mock_stderr:
sh.run('ls /total/garbage/not/real/stuff')
mock_stderr.write.assert_called_once_with(os.linesep.join(sh.errors()) + os.linesep)


0 comments on commit 58625cd

Please sign in to comment.