summaryrefslogtreecommitdiff
path: root/cmd2.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2017-06-29 15:35:13 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2017-06-29 15:35:13 -0400
commit4ed2f013709bd7ca196b68bebbe1b3f8d961699d (patch)
tree4d2419a755d6a6ea861958f27725098c7fc2198c /cmd2.py
parent246a05c3411692ac8bffdcccecb09e434b5fcb53 (diff)
downloadcmd2-git-4ed2f013709bd7ca196b68bebbe1b3f8d961699d.tar.gz
Made output pipe to shell command featuer much more reliable
The ability to pipe the output of a cmd2 command to a shell command no longer depends on the presence of the "cat" shell command. It now directly pipes the contents of a file as the stdin to the shell command.
Diffstat (limited to 'cmd2.py')
-rwxr-xr-xcmd2.py21
1 files changed, 10 insertions, 11 deletions
diff --git a/cmd2.py b/cmd2.py
index 10b12516..ca431279 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -900,17 +900,16 @@ class Cmd(cmd.Cmd):
self.kept_state = None
if statement.parsed.pipeTo:
- # 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)
+ # Pipe the contents of tempfile to the specified shell command
+ with open(self._temp_filename) as fd:
+ pipe_proc = subprocess.Popen(shlex.split(statement.parsed.pipeTo), stdin=fd,
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ output, err = pipe_proc.communicate()
+
+ if six.PY3:
+ self.stdout.write(output.decode())
+ else:
+ self.stdout.write(output)
os.remove(self._temp_filename)
self._temp_filename = None