<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import datetime <EOL> import logging <EOL> import logging . handlers <EOL> import os <EOL> rootlogger = logging . getLogger ( ) <EOL> server_logger = logging . getLogger ( "<STR_LIT>" ) <EOL> def check_pid ( pidfile ) : <EOL> if pidfile and os . path . exists ( pidfile ) : <EOL> try : <EOL> pid = int ( open ( pidfile ) . read ( ) . strip ( ) ) <EOL> os . kill ( pid , <NUM_LIT:0> ) <EOL> return pid <EOL> except BaseException : <EOL> return <NUM_LIT:0> <EOL> return <NUM_LIT:0> <EOL> def write_pid ( pidfile ) : <EOL> server_logger . info ( "<STR_LIT>" ) <EOL> piddir = os . path . dirname ( pidfile ) <EOL> if piddir != '<STR_LIT>' : <EOL> try : <EOL> os . makedirs ( piddir ) <EOL> except OSError : <EOL> pass <EOL> with open ( pidfile , '<STR_LIT:w>' ) as fobj : <EOL> fobj . write ( str ( os . getpid ( ) ) ) <EOL> def get_log_format ( ) : <EOL> return "<STR_LIT>" <EOL> def get_spool_handler ( filename ) : <EOL> handler = logging . handlers . TimedRotatingFileHandler ( <EOL> filename = filename , <EOL> when = '<STR_LIT:d>' , <EOL> encoding = '<STR_LIT:utf8>' , <EOL> backupCount = <NUM_LIT:7> <EOL> ) <EOL> formatter = logging . Formatter ( get_log_format ( ) ) <EOL> handler . setFormatter ( formatter ) <EOL> return handler <EOL> def _server_already_running ( pidfile ) : <EOL> existing_pid = check_pid ( pidfile ) <EOL> if pidfile and existing_pid : <EOL> return True <EOL> return False <EOL> def daemonize ( cmd , pidfile = None , logdir = None , api_port = <NUM_LIT> , address = None , unix_socket = None ) : <EOL> import daemon <EOL> logdir = logdir or "<STR_LIT>" <EOL> if not os . path . exists ( logdir ) : <EOL> os . makedirs ( logdir ) <EOL> log_path = os . path . join ( logdir , "<STR_LIT>" ) <EOL> today = datetime . date . today ( ) <EOL> stdout_path = os . path . join ( <EOL> logdir , <EOL> "<STR_LIT>" . format ( today ) <EOL> ) <EOL> stderr_path = os . path . join ( <EOL> logdir , <EOL> "<STR_LIT>" . format ( today ) <EOL> ) <EOL> stdout_proxy = open ( stdout_path , '<STR_LIT>' ) <EOL> stderr_proxy = open ( stderr_path , '<STR_LIT>' ) <EOL> try : <EOL> ctx = daemon . DaemonContext ( <EOL> stdout = stdout_proxy , <EOL> stderr = stderr_proxy , <EOL> working_directory = '<STR_LIT:.>' , <EOL> initgroups = False , <EOL> ) <EOL> except TypeError : <EOL> ctx = daemon . DaemonContext ( <EOL> stdout = stdout_proxy , <EOL> stderr = stderr_proxy , <EOL> working_directory = '<STR_LIT:.>' , <EOL> ) <EOL> with ctx : <EOL> loghandler = get_spool_handler ( log_path ) <EOL> rootlogger . addHandler ( loghandler ) <EOL> if pidfile : <EOL> server_logger . info ( "<STR_LIT>" ) <EOL> existing_pid = check_pid ( pidfile ) <EOL> if pidfile and existing_pid : <EOL> server_logger . info ( "<STR_LIT>" , existing_pid ) <EOL> return <EOL> write_pid ( pidfile ) <EOL> cmd ( api_port = api_port , address = address , unix_socket = unix_socket ) </s>
