summaryrefslogtreecommitdiff
path: root/cmd2.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2017-06-29 14:13:18 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2017-06-29 14:13:18 -0400
commit37ef113a88e473481f07ec1d2ce99e3abe4ddc30 (patch)
tree50df7785cf6f78f34af974f37c5f93e6ee427bb0 /cmd2.py
parent0e1093fc05a45e613192d54da806d924a7f52d95 (diff)
downloadcmd2-git-37ef113a88e473481f07ec1d2ce99e3abe4ddc30.tar.gz
Command-line pipe is now handled by subprocess module instead of shell
Instead of using shell functionality to pipe output of a command to a shell command, the Python subprocess is now used to pipe stdout of one process to another.
Diffstat (limited to 'cmd2.py')
-rwxr-xr-xcmd2.py23
1 files changed, 11 insertions, 12 deletions
diff --git a/cmd2.py b/cmd2.py
index 0c6c2c1a..10b12516 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -900,18 +900,17 @@ class Cmd(cmd.Cmd):
self.kept_state = None
if statement.parsed.pipeTo:
- # Pipe output from the command to the shell command via echo
- command_line = r'cat {} | {}'.format(self._temp_filename, statement.parsed.pipeTo)
-
- # Get the output and display it. Ignore non-zero return codes and print nothing.
- try:
- result = subprocess.check_output(command_line, shell=True)
- if six.PY3:
- self.stdout.write(result.decode())
- else:
- self.stdout.write(result)
- except subprocess.CalledProcessError:
- pass
+ # cat the tempfile and pipe the output to the specified shell command
+ cat_command = 'cat'
+ p1 = subprocess.Popen([cat_command, self._temp_filename], stdout=subprocess.PIPE)
+ p2 = subprocess.Popen(shlex.split(statement.parsed.pipeTo), stdin=p1.stdout, stdout=subprocess.PIPE)
+ p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
+ output, err = p2.communicate()
+
+ if six.PY3:
+ self.stdout.write(output.decode())
+ else:
+ self.stdout.write(output)
os.remove(self._temp_filename)
self._temp_filename = None