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
Ian Adam Naval 5565f0c4ef Update api to use chain and call
Much sexier than __call__
2015-02-26 15:37:08 -05:00

32 lines
1022 B
Python

from formatters import Printer
from commands import BaseCommand
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 line in input_generator:
input_str += line + b'\n'
outs, errs = p.communicate(input_str)
if outs:
yield outs
return make_output_generator
except:
import traceback
traceback.print_exc()
return []