<s> import os <EOL> import sys <EOL> import time <EOL> import logging <EOL> import threading <EOL> from itertools import chain <EOL> from . base import Cli <EOL> __all__ = [ '<STR_LIT>' ] <EOL> logger = logging . getLogger ( __name__ ) <EOL> try : <EOL> MAXFD = os . sysconf ( "<STR_LIT>" ) <EOL> except : <EOL> MAXFD = <NUM_LIT> <EOL> def flush_fds ( ) : <EOL> for fd in range ( <NUM_LIT:3> , MAXFD + <NUM_LIT:1> ) : <EOL> try : <EOL> os . fsync ( fd ) <EOL> except OSError : <EOL> pass <EOL> class App ( Cli ) : <EOL> '''<STR_LIT>''' <EOL> format = '<STR_LIT>' <EOL> def __init__ ( self , app , shell_namespace = None , extra_files = None , bootstrap = None ) : <EOL> self . app = app <EOL> self . shell_namespace = shell_namespace or { } <EOL> self . extra_files = extra_files <EOL> self . bootstrap = bootstrap <EOL> def command_serve ( self , host = '<STR_LIT>' , port = '<STR_LIT>' , level = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> logging . basicConfig ( level = getattr ( logging , level . upper ( ) ) , format = self . format ) <EOL> if self . bootstrap : <EOL> logger . info ( '<STR_LIT>' ) <EOL> self . bootstrap ( ) <EOL> try : <EOL> server_thread = DevServerThread ( host , port , self . app ) <EOL> server_thread . start ( ) <EOL> wait_for_code_change ( extra_files = self . extra_files ) <EOL> server_thread . running = False <EOL> server_thread . join ( ) <EOL> logger . info ( '<STR_LIT>' ) <EOL> flush_fds ( ) <EOL> pid = os . fork ( ) <EOL> if pid : <EOL> os . closerange ( <NUM_LIT:3> , MAXFD ) <EOL> os . waitpid ( pid , <NUM_LIT:0> ) <EOL> os . execvp ( sys . executable , [ sys . executable ] + sys . argv ) <EOL> else : <EOL> sys . exit ( ) <EOL> except KeyboardInterrupt : <EOL> logger . info ( '<STR_LIT>' ) <EOL> server_thread . running = False <EOL> server_thread . join ( ) <EOL> sys . exit ( ) <EOL> def command_shell ( self ) : <EOL> '''<STR_LIT>''' <EOL> from code import interact <EOL> interact ( '<STR_LIT>' . format ( self . shell_namespace ) , <EOL> local = self . shell_namespace ) <EOL> class DevServerThread ( threading . Thread ) : <EOL> def __init__ ( self , host , port , app ) : <EOL> from wsgiref . simple_server import make_server , WSGIServer , WSGIRequestHandler <EOL> self . host = host <EOL> self . port = port <EOL> class DevServer ( WSGIServer ) : <EOL> timeout = <NUM_LIT> <EOL> class RequestHandler ( WSGIRequestHandler ) : <EOL> def address_string ( slf ) : <EOL> return '<STR_LIT>' . format ( host , port ) <EOL> def log_message ( self , format , * args ) : <EOL> logger . info ( "<STR_LIT>" , <EOL> self . client_address [ <NUM_LIT:0> ] , <EOL> self . log_date_time_string ( ) , <EOL> format % args ) <EOL> try : <EOL> self . port = int ( port ) <EOL> except ValueError : <EOL> raise ValueError ( <EOL> '<STR_LIT>' . format ( port ) ) <EOL> self . running = True <EOL> self . server = make_server ( self . host , self . port , app , server_class = DevServer , <EOL> handler_class = RequestHandler ) <EOL> super ( DevServerThread , self ) . __init__ ( ) <EOL> def run ( self ) : <EOL> logger . info ( '<STR_LIT>' , self . port ) <EOL> while self . running : <EOL> self . server . handle_request ( ) <EOL> def iter_module_files ( ) : <EOL> for module in sys . modules . values ( ) : <EOL> filename = getattr ( module , '<STR_LIT>' , None ) <EOL> if filename : <EOL> while not os . path . isfile ( filename ) : <EOL> filename = os . path . dirname ( filename ) <EOL> if not filename : <EOL> break <EOL> else : <EOL> if filename . endswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> filename = filename [ : - <NUM_LIT:1> ] <EOL> yield filename <EOL> def wait_for_code_change ( extra_files = None , interval = <NUM_LIT:1> ) : <EOL> mtimes = { } <EOL> while <NUM_LIT:1> : <EOL> for filename in chain ( iter_module_files ( ) , extra_files or ( ) ) : <EOL> try : <EOL> mtime = os . stat ( filename ) . st_mtime <EOL> except OSError : <EOL> continue <EOL> old_time = mtimes . get ( filename ) <EOL> if old_time is None : <EOL> mtimes [ filename ] = mtime <EOL> elif mtime > old_time : <EOL> logger . info ( '<STR_LIT>' , filename ) <EOL> return <EOL> time . sleep ( interval ) </s>
