<s> import argparse <EOL> from datetime import datetime <EOL> import os <EOL> import re <EOL> import signal <EOL> import subprocess <EOL> import sys <EOL> import time <EOL> try : <EOL> from urllib . parse import urlsplit , splitport <EOL> except ImportError : <EOL> pass <EOL> from urllib2 import urlopen , splitport <EOL> from urlparse import urlsplit <EOL> import requests <EOL> _VALID_HOST_PATTERN = r'<STR_LIT>' <EOL> class LiveAndLetDieError ( BaseException ) : <EOL> pass <EOL> def _log ( logging , message ) : <EOL> if logging : <EOL> print ( '<STR_LIT>' . format ( message ) ) <EOL> def _validate_host ( host ) : <EOL> if re . match ( _VALID_HOST_PATTERN , host ) : <EOL> return host <EOL> else : <EOL> raise argparse . ArgumentTypeError ( '<STR_LIT>' <EOL> . format ( host ) ) <EOL> def split_host ( host ) : <EOL> """<STR_LIT>""" <EOL> host , port = ( host . split ( '<STR_LIT::>' ) + [ None ] ) [ : <NUM_LIT:2> ] <EOL> return host , int ( port ) <EOL> def check ( server ) : <EOL> """<STR_LIT>""" <EOL> return server . check ( ) <EOL> def live ( app ) : <EOL> """<STR_LIT>""" <EOL> return app . live ( ) <EOL> def start ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> live ( * args , ** kwargs ) <EOL> def die ( app ) : <EOL> """<STR_LIT>""" <EOL> return app . live ( ) <EOL> def stop ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> die ( * args , ** kwargs ) <EOL> def port_in_use ( port , kill = False , logging = False ) : <EOL> """<STR_LIT>""" <EOL> command_template = '<STR_LIT>' <EOL> process = subprocess . Popen ( command_template . format ( port ) . split ( ) , <EOL> stdout = subprocess . PIPE ) <EOL> headers = process . stdout . readline ( ) . decode ( ) . split ( ) <EOL> if '<STR_LIT>' not in headers : <EOL> _log ( logging , '<STR_LIT>' . format ( port ) ) <EOL> return False <EOL> index_pid = headers . index ( '<STR_LIT>' ) <EOL> index_cmd = headers . index ( '<STR_LIT>' ) <EOL> row = process . stdout . readline ( ) . decode ( ) . split ( ) <EOL> if len ( row ) < index_pid : <EOL> _log ( logging , '<STR_LIT>' . format ( port ) ) <EOL> return False <EOL> pid = int ( row [ index_pid ] ) <EOL> command = row [ index_cmd ] <EOL> if pid and command . startswith ( '<STR_LIT>' ) : <EOL> _log ( logging , '<STR_LIT>' <EOL> . format ( port , pid ) ) <EOL> if kill : <EOL> _log ( logging , <EOL> '<STR_LIT>' <EOL> . format ( pid , port ) ) <EOL> os . kill ( pid , signal . SIGKILL ) <EOL> try : <EOL> kill_process ( pid , logging ) <EOL> _log ( logging , <EOL> '<STR_LIT>' <EOL> . format ( pid ) ) <EOL> return port_in_use ( port , kill ) <EOL> except OSError : <EOL> return False <EOL> else : <EOL> return pid <EOL> def kill_process ( pid , logging = False ) : <EOL> try : <EOL> _log ( logging , '<STR_LIT>' . format ( pid ) ) <EOL> os . kill ( int ( pid ) , signal . SIGKILL ) <EOL> return <EOL> except OSError : <EOL> return False <EOL> def _get_total_seconds ( td ) : <EOL> """<STR_LIT>""" <EOL> return ( td . microseconds + ( td . seconds + td . days * <NUM_LIT> * <NUM_LIT> ) * <NUM_LIT:10> ** <NUM_LIT:6> ) / <NUM_LIT:10> ** <NUM_LIT:6> <EOL> class Base ( object ) : <EOL> """<STR_LIT>""" <EOL> _argument_parser = argparse . ArgumentParser ( ) <EOL> def __init__ ( self , path , host = '<STR_LIT:127.0.0.1>' , port = <NUM_LIT> , timeout = <NUM_LIT> , <EOL> check_url = None , executable = '<STR_LIT>' , logging = False , <EOL> suppress_output = True , ** kwargs ) : <EOL> self . path = path <EOL> self . timeout = timeout <EOL> self . host = host <EOL> self . port = port <EOL> self . process = None <EOL> self . executable = executable <EOL> self . logging = logging <EOL> self . suppress_output = suppress_output <EOL> self . check_url = '<STR_LIT>' . format ( host , port ) <EOL> self . scheme = '<STR_LIT:http>' <EOL> if check_url : <EOL> self . check_url = self . _normalize_check_url ( check_url ) <EOL> @ property <EOL> def default_url ( self ) : <EOL> return '<STR_LIT>' . format ( self . scheme , self . host , self . port ) <EOL> def _kill ( self ) : <EOL> if self . process : <EOL> try : <EOL> os . killpg ( self . process . pid , signal . SIGKILL ) <EOL> except OSError : <EOL> self . process . kill ( ) <EOL> self . process . wait ( ) <EOL> def _normalize_check_url ( self , check_url ) : <EOL> """<STR_LIT>""" <EOL> split_url = urlsplit ( check_url ) <EOL> host = splitport ( split_url . path or split_url . netloc ) [ <NUM_LIT:0> ] <EOL> return '<STR_LIT>' . format ( self . scheme , host , self . port ) <EOL> def check ( self , check_url = None ) : <EOL> """<STR_LIT>""" <EOL> if check_url is not None : <EOL> self . check_url = self . _normalize_check_url ( check_url ) <EOL> response = None <EOL> sleeped = <NUM_LIT:0.0> <EOL> t = datetime . now ( ) <EOL> while not response : <EOL> try : <EOL> response = requests . get ( self . check_url , verify = False ) <EOL> except requests . exceptions . ConnectionError : <EOL> if sleeped > self . timeout : <EOL> self . _kill ( ) <EOL> raise LiveAndLetDieError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> self . __class__ . __name__ , <EOL> self . check_url , <EOL> self . timeout , <EOL> '<STR_LIT:U+0020>' . join ( self . create_command ( ) ) <EOL> ) <EOL> ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> sleeped = _get_total_seconds ( datetime . now ( ) - t ) <EOL> return _get_total_seconds ( datetime . now ( ) - t ) <EOL> def live ( self , kill_port = False , check_url = None ) : <EOL> """<STR_LIT>""" <EOL> pid = port_in_use ( self . port , kill_port ) <EOL> if pid : <EOL> raise LiveAndLetDieError ( <EOL> '<STR_LIT>' <EOL> . format ( self . port , pid ) <EOL> ) <EOL> host = str ( self . host ) <EOL> if re . match ( _VALID_HOST_PATTERN , host ) : <EOL> with open ( os . devnull , "<STR_LIT:w>" ) as devnull : <EOL> if self . suppress_output : <EOL> self . process = subprocess . Popen ( self . create_command ( ) , <EOL> stderr = devnull , <EOL> stdout = devnull , <EOL> preexec_fn = os . setsid ) <EOL> else : <EOL> self . process = subprocess . Popen ( self . create_command ( ) , <EOL> preexec_fn = os . setsid ) <EOL> _log ( self . logging , '<STR_LIT>' <EOL> . format ( self . process . pid ) ) <EOL> duration = self . check ( check_url ) <EOL> _log ( self . logging , <EOL> '<STR_LIT>' <EOL> . format ( duration , self . process . pid ) ) <EOL> return self . process <EOL> else : <EOL> raise LiveAndLetDieError ( '<STR_LIT>' . format ( host ) ) <EOL> def start ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . live ( * args , ** kwargs ) <EOL> def die ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . process : <EOL> _log ( self . logging , <EOL> '<STR_LIT>' <EOL> . format ( self . __class__ . __name__ , self . process . pid , <EOL> self . check_url ) ) <EOL> self . _kill ( ) <EOL> def stop ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . die ( * args , ** kwargs ) <EOL> @ classmethod <EOL> def _add_args ( cls ) : <EOL> cls . _argument_parser . add_argument ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> type = _validate_host , <EOL> nargs = '<STR_LIT:?>' , <EOL> const = '<STR_LIT>' ) <EOL> @ classmethod <EOL> def parse_args ( cls , logging = False ) : <EOL> """<STR_LIT>""" <EOL> cls . _add_args ( ) <EOL> args = cls . _argument_parser . parse_args ( ) <EOL> if args . liveandletdie : <EOL> _log ( logging , '<STR_LIT>' <EOL> . format ( args . liveandletdie ) ) <EOL> return split_host ( args . liveandletdie ) <EOL> else : <EOL> return None , None <EOL> class WrapperBase ( Base ) : <EOL> """<STR_LIT>""" <EOL> def create_command ( self ) : <EOL> return [ <EOL> self . executable , <EOL> self . path , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' . format ( self . host , self . port ) , <EOL> ] <EOL> class Flask ( WrapperBase ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . ssl = kwargs . pop ( '<STR_LIT>' , None ) <EOL> super ( Flask , self ) . __init__ ( * args , ** kwargs ) <EOL> if self . ssl : <EOL> self . scheme = '<STR_LIT>' <EOL> @ classmethod <EOL> def _add_args ( cls ) : <EOL> super ( Flask , cls ) . _add_args ( ) <EOL> cls . _argument_parser . add_argument ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> type = bool , <EOL> nargs = '<STR_LIT:?>' , <EOL> default = False ) <EOL> def create_command ( self ) : <EOL> command = super ( Flask , self ) . create_command ( ) <EOL> if self . ssl is True : <EOL> command += [ '<STR_LIT>' ] <EOL> return command <EOL> def check ( self , check_url = None ) : <EOL> url = self . check_url if check_url is None else self . _normalize_check_url ( check_url ) <EOL> if self . ssl : <EOL> url = url . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> super ( Flask , self ) . check ( url ) <EOL> @ classmethod <EOL> def wrap ( cls , app ) : <EOL> """<STR_LIT>""" <EOL> host , port = cls . parse_args ( ) <EOL> ssl = cls . _argument_parser . parse_args ( ) . ssl <EOL> ssl_context = None <EOL> if host : <EOL> if ssl : <EOL> try : <EOL> import OpenSSL <EOL> except ImportError : <EOL> sys . path . append ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( sys . version_info . major , sys . version_info . minor ) <EOL> ) <EOL> try : <EOL> import OpenSSL <EOL> except ImportError : <EOL> sys . path . append ( <EOL> '<STR_LIT>' <EOL> . format ( sys . version_info . major , sys . version_info . minor ) <EOL> ) <EOL> try : <EOL> import OpenSSL <EOL> except ImportError : <EOL> raise LiveAndLetDieError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> ssl_context = '<STR_LIT>' <EOL> app . run ( host = host , port = port , ssl_context = ssl_context ) <EOL> sys . exit ( ) <EOL> class GAE ( Base ) : <EOL> def __init__ ( self , dev_appserver_path , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( GAE , self ) . __init__ ( * args , ** kwargs ) <EOL> self . dev_appserver_path = dev_appserver_path <EOL> self . admin_port = kwargs . get ( '<STR_LIT>' , <NUM_LIT> ) <EOL> def create_command ( self ) : <EOL> command = [ <EOL> self . dev_appserver_path , <EOL> '<STR_LIT>' . format ( self . host ) , <EOL> '<STR_LIT>' . format ( self . port ) , <EOL> '<STR_LIT>' . format ( self . admin_port ) , <EOL> '<STR_LIT>' , <EOL> self . path <EOL> ] <EOL> if self . dev_appserver_path . endswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> command = [ self . executable ] + command <EOL> return command <EOL> class WsgirefSimpleServer ( WrapperBase ) : <EOL> @ classmethod <EOL> def wrap ( cls , app ) : <EOL> host , port = cls . parse_args ( ) <EOL> if host : <EOL> from wsgiref . simple_server import make_server <EOL> s = make_server ( host , port , app ) <EOL> s . serve_forever ( ) <EOL> s . server_close ( ) <EOL> sys . exit ( ) <EOL> class Django ( Base ) : <EOL> def create_command ( self ) : <EOL> return [ <EOL> self . executable , <EOL> os . path . join ( self . path , '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' . format ( self . host , self . port ) , <EOL> ] </s>
