From 80d6727a31cf4c7b232502f82ae6d8e845a082c8 Mon Sep 17 00:00:00 2001 From: "neiljp (Neil Pilgrim)" Date: Sat, 27 Jul 2019 15:09:51 -0700 Subject: [PATCH] bots: Indicate source of bot (from source/module/registry) upon startup. Amend tests to include new parameter. --- zulip_bots/zulip_bots/finder.py | 11 ++++++++--- zulip_bots/zulip_bots/lib.py | 3 ++- zulip_bots/zulip_bots/run.py | 5 ++++- zulip_bots/zulip_bots/tests/test_lib.py | 1 + zulip_bots/zulip_bots/tests/test_run.py | 8 +++++++- 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/zulip_bots/zulip_bots/finder.py b/zulip_bots/zulip_bots/finder.py index 80da346da..4392e8ea3 100644 --- a/zulip_bots/zulip_bots/finder.py +++ b/zulip_bots/zulip_bots/finder.py @@ -31,17 +31,22 @@ class DuplicateRegisteredBotName(Exception): pass -def import_module_from_zulip_bot_registry(name: str) -> Any: +def import_module_from_zulip_bot_registry(name: str) -> Tuple[str, Any]: registered_bots = entrypoints.get_group_all("zulip_bots.registry") matching_bots = [bot for bot in registered_bots if bot.name == name] if len(matching_bots) == 1: # Unique matching entrypoint - return matching_bots[0].load() + bot = matching_bots[0] + if bot.distro is not None: + return "{}: {}".format(bot.distro.name, bot.distro.version), bot.load() + else: + print(bot) + return "editable package: {}".format(bot.module_name), bot.load() if len(matching_bots) > 1: raise DuplicateRegisteredBotName(name) - return None # no matches in registry + return "", None # no matches in registry def resolve_bot_path(name: str) -> Optional[Tuple[Path, str]]: diff --git a/zulip_bots/zulip_bots/lib.py b/zulip_bots/zulip_bots/lib.py index d6d79f7c5..1e526d802 100644 --- a/zulip_bots/zulip_bots/lib.py +++ b/zulip_bots/zulip_bots/lib.py @@ -443,6 +443,7 @@ def run_message_handler_for_bot( config_file: str, bot_config_file: str, bot_name: str, + bot_source: str, ) -> Any: """ lib_module is of type Any, since it can contain any bot's @@ -473,7 +474,7 @@ def run_message_handler_for_bot( message_handler = prepare_message_handler(bot_name, restricted_client, lib_module) if not quiet: - print("Running {} Bot:".format(bot_details["name"])) + print("Running {} Bot (from {}):".format(bot_details["name"], bot_source)) if bot_details["description"] != "": print("\n\t{}".format(bot_details["description"])) if hasattr(message_handler, "usage"): diff --git a/zulip_bots/zulip_bots/run.py b/zulip_bots/zulip_bots/run.py index 3419416fc..ebcb3367d 100755 --- a/zulip_bots/zulip_bots/run.py +++ b/zulip_bots/zulip_bots/run.py @@ -132,16 +132,18 @@ def main() -> None: ) print(dep_err_msg.format(bot_name=bot_name, deps_list=deps_list)) sys.exit(1) + bot_source = "source" else: lib_module = finder.import_module_by_name(args.bot) if lib_module: bot_name = lib_module.__name__ + bot_source = "named module" if args.provision: print("ERROR: Could not load bot's module for '{}'. Exiting now.") sys.exit(1) else: try: - lib_module = finder.import_module_from_zulip_bot_registry(args.bot) + bot_source, lib_module = finder.import_module_from_zulip_bot_registry(args.bot) except finder.DuplicateRegisteredBotName: print( "ERROR: Found duplicate entries for bot name in zulip bot registry. Exiting now." @@ -170,6 +172,7 @@ def main() -> None: bot_config_file=args.bot_config_file, quiet=args.quiet, bot_name=bot_name, + bot_source=bot_source, ) except NoBotConfigException: print( diff --git a/zulip_bots/zulip_bots/tests/test_lib.py b/zulip_bots/zulip_bots/tests/test_lib.py index e60e77b59..e83ba548e 100644 --- a/zulip_bots/zulip_bots/tests/test_lib.py +++ b/zulip_bots/zulip_bots/tests/test_lib.py @@ -197,6 +197,7 @@ def test_message(message, flags): config_file=None, bot_config_file=None, bot_name="testbot", + bot_source="bot code location", ) def test_upload_file(self): diff --git a/zulip_bots/zulip_bots/tests/test_run.py b/zulip_bots/zulip_bots/tests/test_run.py index 20bbcbeca..f7f1c6329 100644 --- a/zulip_bots/zulip_bots/tests/test_run.py +++ b/zulip_bots/zulip_bots/tests/test_run.py @@ -17,7 +17,10 @@ class TestDefaultArguments(TestCase): our_dir = os.path.dirname(__file__) path_to_bot = os.path.abspath(os.path.join(our_dir, "../bots/giphy/giphy.py")) - packaged_bot_entrypoint = entrypoints.EntryPoint("packaged_bot", "module_name", None) + packaged_bot_distro = entrypoints.Distribution("packaged-bot-source", "1.0.0") + packaged_bot_entrypoint = entrypoints.EntryPoint( + "packaged_bot", "module_name", None, distro=packaged_bot_distro + ) @patch("sys.argv", ["zulip-run-bot", "giphy", "--config-file", "/foo/bar/baz.conf"]) @patch("zulip_bots.run.run_message_handler_for_bot") @@ -32,6 +35,7 @@ def test_argument_parsing_with_bot_name( config_file="/foo/bar/baz.conf", bot_config_file=None, lib_module=mock.ANY, + bot_source="source", quiet=False, ) @@ -48,6 +52,7 @@ def test_argument_parsing_with_bot_path( config_file="/foo/bar/baz.conf", bot_config_file=None, lib_module=mock.ANY, + bot_source="source", quiet=False, ) @@ -69,6 +74,7 @@ def test_argument_parsing_with_zulip_bot_registry( config_file="/foo/bar/baz.conf", bot_config_file=None, lib_module=mock.ANY, + bot_source="packaged-bot-source: 1.0.0", quiet=False, )