mirror of
1
0
Fork 0
dotbot/dotbot/cli.py

93 lines
3.6 KiB
Python
Raw Normal View History

2016-01-16 22:00:15 -05:00
import os, glob
2014-03-19 23:07:30 -04:00
from argparse import ArgumentParser
from .config import ConfigReader, ReadingError
from .dispatcher import Dispatcher, DispatchError
from .messenger import Messenger
from .messenger import Level
2016-01-16 22:00:15 -05:00
from .util import module
2014-03-19 23:07:30 -04:00
import dotbot
import yaml
2014-03-19 23:07:30 -04:00
def add_options(parser):
parser.add_argument('-Q', '--super-quiet', action='store_true',
help='suppress almost all output')
parser.add_argument('-q', '--quiet', action='store_true',
help='suppress most output')
parser.add_argument('-v', '--verbose', action='store_true',
help='enable verbose output')
parser.add_argument('-d', '--base-directory',
help='execute commands from within BASEDIR',
metavar='BASEDIR')
parser.add_argument('-c', '--config-file',
help='run commands given in CONFIGFILE', metavar='CONFIGFILE')
2016-01-16 22:00:15 -05:00
parser.add_argument('-p', '--plugin', action='append', dest='plugins', default=[],
help='load PLUGIN as a plugin', metavar='PLUGIN')
parser.add_argument('--disable-built-in-plugins',
2016-01-16 22:00:15 -05:00
action='store_true', help='disable built-in plugins')
parser.add_argument('--plugin-dir', action='append', dest='plugin_dirs', default=[],
metavar='PLUGIN_DIR', help='load all plugins in PLUGIN_DIR')
parser.add_argument('--no-color', dest='no_color', action='store_true',
help='disable color output')
parser.add_argument('--version', action='store_true',
help='show program\'s version number and exit')
2014-03-19 23:07:30 -04:00
def read_config(config_file):
reader = ConfigReader(config_file)
return reader.get_config()
def main():
log = Messenger()
try:
parser = ArgumentParser()
add_options(parser)
options = parser.parse_args()
if options.version:
print('Dotbot version %s (yaml: %s)' % (dotbot.__version__, yaml.__version__))
exit(0)
2016-01-16 22:00:15 -05:00
if options.super_quiet:
2014-03-19 23:07:30 -04:00
log.set_level(Level.WARNING)
2016-01-16 22:00:15 -05:00
if options.quiet:
2014-03-19 23:07:30 -04:00
log.set_level(Level.INFO)
2016-01-16 22:00:15 -05:00
if options.verbose:
2014-03-19 23:07:30 -04:00
log.set_level(Level.DEBUG)
if options.no_color:
log.use_color(False)
2016-01-16 22:00:15 -05:00
plugin_directories = list(options.plugin_dirs)
if not options.disable_built_in_plugins:
from .plugins import Clean, Create, Link, Shell
2016-01-16 22:00:15 -05:00
plugin_paths = []
for directory in plugin_directories:
for plugin_path in glob.glob(os.path.join(directory, '*.py')):
plugin_paths.append(plugin_path)
for plugin_path in options.plugins:
plugin_paths.append(plugin_path)
for plugin_path in plugin_paths:
abspath = os.path.abspath(plugin_path)
module.load(abspath)
if not options.config_file:
log.error('No configuration file specified')
exit(1)
tasks = read_config(options.config_file)
if not isinstance(tasks, list):
raise ReadingError('Configuration file must be a list of tasks')
if options.base_directory:
2020-01-03 16:07:44 -05:00
base_directory = os.path.abspath(options.base_directory)
else:
# default to directory of config file
2020-01-03 16:07:44 -05:00
base_directory = os.path.dirname(os.path.abspath(options.config_file))
os.chdir(base_directory)
dispatcher = Dispatcher(base_directory)
2014-03-19 23:07:30 -04:00
success = dispatcher.dispatch(tasks)
if success:
log.info('\n==> All tasks executed successfully')
else:
raise DispatchError('\n==> Some tasks were not executed successfully')
except (ReadingError, DispatchError) as e:
log.error('%s' % e)
exit(1)
2014-07-19 03:12:51 -04:00
except KeyboardInterrupt:
log.error('\n==> Operation aborted')
exit(1)