<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import logging <EOL> import os <EOL> import sys <EOL> import threading <EOL> import time <EOL> import subprocess <EOL> from wsgiref . simple_server import WSGIRequestHandler <EOL> from pecan . commands import BaseCommand <EOL> from pecan import util <EOL> logger = logging . getLogger ( __name__ ) <EOL> class ServeCommand ( BaseCommand ) : <EOL> """<STR_LIT>""" <EOL> arguments = BaseCommand . arguments + ( { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:default>' : False , <EOL> '<STR_LIT:action>' : '<STR_LIT:store_true>' <EOL> } , ) <EOL> def run ( self , args ) : <EOL> super ( ServeCommand , self ) . run ( args ) <EOL> app = self . load_app ( ) <EOL> self . serve ( app , app . config ) <EOL> def create_subprocess ( self ) : <EOL> self . server_process = subprocess . Popen ( <EOL> [ arg for arg in sys . argv if arg != '<STR_LIT>' ] , <EOL> stdout = sys . stdout , stderr = sys . stderr <EOL> ) <EOL> def watch_and_spawn ( self , conf ) : <EOL> from watchdog . observers import Observer <EOL> from watchdog . events import ( <EOL> FileSystemEventHandler , FileSystemMovedEvent , FileModifiedEvent , <EOL> DirModifiedEvent <EOL> ) <EOL> print ( '<STR_LIT>' ) <EOL> self . create_subprocess ( ) <EOL> parent = self <EOL> class AggressiveEventHandler ( FileSystemEventHandler ) : <EOL> lock = threading . Lock ( ) <EOL> def should_reload ( self , event ) : <EOL> for t in ( <EOL> FileSystemMovedEvent , FileModifiedEvent , DirModifiedEvent <EOL> ) : <EOL> if isinstance ( event , t ) : <EOL> return True <EOL> return False <EOL> def on_modified ( self , event ) : <EOL> if self . should_reload ( event ) and self . lock . acquire ( False ) : <EOL> parent . server_process . kill ( ) <EOL> parent . create_subprocess ( ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> self . lock . release ( ) <EOL> paths = self . paths_to_monitor ( conf ) <EOL> event_handler = AggressiveEventHandler ( ) <EOL> for path , recurse in paths : <EOL> observer = Observer ( ) <EOL> observer . schedule ( <EOL> event_handler , <EOL> path = path , <EOL> recursive = recurse <EOL> ) <EOL> observer . start ( ) <EOL> try : <EOL> while True : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> except KeyboardInterrupt : <EOL> pass <EOL> def paths_to_monitor ( self , conf ) : <EOL> paths = [ ] <EOL> for package_name in getattr ( conf . app , '<STR_LIT>' , [ ] ) : <EOL> module = __import__ ( package_name , fromlist = [ '<STR_LIT>' ] ) <EOL> if hasattr ( module , '<STR_LIT>' ) and hasattr ( module . app , '<STR_LIT>' ) : <EOL> paths . append ( ( <EOL> os . path . dirname ( module . __file__ ) , <EOL> True <EOL> ) ) <EOL> break <EOL> paths . append ( ( os . path . dirname ( conf . __file__ ) , False ) ) <EOL> return paths <EOL> def _serve ( self , app , conf ) : <EOL> from wsgiref . simple_server import make_server <EOL> host , port = conf . server . host , int ( conf . server . port ) <EOL> srv = make_server ( <EOL> host , <EOL> port , <EOL> app , <EOL> handler_class = PecanWSGIRequestHandler , <EOL> ) <EOL> print ( '<STR_LIT>' % os . getpid ( ) ) <EOL> if host == '<STR_LIT>' : <EOL> print ( <EOL> '<STR_LIT>' % <EOL> ( port , port ) <EOL> ) <EOL> else : <EOL> print ( "<STR_LIT>" % ( host , port ) ) <EOL> try : <EOL> srv . serve_forever ( ) <EOL> except KeyboardInterrupt : <EOL> pass <EOL> def serve ( self , app , conf ) : <EOL> """<STR_LIT>""" <EOL> if self . args . reload : <EOL> try : <EOL> self . watch_and_spawn ( conf ) <EOL> except ImportError : <EOL> print ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> else : <EOL> self . _serve ( app , conf ) <EOL> def gunicorn_run ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> from gunicorn . app . wsgiapp import WSGIApplication <EOL> except ImportError as exc : <EOL> args = exc . args <EOL> arg0 = args [ <NUM_LIT:0> ] if args else '<STR_LIT>' <EOL> arg0 += '<STR_LIT>' <EOL> exc . args = ( arg0 , ) + args [ <NUM_LIT:1> : ] <EOL> raise <EOL> class PecanApplication ( WSGIApplication ) : <EOL> def init ( self , parser , opts , args ) : <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> parser . error ( "<STR_LIT>" ) <EOL> self . cfgfname = os . path . normpath ( <EOL> os . path . join ( os . getcwd ( ) , args [ <NUM_LIT:0> ] ) <EOL> ) <EOL> self . cfgfname = os . path . abspath ( self . cfgfname ) <EOL> if not os . path . exists ( self . cfgfname ) : <EOL> parser . error ( "<STR_LIT>" % self . cfgfname ) <EOL> from pecan . configuration import _runtime_conf , set_config <EOL> set_config ( self . cfgfname , overwrite = True ) <EOL> cfg = { } <EOL> if _runtime_conf . get ( '<STR_LIT>' ) : <EOL> server = _runtime_conf [ '<STR_LIT>' ] <EOL> if hasattr ( server , '<STR_LIT:host>' ) and hasattr ( server , '<STR_LIT:port>' ) : <EOL> cfg [ '<STR_LIT>' ] = '<STR_LIT>' % ( <EOL> server . host , server . port <EOL> ) <EOL> return cfg <EOL> def load ( self ) : <EOL> from pecan . deploy import deploy <EOL> return deploy ( self . cfgfname ) <EOL> PecanApplication ( "<STR_LIT>" ) . run ( ) <EOL> class PecanWSGIRequestHandler ( WSGIRequestHandler , object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . path = '<STR_LIT>' <EOL> super ( PecanWSGIRequestHandler , self ) . __init__ ( * args , ** kwargs ) <EOL> def log_message ( self , format , * args ) : <EOL> """<STR_LIT>""" <EOL> code = args [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> levels = { <EOL> '<STR_LIT:4>' : '<STR_LIT>' , <EOL> '<STR_LIT:5>' : '<STR_LIT:error>' <EOL> } <EOL> log_handler = getattr ( logger , levels . get ( code , '<STR_LIT:info>' ) ) <EOL> log_handler ( format % args ) </s>
