mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-30 03:14:22 +00:00
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:
parent
1e81747a2a
commit
abf291b0f9
@ -65,13 +65,27 @@ 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:
|
||||||
|
with contextlib.redirect_stdout(stdout_replacement):
|
||||||
yield
|
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)
|
||||||
|
@ -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,6 +98,7 @@ class TestParseCommand:
|
|||||||
|
|
||||||
|
|
||||||
def test_load_script():
|
def test_load_script():
|
||||||
|
with taddons.context():
|
||||||
ns = script.load_script(
|
ns = script.load_script(
|
||||||
tutils.test_data.path(
|
tutils.test_data.path(
|
||||||
"mitmproxy/data/addonscripts/recorder.py"
|
"mitmproxy/data/addonscripts/recorder.py"
|
||||||
@ -105,6 +107,19 @@ def test_load_script():
|
|||||||
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:
|
||||||
def test_simple(self):
|
def test_simple(self):
|
||||||
with taddons.context():
|
with taddons.context():
|
||||||
|
2
test/mitmproxy/data/addonscripts/print.py
Normal file
2
test/mitmproxy/data/addonscripts/print.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
def start(opts):
|
||||||
|
print("stdoutprint")
|
Loading…
Reference in New Issue
Block a user