Remove load_plugin interface.

This commit is contained in:
Daniel Goldberg 2019-11-12 19:35:57 +02:00
parent 69c66072af
commit 7f58bd9693
8 changed files with 0 additions and 96 deletions

View File

@ -1,45 +0,0 @@
import importlib
import inspect
import logging
from os.path import dirname, basename, isfile, join
import glob
from infection_monkey.utils.plugins.plugin import Plugin
LOG = logging.getLogger(__name__)
def _get_candidate_files(base_package_file):
files = glob.glob(join(dirname(base_package_file), "*.py"))
return [basename(f)[:-3] for f in files if isfile(f) and not f.endswith('__init__.py')]
def get_instances(base_package_name, base_package_file, parent_class: Plugin):
"""
Returns the parent_class type objects from base_package_spec.
parent_class must be a class object that inherits from Plugin
base_package name and file must refer to the same package otherwise bad results
:return: A list of parent_class objects.
"""
objects = []
candidate_files = _get_candidate_files(base_package_file)
LOG.info("looking for classes of type {} in {}".format(parent_class.__name__, base_package_name))
# Go through all of files
for file in candidate_files:
# Import module from that file
module = importlib.import_module('.' + file, base_package_name)
# Get all classes in a module
# m[1] because return object is (name,class)
classes = [m[1] for m in inspect.getmembers(module, inspect.isclass)
if ((m[1].__module__ == module.__name__) and issubclass(m[1], parent_class))]
# Get object from class
for class_object in classes:
LOG.debug("Checking if should run object {}".format(class_object.__name__))
try:
if class_object.should_run(class_object.__name__):
instance = class_object()
objects.append(instance)
LOG.debug("Added {} to list".format(class_object.__name__))
except Exception as e:
LOG.warning("Exception {} when checking if {} should run".format(str(e), class_object.__name__))
return objects

View File

@ -1,21 +0,0 @@
# test - should run true
# test - should run false
# test - invalid parent class but should run true
# test - imported from other file, should not collect
# test - failed to instance
from unittest import TestCase
import infection_monkey.utils.plugins.plugins_testcases
from infection_monkey.utils.plugins.load_plugins import get_instances
from infection_monkey.utils.plugins.plugin import Plugin
class PluginTester(TestCase):
def setUp(self):
pass
def test_plugins(self):
res = get_instances(infection_monkey.utils.plugins.plugins_testcases.__package__,infection_monkey.utils.plugins.plugins_testcases.__file__,Plugin)
self.assertEqual(len(res), 1)

View File

@ -1 +0,0 @@
from infection_monkey.utils.plugins.plugins_testcases.plugin_test_run import PluginShouldRun

View File

@ -1,11 +0,0 @@
from infection_monkey.utils.plugins.plugin import Plugin
class PluginShouldRun(Plugin):
def __init__(self):
raise ValueError("Some Error")
@staticmethod
def should_run(class_name: str) -> bool:
return True

View File

@ -1,4 +0,0 @@
class PluginNoInherit:
@staticmethod
def should_run(class_name: str) -> bool:
return True

View File

@ -1,7 +0,0 @@
from infection_monkey.utils.plugins.plugin import Plugin
class PluginDontRun(Plugin):
@staticmethod
def should_run(class_name: str) -> bool:
return False

View File

@ -1,7 +0,0 @@
from infection_monkey.utils.plugins.plugin import Plugin
class PluginShouldRun(Plugin):
@staticmethod
def should_run(class_name: str) -> bool:
return True