<s> from __future__ import print_function <EOL> import geventreactor <EOL> geventreactor . install ( ) <EOL> import sys <EOL> import traceback <EOL> from twisted . application . service import IServiceMaker , MultiService <EOL> from twisted . plugin import IPlugin <EOL> from twisted . python import usage <EOL> from zope . interface import implements <EOL> from spinoff . actor . _actor import _validate_nodeid <EOL> from spinoff . actor . runner import ActorRunner <EOL> from spinoff . util . logging import fatal , log <EOL> class _EMPTY ( object ) : <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> def __nonzero__ ( self ) : <EOL> return False <EOL> _EMPTY = _EMPTY ( ) <EOL> class Options ( usage . Options ) : <EOL> optParameters = [ <EOL> [ '<STR_LIT>' , '<STR_LIT:a>' , None , "<STR_LIT>" ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:i>' , _EMPTY , ( u"<STR_LIT>" <EOL> u"<STR_LIT>" <EOL> u"<STR_LIT>" ) ] , <EOL> [ '<STR_LIT:message>' , '<STR_LIT:m>' , _EMPTY , "<STR_LIT>" ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:r>' , None , "<STR_LIT>" ] , <EOL> [ '<STR_LIT:name>' , '<STR_LIT:n>' , None , "<STR_LIT>" ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:p>' , <NUM_LIT> , "<STR_LIT>" ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:u>' , '<STR_LIT>' , "<STR_LIT>" ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:P>' , '<STR_LIT>' , "<STR_LIT>" ] , <EOL> ] <EOL> optFlags = [ <EOL> [ '<STR_LIT>' , '<STR_LIT:k>' , "<STR_LIT>" ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:d>' , "<STR_LIT>" ] , <EOL> ] <EOL> class ActorRunnerMaker ( object ) : <EOL> implements ( IServiceMaker , IPlugin ) <EOL> tapname = '<STR_LIT>' <EOL> description = '<STR_LIT>' <EOL> options = Options <EOL> def makeService ( self , options ) : <EOL> actor = options [ '<STR_LIT>' ] <EOL> if not actor : <EOL> fatal ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> try : <EOL> module_path , actor_cls_name = actor . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> fatal ( "<STR_LIT>" % actor ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> try : <EOL> mod = __import__ ( module_path , globals ( ) , locals ( ) , [ actor_cls_name ] , - <NUM_LIT:1> ) <EOL> except ImportError : <EOL> fatal ( "<STR_LIT>" % ( actor , traceback . format_exc ( ) ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> try : <EOL> actor_cls = getattr ( mod , actor_cls_name ) <EOL> except AttributeError : <EOL> fatal ( "<STR_LIT>" % actor ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> kwargs = { } <EOL> if options [ '<STR_LIT>' ] is not _EMPTY : <EOL> params = '<STR_LIT>' % ( options [ '<STR_LIT>' ] , ) <EOL> try : <EOL> params = eval ( params ) <EOL> except : <EOL> fatal ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] = params <EOL> if options [ '<STR_LIT:message>' ] is not _EMPTY : <EOL> initial_message = options [ '<STR_LIT:message>' ] <EOL> try : <EOL> initial_message = eval ( initial_message ) <EOL> except : <EOL> fatal ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] = initial_message <EOL> if options [ '<STR_LIT:name>' ] : <EOL> name = options [ '<STR_LIT:name>' ] <EOL> if '<STR_LIT:/>' in name : <EOL> fatal ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> kwargs [ '<STR_LIT:name>' ] = name <EOL> if options [ '<STR_LIT>' ] : <EOL> nodeid = options [ '<STR_LIT>' ] <EOL> try : <EOL> _validate_nodeid ( nodeid ) <EOL> except TypeError : <EOL> fatal ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] = nodeid <EOL> kwargs [ '<STR_LIT>' ] = options [ '<STR_LIT>' ] <EOL> m = MultiService ( ) <EOL> actor_runner = ActorRunner ( actor_cls , ** kwargs ) <EOL> actor_runner . setServiceParent ( m ) <EOL> if options [ '<STR_LIT>' ] : <EOL> port = options [ '<STR_LIT>' ] <EOL> try : <EOL> port = int ( port ) <EOL> except ValueError : <EOL> fatal ( "<STR_LIT>" % port ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> username = options [ '<STR_LIT>' ] <EOL> password = options [ '<STR_LIT>' ] <EOL> manhole = self . make_manhole_server ( port , username , password ) <EOL> manhole . setServiceParent ( m ) <EOL> return m <EOL> def make_manhole_server ( self , port , username , password ) : <EOL> from twisted . application . internet import TCPServer <EOL> from twisted . cred . portal import Portal <EOL> from twisted . cred . checkers import InMemoryUsernamePasswordDatabaseDontUse <EOL> from twisted . conch . manhole import ColoredManhole <EOL> from twisted . conch . manhole_ssh import ConchFactory , TerminalRealm <EOL> from twisted . conch . insults . insults import ServerProtocol <EOL> rlm = TerminalRealm ( ) <EOL> rlm . chainedProtocolFactory = lambda : ServerProtocol ( ColoredManhole , None ) <EOL> auth_checker = InMemoryUsernamePasswordDatabaseDontUse ( ** { username : password } ) <EOL> return TCPServer ( port , ConchFactory ( Portal ( rlm , [ auth_checker ] ) ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> serviceMaker = ActorRunnerMaker ( ) </s>
