<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , print_function <EOL> from django . core import management <EOL> from optparse import OptionParser <EOL> import os <EOL> import re <EOL> import sys <EOL> from logan import importer <EOL> from logan . settings import create_default_settings <EOL> try : <EOL> raw_input <EOL> except NameError : <EOL> raw_input = input <EOL> __configured = False <EOL> def sanitize_name ( project ) : <EOL> project = project . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:->' ) <EOL> return re . sub ( '<STR_LIT>' , '<STR_LIT:->' , project ) <EOL> def parse_args ( args ) : <EOL> """<STR_LIT>""" <EOL> index = None <EOL> for arg_i , arg in enumerate ( args ) : <EOL> if not arg . startswith ( '<STR_LIT:->' ) : <EOL> index = arg_i <EOL> break <EOL> if index is None : <EOL> return ( args , None , [ ] ) <EOL> return ( args [ : index ] , args [ index ] , args [ ( index + <NUM_LIT:1> ) : ] ) <EOL> def is_configured ( ) : <EOL> global __configured <EOL> return __configured <EOL> def configure_app ( config_path = None , project = None , default_config_path = None , <EOL> default_settings = None , settings_initializer = None , <EOL> settings_envvar = None , initializer = None , allow_extras = True , <EOL> config_module_name = None , runner_name = None , on_configure = None ) : <EOL> """<STR_LIT>""" <EOL> global __configured <EOL> project_filename = sanitize_name ( project ) <EOL> if default_config_path is None : <EOL> default_config_path = '<STR_LIT>' % ( project_filename , project_filename ) <EOL> if settings_envvar is None : <EOL> settings_envvar = project_filename . upper ( ) + '<STR_LIT>' <EOL> if config_module_name is None : <EOL> config_module_name = project_filename + '<STR_LIT>' <EOL> if settings_envvar in os . environ : <EOL> default_config_path = os . environ . get ( settings_envvar ) <EOL> else : <EOL> default_config_path = os . path . normpath ( os . path . abspath ( os . path . expanduser ( default_config_path ) ) ) <EOL> if not config_path : <EOL> config_path = default_config_path <EOL> config_path = os . path . expanduser ( config_path ) <EOL> if not os . path . exists ( config_path ) : <EOL> if runner_name : <EOL> raise ValueError ( "<STR_LIT>" % ( runner_name , ) ) <EOL> raise ValueError ( "<STR_LIT>" % ( config_path , ) ) <EOL> os . environ [ '<STR_LIT>' ] = config_module_name <EOL> def settings_callback ( settings ) : <EOL> if initializer is None : <EOL> return <EOL> try : <EOL> initializer ( { <EOL> '<STR_LIT>' : project , <EOL> '<STR_LIT>' : config_path , <EOL> '<STR_LIT>' : settings , <EOL> } ) <EOL> except Exception : <EOL> import sys <EOL> import traceback <EOL> traceback . print_exc ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> importer . install ( <EOL> config_module_name , config_path , default_settings , <EOL> allow_extras = allow_extras , callback = settings_callback ) <EOL> __configured = True <EOL> from django . conf import settings <EOL> hasattr ( settings , '<STR_LIT>' ) <EOL> if on_configure : <EOL> on_configure ( { <EOL> '<STR_LIT>' : project , <EOL> '<STR_LIT>' : config_path , <EOL> '<STR_LIT>' : settings , <EOL> } ) <EOL> def run_app ( ** kwargs ) : <EOL> sys_args = sys . argv <EOL> runner_name = os . path . basename ( sys_args [ <NUM_LIT:0> ] ) <EOL> args , command , command_args = parse_args ( sys_args [ <NUM_LIT:1> : ] ) <EOL> if not command : <EOL> print ( "<STR_LIT>" % runner_name ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> default_config_path = kwargs . get ( '<STR_LIT>' ) <EOL> parser = OptionParser ( ) <EOL> if command == '<STR_LIT>' : <EOL> ( options , opt_args ) = parser . parse_args ( ) <EOL> settings_initializer = kwargs . get ( '<STR_LIT>' ) <EOL> config_path = os . path . expanduser ( '<STR_LIT:U+0020>' . join ( opt_args [ <NUM_LIT:1> : ] ) or default_config_path ) <EOL> if os . path . exists ( config_path ) : <EOL> resp = None <EOL> while resp not in ( '<STR_LIT:Y>' , '<STR_LIT:n>' ) : <EOL> resp = raw_input ( '<STR_LIT>' % config_path ) <EOL> if resp == '<STR_LIT:n>' : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> try : <EOL> create_default_settings ( config_path , settings_initializer ) <EOL> except OSError as e : <EOL> raise e . __class__ ( '<STR_LIT>' % config_path ) <EOL> print ( "<STR_LIT>" % config_path ) <EOL> return <EOL> parser . add_option ( '<STR_LIT>' , metavar = '<STR_LIT>' ) <EOL> ( options , logan_args ) = parser . parse_args ( args ) <EOL> config_path = options . config <EOL> configure_app ( config_path = config_path , ** kwargs ) <EOL> management . execute_from_command_line ( [ runner_name , command ] + command_args ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> run_app ( ) </s>
