scripts: redirect stdout to ctx.log.warn

Redirect messages written to stdout in scripts to ctx.log.warn.
(closes #1530)

Signed-off-by: Nikos Filippakis <nikolaos.filippakis@cern.ch>
This commit is contained in:
Nikos Filippakis 2017-03-24 00:15:25 +01:00
parent 1e81747a2a
commit abf291b0f9
3 changed files with 38 additions and 7 deletions

View File

@ -65,14 +65,28 @@ def cut_traceback(tb, func_name):
return tb return tb
class StreamLog:
"""
A class for redirecting output using contextlib.
"""
def __init__(self, log):
self.log = log
def write(self, buf):
if buf.strip():
self.log(buf)
@contextlib.contextmanager @contextlib.contextmanager
def scriptenv(path, args): def scriptenv(path, args):
oldargs = sys.argv oldargs = sys.argv
sys.argv = [path] + args sys.argv = [path] + args
script_dir = os.path.dirname(os.path.abspath(path)) script_dir = os.path.dirname(os.path.abspath(path))
sys.path.append(script_dir) sys.path.append(script_dir)
stdout_replacement = StreamLog(ctx.log.warn)
try: try:
yield with contextlib.redirect_stdout(stdout_replacement):
yield
except SystemExit as v: except SystemExit as v:
ctx.log.error("Script exited with code %s" % v.code) ctx.log.error("Script exited with code %s" % v.code)
except Exception: except Exception:

View File

@ -5,6 +5,7 @@ import re
import watchdog.events import watchdog.events
import pytest import pytest
from unittest import mock
from mitmproxy.test import tflow from mitmproxy.test import tflow
from mitmproxy.test import tutils from mitmproxy.test import tutils
from mitmproxy.test import taddons from mitmproxy.test import taddons
@ -97,12 +98,26 @@ class TestParseCommand:
def test_load_script(): def test_load_script():
ns = script.load_script( with taddons.context():
tutils.test_data.path( ns = script.load_script(
"mitmproxy/data/addonscripts/recorder.py" tutils.test_data.path(
), [] "mitmproxy/data/addonscripts/recorder.py"
) ), []
assert ns.start )
assert ns.start
def test_script_print_stdout():
with taddons.context() as tctx:
with mock.patch('mitmproxy.ctx.log.warn') as mock_warn:
with script.scriptenv("path", []):
ns = script.load_script(
tutils.test_data.path(
"mitmproxy/data/addonscripts/print.py"
), []
)
ns.start(tctx.options)
mock_warn.assert_called_once_with("stdoutprint")
class TestScript: class TestScript:

View File

@ -0,0 +1,2 @@
def start(opts):
print("stdoutprint")