diff --git a/monkey/infection_monkey/utils/plugins/load_plugins.py b/monkey/infection_monkey/utils/plugins/load_plugins.py deleted file mode 100644 index 0f39e70aa..000000000 --- a/monkey/infection_monkey/utils/plugins/load_plugins.py +++ /dev/null @@ -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 diff --git a/monkey/infection_monkey/utils/plugins/load_plugins_test.py b/monkey/infection_monkey/utils/plugins/load_plugins_test.py deleted file mode 100644 index cb77f5d9d..000000000 --- a/monkey/infection_monkey/utils/plugins/load_plugins_test.py +++ /dev/null @@ -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) diff --git a/monkey/infection_monkey/utils/plugins/plugins_testcases/__init__.py b/monkey/infection_monkey/utils/plugins/plugins_testcases/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/monkey/infection_monkey/utils/plugins/plugins_testcases/plugin_bad_import.py b/monkey/infection_monkey/utils/plugins/plugins_testcases/plugin_bad_import.py deleted file mode 100644 index 65cbc6e8b..000000000 --- a/monkey/infection_monkey/utils/plugins/plugins_testcases/plugin_bad_import.py +++ /dev/null @@ -1 +0,0 @@ -from infection_monkey.utils.plugins.plugins_testcases.plugin_test_run import PluginShouldRun \ No newline at end of file diff --git a/monkey/infection_monkey/utils/plugins/plugins_testcases/plugin_bad_init.py b/monkey/infection_monkey/utils/plugins/plugins_testcases/plugin_bad_init.py deleted file mode 100644 index fd21b58bc..000000000 --- a/monkey/infection_monkey/utils/plugins/plugins_testcases/plugin_bad_init.py +++ /dev/null @@ -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 diff --git a/monkey/infection_monkey/utils/plugins/plugins_testcases/plugin_bad_parent.py b/monkey/infection_monkey/utils/plugins/plugins_testcases/plugin_bad_parent.py deleted file mode 100644 index 72a348ea0..000000000 --- a/monkey/infection_monkey/utils/plugins/plugins_testcases/plugin_bad_parent.py +++ /dev/null @@ -1,4 +0,0 @@ -class PluginNoInherit: - @staticmethod - def should_run(class_name: str) -> bool: - return True diff --git a/monkey/infection_monkey/utils/plugins/plugins_testcases/plugin_test_dontrun.py b/monkey/infection_monkey/utils/plugins/plugins_testcases/plugin_test_dontrun.py deleted file mode 100644 index 88214bbff..000000000 --- a/monkey/infection_monkey/utils/plugins/plugins_testcases/plugin_test_dontrun.py +++ /dev/null @@ -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 diff --git a/monkey/infection_monkey/utils/plugins/plugins_testcases/plugin_test_run.py b/monkey/infection_monkey/utils/plugins/plugins_testcases/plugin_test_run.py deleted file mode 100644 index ca2f2a523..000000000 --- a/monkey/infection_monkey/utils/plugins/plugins_testcases/plugin_test_run.py +++ /dev/null @@ -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