Improve shell argument parsing

For example, this patch fixes the command "df | awk '{print $1}'"
This commit is contained in:
Ian Adam Naval 2015-02-20 22:53:22 -05:00
parent 0a78e571fd
commit 9324774208
2 changed files with 6 additions and 8 deletions

View File

@ -21,19 +21,18 @@ class RawCommand(BaseCommand):
with the builtin subprocess module. Each output object is just a tree node with the builtin subprocess module. Each output object is just a tree node
whose data is a simple string.""" whose data is a simple string."""
def __init__(self, cmd, input_generator=[], args=tuple()): def __init__(self, cmd, input_generator=[]):
self.input_generator = input_generator self.input_generator = input_generator
self.cmd = cmd self.cmd = cmd
self.args = args
def __call__(self, input_generator=[], *args, **kwargs): def __call__(self, input_generator=[], *args, **kwargs):
import subprocess import subprocess
try: try:
p = subprocess.Popen((self.cmd,) + self.args, stdin=subprocess.PIPE, stdout=subprocess.PIPE) p = subprocess.Popen(self.cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
def output_generator(): def output_generator():
input_str = b"" input_str = b""
for line in input_generator: for line in input_generator:
input_str += line input_str += line + b'\n'
outs, errs = p.communicate(input_str) outs, errs = p.communicate(input_str)
if outs: if outs:
yield outs yield outs

View File

@ -4,6 +4,8 @@ from example_cmd import example_cmd, echo
import os import os
import os.path import os.path
import shlex
for path in os.environ['PATH'].split(':'): for path in os.environ['PATH'].split(':'):
if os.path.exists(path): if os.path.exists(path):
binaries = os.listdir(path) binaries = os.listdir(path)
@ -25,10 +27,7 @@ def parse_cmd(potential_cmd):
if args: if args:
args = args[1:] args = args[1:]
if cmd_name not in registered_cmds: if cmd_name not in registered_cmds:
if args: return "RawCommand({})".format(shlex.split(potential_cmd))
return "RawCommand('{}', args={})".format(cmd_name, str(tuple(args)))
else:
return "RawCommand('{}')".format(cmd_name)
else: else:
return "{}()".format(cmd_name) return "{}()".format(cmd_name)