Fix plugin modules not being properly reloaded from disk

When using importlib.import_module, Python loads the module from disk
only once and any subsequent call to this method will just re-import
the already loaded module from RAM. Wrapping importlib.import_module
with importlib.reload will make Python force-reload the module from
disk.
This commit is contained in:
Dan 2020-01-16 22:01:10 +01:00
parent 0f84f91939
commit ac8fad3a18

View File

@ -26,7 +26,7 @@ import threading
import time import time
from configparser import ConfigParser from configparser import ConfigParser
from hashlib import sha256, md5 from hashlib import sha256, md5
from importlib import import_module from importlib import import_module, reload
from pathlib import Path from pathlib import Path
from signal import signal, SIGINT, SIGTERM, SIGABRT from signal import signal, SIGINT, SIGTERM, SIGABRT
from threading import Thread from threading import Thread
@ -1531,7 +1531,7 @@ class Client(Methods, BaseClient):
if not include: if not include:
for path in sorted(Path(root).rglob("*.py")): for path in sorted(Path(root).rglob("*.py")):
module_path = '.'.join(path.parent.parts + (path.stem,)) module_path = '.'.join(path.parent.parts + (path.stem,))
module = import_module(module_path) module = reload(import_module(module_path))
for name in vars(module).keys(): for name in vars(module).keys():
# noinspection PyBroadException # noinspection PyBroadException
@ -1553,7 +1553,7 @@ class Client(Methods, BaseClient):
warn_non_existent_functions = True warn_non_existent_functions = True
try: try:
module = import_module(module_path) module = reload(import_module(module_path))
except ImportError: except ImportError:
log.warning('[{}] [LOAD] Ignoring non-existent module "{}"'.format( log.warning('[{}] [LOAD] Ignoring non-existent module "{}"'.format(
self.session_name, module_path)) self.session_name, module_path))
@ -1591,7 +1591,7 @@ class Client(Methods, BaseClient):
warn_non_existent_functions = True warn_non_existent_functions = True
try: try:
module = import_module(module_path) module = reload(import_module(module_path))
except ImportError: except ImportError:
log.warning('[{}] [UNLOAD] Ignoring non-existent module "{}"'.format( log.warning('[{}] [UNLOAD] Ignoring non-existent module "{}"'.format(
self.session_name, module_path)) self.session_name, module_path))