<s> """<STR_LIT>""" <EOL> from prestoadmin import __version__ <EOL> from prestoadmin . util import constants <EOL> from prestoadmin . util import filesystem <EOL> from prestoadmin . util . exception import ExceptionWithCause <EOL> import __main__ as main <EOL> import functools <EOL> import logging <EOL> import logging . config <EOL> import os <EOL> import sys <EOL> import traceback <EOL> logger = logging . getLogger ( '<STR_LIT:__main__>' ) <EOL> class Application ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , version = None , log_file_path = None ) : <EOL> self . name = str ( name ) <EOL> self . __log_file_path = log_file_path or ( self . name + '<STR_LIT>' ) <EOL> if not os . path . isabs ( self . __log_file_path ) : <EOL> self . __log_file_path = os . path . join ( <EOL> constants . PRESTOADMIN_LOG_DIR , <EOL> self . __log_file_path <EOL> ) <EOL> self . version = version or __version__ <EOL> def __enter__ ( self ) : <EOL> self . __configure_logging ( ) <EOL> return self <EOL> def __configure_logging ( self ) : <EOL> try : <EOL> for maybe_file_path in self . __logging_configuration_file_paths ( ) : <EOL> if not os . path . exists ( maybe_file_path ) : <EOL> continue <EOL> else : <EOL> config_file_path = maybe_file_path <EOL> filesystem . ensure_parent_directories_exist ( <EOL> self . __log_file_path <EOL> ) <EOL> logging . config . fileConfig ( <EOL> config_file_path , <EOL> defaults = { '<STR_LIT>' : self . __log_file_path } , <EOL> disable_existing_loggers = False <EOL> ) <EOL> self . __log_application_start ( ) <EOL> logger . debug ( <EOL> '<STR_LIT>' , <EOL> config_file_path <EOL> ) <EOL> break <EOL> except Exception as e : <EOL> sys . stderr . write ( <EOL> '<STR_LIT>' % self . name <EOL> ) <EOL> sys . stderr . flush ( ) <EOL> sys . exit ( str ( e ) ) <EOL> def __logging_configuration_file_paths ( self ) : <EOL> yield constants . LOGGING_CONFIG_FILE_NAME <EOL> yield ( self . __log_file_path + '<STR_LIT>' ) <EOL> yield ( self . __main_module_path ( ) + '<STR_LIT>' ) <EOL> for dir_path in constants . LOGGING_CONFIG_FILE_DIRECTORIES : <EOL> yield os . path . join ( dir_path , constants . LOGGING_CONFIG_FILE_NAME ) <EOL> def __main_module_path ( self ) : <EOL> return os . path . abspath ( main . __file__ ) <EOL> def __log_application_start ( self ) : <EOL> LOG_SEPARATOR = '<STR_LIT>' <EOL> logger . debug ( LOG_SEPARATOR ) <EOL> logger . debug ( <EOL> '<STR_LIT>' . format ( <EOL> name = self . name , <EOL> version = self . version <EOL> ) <EOL> ) <EOL> logger . debug ( LOG_SEPARATOR ) <EOL> logger . debug ( '<STR_LIT>' . format ( sys . argv ) ) <EOL> def __exit__ ( self , exc_type , exception , trace ) : <EOL> self . exc_type = exc_type <EOL> self . exception = exception <EOL> self . trace = trace <EOL> try : <EOL> if exc_type is None : <EOL> self . __handle_no_exception ( ) <EOL> elif exc_type == SystemExit : <EOL> self . __handle_system_exit ( ) <EOL> else : <EOL> self . _handle_error ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> finally : <EOL> self . _exit_cleanup_hook ( ) <EOL> def _exit_cleanup_hook ( self ) : <EOL> logging . shutdown ( ) <EOL> def __handle_no_exception ( self ) : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> def __handle_system_exit ( self ) : <EOL> code = None <EOL> try : <EOL> if self . exception is None or self . exception . code is None : <EOL> code = <NUM_LIT:0> <EOL> else : <EOL> code = int ( self . exception . code ) <EOL> except ValueError : <EOL> code = <NUM_LIT:1> <EOL> except AttributeError : <EOL> try : <EOL> code = int ( self . exception ) <EOL> except ValueError : <EOL> code = <NUM_LIT:1> <EOL> except : <EOL> logger . exception ( "<STR_LIT>" % str ( self . exception ) ) <EOL> if code is not None : <EOL> if code is not <NUM_LIT:0> : <EOL> self . _log_exception ( ) <EOL> logger . debug ( '<STR_LIT>' , code ) <EOL> else : <EOL> self . _log_exception ( ) <EOL> sys . exit ( code ) <EOL> def _handle_error ( self ) : <EOL> self . _log_exception ( ) <EOL> self . __display_error_message ( str ( self . exception ) ) <EOL> def __display_error_message ( self , message ) : <EOL> log_file_path = self . __get_root_log_file_path ( ) <EOL> error_message = '<STR_LIT>' <EOL> if log_file_path : <EOL> error_message += '<STR_LIT>' <EOL> error_message += log_file_path <EOL> print >> sys . stderr , message + error_message <EOL> def __get_root_log_file_path ( self ) : <EOL> for handler in logging . root . handlers : <EOL> if isinstance ( handler , logging . FileHandler ) : <EOL> return handler . baseFilename <EOL> return None <EOL> def _log_exception ( self ) : <EOL> formatted_stack_trace = '<STR_LIT>' . join ( <EOL> traceback . format_exception ( <EOL> self . exc_type , <EOL> self . exception , <EOL> self . trace <EOL> ) + [ ExceptionWithCause . get_cause_if_supported ( self . exception ) ] <EOL> ) <EOL> logger . error ( <EOL> '<STR_LIT>' . format ( <EOL> t = self . exc_type , <EOL> ex = str ( self . exception ) , <EOL> tb = formatted_stack_trace <EOL> ) <EOL> ) <EOL> def entry_point ( name , version = None , log_file_path = None , <EOL> application_class = Application ) : <EOL> """<STR_LIT>""" <EOL> def application_decorator ( method ) : <EOL> @ functools . wraps ( method ) <EOL> def wrapped_application ( * args , ** kwargs ) : <EOL> with application_class ( <EOL> name , <EOL> version = version , <EOL> log_file_path = log_file_path <EOL> ) : <EOL> return method ( * args , ** kwargs ) <EOL> return wrapped_application <EOL> return application_decorator </s>
