Handle SystemExit properly in migration script

The migration script was catching all exceptions and using
log.exception in case there was any. The problem is, argparse raises a
SystemExit exception after printing the "help" with an exit code of 0.
This would eventually get caught as an exception. Being this an
exception, the logging would print the stack trace, which in the case
of a proper exit would be very confusing, as one might think there is
a problem, when there isn't.

This change now omits the logging if there as a system exit with a 0
or None code (Which is the default code if you call "sys.exit()").

Also, some pep8 issues were fixed here.

Change-Id: I300358e28b39a8a65d7b4253dcca975b46bf757c
This commit is contained in:
Juan Antonio Osorio Robles 2015-01-28 13:25:11 +02:00
parent 852883b99c
commit 14765056fb
1 changed files with 12 additions and 5 deletions

View File

@ -18,9 +18,9 @@ class DatabaseManager:
def __init__(self):
self.parser = self.get_main_parser()
self.subparsers = self.parser.add_subparsers(title='subcommands',
description=
'Action to perform')
self.subparsers = self.parser.add_subparsers(
title='subcommands',
description='Action to perform')
self.add_revision_args()
self.add_downgrade_args()
self.add_upgrade_args()
@ -29,7 +29,7 @@ class DatabaseManager:
"""Create top-level parser and arguments."""
parser = argparse.ArgumentParser(description='Barbican DB manager.')
parser.add_argument('--dburl', '-d', default=None,
help='URL to the database.')
help='URL to the database.')
return parser
@ -90,6 +90,11 @@ class DatabaseManager:
args.func(args)
def _exception_is_successfull_exit(thrown_exception):
return (isinstance(thrown_exception, SystemExit) and
(thrown_exception.code is None or thrown_exception.code == 0))
def main():
# Import and configure logging.
log.setup('barbican-db-manage')
@ -99,7 +104,9 @@ def main():
try:
dm = DatabaseManager()
dm.execute()
except:
except Exception as ex:
if _exception_is_successfull_exit(ex):
pass
LOG.exception('Problem trying to execute Alembic commands')