2017-12-12 07:05:41 -06:00
|
|
|
#!/usr/bin/env python3
|
2017-07-18 01:47:16 -02:30
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import logging
|
2017-07-23 15:36:10 -02:30
|
|
|
import argparse
|
2017-07-18 01:47:16 -02:30
|
|
|
import sys
|
2017-07-23 16:16:42 -02:30
|
|
|
import os
|
2017-07-18 01:47:16 -02:30
|
|
|
from types import ModuleType
|
|
|
|
from importlib import import_module
|
2017-07-22 17:51:07 -02:30
|
|
|
from os.path import basename, splitext
|
2017-12-06 12:44:42 +01:00
|
|
|
|
|
|
|
if False:
|
|
|
|
from typing import Any, Optional, Text
|
2017-07-22 17:51:07 -02:30
|
|
|
|
2017-11-27 13:45:05 -08:00
|
|
|
from zulip_bots.lib import (
|
|
|
|
run_message_handler_for_bot,
|
|
|
|
NoBotConfigException,
|
|
|
|
)
|
|
|
|
|
2017-07-23 16:16:42 -02:30
|
|
|
from zulip_bots.provision import provision_bot
|
2017-07-18 01:47:16 -02:30
|
|
|
|
2017-09-01 12:42:01 +02:00
|
|
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
2017-07-22 17:51:07 -02:30
|
|
|
def import_module_from_source(path, name=None):
|
2017-10-10 02:42:46 -04:00
|
|
|
# type: (Text, Optional[Text]) -> Any
|
2017-08-26 04:36:35 +05:30
|
|
|
if not name:
|
2017-07-22 17:51:07 -02:30
|
|
|
name = splitext(basename(path))[0]
|
|
|
|
|
2017-09-07 10:36:19 +02:00
|
|
|
# importlib.util.module_from_spec is supported from Python3.5
|
|
|
|
py_version = sys.version_info
|
|
|
|
if py_version.major < 3 or (py_version.major == 3 and py_version.minor < 5):
|
2017-07-22 17:51:07 -02:30
|
|
|
import imp
|
|
|
|
module = imp.load_source(name, path)
|
|
|
|
else:
|
|
|
|
import importlib.util
|
|
|
|
spec = importlib.util.spec_from_file_location(name, path)
|
|
|
|
module = importlib.util.module_from_spec(spec)
|
|
|
|
spec.loader.exec_module(module)
|
2017-08-26 04:36:35 +05:30
|
|
|
|
|
|
|
return module
|
2017-07-22 17:51:07 -02:30
|
|
|
|
2017-09-06 13:38:14 +02:00
|
|
|
def name_and_path_match(given_name, path_to_bot):
|
2017-10-10 02:42:46 -04:00
|
|
|
# type: (Text, Text) -> bool
|
2017-08-26 05:11:57 +05:30
|
|
|
if given_name and path_to_bot:
|
|
|
|
name_by_path = os.path.splitext(os.path.basename(path_to_bot))[0]
|
|
|
|
if (given_name != name_by_path):
|
|
|
|
return False
|
|
|
|
return True
|
2017-07-22 17:51:07 -02:30
|
|
|
|
2017-07-18 01:47:16 -02:30
|
|
|
def parse_args():
|
2017-10-10 02:42:46 -04:00
|
|
|
# type: () -> argparse.Namespace
|
2017-07-18 01:47:16 -02:30
|
|
|
usage = '''
|
2017-11-20 14:45:52 -08:00
|
|
|
zulip-run-bot <bot_name> --config-file ~/zuliprc
|
|
|
|
zulip-run-bot --help
|
2017-07-18 01:47:16 -02:30
|
|
|
'''
|
|
|
|
|
2017-07-23 15:36:10 -02:30
|
|
|
parser = argparse.ArgumentParser(usage=usage)
|
2017-09-01 12:42:01 +02:00
|
|
|
parser.add_argument('bot',
|
2017-07-23 15:36:10 -02:30
|
|
|
action='store',
|
2017-09-01 12:42:01 +02:00
|
|
|
help='the name or path of an existing bot to run')
|
2017-07-18 01:47:16 -02:30
|
|
|
|
2017-07-23 15:36:10 -02:30
|
|
|
parser.add_argument('--quiet', '-q',
|
|
|
|
action='store_true',
|
2017-09-01 12:29:19 +02:00
|
|
|
help='turn off logging output')
|
2017-07-18 01:47:16 -02:30
|
|
|
|
2017-11-27 16:45:37 +01:00
|
|
|
parser.add_argument('--config-file', '-c',
|
2017-07-23 15:36:10 -02:30
|
|
|
action='store',
|
2017-11-20 14:45:52 -08:00
|
|
|
required=True,
|
|
|
|
help='zulip configuration file (e.g. ~/Downloads/zuliprc)')
|
2017-07-22 17:51:07 -02:30
|
|
|
|
2017-11-27 13:45:05 -08:00
|
|
|
parser.add_argument('--bot-config-file', '-b',
|
|
|
|
action='store',
|
|
|
|
help='third party configuration file (e.g. ~/giphy.conf')
|
|
|
|
|
2017-07-23 15:36:10 -02:30
|
|
|
parser.add_argument('--force',
|
|
|
|
action='store_true',
|
2017-09-01 12:29:19 +02:00
|
|
|
help='try running the bot even if dependencies install fails')
|
2017-07-23 16:16:42 -02:30
|
|
|
|
|
|
|
parser.add_argument('--provision',
|
|
|
|
action='store_true',
|
2017-09-01 12:29:19 +02:00
|
|
|
help='install dependencies for the bot')
|
2017-07-18 01:47:16 -02:30
|
|
|
|
2017-09-01 12:42:01 +02:00
|
|
|
args = parser.parse_args()
|
2017-09-01 12:01:04 +02:00
|
|
|
return args
|
2017-07-18 01:47:16 -02:30
|
|
|
|
|
|
|
|
2017-11-27 13:45:05 -08:00
|
|
|
def exit_gracefully_if_zulip_config_file_does_not_exist(config_file):
|
2017-11-20 14:58:20 -08:00
|
|
|
# type: (str) -> None
|
|
|
|
if not os.path.exists(config_file):
|
|
|
|
print('''
|
|
|
|
ERROR: %s does not exist.
|
|
|
|
|
|
|
|
You may need to download a config file from the Zulip app, or
|
|
|
|
if you have already done that, you need to specify the file
|
|
|
|
location correctly.
|
|
|
|
''' % (config_file,))
|
|
|
|
sys.exit(1)
|
|
|
|
|
2017-11-27 13:45:05 -08:00
|
|
|
def exit_gracefully_if_bot_config_file_does_not_exist(bot_config_file):
|
|
|
|
# type: (str) -> None
|
|
|
|
if bot_config_file is None:
|
|
|
|
# This is a common case, just so succeed quietly. (Some
|
|
|
|
# bots don't have third party configuration.)
|
|
|
|
return
|
|
|
|
|
|
|
|
if not os.path.exists(bot_config_file):
|
|
|
|
print('''
|
|
|
|
ERROR: %s does not exist.
|
|
|
|
|
|
|
|
You probably just specified the wrong file location here.
|
|
|
|
''' % (bot_config_file,))
|
|
|
|
sys.exit(1)
|
|
|
|
|
2017-07-18 01:47:16 -02:30
|
|
|
def main():
|
|
|
|
# type: () -> None
|
2017-09-01 12:01:04 +02:00
|
|
|
args = parse_args()
|
2017-09-01 12:42:01 +02:00
|
|
|
if os.path.isfile(args.bot):
|
|
|
|
bot_path = os.path.abspath(args.bot)
|
|
|
|
bot_name = os.path.splitext(basename(bot_path))[0]
|
|
|
|
else:
|
|
|
|
bot_path = os.path.abspath(os.path.join(current_dir, 'bots', args.bot, args.bot+'.py'))
|
|
|
|
bot_name = args.bot
|
|
|
|
if args.provision:
|
|
|
|
provision_bot(os.path.dirname(bot_path), args.force)
|
|
|
|
lib_module = import_module_from_source(bot_path, bot_name)
|
2017-07-22 17:51:07 -02:30
|
|
|
|
2017-09-01 12:01:04 +02:00
|
|
|
if not args.quiet:
|
2017-07-18 01:47:16 -02:30
|
|
|
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
|
|
|
|
2017-11-27 13:45:05 -08:00
|
|
|
# It's a bit unfortunate that we have two config files, but the
|
|
|
|
# alternative would be way worse for people running multiple bots
|
|
|
|
# or testing against multiple Zulip servers.
|
|
|
|
exit_gracefully_if_zulip_config_file_does_not_exist(args.config_file)
|
|
|
|
exit_gracefully_if_bot_config_file_does_not_exist(args.bot_config_file)
|
|
|
|
|
|
|
|
try:
|
|
|
|
run_message_handler_for_bot(
|
|
|
|
lib_module=lib_module,
|
|
|
|
config_file=args.config_file,
|
|
|
|
bot_config_file=args.bot_config_file,
|
|
|
|
quiet=args.quiet,
|
|
|
|
bot_name=bot_name
|
|
|
|
)
|
|
|
|
except NoBotConfigException:
|
|
|
|
print('''
|
|
|
|
ERROR: Your bot requires you to specify a third party
|
|
|
|
config file with the --bot-config-file option.
|
2017-11-20 14:58:20 -08:00
|
|
|
|
2017-11-27 13:45:05 -08:00
|
|
|
Exiting now.
|
|
|
|
''')
|
|
|
|
sys.exit(1)
|
2017-07-18 01:47:16 -02:30
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|