<s> import os <EOL> import sys <EOL> from gunicorn . app . base import Application <EOL> from gunicorn import util <EOL> def is_setting_mod ( path ) : <EOL> return ( os . path . isfile ( os . path . join ( path , "<STR_LIT>" ) ) or <EOL> os . path . isfile ( os . path . join ( path , "<STR_LIT>" ) ) ) <EOL> def find_settings_module ( path ) : <EOL> path = os . path . abspath ( path ) <EOL> project_path = None <EOL> settings_name = "<STR_LIT>" <EOL> if os . path . isdir ( path ) : <EOL> project_path = None <EOL> if not is_setting_mod ( path ) : <EOL> for d in os . listdir ( path ) : <EOL> if d in ( '<STR_LIT:..>' , '<STR_LIT:.>' ) : <EOL> continue <EOL> root = os . path . join ( path , d ) <EOL> if is_setting_mod ( root ) : <EOL> project_path = root <EOL> break <EOL> else : <EOL> project_path = path <EOL> elif os . path . isfile ( path ) : <EOL> project_path = os . path . dirname ( path ) <EOL> settings_name , _ = os . path . splitext ( os . path . basename ( path ) ) <EOL> return project_path , settings_name <EOL> def make_default_env ( cfg ) : <EOL> if cfg . django_settings : <EOL> os . environ [ '<STR_LIT>' ] = cfg . django_settings <EOL> if cfg . pythonpath and cfg . pythonpath is not None : <EOL> paths = cfg . pythonpath . split ( "<STR_LIT:U+002C>" ) <EOL> for path in paths : <EOL> pythonpath = os . path . abspath ( cfg . pythonpath ) <EOL> if pythonpath not in sys . path : <EOL> sys . path . insert ( <NUM_LIT:0> , pythonpath ) <EOL> try : <EOL> os . environ [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> cwd = util . getcwd ( ) <EOL> project_path , settings_name = find_settings_module ( cwd ) <EOL> if not project_path : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> pythonpath , project_name = os . path . split ( project_path ) <EOL> os . environ [ '<STR_LIT>' ] = "<STR_LIT>" % ( project_name , <EOL> settings_name ) <EOL> if pythonpath not in sys . path : <EOL> sys . path . insert ( <NUM_LIT:0> , pythonpath ) <EOL> if project_path not in sys . path : <EOL> sys . path . insert ( <NUM_LIT:0> , project_path ) <EOL> class DjangoApplication ( Application ) : <EOL> def init ( self , parser , opts , args ) : <EOL> if args : <EOL> if ( "<STR_LIT:.>" in args [ <NUM_LIT:0> ] and not ( os . path . isfile ( args [ <NUM_LIT:0> ] ) <EOL> or os . path . isdir ( args [ <NUM_LIT:0> ] ) ) ) : <EOL> self . cfg . set ( "<STR_LIT>" , args [ <NUM_LIT:0> ] ) <EOL> else : <EOL> project_path , settings_name = find_settings_module ( <EOL> os . path . abspath ( args [ <NUM_LIT:0> ] ) ) <EOL> if project_path not in sys . path : <EOL> sys . path . insert ( <NUM_LIT:0> , project_path ) <EOL> if not project_path : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> pythonpath , project_name = os . path . split ( project_path ) <EOL> self . cfg . set ( "<STR_LIT>" , "<STR_LIT>" % ( project_name , <EOL> settings_name ) ) <EOL> self . cfg . set ( "<STR_LIT>" , pythonpath ) <EOL> def load ( self ) : <EOL> os . chdir ( self . cfg . chdir ) <EOL> make_default_env ( self . cfg ) <EOL> mod = util . import_module ( "<STR_LIT>" ) <EOL> return mod . make_wsgi_application ( ) <EOL> class DjangoApplicationCommand ( Application ) : <EOL> def __init__ ( self , options , admin_media_path ) : <EOL> self . usage = None <EOL> self . prog = None <EOL> self . cfg = None <EOL> self . config_file = options . get ( "<STR_LIT>" ) or "<STR_LIT>" <EOL> self . options = options <EOL> self . admin_media_path = admin_media_path <EOL> self . callable = None <EOL> self . project_path = None <EOL> self . do_load_config ( ) <EOL> def init ( self , * args ) : <EOL> if '<STR_LIT>' in self . options : <EOL> self . options [ '<STR_LIT>' ] = self . options . pop ( '<STR_LIT>' ) <EOL> cfg = { } <EOL> for k , v in self . options . items ( ) : <EOL> if k . lower ( ) in self . cfg . settings and v is not None : <EOL> cfg [ k . lower ( ) ] = v <EOL> return cfg <EOL> def load ( self ) : <EOL> os . chdir ( self . cfg . chdir ) <EOL> make_default_env ( self . cfg ) <EOL> mod = util . import_module ( "<STR_LIT>" ) <EOL> return mod . make_command_wsgi_application ( self . admin_media_path ) <EOL> def run ( ) : <EOL> """<STR_LIT>""" <EOL> util . warn ( """<STR_LIT>""" ) <EOL> from gunicorn . app . djangoapp import DjangoApplication <EOL> DjangoApplication ( "<STR_LIT>" ) . run ( ) </s>
