This repository has been archived on 2015-04-29. You can view files and clone it, but cannot push or open issues or pull requests.
psh/raw_commands.py
2015-02-26 16:22:21 -05:00

35 lines
1.1 KiB
Python

from formatters import Printer
from commands import BaseCommand
from tree import TreeNode
class RawCommand(BaseCommand):
"""Fallback raw command that just invokes an existing Unix utility program
with the builtin subprocess module. Each output object is just a tree node
whose data is a simple string."""
def __init__(self, cmd):
super(RawCommand, self).__init__()
self.cmd = cmd
def call(self, *args, **kwargs):
input_generator = self.get_input_generator()
import subprocess
try:
p = subprocess.Popen(self.cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
def make_output_generator():
input_str = b""
for node in input_generator:
line = node.data
input_str += line + b'\n'
outs, errs = p.communicate(input_str)
if outs:
yield TreeNode(outs)
return make_output_generator
except:
import traceback
traceback.print_exc()
return []