diff options
author | Maximilian Hils <git@maximilianhils.com> | 2016-07-07 23:50:55 -0700 |
---|---|---|
committer | Maximilian Hils <git@maximilianhils.com> | 2016-07-07 23:50:55 -0700 |
commit | 7c67faa8da39f428d1860bccae806137943b66a6 (patch) | |
tree | 67d4eb8297b901386d0b91ebc0cb55d1463adf8f /mitmproxy/script | |
parent | c048ae1d5b652ad4778917e624ace217e1ecfd91 (diff) | |
download | mitmproxy-7c67faa8da39f428d1860bccae806137943b66a6.tar.gz mitmproxy-7c67faa8da39f428d1860bccae806137943b66a6.tar.bz2 mitmproxy-7c67faa8da39f428d1860bccae806137943b66a6.zip |
remove script contexts
Diffstat (limited to 'mitmproxy/script')
-rw-r--r-- | mitmproxy/script/__init__.py | 2 | ||||
-rw-r--r-- | mitmproxy/script/concurrent.py | 4 | ||||
-rw-r--r-- | mitmproxy/script/reloader.py | 4 | ||||
-rw-r--r-- | mitmproxy/script/script.py | 72 | ||||
-rw-r--r-- | mitmproxy/script/script_context.py | 61 |
5 files changed, 35 insertions, 108 deletions
diff --git a/mitmproxy/script/__init__.py b/mitmproxy/script/__init__.py index d6bff4c7..9a3985ab 100644 --- a/mitmproxy/script/__init__.py +++ b/mitmproxy/script/__init__.py @@ -1,12 +1,10 @@ from . import reloader from .concurrent import concurrent from .script import Script -from .script_context import ScriptContext from ..exceptions import ScriptException __all__ = [ "Script", - "ScriptContext", "concurrent", "ScriptException", "reloader" diff --git a/mitmproxy/script/concurrent.py b/mitmproxy/script/concurrent.py index 56d39d0b..010a5fa0 100644 --- a/mitmproxy/script/concurrent.py +++ b/mitmproxy/script/concurrent.py @@ -18,9 +18,9 @@ def concurrent(fn): "Concurrent decorator not supported for '%s' method." % fn.__name__ ) - def _concurrent(ctx, obj): + def _concurrent(obj): def run(): - fn(ctx, obj) + fn(obj) if not obj.reply.acked: obj.reply.ack() obj.reply.take() diff --git a/mitmproxy/script/reloader.py b/mitmproxy/script/reloader.py index 50401034..857d76cd 100644 --- a/mitmproxy/script/reloader.py +++ b/mitmproxy/script/reloader.py @@ -15,8 +15,8 @@ _observers = {} def watch(script, callback): if script in _observers: raise RuntimeError("Script already observed") - script_dir = os.path.dirname(os.path.abspath(script.filename)) - script_name = os.path.basename(script.filename) + script_dir = os.path.dirname(os.path.abspath(script.path)) + script_name = os.path.basename(script.path) event_handler = _ScriptModificationHandler(callback, filename=script_name) observer = Observer() observer.schedule(event_handler, script_dir) diff --git a/mitmproxy/script/script.py b/mitmproxy/script/script.py index 9ff79f52..db4909ca 100644 --- a/mitmproxy/script/script.py +++ b/mitmproxy/script/script.py @@ -6,38 +6,40 @@ by the mitmproxy-specific ScriptContext. # Do not import __future__ here, this would apply transitively to the inline scripts. from __future__ import absolute_import, print_function, division -import inspect import os import shlex import sys import contextlib -import warnings import six +from typing import List # noqa from mitmproxy import exceptions @contextlib.contextmanager -def setargs(args): +def scriptenv(path, args): + # type: (str, List[str]) -> None oldargs = sys.argv - sys.argv = args + script_dir = os.path.dirname(os.path.abspath(path)) + + sys.argv = [path] + args + sys.path.append(script_dir) try: yield finally: sys.argv = oldargs + sys.path.pop() class Script(object): - """ Script object representing an inline script. """ - def __init__(self, command, context): + def __init__(self, command): self.command = command - self.args = self.parse_command(command) - self.ctx = context + self.path, self.args = self.parse_command(command) self.ns = None def __enter__(self): @@ -46,15 +48,15 @@ class Script(object): def __exit__(self, exc_type, exc_val, exc_tb): if exc_val: - return False # reraise the exception + return False # re-raise the exception self.unload() - @property - def filename(self): - return self.args[0] - @staticmethod def parse_command(command): + # type: (str) -> Tuple[str,List[str]] + """ + Returns a (path, args) tuple. + """ if not command or not command.strip(): raise exceptions.ScriptException("Empty script command.") # Windows: escape all backslashes in the path. @@ -71,7 +73,7 @@ class Script(object): args[0]) elif os.path.isdir(args[0]): raise exceptions.ScriptException("Not a file: %s" % args[0]) - return args + return args[0], args[1:] def load(self): """ @@ -85,31 +87,19 @@ class Script(object): """ if self.ns is not None: raise exceptions.ScriptException("Script is already loaded") - script_dir = os.path.dirname(os.path.abspath(self.args[0])) - self.ns = {'__file__': os.path.abspath(self.args[0])} - sys.path.append(script_dir) - sys.path.append(os.path.join(script_dir, "..")) - try: - with open(self.filename) as f: - code = compile(f.read(), self.filename, 'exec') - exec(code, self.ns, self.ns) - except Exception: - six.reraise( - exceptions.ScriptException, - exceptions.ScriptException.from_exception_context(), - sys.exc_info()[2] - ) - finally: - sys.path.pop() - sys.path.pop() - - start_fn = self.ns.get("start") - if start_fn and len(inspect.getargspec(start_fn).args) == 2: - warnings.warn( - "The 'args' argument of the start() script hook is deprecated. " - "Please use sys.argv instead." - ) - return self.run("start", self.args) + self.ns = {'__file__': os.path.abspath(self.path)} + + with scriptenv(self.path, self.args): + try: + with open(self.path) as f: + code = compile(f.read(), self.path, 'exec') + exec(code, self.ns, self.ns) + except Exception: + six.reraise( + exceptions.ScriptException, + exceptions.ScriptException.from_exception_context(), + sys.exc_info()[2] + ) return self.run("start") def unload(self): @@ -134,8 +124,8 @@ class Script(object): f = self.ns.get(name) if f: try: - with setargs(self.args): - return f(self.ctx, *args, **kwargs) + with scriptenv(self.path, self.args): + return f(*args, **kwargs) except Exception: six.reraise( exceptions.ScriptException, diff --git a/mitmproxy/script/script_context.py b/mitmproxy/script/script_context.py deleted file mode 100644 index 44e2736b..00000000 --- a/mitmproxy/script/script_context.py +++ /dev/null @@ -1,61 +0,0 @@ -""" -The mitmproxy script context provides an API to inline scripts. -""" -from __future__ import absolute_import, print_function, division - -from mitmproxy import contentviews - - -class ScriptContext(object): - - """ - The script context should be used to interact with the global mitmproxy state from within a - script. - """ - - def __init__(self, master): - self._master = master - - def log(self, message, level="info"): - """ - Logs an event. - - By default, only events with level "error" get displayed. This can be controlled with the "-v" switch. - How log messages are handled depends on the front-end. mitmdump will print them to stdout, - mitmproxy sends output to the eventlog for display ("e" keyboard shortcut). - """ - self._master.add_event(message, level) - - def kill_flow(self, f): - """ - Kills a flow immediately. No further data will be sent to the client or the server. - """ - f.kill(self._master) - - def duplicate_flow(self, f): - """ - Returns a duplicate of the specified flow. The flow is also - injected into the current state, and is ready for editing, replay, - etc. - """ - self._master.pause_scripts = True - f = self._master.duplicate_flow(f) - self._master.pause_scripts = False - return f - - def replay_request(self, f): - """ - Replay the request on the current flow. The response will be added - to the flow object. - """ - return self._master.replay_request(f, block=True, run_scripthooks=False) - - @property - def app_registry(self): - return self._master.apps - - def add_contentview(self, view_obj): - contentviews.add(view_obj) - - def remove_contentview(self, view_obj): - contentviews.remove(view_obj) |