def main(): """Starts the execution of anvil without injecting variables into the global namespace. Ensures that logging is setup and that sudo access is available and in-use. Arguments: N/A Returns: 1 for success, 0 for failure and 2 for permission change failure. """ # Do this first so people can see the help message... args = opts.parse(load_previous_settings()) # Configure logging levels log_level = logging.INFO if args['verbose']: log_level = logging.DEBUG logging.setupLogging(log_level, tee_filename=args['tee_file']) LOG.debug("Log level is: %s" % (logging.getLevelName(log_level))) def print_exc(exc): if not exc: return msg = str(exc).strip() if not msg: return if not (msg.endswith(".") or msg.endswith("!")): msg = msg + "." if msg: print(msg) def print_traceback(): traceback = None if log_level < logging.INFO: # See: http://docs.python.org/library/traceback.html # When its not none u get more detailed info about the exception traceback = sys.exc_traceback tb.print_exception(sys.exc_type, sys.exc_value, traceback, file=sys.stdout) try: run(args) utils.goodbye(True) return 0 except excp.PermException as e: print_exc(e) print(( "This program should be running via %s as it performs some root-only commands is it not?" ) % (colorizer.quote('sudo', quote_color='red'))) return 2 except excp.OptionException as e: print_exc(e) print("Perhaps you should try %s" % (colorizer.quote('--help', quote_color='red'))) return 1 except Exception: utils.goodbye(False) print_traceback() return 1
def main(): """ Starts the execution of without injecting variables into the global namespace. Ensures that logging is setup and that sudo access is available and in-use. Arguments: N/A Returns: 1 for success, 0 for failure and 2 for permission change failure. """ # Do this first so people can see the help message... args = opts.parse(load_previous_settings()) # Configure logging levels log_level = logging.INFO if args['verbose'] or args['dryrun']: log_level = logging.DEBUG logging.setupLogging(log_level) LOG.debug("Log level is: %s" % (logging.getLevelName(log_level))) def print_exc(exc): if not exc: return msg = str(exc).strip() if not msg: return if not (msg.endswith(".") or msg.endswith("!")): msg = msg + "." if msg: print(msg) def print_traceback(): traceback = None if log_level < logging.INFO: # See: http://docs.python.org/library/traceback.html # When its not none u get more detailed info about the exception traceback = sys.exc_traceback tb.print_exception(sys.exc_type, sys.exc_value, traceback, file=sys.stdout) try: ensure_perms() except excp.PermException as e: print_exc(e) print(("This program should be running via %s as it performs some root-only commands is it not?") % (colorizer.quote('sudo', quote_color='red'))) return 2 try: run(args) utils.goodbye(True) return 0 except excp.OptionException as e: print_exc(e) print("Perhaps you should try %s" % (colorizer.quote('--help', quote_color='red'))) return 1 except Exception: utils.goodbye(False) print_traceback() return 1
def main(): """ Starts the execution of without injecting variables into the global namespace. Ensures that logging is setup and that sudo access is available and in-use. Arguments: N/A Returns: 1 for success, 0 for failure """ # Do this first so people can see the help message... args = opts.parse(load_previous_settings()) # Configure logging levels log_level = logging.INFO if args['verbose'] or args['dryrun']: log_level = logging.DEBUG logging.setupLogging(log_level) LOG.debug("Log level is: %s" % (logging.getLevelName(log_level))) def clean_exc(exc): msg = str(exc).strip() if msg.endswith(".") or msg.endswith("!"): return msg else: return msg + "." def traceback_fn(): traceback = None if log_level < logging.INFO: # See: http://docs.python.org/library/traceback.html # When its not none u get more detailed info about the exception traceback = sys.exc_traceback tb.print_exception(sys.exc_type, sys.exc_value, traceback, file=sys.stdout) try: # Drop to usermode sh.user_mode(quiet=False) except excp.AnvilException as e: print(clean_exc(e)) print("This program should be running via %s, is it not?" % (colorizer.quote('sudo', quote_color='red'))) return 1 try: run(args) utils.goodbye(True) return 0 except excp.OptionException as e: print(clean_exc(e)) print("Perhaps you should try %s" % (colorizer.quote('--help', quote_color='red'))) return 1 except Exception: utils.goodbye(False) traceback_fn() return 1