Skip to content

Commit d8a3264

Browse files
committed
bots: Fall back to bot registry.
This removes the need to use -r flag and instead load the bot from the registry when no other method works.
1 parent 285a946 commit d8a3264

File tree

2 files changed

+38
-46
lines changed

2 files changed

+38
-46
lines changed

zulip_bots/zulip_bots/run.py

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,6 @@ def parse_args() -> argparse.Namespace:
4848
help="try running the bot even if dependencies install fails",
4949
)
5050

51-
parser.add_argument(
52-
"--registry",
53-
"-r",
54-
action="store_true",
55-
help="run the bot via zulip_bots registry",
56-
)
57-
5851
parser.add_argument("--provision", action="store_true", help="install dependencies for the bot")
5952

6053
args = parser.parse_args()
@@ -116,50 +109,49 @@ def exit_gracefully_if_bot_config_file_does_not_exist(bot_config_file: Optional[
116109
def main() -> None:
117110
args = parse_args()
118111

119-
if args.registry:
112+
result = finder.resolve_bot_path(args.bot)
113+
if result:
114+
bot_path, bot_name = result
115+
sys.path.insert(0, os.path.dirname(bot_path))
116+
117+
if args.provision:
118+
provision_bot(os.path.dirname(bot_path), args.force)
119+
120120
try:
121-
bot_source, lib_module = finder.import_module_from_zulip_bot_registry(args.bot)
122-
except finder.DuplicateRegisteredBotName as error:
123-
print(
124-
f'ERROR: Found duplicate entries for "{error}" in zulip bots registry.\n'
125-
"Make sure that you don't install bots using the same entry point. Exiting now."
121+
lib_module = finder.import_module_from_source(bot_path.as_posix(), bot_name)
122+
except ImportError:
123+
req_path = os.path.join(os.path.dirname(bot_path), "requirements.txt")
124+
with open(req_path) as fp:
125+
deps_list = fp.read()
126+
127+
dep_err_msg = (
128+
"ERROR: The following dependencies for the {bot_name} bot are not installed:\n\n"
129+
"{deps_list}\n"
130+
"If you'd like us to install these dependencies, run:\n"
131+
" zulip-run-bot {bot_name} --provision"
126132
)
133+
print(dep_err_msg.format(bot_name=bot_name, deps_list=deps_list))
127134
sys.exit(1)
128-
if lib_module:
129-
bot_name = args.bot
135+
bot_source = "source"
130136
else:
131-
result = finder.resolve_bot_path(args.bot)
132-
if result:
133-
bot_path, bot_name = result
134-
sys.path.insert(0, os.path.dirname(bot_path))
135-
137+
lib_module = finder.import_module_by_name(args.bot)
138+
if lib_module and hasattr(lib_module, "handler_class"):
139+
bot_name = lib_module.__name__
140+
bot_source = "named module"
136141
if args.provision:
137-
provision_bot(os.path.dirname(bot_path), args.force)
138-
142+
print("ERROR: Could not load bot's module for '{}'. Exiting now.")
143+
sys.exit(1)
144+
else:
139145
try:
140-
lib_module = finder.import_module_from_source(bot_path.as_posix(), bot_name)
141-
except ImportError:
142-
req_path = os.path.join(os.path.dirname(bot_path), "requirements.txt")
143-
with open(req_path) as fp:
144-
deps_list = fp.read()
145-
146-
dep_err_msg = (
147-
"ERROR: The following dependencies for the {bot_name} bot are not installed:\n\n"
148-
"{deps_list}\n"
149-
"If you'd like us to install these dependencies, run:\n"
150-
" zulip-run-bot {bot_name} --provision"
146+
bot_source, lib_module = finder.import_module_from_zulip_bot_registry(args.bot)
147+
except finder.DuplicateRegisteredBotName as error:
148+
print(
149+
f'ERROR: Found duplicate entries for "{error}" in zulip bots registry.\n'
150+
"Make sure that you don't install bots using the same entry point. Exiting now."
151151
)
152-
print(dep_err_msg.format(bot_name=bot_name, deps_list=deps_list))
153152
sys.exit(1)
154-
bot_source = "source"
155-
else:
156-
lib_module = finder.import_module_by_name(args.bot)
157153
if lib_module:
158-
bot_name = lib_module.__name__
159-
bot_source = "named module"
160-
if args.provision:
161-
print("ERROR: Could not load bot's module for '{}'. Exiting now.")
162-
sys.exit(1)
154+
bot_name = args.bot
163155

164156
if lib_module is None:
165157
print("ERROR: Could not load bot module. Exiting now.")

zulip_bots/zulip_bots/tests/test_run.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ def test_argument_parsing_with_bot_path(
5555
quiet=False,
5656
)
5757

58-
@patch(
59-
"sys.argv", ["zulip-run-bot", "packaged_bot", "--config-file", "/foo/bar/baz.conf", "-r"]
60-
)
58+
@patch("sys.argv", ["zulip-run-bot", "packaged_bot", "--config-file", "/foo/bar/baz.conf"])
6159
@patch("zulip_bots.run.run_message_handler_for_bot")
6260
def test_argument_parsing_with_zulip_bot_registry(
6361
self, mock_run_message_handler_for_bot: mock.Mock
6462
) -> None:
65-
with patch("zulip_bots.run.exit_gracefully_if_zulip_config_is_missing"), patch(
63+
with patch("importlib.import_module", return_value={}), patch(
64+
"zulip_bots.run.exit_gracefully_if_zulip_config_is_missing"
65+
), patch(
6666
"zulip_bots.finder.metadata.EntryPoint.load",
6767
return_value=self.packaged_bot_module,
6868
), patch(

0 commit comments

Comments
 (0)