diff options
Diffstat (limited to 'libmproxy/script.py')
-rw-r--r-- | libmproxy/script.py | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/libmproxy/script.py b/libmproxy/script.py index a80f4694..0912c9ae 100644 --- a/libmproxy/script.py +++ b/libmproxy/script.py @@ -47,11 +47,21 @@ class Script: """ def __init__(self, command, master): self.command = command - self.argv = shlex.split(command, posix=(os.name != "nt")) + self.argv = self.parse_command(command) self.ctx = ScriptContext(master) self.ns = None self.load() + @classmethod + def parse_command(klass, command): + args = shlex.split(command, posix=(os.name != "nt")) + args[0] = os.path.expanduser(args[0]) + if not os.path.exists(args[0]): + raise ScriptError("Command not found.") + elif not os.path.isfile(args[0]): + raise ScriptError("Not a file: %s" % args[0]) + return args + def load(self): """ Loads a module. @@ -59,14 +69,9 @@ class Script: Raises ScriptError on failure, with argument equal to an error message that may be a formatted traceback. """ - path = os.path.expanduser(self.argv[0]) - if not os.path.exists(path): - raise ScriptError("No such file: %s" % path) - if not os.path.isfile(path): - raise ScriptError("Not a file: %s" % path) ns = {} try: - execfile(path, ns, ns) + execfile(self.argv[0], ns, ns) except Exception, v: raise ScriptError(traceback.format_exc(v)) self.ns = ns |