From 3c6fdbaa67bab5ddfdf94a851d46dd487f4d50f7 Mon Sep 17 00:00:00 2001 From: Michal Bocek Date: Tue, 11 Feb 2020 16:27:15 +0100 Subject: [PATCH] Return 0 on help message --- convert2rhel/main.py | 42 ++++++++++++++++++++++++------------------ convert2rhel/utils.py | 14 ++++++++++++++ 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/convert2rhel/main.py b/convert2rhel/main.py index b09dbede8c..1fec8f798c 100644 --- a/convert2rhel/main.py +++ b/convert2rhel/main.py @@ -92,28 +92,24 @@ def main(): # restart system if required utils.restart_system() - except: - traceback_str = utils.get_traceback_str() - if toolopts.tool_opts.debug: - # Print the traceback to the user when debug option used - loggerinst.debug(traceback_str) - else: - # Print the traceback to the log file in any way - loggerinst.file(traceback_str) - - print("\n") - if process_phase == ConversionPhase.POST_CLI: + except (Exception, SystemExit, KeyboardInterrupt), err: + # Catching the three exception types separately due to python 2.4 + # (RHEL 5) - 2.7 (RHEL 7) compatibility. + + utils.log_traceback(toolopts.tool_opts.debug) + + if is_help_msg_exit(process_phase, err): + return 0 + elif process_phase in (ConversionPhase.INIT, ConversionPhase.POST_CLI): print("No changes were made to the system.") elif process_phase == ConversionPhase.PRE_PONR_CHANGES: rollback_changes() elif process_phase == ConversionPhase.POST_PONR_CHANGES: - """ - After the process of subscription is done and the mass update of - packages is started convert2rhel will not be able to guarantee a - system rollback without user intervation. If a proper rollback - solution is necessary it will need to be future implemented here - or with the use of other backup tools. - """ + # After the process of subscription is done and the mass update of + # packages is started convert2rhel will not be able to guarantee a + # system rollback without user intervation. If a proper rollback + # solution is necessary it will need to be future implemented here + # or with the use of other backup tools. print("Conversion process interrupted and manual user intervation" " will be necessary.") @@ -194,6 +190,16 @@ def post_ponr_conversion(): return +def is_help_msg_exit(process_phase, err): + """After printing the help message, optparse within the toolopts.CLI() + call terminates the process with sys.exit(0). + """ + if process_phase == ConversionPhase.INIT and \ + isinstance(err, SystemExit) and err.args[0] == 0: + return True + return False + + def rollback_changes(): """Perform a rollback of changes made during conversion.""" loggerinst = logging.getLogger(__name__) diff --git a/convert2rhel/utils.py b/convert2rhel/utils.py index 550645368f..d02af60b1f 100644 --- a/convert2rhel/utils.py +++ b/convert2rhel/utils.py @@ -197,6 +197,20 @@ def prompt_user(question, password=False): return response +def log_traceback(debug): + """Log a traceback either to both a file and stdout, or just file, based + on the debug parameter. + """ + loggerinst = logging.getLogger(__name__) + traceback_str = get_traceback_str() + if debug: + # Print the traceback to the user when debug option used + loggerinst.debug(traceback_str) + else: + # Print the traceback to the log file in any way + loggerinst.file(traceback_str) + + def get_traceback_str(): """Get a traceback of an exception as a string.""" exc_type, exc_value, exc_traceback = sys.exc_info()