Skip to content

Commit

Permalink
Return 0 on help message
Browse files Browse the repository at this point in the history
  • Loading branch information
bocekm committed Feb 11, 2020
1 parent ff4844d commit 3c6fdba
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
42 changes: 24 additions & 18 deletions convert2rhel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down Expand Up @@ -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__)
Expand Down
14 changes: 14 additions & 0 deletions convert2rhel/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 3c6fdba

Please sign in to comment.