From e255c73fc380b77c8dca3df21abe5f0c71f20feb Mon Sep 17 00:00:00 2001 From: Eeshan Garg Date: Sat, 22 Jul 2017 20:30:28 -0230 Subject: [PATCH] zulip_bots/provision: Improve logging. --- zulip_bots/zulip_bots/provision.py | 75 +++++++++++++++++++----------- 1 file changed, 47 insertions(+), 28 deletions(-) diff --git a/zulip_bots/zulip_bots/provision.py b/zulip_bots/zulip_bots/provision.py index 6c49e63..de82a75 100755 --- a/zulip_bots/zulip_bots/provision.py +++ b/zulip_bots/zulip_bots/provision.py @@ -1,67 +1,86 @@ #!/usr/bin/env python from __future__ import absolute_import -from __future__ import print_function import argparse +import logging import os import sys +import glob import pip def provision_bot(path_to_bot, force): # type: (str, bool) -> None req_path = os.path.join(path_to_bot, 'requirements.txt') install_path = os.path.join(path_to_bot, 'bot_dependencies') + if os.path.isfile(req_path): - print('Installing dependencies...') + bot_name = os.path.basename(path_to_bot) + logging.info('Installing dependencies for {}...'.format(bot_name)) if not os.path.isdir(install_path): os.makedirs(install_path) + # pip install -r $BASEDIR/requirements.txt -t $BASEDIR/bot_dependencies --quiet rcode = pip.main(['install', '-r', req_path, '-t', install_path, '--quiet']) - if not rcode == 0: - print('Error. Check output of `pip install` above for details.') + + if rcode != 0: + logging.error('Error. Check output of `pip install` above for details.') if not force: - print('Use --force to try running anyway.') + logging.error('Use --force to try running anyway.') sys.exit(rcode) # Use pip's exit code else: - print('Installed.') + logging.info('Installed dependencies successfully.') + sys.path.insert(0, install_path) -def dir_join(dir1, dir2): - # type: (str, str) -> str - return os.path.abspath(os.path.join(dir1, dir2)) -def run(): - # type: () -> None - usage = ''' - Installs dependencies of bots in api/bots directory. Add a - reuirements.txt file in a bot's folder before provisioning. +def parse_args(available_bots): + usage = """ +Installs dependencies of bots in the bots/ +directories. Add a requirements.txt file in a bot's folder +before provisioning. - To provision all bots, use: - ./provision.py - - To provision specific bots, use: - ./provision.py [names of bots] - Example: ./provision.py helloworld xkcd wikipedia - - ''' - - bots_dir = dir_join(os.path.dirname(os.path.abspath(__file__)), 'bots') - available_bots = [b for b in os.listdir(bots_dir) if os.path.isdir(dir_join(bots_dir, b))] +To provision all bots, use: +./provision.py +To provision specific bots, use: +./provision.py [names of bots] +Example: ./provision.py helloworld xkcd wikipedia +""" parser = argparse.ArgumentParser(usage=usage) + parser.add_argument('bots_to_provision', metavar='bots', nargs='*', default=available_bots, help='specific bots to provision (default is all)') + parser.add_argument('--force', default=False, action="store_true", help='Continue installation despite pip errors.') - options = parser.parse_args() + + parser.add_argument('--quiet', '-q', + action='store_true', + default=False, + help='Turn off logging output.') + + return parser.parse_args() + + +def main(): + # type: () -> None + bots_dir = os.path.abspath("bots") + bots_subdirs = map(os.path.abspath, glob.glob("bots/*")) + available_bots = filter(lambda d: os.path.isdir(d), bots_subdirs) + + options = parse_args(available_bots) + + if not options.quiet: + logging.basicConfig(stream=sys.stdout, level=logging.INFO) + for bot in options.bots_to_provision: - provision_bot(os.path.join(dir_join(bots_dir, bot)), options.force) + provision_bot(os.path.join(bots_dir, bot), options.force) if __name__ == '__main__': - run() + main()