Merge pull request #2418 from ujjwal96/script-addon

Fixes the issue of script reloader not unloading previous script
This commit is contained in:
Maximilian Hils 2017-06-28 20:51:18 +02:00 committed by GitHub
commit 321352ef0b
2 changed files with 24 additions and 1 deletions

View File

@ -52,11 +52,19 @@ class Script:
def tick(self): def tick(self):
if time.time() - self.last_load > self.ReloadInterval: if time.time() - self.last_load > self.ReloadInterval:
try:
mtime = os.stat(self.fullpath).st_mtime mtime = os.stat(self.fullpath).st_mtime
except FileNotFoundError:
scripts = ctx.options.scripts
scripts.remove(self.path)
ctx.options.update(scripts=scripts)
return
if mtime > self.last_mtime: if mtime > self.last_mtime:
ctx.log.info("Loading script: %s" % self.path) ctx.log.info("Loading script: %s" % self.path)
if self.ns: if self.ns:
ctx.master.addons.remove(self.ns) ctx.master.addons.remove(self.ns)
del sys.modules[self.ns.__name__]
self.ns = load_script(ctx, self.fullpath) self.ns = load_script(ctx, self.fullpath)
if self.ns: if self.ns:
# We're already running, so we have to explicitly register and # We're already running, so we have to explicitly register and

View File

@ -1,6 +1,7 @@
import traceback import traceback
import sys import sys
import time import time
import os
import pytest import pytest
from unittest import mock from unittest import mock
@ -183,6 +184,20 @@ class TestScriptLoader:
scripts = ["one", "one"] scripts = ["one", "one"]
) )
def test_script_deletion(self):
tdir = tutils.test_data.path("mitmproxy/data/addonscripts/")
with open(tdir + "/dummy.py", 'w') as f:
f.write("\n")
with taddons.context() as tctx:
sl = script.ScriptLoader()
tctx.master.addons.add(sl)
tctx.configure(sl, scripts=[tutils.test_data.path("mitmproxy/data/addonscripts/dummy.py")])
os.remove(tutils.test_data.path("mitmproxy/data/addonscripts/dummy.py"))
tctx.invoke(sl, "tick")
assert not tctx.options.scripts
assert not sl.addons
def test_order(self): def test_order(self):
rec = tutils.test_data.path("mitmproxy/data/addonscripts/recorder") rec = tutils.test_data.path("mitmproxy/data/addonscripts/recorder")
sc = script.ScriptLoader() sc = script.ScriptLoader()