Skip to content

Commit fa8f6be

Browse files
committed
Fix usages of importing bot's module
1 parent cfdba2f commit fa8f6be

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

intelmq/bin/intelmqctl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ def check(self, no_connections=False, check_executables=True):
931931
if bot_id != 'global':
932932
# importable module
933933
try:
934-
bot_module = importlib.import_module(bot_config['module'])
934+
bot_module = importlib.import_module(utils.get_bot_module_name(bot_config['module']))
935935
except ImportError as exc:
936936
check_logger.error('Incomplete installation: Bot %r not importable: %r.', bot_id, exc)
937937
retval = 1

intelmq/lib/bot_debugger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __init__(self, runtime_configuration, bot_id, run_subcommand=None, console_t
5050
self.dryrun = dryrun
5151
self.msg = msg
5252
self.show = show
53-
module = import_module(self.runtime_configuration['module'])
53+
module = import_module(utils.get_bot_module_name(self.runtime_configuration['module']))
5454

5555
if loglevel:
5656
self.leverageLogger(loglevel)

intelmq/lib/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,19 @@ def _get_console_entry_points():
852852
return entries.get("console_scripts", []) # it's a dict
853853

854854

855+
def get_bot_module_name(bot_name: str) -> str:
856+
entries = entry_points()
857+
if hasattr(entries, "select"):
858+
entries = entries.select(name=bot_name, group="console_scripts")
859+
else:
860+
entries = [entry for entry in entries.get("console_scripts", []) if entry.name == bot_name]
861+
862+
if not entries:
863+
return None
864+
else:
865+
return entries[0].value.replace(":BOT.run", '')
866+
867+
855868
def list_all_bots() -> dict:
856869
"""
857870
Compile a dictionary with all bots and their parameters.

intelmq/tests/bin/test_intelmqctl.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ def test_check_handles_syntaxerror_when_importing_bots(self):
125125
self.assertIsNotNone(
126126
next(filter(lambda l: "SyntaxError in bot 'test-bot'" in l, captured.output)))
127127

128+
@skip_installation()
129+
@mock.patch.object(utils, "get_bot_module_name", mock.Mock(return_value="mocked-module"))
130+
def test_check_imports_real_bot_module(self):
131+
self._load_default_harmonization()
132+
self._extend_config(self.tmp_runtime, self.BOT_CONFIG)
133+
134+
# raise SyntaxError to stop checking after import
135+
with mock.patch.object(ctl.importlib, "import_module", mock.Mock(side_effect=SyntaxError)) as import_mock:
136+
self.intelmqctl.check(no_connections=True, check_executables=False)
137+
138+
import_mock.assert_called_once_with("mocked-module")
139+
128140

129141
if __name__ == '__main__': # pragma: nocover
130142
unittest.main()

intelmq/tests/lib/test_utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ def test_list_all_bots_filters_entrypoints(self):
346346
)
347347
self.assertEqual(2, import_mock.call_count)
348348

349+
def test_get_bot_module_name_builtin_bot(self):
350+
found_name = utils.get_bot_module_name("intelmq.bots.collectors.api.collector_api")
351+
self.assertEqual("intelmq.bots.collectors.api.collector_api", found_name)
352+
349353
def test_get_bots_settings(self):
350354
with unittest.mock.patch.object(utils, "get_runtime", new_get_runtime):
351355
runtime = utils.get_bots_settings()
@@ -381,14 +385,14 @@ def test_load_configuration_yaml(self):
381385
filename = os.path.join(os.path.dirname(__file__), '../assets/example.yaml')
382386
self.assertEqual(utils.load_configuration(filename),
383387
{
384-
'some_string': 'Hello World!',
385-
'other_string': 'with a : in it',
388+
'some_string': 'Hello World!',
389+
'other_string': 'with a : in it',
386390
'now more': ['values', 'in', 'a', 'list'],
387391
'types': -4,
388392
'other': True,
389393
'final': 0.5,
390-
}
391-
)
394+
}
395+
)
392396

393397
def test_load_configuration_yaml_invalid(self):
394398
""" Test load_configuration with an invalid YAML file """

0 commit comments

Comments
 (0)