<s> import os <EOL> import sys <EOL> import time <EOL> import errno <EOL> import signal <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> from . base import Cli <EOL> from iktomi . utils . system import safe_makedirs , is_running , terminate , doublefork <EOL> def flup_fastcgi ( wsgi_app , bind , cwd = None , pidfile = None , logfile = None , <EOL> daemonize = False , umask = None , ** params ) : <EOL> if params . pop ( '<STR_LIT>' , False ) : <EOL> from flup . server import fcgi_fork as fcgi <EOL> else : <EOL> from flup . server import fcgi <EOL> if daemonize : <EOL> if os . path . isfile ( pidfile ) : <EOL> with open ( pidfile , '<STR_LIT:r>' ) as f : <EOL> try : <EOL> pid = int ( f . read ( ) ) <EOL> except ValueError : <EOL> pid = None <EOL> if pid is not None and is_running ( pid ) : <EOL> sys . exit ( '<STR_LIT>' . format ( pid ) ) <EOL> elif pid is not None : <EOL> logger . info ( '<STR_LIT>' , <EOL> pid ) <EOL> else : <EOL> logger . info ( '<STR_LIT>' ) <EOL> doublefork ( pidfile , logfile , cwd , umask ) <EOL> logger . info ( '<STR_LIT>' , cwd ) <EOL> fcgi . WSGIServer ( wsgi_app , bindAddress = bind , umask = umask , <EOL> debug = False , ** params ) . run ( ) <EOL> class Flup ( Cli ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , app , bind = '<STR_LIT>' , logfile = None , pidfile = None , <EOL> cwd = '<STR_LIT:.>' , umask = <NUM_LIT> <NUM_LIT:2> , fastcgi_params = None ) : <EOL> self . app = app <EOL> self . cwd = os . path . abspath ( cwd ) <EOL> if '<STR_LIT::>' in bind : <EOL> host , port = bind . split ( '<STR_LIT::>' ) <EOL> port = int ( port ) <EOL> else : <EOL> bind = os . path . abspath ( bind or os . path . join ( self . cwd , '<STR_LIT>' ) ) <EOL> safe_makedirs ( bind ) <EOL> self . bind = bind <EOL> self . umask = umask <EOL> self . logfile = logfile or os . path . join ( self . cwd , '<STR_LIT>' ) <EOL> self . pidfile = pidfile or os . path . join ( self . cwd , '<STR_LIT>' ) <EOL> self . fastcgi_params = fastcgi_params or { } <EOL> def command_start ( self , daemonize = False ) : <EOL> '''<STR_LIT>''' <EOL> if daemonize : <EOL> safe_makedirs ( self . logfile , self . pidfile ) <EOL> flup_fastcgi ( self . app , bind = self . bind , pidfile = self . pidfile , <EOL> logfile = self . logfile , daemonize = daemonize , <EOL> cwd = self . cwd , umask = self . umask , ** self . fastcgi_params ) <EOL> def command_stop ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . pidfile : <EOL> if not os . path . exists ( self . pidfile ) : <EOL> sys . exit ( "<STR_LIT>" . format ( self . pidfile ) ) <EOL> with open ( self . pidfile ) as pidfile : <EOL> pid = int ( pidfile . read ( ) ) <EOL> for sig in [ signal . SIGINT , signal . SIGTERM , signal . SIGKILL ] : <EOL> try : <EOL> if terminate ( pid , sig , <NUM_LIT:3> ) : <EOL> os . remove ( self . pidfile ) <EOL> sys . exit ( ) <EOL> except OSError as exc : <EOL> if exc . errno != errno . ESRCH : <EOL> raise <EOL> elif sig == signal . SIGINT : <EOL> sys . exit ( '<STR_LIT>' ) <EOL> sys . exit ( '<STR_LIT>' ) <EOL> def command_restart ( self ) : <EOL> self . command_stop ( ) <EOL> self . command_start ( ) </s>
