<s> from __future__ import print_function <EOL> import os <EOL> import pkg_resources <EOL> import sys <EOL> try : <EOL> import configparser as ConfigParser <EOL> except ImportError : <EOL> import ConfigParser <EOL> from paste . deploy import loadapp , loadwsgi <EOL> SERVER = loadwsgi . SERVER <EOL> from gunicorn . app . base import Application <EOL> from gunicorn . config import Config , get_default_config_file <EOL> from gunicorn import util <EOL> def _has_logging_config ( paste_file ) : <EOL> cfg_parser = ConfigParser . ConfigParser ( ) <EOL> cfg_parser . read ( [ paste_file ] ) <EOL> return cfg_parser . has_section ( '<STR_LIT>' ) <EOL> def paste_config ( gconfig , config_url , relative_to , global_conf = None ) : <EOL> sys . path . insert ( <NUM_LIT:0> , relative_to ) <EOL> pkg_resources . working_set . add_entry ( relative_to ) <EOL> config_url = config_url . split ( '<STR_LIT:#>' ) [ <NUM_LIT:0> ] <EOL> cx = loadwsgi . loadcontext ( SERVER , config_url , relative_to = relative_to , <EOL> global_conf = global_conf ) <EOL> gc , lc = cx . global_conf . copy ( ) , cx . local_conf . copy ( ) <EOL> cfg = { } <EOL> host , port = lc . pop ( '<STR_LIT:host>' , '<STR_LIT>' ) , lc . pop ( '<STR_LIT:port>' , '<STR_LIT>' ) <EOL> if host and port : <EOL> cfg [ '<STR_LIT>' ] = '<STR_LIT>' % ( host , port ) <EOL> elif host : <EOL> cfg [ '<STR_LIT>' ] = host . split ( '<STR_LIT:U+002C>' ) <EOL> cfg [ '<STR_LIT>' ] = gc . get ( '<STR_LIT>' ) <EOL> config_file = config_url . split ( '<STR_LIT::>' ) [ <NUM_LIT:1> ] <EOL> if _has_logging_config ( config_file ) : <EOL> cfg . setdefault ( '<STR_LIT>' , config_file ) <EOL> for k , v in gc . items ( ) : <EOL> if k not in gconfig . settings : <EOL> continue <EOL> cfg [ k ] = v <EOL> for k , v in lc . items ( ) : <EOL> if k not in gconfig . settings : <EOL> continue <EOL> cfg [ k ] = v <EOL> return cfg <EOL> def load_pasteapp ( config_url , relative_to , global_conf = None ) : <EOL> return loadapp ( config_url , relative_to = relative_to , <EOL> global_conf = global_conf ) <EOL> class PasterBaseApplication ( Application ) : <EOL> gcfg = None <EOL> def app_config ( self ) : <EOL> return paste_config ( self . cfg , self . cfgurl , self . relpath , <EOL> global_conf = self . gcfg ) <EOL> def load_config ( self ) : <EOL> super ( PasterBaseApplication , self ) . load_config ( ) <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> parser = ConfigParser . ConfigParser ( ) <EOL> parser . read ( [ self . cfgfname ] ) <EOL> if parser . has_section ( '<STR_LIT>' ) : <EOL> from logging . config import fileConfig <EOL> config_file = os . path . abspath ( self . cfgfname ) <EOL> fileConfig ( config_file , dict ( __file__ = config_file , <EOL> here = os . path . dirname ( config_file ) ) ) <EOL> class PasterApplication ( PasterBaseApplication ) : <EOL> def init ( self , parser , opts , args ) : <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> parser . error ( "<STR_LIT>" ) <EOL> cwd = util . getcwd ( ) <EOL> cfgfname = os . path . normpath ( os . path . join ( cwd , args [ <NUM_LIT:0> ] ) ) <EOL> cfgfname = os . path . abspath ( cfgfname ) <EOL> if not os . path . exists ( cfgfname ) : <EOL> parser . error ( "<STR_LIT>" % cfgfname ) <EOL> self . cfgurl = '<STR_LIT>' % cfgfname <EOL> self . relpath = os . path . dirname ( cfgfname ) <EOL> self . cfgfname = cfgfname <EOL> sys . path . insert ( <NUM_LIT:0> , self . relpath ) <EOL> pkg_resources . working_set . add_entry ( self . relpath ) <EOL> return self . app_config ( ) <EOL> def load ( self ) : <EOL> os . chdir ( self . cfg . chdir ) <EOL> return load_pasteapp ( self . cfgurl , self . relpath , global_conf = self . gcfg ) <EOL> class PasterServerApplication ( PasterBaseApplication ) : <EOL> def __init__ ( self , app , gcfg = None , host = "<STR_LIT:127.0.0.1>" , port = None , * args , ** kwargs ) : <EOL> self . cfg = Config ( ) <EOL> self . gcfg = gcfg <EOL> self . app = app <EOL> self . callable = None <EOL> gcfg = gcfg or { } <EOL> cfgfname = gcfg . get ( "<STR_LIT>" ) <EOL> if cfgfname is not None : <EOL> self . cfgurl = '<STR_LIT>' % cfgfname <EOL> self . relpath = os . path . dirname ( cfgfname ) <EOL> self . cfgfname = cfgfname <EOL> cfg = kwargs . copy ( ) <EOL> if port and not host . startswith ( "<STR_LIT>" ) : <EOL> bind = "<STR_LIT>" % ( host , port ) <EOL> else : <EOL> bind = host <EOL> cfg [ "<STR_LIT>" ] = bind . split ( '<STR_LIT:U+002C>' ) <EOL> if gcfg : <EOL> for k , v in gcfg . items ( ) : <EOL> cfg [ k ] = v <EOL> cfg [ "<STR_LIT>" ] = cfg [ '<STR_LIT>' ] <EOL> try : <EOL> for k , v in cfg . items ( ) : <EOL> if k . lower ( ) in self . cfg . settings and v is not None : <EOL> self . cfg . set ( k . lower ( ) , v ) <EOL> except Exception as e : <EOL> print ( "<STR_LIT>" % str ( e ) , file = sys . stderr ) <EOL> sys . stderr . flush ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if cfg . get ( "<STR_LIT>" ) : <EOL> self . load_config_from_file ( cfg [ "<STR_LIT>" ] ) <EOL> else : <EOL> default_config = get_default_config_file ( ) <EOL> if default_config is not None : <EOL> self . load_config_from_file ( default_config ) <EOL> def load ( self ) : <EOL> os . chdir ( self . cfg . chdir ) <EOL> return self . app <EOL> def run ( ) : <EOL> """<STR_LIT>""" <EOL> util . warn ( """<STR_LIT>""" ) <EOL> from gunicorn . app . pasterapp import PasterApplication <EOL> PasterApplication ( "<STR_LIT>" ) . run ( ) <EOL> def paste_server ( app , gcfg = None , host = "<STR_LIT:127.0.0.1>" , port = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> util . warn ( """<STR_LIT>""" ) <EOL> from gunicorn . app . pasterapp import PasterServerApplication <EOL> PasterServerApplication ( app , gcfg = gcfg , host = host , port = port , * args , ** kwargs ) . run ( ) </s>
