<s> import argparse <EOL> import hashlib <EOL> import os <EOL> import sys <EOL> import threading <EOL> import time <EOL> from app . subcommands . build_subcommand import BuildSubcommand <EOL> from app . subcommands . deploy_subcommand import DeploySubcommand <EOL> from app . subcommands . master_subcommand import MasterSubcommand <EOL> from app . subcommands . shutdown_subcommand import ShutdownSubcommand <EOL> from app . subcommands . slave_subcommand import SlaveSubcommand <EOL> from app . subcommands . stop_subcommand import StopSubcommand <EOL> from app . util import app_info , autoversioning , log , util <EOL> from app . util . argument_parsing import ClusterRunnerArgumentParser , ClusterRunnerHelpFormatter <EOL> from app . util . conf . configuration import Configuration <EOL> from app . util . conf . base_config_loader import BaseConfigLoader , BASE_CONFIG_FILE_SECTION <EOL> from app . util . conf . config_file import ConfigFile <EOL> from app . util . conf . deploy_config_loader import DeployConfigLoader <EOL> from app . util . conf . master_config_loader import MasterConfigLoader <EOL> from app . util . conf . slave_config_loader import SlaveConfigLoader <EOL> from app . util . conf . stop_config_loader import StopConfigLoader <EOL> from app . util . secret import Secret <EOL> from app . util . unhandled_exception_handler import UnhandledExceptionHandler <EOL> def _parse_args ( args ) : <EOL> parser = ClusterRunnerArgumentParser ( ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:version>' , version = '<STR_LIT>' + autoversioning . get_version ( ) ) <EOL> subparsers = parser . add_subparsers ( <EOL> title = '<STR_LIT>' , <EOL> description = '<STR_LIT>' . format ( sys . argv [ <NUM_LIT:0> ] ) , <EOL> dest = '<STR_LIT>' , <EOL> ) <EOL> subparsers . required = True <EOL> master_parser = subparsers . add_parser ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , formatter_class = ClusterRunnerHelpFormatter ) <EOL> master_parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> type = int , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> master_parser . set_defaults ( subcommand_class = MasterSubcommand ) <EOL> slave_parser = subparsers . add_parser ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , formatter_class = ClusterRunnerHelpFormatter ) <EOL> slave_parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> type = int , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> slave_parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> slave_parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> type = int , help = '<STR_LIT>' ) <EOL> slave_parser . set_defaults ( subcommand_class = SlaveSubcommand ) <EOL> for subparser in ( master_parser , slave_parser ) : <EOL> subparser . add_argument ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> stop_parser = subparsers . add_parser ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , formatter_class = ClusterRunnerHelpFormatter ) <EOL> stop_parser . set_defaults ( subcommand_class = StopSubcommand ) <EOL> deploy_parser = subparsers . add_parser ( <EOL> '<STR_LIT>' , help = '<STR_LIT>' , formatter_class = ClusterRunnerHelpFormatter ) <EOL> deploy_parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , type = str , <EOL> help = '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> deploy_parser . add_argument ( <EOL> '<STR_LIT>' , type = int , help = '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> deploy_parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , type = str , nargs = '<STR_LIT:+>' , <EOL> help = '<STR_LIT>' ) <EOL> deploy_parser . add_argument ( <EOL> '<STR_LIT>' , type = int , help = '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> deploy_parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , type = int , help = '<STR_LIT>' ) <EOL> deploy_parser . set_defaults ( subcommand_class = DeploySubcommand ) <EOL> build_parser = subparsers . add_parser ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , formatter_class = ClusterRunnerHelpFormatter ) <EOL> build_parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> build_parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> build_parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> default = None , <EOL> help = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , <EOL> nargs = <NUM_LIT:2> ) <EOL> _add_project_type_subparsers ( build_parser ) <EOL> build_parser . set_defaults ( subcommand_class = BuildSubcommand ) <EOL> shutdown_parser = subparsers . add_parser ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' + <EOL> '<STR_LIT>' , <EOL> formatter_class = ClusterRunnerHelpFormatter <EOL> ) <EOL> shutdown_parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> shutdown_parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> shutdown_parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> shutdown_parser . set_defaults ( subcommand_class = ShutdownSubcommand ) <EOL> for subparser in ( master_parser , slave_parser , build_parser , stop_parser , deploy_parser , shutdown_parser ) : <EOL> subparser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT>' , const = '<STR_LIT>' , dest = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> subparser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT>' , const = '<STR_LIT>' , dest = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> subparser . add_argument ( <EOL> '<STR_LIT:-c>' , '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> parsed_args = vars ( parser . parse_args ( args ) ) <EOL> return parsed_args <EOL> def _add_project_type_subparsers ( build_parser ) : <EOL> """<STR_LIT>""" <EOL> project_type_subparsers = build_parser . add_subparsers ( <EOL> title = '<STR_LIT>' , <EOL> description = '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> ) <EOL> project_types = util . project_type_subclasses_by_name ( ) <EOL> help_argument_blacklist = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> for project_type_name , project_type_class in project_types . items ( ) : <EOL> project_type_parser = project_type_subparsers . add_parser ( <EOL> project_type_name , <EOL> help = '<STR_LIT>' . format ( project_type_name . title ( ) ) , <EOL> formatter_class = ClusterRunnerHelpFormatter , <EOL> ) <EOL> env_args_info = project_type_class . constructor_arguments_info ( blacklist = help_argument_blacklist ) <EOL> for arg_name , arg_info in env_args_info . items ( ) : <EOL> fixed_arg_name = arg_name . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) <EOL> if isinstance ( arg_info . default , bool ) : <EOL> project_type_parser . add_argument ( <EOL> '<STR_LIT>' + fixed_arg_name , <EOL> dest = arg_name , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = arg_info . help , <EOL> default = argparse . SUPPRESS <EOL> ) <EOL> project_type_parser . add_argument ( <EOL> '<STR_LIT>' + fixed_arg_name , <EOL> dest = arg_name , <EOL> action = '<STR_LIT>' , <EOL> help = arg_info . help , <EOL> default = argparse . SUPPRESS <EOL> ) <EOL> project_type_parser . set_defaults ( arg_name = arg_info . default ) <EOL> else : <EOL> project_type_parser . add_argument ( <EOL> '<STR_LIT>' + fixed_arg_name , <EOL> help = arg_info . help , <EOL> required = arg_info . required , <EOL> default = argparse . SUPPRESS , <EOL> ) <EOL> def _initialize_configuration ( app_subcommand , config_filename ) : <EOL> """<STR_LIT>""" <EOL> app_subcommand_conf_loaders = { <EOL> '<STR_LIT>' : MasterConfigLoader ( ) , <EOL> '<STR_LIT>' : SlaveConfigLoader ( ) , <EOL> '<STR_LIT>' : MasterConfigLoader ( ) , <EOL> '<STR_LIT>' : DeployConfigLoader ( ) , <EOL> '<STR_LIT>' : StopConfigLoader ( ) , <EOL> } <EOL> conf_loader = app_subcommand_conf_loaders . get ( app_subcommand ) or BaseConfigLoader ( ) <EOL> config = Configuration . singleton ( ) <EOL> conf_loader . configure_defaults ( config ) <EOL> config_filename = config_filename or Configuration [ '<STR_LIT>' ] <EOL> conf_loader . load_from_config_file ( config , config_filename ) <EOL> conf_loader . configure_postload ( config ) <EOL> _set_secret ( config_filename ) <EOL> def _set_secret ( config_filename ) : <EOL> if '<STR_LIT>' in Configuration and Configuration [ '<STR_LIT>' ] is not None : <EOL> secret = Configuration [ '<STR_LIT>' ] <EOL> else : <EOL> secret = hashlib . sha512 ( ) . hexdigest ( ) <EOL> conf_file = ConfigFile ( config_filename ) <EOL> conf_file . write_value ( '<STR_LIT>' , secret , BASE_CONFIG_FILE_SECTION ) <EOL> Secret . set ( secret ) <EOL> def _start_app_force_kill_countdown ( seconds ) : <EOL> """<STR_LIT>""" <EOL> def log_app_debug_info_and_force_kill_after_delay ( ) : <EOL> time . sleep ( seconds ) <EOL> logger = log . get_logger ( __name__ ) <EOL> logger . error ( '<STR_LIT>' , <EOL> seconds , app_info . get_app_info_string ( ) ) <EOL> logger . critical ( '<STR_LIT>' ) <EOL> os . _exit ( <NUM_LIT:1> ) <EOL> threading . Thread ( target = log_app_debug_info_and_force_kill_after_delay , name = '<STR_LIT>' , daemon = True ) . start ( ) <EOL> def main ( args ) : <EOL> """<STR_LIT>""" <EOL> parsed_args = _parse_args ( args ) <EOL> _initialize_configuration ( parsed_args . pop ( '<STR_LIT>' ) , parsed_args . pop ( '<STR_LIT>' ) ) <EOL> subcommand_class = parsed_args . pop ( '<STR_LIT>' ) <EOL> try : <EOL> unhandled_exception_handler = UnhandledExceptionHandler . singleton ( ) <EOL> with unhandled_exception_handler : <EOL> subcommand_class ( ) . run ( ** parsed_args ) <EOL> finally : <EOL> _start_app_force_kill_countdown ( seconds = <NUM_LIT:10> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( sys . argv [ <NUM_LIT:1> : ] ) </s>
