192 lines
7.9 KiB
Python
Executable file
192 lines
7.9 KiB
Python
Executable file
#!/usr/bin/python
|
|
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
|
|
# vi: set ft=python sts=4 ts=4 sw=4 et:
|
|
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
|
|
#
|
|
# See COPYING file distributed along with the openfmri package for the
|
|
# copyright and license terms.
|
|
#
|
|
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
|
|
""""""
|
|
|
|
__docformat__ = 'restructuredtext'
|
|
|
|
# set up logging first
|
|
import logging
|
|
log_levels = dict(critical=logging.CRITICAL, error=logging.ERROR,
|
|
warning=logging.WARNING, info=logging.INFO,
|
|
debug=logging.DEBUG)
|
|
lgr = logging.getLogger('openfmri')
|
|
# do chatterbox by default (but only for the openfmri.* loggers)
|
|
lgr.setLevel(logging.DEBUG)
|
|
|
|
import os
|
|
import sys
|
|
import argparse
|
|
import textwrap
|
|
import openfmri.cmdline as mvcmd
|
|
from openfmri.cmdline import helpers, common_args
|
|
import openfmri
|
|
|
|
def _license_info():
|
|
return """\
|
|
Copyright (c) 2012 openfmri developers
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
|
|
Written by Michael Hanke with the help of numerous other contributors.
|
|
"""
|
|
|
|
|
|
# setup cmdline args parser
|
|
# main parser
|
|
parser = argparse.ArgumentParser(
|
|
fromfile_prefix_chars='@',
|
|
# usage="%(prog)s ...",
|
|
description="""\
|
|
Here is an initial description that clearly needs improvement.
|
|
Anyway, here are the currently available commands:
|
|
""",
|
|
epilog='',
|
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
add_help=False
|
|
)
|
|
# common options
|
|
helpers.parser_add_common_opt(parser, 'help')
|
|
helpers.parser_add_common_opt(parser,
|
|
'version',
|
|
version='openfmri %s\n\n%s' % (openfmri.__version__,
|
|
_license_info()))
|
|
if __debug__:
|
|
parser.add_argument(
|
|
'--dbg', action='store_true', dest='common_debug',
|
|
help="do not catch exceptions and show exception traceback")
|
|
parser.add_argument('--log-level',
|
|
choices=('critical', 'error', 'warning', 'info', 'debug'),
|
|
dest='common_log_level',
|
|
help="""level of verbosity in log files. By default
|
|
everything, including debug messages is logged.""")
|
|
parser.add_argument('--verbose-level',
|
|
choices=('critical', 'error', 'warning', 'info', 'debug'),
|
|
dest='common_verbose_level',
|
|
help="""level of verbosity of console output. By default
|
|
only warnings and errors are printed.""")
|
|
parser.add_argument('-c', '--cfg', '--config',
|
|
dest='common_config_file', nargs=1,
|
|
help="""configuration file with settings to overwrite all
|
|
defaults and configurations from other locations.""")
|
|
parser.add_argument('--run-plugin',
|
|
help="""Nipype plugin for executing workflows.
|
|
Default: serial execution one the local machine""")
|
|
parser.add_argument('--store-graph-only', action='store_true', dest='common_store_graph',
|
|
help="""Store a graphical representation of the workflow in its
|
|
working directory, but do not execute the workflow.""")
|
|
|
|
# subparsers
|
|
subparsers = parser.add_subparsers()
|
|
# for all subcommand modules it can find
|
|
cmd_short_description = []
|
|
for cmd in sorted([c for c in dir(mvcmd) if c.startswith('cmd_')]):
|
|
cmd_name = cmd[4:]
|
|
subcmdmod = getattr(__import__('openfmri.cmdline',
|
|
globals(), locals(),
|
|
[cmd], -1),
|
|
cmd)
|
|
# deal with optional parser args
|
|
if 'parser_args' in subcmdmod.__dict__:
|
|
parser_args = subcmdmod.parser_args
|
|
else:
|
|
parser_args = dict()
|
|
# use module description, if no explicit description is available
|
|
if not 'description' in parser_args:
|
|
parser_args['description'] = subcmdmod.__doc__
|
|
# create subparser, use module suffix as cmd name
|
|
subparser = subparsers.add_parser(cmd_name, add_help=False, **parser_args)
|
|
# all subparser can report the version
|
|
helpers.parser_add_common_opt(
|
|
subparser, 'version',
|
|
version='openfmri-%s %s\n\n%s' % (cmd_name, openfmri.__version__,
|
|
_license_info()))
|
|
# our own custom help for all commands
|
|
helpers.parser_add_common_opt(subparser, 'help')
|
|
# let module configure the parser
|
|
subcmdmod.setup_parser(subparser)
|
|
# logger for command
|
|
|
|
# configure 'run' function for this command
|
|
subparser.set_defaults(func=subcmdmod.run,
|
|
logger=logging.getLogger('openfmri.%s' % cmd))
|
|
# store short description for later
|
|
sdescr = getattr(subcmdmod, 'short_description',
|
|
parser_args['description'].split('\n')[0])
|
|
cmd_short_description.append((cmd_name, sdescr))
|
|
|
|
# create command summary
|
|
cmd_summary = []
|
|
for cd in cmd_short_description:
|
|
cmd_summary.append('%s\n%s\n\n' \
|
|
% (cd[0],
|
|
textwrap.fill(cd[1], 75,
|
|
initial_indent=' ' * 4,
|
|
subsequent_indent=' ' * 4)))
|
|
parser.description = '%s\n%s\n\n%s' \
|
|
% (parser.description,
|
|
'\n'.join(cmd_summary),
|
|
textwrap.fill("""\
|
|
Detailed usage information for individual commands is
|
|
available via command-specific help options, i.e.:
|
|
%s <command> --help""" % sys.argv[0],
|
|
75, initial_indent='',
|
|
subsequent_indent=''))
|
|
|
|
# parse cmd args
|
|
args = parser.parse_args()
|
|
# load custom config
|
|
openfmri.cfg.reload(args.common_config_file)
|
|
# default log levels
|
|
args.common_log_level = log_levels.get(args.common_log_level, logging.DEBUG)
|
|
args.common_verbose_level = log_levels.get(args.common_verbose_level,
|
|
logging.WARNING)
|
|
# configure the loggers
|
|
# console output
|
|
console = logging.StreamHandler()
|
|
console.setLevel(args.common_verbose_level)
|
|
# TODO: we may want a more fancy custom formatter
|
|
formatter = logging.Formatter(openfmri.cfg.get('logging', 'console format'))
|
|
console.setFormatter(formatter)
|
|
lgr.addHandler(console)
|
|
|
|
# post-mortem test of config file presence after the logger is functional
|
|
if not args.common_config_file is None and not os.path.isfile(args.common_config_file[0]):
|
|
raise ValueError("configuration file '%s' does not exist"
|
|
% args.common_config_file[0])
|
|
# run the function associated with the selected command
|
|
try:
|
|
# return the workflow
|
|
wf = args.func(args)
|
|
if args.common_store_graph:
|
|
wf.write_graph(graph2use='flat')
|
|
else:
|
|
wf.run(plugin=args.run_plugin)
|
|
except Exception as exc:
|
|
lgr.error('%s (%s)' % (str(exc), exc.__class__.__name__))
|
|
if args.common_debug:
|
|
import pdb
|
|
pdb.post_mortem()
|
|
sys.exit(1)
|