mirror of
https://github.com/aljazceru/recon-pipeline.git
synced 2025-12-19 23:34:27 +01:00
40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
import sys
|
|
from contextlib import redirect_stdout, redirect_stderr
|
|
|
|
from cmd2.utils import StdSim
|
|
|
|
|
|
def normalize(block):
|
|
""" Normalize a block of text to perform comparison.
|
|
Strip newlines from the very beginning and very end Then split into separate lines and strip trailing whitespace
|
|
from each line.
|
|
"""
|
|
assert isinstance(block, str)
|
|
block = block.strip("\n")
|
|
return [line.rstrip() for line in block.splitlines()]
|
|
|
|
|
|
def run_cmd(app, cmd):
|
|
""" Clear out and err StdSim buffers, run the command, and return out and err """
|
|
saved_sysout = sys.stdout
|
|
sys.stdout = app.stdout
|
|
|
|
# This will be used to capture app.stdout and sys.stdout
|
|
copy_cmd_stdout = StdSim(app.stdout)
|
|
|
|
# This will be used to capture sys.stderr
|
|
copy_stderr = StdSim(sys.stderr)
|
|
|
|
try:
|
|
app.stdout = copy_cmd_stdout
|
|
with redirect_stdout(copy_cmd_stdout):
|
|
with redirect_stderr(copy_stderr):
|
|
app.onecmd_plus_hooks(cmd)
|
|
finally:
|
|
app.stdout = copy_cmd_stdout.inner_stream
|
|
sys.stdout = saved_sysout
|
|
|
|
out = copy_cmd_stdout.getvalue()
|
|
err = copy_stderr.getvalue()
|
|
return normalize(out), normalize(err)
|