Improve dynamic module import in JobScheduler to manage sys.path more safely

This commit is contained in:
Théophile Diot 2024-12-24 12:52:42 +01:00
parent 08cba813c6
commit b27ed6f94d
No known key found for this signature in database
GPG key ID: FA995104A0BA376A

View file

@ -155,9 +155,14 @@ class JobScheduler(ApiCaller):
def __exec_plugin_module(self, path: str, name: str) -> ModuleType:
"""Dynamically import plugin module."""
spec = spec_from_file_location(name, path)
module = module_from_spec(spec)
spec.loader.exec_module(module)
module_dir = dirname(path)
sys_path.insert(0, module_dir)
try:
spec = spec_from_file_location(name, path)
module = module_from_spec(spec)
spec.loader.exec_module(module)
finally:
sys_path.remove(module_dir)
def __job_wrapper(self, path: str, plugin: str, name: str, file: str) -> int:
self.__logger.info(f"Executing job '{name}' from plugin '{plugin}'...")