View script debug output (stderr) in pager.

This commit is contained in:
Aldo Cortesi 2011-02-02 10:08:24 +13:00
parent b886f808be
commit 405e60215c
4 changed files with 46 additions and 34 deletions

View File

@ -486,33 +486,7 @@ class ConnectionView(WWrap):
if conn.content: if conn.content:
t = conn.headers.get("content-type", [None]) t = conn.headers.get("content-type", [None])
t = t[0] t = t[0]
if t: self.master.spawn_external_viewer(conn.content, t)
ext = mimetypes.guess_extension(t) or ""
else:
ext = ""
fd, name = tempfile.mkstemp(ext, "mproxy")
os.write(fd, conn.content)
os.close(fd)
t = conn.headers.get("content-type", [None])
t = t[0]
cmd = None
shell = False
if t:
c = mailcap.getcaps()
cmd, _ = mailcap.findmatch(c, t, filename=name)
if cmd:
shell = True
if not cmd:
c = os.environ.get("PAGER") or os.environ.get("EDITOR")
cmd = [c, name]
ret = subprocess.call(cmd, shell=shell)
# Not sure why, unless we do this we get a visible cursor after
# spawning 'less'.
self.master.ui._curs_set(1)
self.master.ui.clear()
os.unlink(name)
elif key == "w": elif key == "w":
if self.state.view_flow_mode == VIEW_FLOW_REQUEST: if self.state.view_flow_mode == VIEW_FLOW_REQUEST:
self.master.prompt("Save request body: ", self.save_body) self.master.prompt("Save request body: ", self.save_body)
@ -521,16 +495,19 @@ class ConnectionView(WWrap):
elif key == " ": elif key == " ":
self.master.view_next_flow(self.flow) self.master.view_next_flow(self.flow)
elif key == "|": elif key == "|":
self.master.path_prompt("Script:", self.run_script) self.master.path_prompt("Script: ", self.run_script)
return key return key
def run_script(self, path): def run_script(self, path):
path = os.path.expanduser(path) path = os.path.expanduser(path)
try: try:
newflow = self.flow.run_script(path) newflow, serr = self.flow.run_script(path)
except flow.RunException, e: except flow.RunException, e:
self.master.statusbar.message("Script error: %s"%e) self.master.statusbar.message("Script error: %s"%e)
return return
if serr:
serr = "Script output:\n\n" + serr
self.master.spawn_external_viewer(serr, None)
self.flow.load_state(newflow.get_state()) self.flow.load_state(newflow.get_state())
self.master.refresh_connection(self.flow) self.master.refresh_connection(self.flow)
@ -775,6 +752,33 @@ class ConsoleMaster(controller.Master):
self.stickycookie = None self.stickycookie = None
self.stickyhosts = {} self.stickyhosts = {}
def spawn_external_viewer(self, data, contenttype):
if contenttype:
ext = mimetypes.guess_extension(contenttype) or ""
else:
ext = ""
fd, name = tempfile.mkstemp(ext, "mproxy")
os.write(fd, data)
os.close(fd)
cmd = None
shell = False
if contenttype:
c = mailcap.getcaps()
cmd, _ = mailcap.findmatch(c, contenttype, filename=name)
if cmd:
shell = True
if not cmd:
c = os.environ.get("PAGER") or os.environ.get("EDITOR")
cmd = [c, name]
ret = subprocess.call(cmd, shell=shell)
# Not sure why, unless we do this we get a visible cursor after
# spawning 'less'.
self.ui._curs_set(1)
self.ui.clear()
os.unlink(name)
def set_palette(self): def set_palette(self):
self.palette = [ self.palette = [
('body', 'black', 'dark cyan', 'standout'), ('body', 'black', 'dark cyan', 'standout'),

View File

@ -53,13 +53,19 @@ class Flow:
def run_script(self, path): def run_script(self, path):
""" """
Run a script on a flow, returning the modified flow. Run a script on a flow.
Raises RunException if there's an error. Returns a (flow, stderr output) tuple, or raises RunException if
there's an error.
""" """
data = self.script_serialize() data = self.script_serialize()
try: try:
p = subprocess.Popen([path], stdout=subprocess.PIPE, stdin=subprocess.PIPE) p = subprocess.Popen(
[path],
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE,
)
except OSError, e: except OSError, e:
raise RunException(e.args[1]) raise RunException(e.args[1])
so, se = p.communicate(data) so, se = p.communicate(data)
@ -68,7 +74,7 @@ class Flow:
f = Flow.script_deserialize(so) f = Flow.script_deserialize(so)
if not f: if not f:
raise RunException("Invalid response from script.") raise RunException("Invalid response from script.")
return f return f, se
def dump(self): def dump(self):
data = dict( data = dict(

View File

@ -5,4 +5,5 @@ from libmproxy import script
f = script.load_flow() f = script.load_flow()
f.request.host = "TESTOK" f.request.host = "TESTOK"
print >> sys.stderr, "DEBUG"
script.return_flow(f) script.return_flow(f)

View File

@ -7,7 +7,8 @@ class uFlow(libpry.AutoTree):
f = utils.tflow() f = utils.tflow()
f.response = utils.tresp() f.response = utils.tresp()
f.request = f.response.request f.request = f.response.request
f = f.run_script("scripts/a") f, se = f.run_script("scripts/a")
assert "DEBUG" == se.strip()
assert f.request.host == "TESTOK" assert f.request.host == "TESTOK"
def test_run_script_err(self): def test_run_script_err(self):