Decouple libmproxy.script from libmproxy.flow

This commit is contained in:
Matthew Shao 2015-11-13 21:08:39 +08:00
parent 2522ba69fa
commit cd0b9e01be
3 changed files with 25 additions and 18 deletions

View File

@ -731,3 +731,7 @@ class ConsoleMaster(flow.FlowMaster):
if f: if f:
self.process_flow(f) self.process_flow(f)
return f return f
def script_change(self, script):
self.masterq.put(("script_change", script))
signals.status_message.send(message="<{}> reloaded.".format(script.args[0]))

View File

@ -642,6 +642,7 @@ class FlowMaster(controller.Master):
self.stream = None self.stream = None
self.apps = AppRegistry() self.apps = AppRegistry()
script.sig_script_change.connect(self.script_change)
def start_app(self, host, port): def start_app(self, host, port):
self.apps.add( self.apps.add(
@ -667,10 +668,6 @@ class FlowMaster(controller.Master):
self.add_event("Script error:\n" + str(e), "error") self.add_event("Script error:\n" + str(e), "error")
self.scripts.remove(script_obj) self.scripts.remove(script_obj)
def reload_scripts(self):
for s in self.scripts[:]:
s.load()
def load_script(self, command): def load_script(self, command):
""" """
Loads a script. Returns an error description if something went Loads a script. Returns an error description if something went
@ -1025,6 +1022,9 @@ class FlowMaster(controller.Master):
def handle_accept_intercept(self, f): def handle_accept_intercept(self, f):
self.state.update_flow(f) self.state.update_flow(f)
def handle_script_change(self, script):
script.load()
def shutdown(self): def shutdown(self):
self.unload_scripts() self.unload_scripts()
controller.Master.shutdown(self) controller.Master.shutdown(self)
@ -1041,6 +1041,10 @@ class FlowMaster(controller.Master):
self.stream.fo.close() self.stream.fo.close()
self.stream = None self.stream = None
def script_change(self, script):
self.masterq.put(("script_change", script))
self.add_event("<{}> reloaded.".format(script.args[0]))
def read_flows_from_paths(paths): def read_flows_from_paths(paths):
""" """

View File

@ -6,7 +6,9 @@ import shlex
import sys import sys
from watchdog.observers import Observer from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler, FileModifiedEvent from watchdog.events import PatternMatchingEventHandler, FileModifiedEvent
import blinker
sig_script_change = blinker.Signal()
class ScriptError(Exception): class ScriptError(Exception):
pass pass
@ -70,7 +72,7 @@ class Script:
self.ctx = ScriptContext(master) self.ctx = ScriptContext(master)
self.ns = None self.ns = None
self.load() self.load()
observe_scripts(master, self.args[0]) self.observe_scripts()
@classmethod @classmethod
def parse_command(cls, command): def parse_command(cls, command):
@ -140,6 +142,13 @@ class Script:
else: else:
return None return None
def observe_scripts(self):
script_dir = os.path.dirname(self.args[0])
event_handler = ScriptModified(self)
observer = Observer()
observer.schedule(event_handler, script_dir)
observer.start()
class ReplyProxy(object): class ReplyProxy(object):
def __init__(self, original_reply, script_thread): def __init__(self, original_reply, script_thread):
@ -199,19 +208,9 @@ def concurrent(fn):
class ScriptModified(PatternMatchingEventHandler): class ScriptModified(PatternMatchingEventHandler):
def __init__(self, flow_master): def __init__(self, script):
self.flow_master = flow_master
PatternMatchingEventHandler.__init__(self, ignore_directories=True, patterns=["*.py"]) PatternMatchingEventHandler.__init__(self, ignore_directories=True, patterns=["*.py"])
self.context = ScriptContext(self.flow_master) self.script = script
def on_modified(self, event=FileModifiedEvent): def on_modified(self, event=FileModifiedEvent):
self.flow_master.reload_scripts() sig_script_change.send(self.script)
self.context.log("script: <{}> reloaded.".format(event.src_path))
def observe_scripts(flow_master, path):
script_dir = os.path.dirname(path)
event_handler = ScriptModified(flow_master)
observer = Observer()
observer.schedule(event_handler, script_dir)
observer.start()