2016-11-22 01:44:16 +01:00
|
|
|
#!/usr/bin/env python
|
2016-08-17 18:34:11 +02:00
|
|
|
from __future__ import print_function
|
2017-05-22 05:14:56 +02:00
|
|
|
from __future__ import absolute_import
|
2016-08-17 18:34:11 +02:00
|
|
|
|
|
|
|
import importlib
|
|
|
|
import logging
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
our_dir = os.path.dirname(os.path.abspath(__file__))
|
2017-01-14 19:58:03 +01:00
|
|
|
sys.path.insert(0, our_dir)
|
2016-08-17 18:34:11 +02:00
|
|
|
|
2017-01-14 19:58:03 +01:00
|
|
|
from bot_lib import run_message_handler_for_bot
|
2017-01-10 21:41:02 +01:00
|
|
|
|
2017-01-22 11:38:34 +01:00
|
|
|
def get_lib_module(bots_fn):
|
2017-05-23 00:42:40 +02:00
|
|
|
bots_fn = os.path.realpath(bots_fn)
|
2017-01-22 11:38:34 +01:00
|
|
|
if not os.path.dirname(bots_fn).startswith(os.path.join(our_dir, 'bots')):
|
|
|
|
print('Sorry, we will only import code from contrib_bots/bots.')
|
2016-08-17 18:34:11 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
2017-01-22 11:38:34 +01:00
|
|
|
if not bots_fn.endswith('.py'):
|
2016-08-17 18:34:11 +02:00
|
|
|
print('Please use a .py extension for library files.')
|
|
|
|
sys.exit(1)
|
2017-01-22 11:38:34 +01:00
|
|
|
base_bots_fn = os.path.basename(os.path.splitext(bots_fn)[0])
|
2017-05-25 02:06:43 +02:00
|
|
|
sys.path.append(os.path.dirname(bots_fn))
|
2017-01-22 11:38:34 +01:00
|
|
|
module_name = base_bots_fn
|
2016-08-17 18:34:11 +02:00
|
|
|
module = importlib.import_module(module_name)
|
|
|
|
return module
|
|
|
|
|
|
|
|
def run():
|
|
|
|
usage = '''
|
2016-11-22 01:44:16 +01:00
|
|
|
./run.py <lib file>
|
|
|
|
Example: ./run.py lib/followup.py
|
2016-08-17 18:34:11 +02:00
|
|
|
(This program loads bot-related code from the
|
|
|
|
library code and then runs a message loop,
|
|
|
|
feeding messages to the library code to handle.)
|
|
|
|
Please make sure you have a current ~/.zuliprc
|
|
|
|
file with the credentials you want to use for
|
|
|
|
this bot.
|
|
|
|
See lib/readme.md for more context.
|
|
|
|
'''
|
|
|
|
|
|
|
|
parser = optparse.OptionParser(usage=usage)
|
|
|
|
parser.add_option('--quiet', '-q',
|
2016-12-11 14:30:45 +01:00
|
|
|
action='store_true',
|
|
|
|
help='Turn off logging output.')
|
2016-08-24 21:09:11 +02:00
|
|
|
parser.add_option('--config-file',
|
2016-12-11 14:30:45 +01:00
|
|
|
action='store',
|
|
|
|
help='(alternate config file to ~/.zuliprc)')
|
2016-08-17 18:34:11 +02:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
if len(args) == 0:
|
|
|
|
print('You must specify a library!')
|
|
|
|
sys.exit(1)
|
|
|
|
|
2017-01-22 11:38:34 +01:00
|
|
|
lib_module = get_lib_module(bots_fn=args[0])
|
2016-08-17 18:34:11 +02:00
|
|
|
|
|
|
|
if not options.quiet:
|
|
|
|
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
|
|
|
|
2016-08-24 21:09:11 +02:00
|
|
|
run_message_handler_for_bot(
|
|
|
|
lib_module=lib_module,
|
|
|
|
config_file=options.config_file,
|
|
|
|
quiet=options.quiet
|
|
|
|
)
|
2016-08-17 18:34:11 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
run()
|