<s> import os <EOL> import sys <EOL> import signal <EOL> import syslog <EOL> from lockfile import FileLock , LockTimeout , AlreadyLocked <EOL> from twisted . python import usage <EOL> from twisted . cred import portal <EOL> from twisted . cred . checkers import AllowAnonymousAccess , InMemoryUsernamePasswordDatabaseDontUse <EOL> from twisted . spread import pb <EOL> from twisted . internet import reactor , defer <EOL> from jasmin . interceptor . interceptor import InterceptorPB <EOL> from jasmin . interceptor . configs import InterceptorPBConfig <EOL> from jasmin . tools . cred . portal import JasminPBRealm <EOL> from jasmin . tools . spread . pb import JasminPBPortalRoot <EOL> ROOT_PATH = os . getenv ( '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> class Options ( usage . Options ) : <EOL> optParameters = [ <EOL> [ '<STR_LIT>' , '<STR_LIT:c>' , '<STR_LIT>' % ROOT_PATH , <EOL> '<STR_LIT>' ] , <EOL> ] <EOL> class InterceptorDaemon ( object ) : <EOL> def __init__ ( self , opt ) : <EOL> self . options = opt <EOL> self . components = { } <EOL> def startInterceptorPBService ( self ) : <EOL> "<STR_LIT>" <EOL> InterceptorPBConfigInstance = InterceptorPBConfig ( self . options [ '<STR_LIT>' ] ) <EOL> self . components [ '<STR_LIT>' ] = InterceptorPB ( ) <EOL> self . components [ '<STR_LIT>' ] . setConfig ( InterceptorPBConfigInstance ) <EOL> p = portal . Portal ( JasminPBRealm ( self . components [ '<STR_LIT>' ] ) ) <EOL> if InterceptorPBConfigInstance . authentication : <EOL> c = InMemoryUsernamePasswordDatabaseDontUse ( ) <EOL> c . addUser ( InterceptorPBConfigInstance . admin_username , <EOL> InterceptorPBConfigInstance . admin_password ) <EOL> p . registerChecker ( c ) <EOL> else : <EOL> p . registerChecker ( AllowAnonymousAccess ( ) ) <EOL> jPBPortalRoot = JasminPBPortalRoot ( p ) <EOL> self . components [ '<STR_LIT>' ] = reactor . listenTCP ( <EOL> InterceptorPBConfigInstance . port , <EOL> pb . PBServerFactory ( jPBPortalRoot ) , <EOL> interface = InterceptorPBConfigInstance . bind ) <EOL> def stopInterceptorPBService ( self ) : <EOL> "<STR_LIT>" <EOL> return self . components [ '<STR_LIT>' ] . stopListening ( ) <EOL> @ defer . inlineCallbacks <EOL> def start ( self ) : <EOL> "<STR_LIT>" <EOL> syslog . syslog ( syslog . LOG_INFO , "<STR_LIT>" ) <EOL> try : <EOL> yield self . startInterceptorPBService ( ) <EOL> except Exception , e : <EOL> syslog . syslog ( syslog . LOG_ERR , "<STR_LIT>" % e ) <EOL> else : <EOL> syslog . syslog ( syslog . LOG_INFO , "<STR_LIT>" ) <EOL> @ defer . inlineCallbacks <EOL> def stop ( self ) : <EOL> "<STR_LIT>" <EOL> syslog . syslog ( syslog . LOG_INFO , "<STR_LIT>" ) <EOL> if '<STR_LIT>' in self . components : <EOL> yield self . stopInterceptorPBService ( ) <EOL> syslog . syslog ( syslog . LOG_INFO , "<STR_LIT>" ) <EOL> reactor . stop ( ) <EOL> def sighandler_stop ( self , signum , frame ) : <EOL> "<STR_LIT>" <EOL> syslog . syslog ( syslog . LOG_INFO , "<STR_LIT>" ) <EOL> return self . stop ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> lock = FileLock ( "<STR_LIT>" ) <EOL> try : <EOL> options = Options ( ) <EOL> options . parseOptions ( ) <EOL> lock . acquire ( timeout = <NUM_LIT:2> ) <EOL> in_d = InterceptorDaemon ( options ) <EOL> signal . signal ( signal . SIGINT , in_d . sighandler_stop ) <EOL> in_d . start ( ) <EOL> reactor . run ( ) <EOL> except usage . UsageError , errortext : <EOL> print '<STR_LIT>' % ( sys . argv [ <NUM_LIT:0> ] , errortext ) <EOL> print '<STR_LIT>' % ( sys . argv [ <NUM_LIT:0> ] ) <EOL> except LockTimeout : <EOL> print "<STR_LIT>" <EOL> except AlreadyLocked : <EOL> print "<STR_LIT>" <EOL> finally : <EOL> if lock . i_am_locking ( ) : <EOL> lock . release ( ) </s>
