<s> import sys , os , pdb , getpass , traceback , signal , warnings <EOL> from twisted . python import runtime , log , usage , failure , util , logfile <EOL> from twisted . python . versions import Version <EOL> from twisted . python . reflect import qual <EOL> from twisted . python . deprecate import deprecated <EOL> from twisted . python . log import ILogObserver <EOL> from twisted . persisted import sob <EOL> from twisted . application import service , reactors <EOL> from twisted . internet import defer <EOL> from twisted import copyright <EOL> from twisted . application . reactors import installReactor <EOL> from twisted . application . reactors import NoSuchReactor <EOL> class _BasicProfiler ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , profileOutput , saveStats ) : <EOL> self . profileOutput = profileOutput <EOL> self . saveStats = saveStats <EOL> def _reportImportError ( self , module , e ) : <EOL> """<STR_LIT>""" <EOL> s = "<STR_LIT>" % ( module , e ) <EOL> s += """<STR_LIT>""" <EOL> raise SystemExit ( s ) <EOL> class ProfileRunner ( _BasicProfiler ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , reactor ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import profile <EOL> except ImportError , e : <EOL> self . _reportImportError ( "<STR_LIT>" , e ) <EOL> p = profile . Profile ( ) <EOL> p . runcall ( reactor . run ) <EOL> if self . saveStats : <EOL> p . dump_stats ( self . profileOutput ) <EOL> else : <EOL> tmp , sys . stdout = sys . stdout , open ( self . profileOutput , '<STR_LIT:a>' ) <EOL> try : <EOL> p . print_stats ( ) <EOL> finally : <EOL> sys . stdout , tmp = tmp , sys . stdout <EOL> tmp . close ( ) <EOL> class HotshotRunner ( _BasicProfiler ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , reactor ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import hotshot . stats <EOL> except ( ImportError , SystemExit ) , e : <EOL> self . _reportImportError ( "<STR_LIT>" , e ) <EOL> p = hotshot . Profile ( self . profileOutput ) <EOL> p . runcall ( reactor . run ) <EOL> if self . saveStats : <EOL> return <EOL> else : <EOL> s = hotshot . stats . load ( self . profileOutput ) <EOL> s . strip_dirs ( ) <EOL> s . sort_stats ( - <NUM_LIT:1> ) <EOL> if getattr ( s , '<STR_LIT>' , None ) is not None : <EOL> s . stream = open ( self . profileOutput , '<STR_LIT:w>' ) <EOL> s . print_stats ( ) <EOL> s . stream . close ( ) <EOL> else : <EOL> tmp , sys . stdout = sys . stdout , open ( self . profileOutput , '<STR_LIT:w>' ) <EOL> try : <EOL> s . print_stats ( ) <EOL> finally : <EOL> sys . stdout , tmp = tmp , sys . stdout <EOL> tmp . close ( ) <EOL> class CProfileRunner ( _BasicProfiler ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , reactor ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import cProfile , pstats <EOL> except ImportError , e : <EOL> self . _reportImportError ( "<STR_LIT>" , e ) <EOL> p = cProfile . Profile ( ) <EOL> p . runcall ( reactor . run ) <EOL> if self . saveStats : <EOL> p . dump_stats ( self . profileOutput ) <EOL> else : <EOL> stream = open ( self . profileOutput , '<STR_LIT:w>' ) <EOL> s = pstats . Stats ( p , stream = stream ) <EOL> s . strip_dirs ( ) <EOL> s . sort_stats ( - <NUM_LIT:1> ) <EOL> s . print_stats ( ) <EOL> stream . close ( ) <EOL> class AppProfiler ( object ) : <EOL> """<STR_LIT>""" <EOL> profilers = { "<STR_LIT>" : ProfileRunner , "<STR_LIT>" : HotshotRunner , <EOL> "<STR_LIT>" : CProfileRunner } <EOL> def __init__ ( self , options ) : <EOL> saveStats = options . get ( "<STR_LIT>" , False ) <EOL> profileOutput = options . get ( "<STR_LIT>" , None ) <EOL> self . profiler = options . get ( "<STR_LIT>" , "<STR_LIT>" ) . lower ( ) <EOL> if self . profiler in self . profilers : <EOL> profiler = self . profilers [ self . profiler ] ( profileOutput , saveStats ) <EOL> self . run = profiler . run <EOL> else : <EOL> raise SystemExit ( "<STR_LIT>" % ( self . profiler , ) ) <EOL> class AppLogger ( object ) : <EOL> """<STR_LIT>""" <EOL> _observer = None <EOL> def __init__ ( self , options ) : <EOL> self . _logfilename = options . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def start ( self , application ) : <EOL> """<STR_LIT>""" <EOL> observer = application . getComponent ( ILogObserver , None ) <EOL> if observer is None : <EOL> observer = self . _getLogObserver ( ) <EOL> self . _observer = observer <EOL> log . startLoggingWithObserver ( self . _observer ) <EOL> self . _initialLog ( ) <EOL> def _initialLog ( self ) : <EOL> """<STR_LIT>""" <EOL> from twisted . internet import reactor <EOL> log . msg ( "<STR_LIT>" % ( copyright . version , <EOL> sys . executable , <EOL> runtime . shortPythonVersion ( ) ) ) <EOL> log . msg ( '<STR_LIT>' % ( qual ( reactor . __class__ ) , ) ) <EOL> def _getLogObserver ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _logfilename == '<STR_LIT:->' or not self . _logfilename : <EOL> logFile = sys . stdout <EOL> else : <EOL> logFile = logfile . LogFile . fromFullPath ( self . _logfilename ) <EOL> return log . FileLogObserver ( logFile ) . emit <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> log . msg ( "<STR_LIT>" ) <EOL> if self . _observer is not None : <EOL> log . removeObserver ( self . _observer ) <EOL> self . _observer = None <EOL> def fixPdb ( ) : <EOL> def do_stop ( self , arg ) : <EOL> self . clear_all_breaks ( ) <EOL> self . set_continue ( ) <EOL> from twisted . internet import reactor <EOL> reactor . callLater ( <NUM_LIT:0> , reactor . stop ) <EOL> return <NUM_LIT:1> <EOL> def help_stop ( self ) : <EOL> print """<STR_LIT>""" <EOL> def set_quit ( self ) : <EOL> os . _exit ( <NUM_LIT:0> ) <EOL> pdb . Pdb . set_quit = set_quit <EOL> pdb . Pdb . do_stop = do_stop <EOL> pdb . Pdb . help_stop = help_stop <EOL> def runReactorWithLogging ( config , oldstdout , oldstderr , profiler = None , reactor = None ) : <EOL> """<STR_LIT>""" <EOL> if reactor is None : <EOL> from twisted . internet import reactor <EOL> try : <EOL> if config [ '<STR_LIT>' ] : <EOL> if profiler is not None : <EOL> profiler . run ( reactor ) <EOL> elif config [ '<STR_LIT>' ] : <EOL> sys . stdout = oldstdout <EOL> sys . stderr = oldstderr <EOL> if runtime . platformType == '<STR_LIT>' : <EOL> signal . signal ( signal . SIGUSR2 , lambda * args : pdb . set_trace ( ) ) <EOL> signal . signal ( signal . SIGINT , lambda * args : pdb . set_trace ( ) ) <EOL> fixPdb ( ) <EOL> pdb . runcall ( reactor . run ) <EOL> else : <EOL> reactor . run ( ) <EOL> except : <EOL> if config [ '<STR_LIT>' ] : <EOL> file = oldstdout <EOL> else : <EOL> file = open ( "<STR_LIT>" , '<STR_LIT:a>' ) <EOL> traceback . print_exc ( file = file ) <EOL> file . flush ( ) <EOL> def getPassphrase ( needed ) : <EOL> if needed : <EOL> return getpass . getpass ( '<STR_LIT>' ) <EOL> else : <EOL> return None <EOL> def getSavePassphrase ( needed ) : <EOL> if needed : <EOL> passphrase = util . getPassword ( "<STR_LIT>" ) <EOL> else : <EOL> return None <EOL> class ApplicationRunner ( object ) : <EOL> """<STR_LIT>""" <EOL> profilerFactory = AppProfiler <EOL> loggerFactory = AppLogger <EOL> def __init__ ( self , config ) : <EOL> self . config = config <EOL> self . profiler = self . profilerFactory ( config ) <EOL> self . logger = self . loggerFactory ( config ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> self . preApplication ( ) <EOL> self . application = self . createOrGetApplication ( ) <EOL> self . logger . start ( self . application ) <EOL> self . postApplication ( ) <EOL> self . logger . stop ( ) <EOL> def startReactor ( self , reactor , oldstdout , oldstderr ) : <EOL> """<STR_LIT>""" <EOL> runReactorWithLogging ( <EOL> self . config , oldstdout , oldstderr , self . profiler , reactor ) <EOL> def preApplication ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def postApplication ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def createOrGetApplication ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . config . subCommand : <EOL> plg = self . config . loadedPlugins [ self . config . subCommand ] <EOL> ser = plg . makeService ( self . config . subOptions ) <EOL> application = service . Application ( plg . tapname ) <EOL> ser . setServiceParent ( application ) <EOL> else : <EOL> passphrase = getPassphrase ( self . config [ '<STR_LIT>' ] ) <EOL> application = getApplication ( self . config , passphrase ) <EOL> return application <EOL> def getApplication ( config , passphrase ) : <EOL> s = [ ( config [ t ] , t ) <EOL> for t in [ '<STR_LIT>' , '<STR_LIT:source>' , '<STR_LIT:file>' ] if config [ t ] ] [ <NUM_LIT:0> ] <EOL> filename , style = s [ <NUM_LIT:0> ] , { '<STR_LIT:file>' : '<STR_LIT>' } . get ( s [ <NUM_LIT:1> ] , s [ <NUM_LIT:1> ] ) <EOL> try : <EOL> log . msg ( "<STR_LIT>" % filename ) <EOL> application = service . loadApplication ( filename , style , passphrase ) <EOL> log . msg ( "<STR_LIT>" ) <EOL> except Exception , e : <EOL> s = "<STR_LIT>" % e <EOL> if isinstance ( e , KeyError ) and e . args [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> s += """<STR_LIT>""" <EOL> traceback . print_exc ( file = log . logfile ) <EOL> log . msg ( s ) <EOL> log . deferr ( ) <EOL> sys . exit ( '<STR_LIT:\n>' + s + '<STR_LIT:\n>' ) <EOL> return application <EOL> def _reactorZshAction ( ) : <EOL> return "<STR_LIT>" % "<STR_LIT:U+0020>" . join ( [ r . shortName for r in reactors . getReactorTypes ( ) ] ) <EOL> class ReactorSelectionMixin : <EOL> """<STR_LIT>""" <EOL> zsh_actions = { "<STR_LIT>" : _reactorZshAction } <EOL> messageOutput = sys . stdout <EOL> def opt_help_reactors ( self ) : <EOL> """<STR_LIT>""" <EOL> for r in reactors . getReactorTypes ( ) : <EOL> self . messageOutput . write ( '<STR_LIT>' % <EOL> ( r . shortName , r . description ) ) <EOL> raise SystemExit ( <NUM_LIT:0> ) <EOL> def opt_reactor ( self , shortName ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> installReactor ( shortName ) <EOL> except NoSuchReactor : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( shortName , ) ) <EOL> raise usage . UsageError ( msg ) <EOL> except Exception , e : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( e , ) ) <EOL> raise usage . UsageError ( msg ) <EOL> else : <EOL> self [ "<STR_LIT>" ] = shortName <EOL> opt_r = opt_reactor <EOL> class ServerOptions ( usage . Options , ReactorSelectionMixin ) : <EOL> longdesc = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> optFlags = [ [ '<STR_LIT>' , None , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:o>' , "<STR_LIT>" ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:e>' , <EOL> "<STR_LIT>" ] ] <EOL> optParameters = [ [ '<STR_LIT>' , '<STR_LIT:l>' , None , <EOL> "<STR_LIT>" ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:p>' , None , <EOL> "<STR_LIT>" ] , <EOL> [ '<STR_LIT>' , None , "<STR_LIT>" , <EOL> "<STR_LIT>" % <EOL> "<STR_LIT:U+002CU+0020>" . join ( AppProfiler . profilers ) ] , <EOL> [ '<STR_LIT:file>' , '<STR_LIT:f>' , '<STR_LIT>' , <EOL> "<STR_LIT>" ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:y>' , None , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ] , <EOL> [ '<STR_LIT:source>' , '<STR_LIT:s>' , None , <EOL> "<STR_LIT>" ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:d>' , '<STR_LIT:.>' , <EOL> '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , None , None , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ] ] <EOL> zsh_mutuallyExclusive = [ ( "<STR_LIT:file>" , "<STR_LIT>" , "<STR_LIT:source>" ) ] <EOL> zsh_actions = { "<STR_LIT:file>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT:source>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : "<STR_LIT>" } <EOL> def __init__ ( self , * a , ** kw ) : <EOL> self [ '<STR_LIT>' ] = False <EOL> usage . Options . __init__ ( self , * a , ** kw ) <EOL> def opt_debug ( self ) : <EOL> """<STR_LIT>""" <EOL> defer . setDebugging ( True ) <EOL> failure . startDebugMode ( ) <EOL> self [ '<STR_LIT>' ] = True <EOL> opt_b = opt_debug <EOL> def opt_spew ( self ) : <EOL> """<STR_LIT>""" <EOL> sys . settrace ( util . spewer ) <EOL> try : <EOL> import threading <EOL> except ImportError : <EOL> return <EOL> threading . settrace ( util . spewer ) <EOL> def opt_report_profile ( self , value ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" , category = DeprecationWarning ) <EOL> def parseOptions ( self , options = None ) : <EOL> if options is None : <EOL> options = sys . argv [ <NUM_LIT:1> : ] or [ "<STR_LIT>" ] <EOL> usage . Options . parseOptions ( self , options ) <EOL> def postOptions ( self ) : <EOL> if self . subCommand or self [ '<STR_LIT>' ] : <EOL> self [ '<STR_LIT>' ] = True <EOL> def subCommands ( self ) : <EOL> from twisted import plugin <EOL> plugins = plugin . getPlugins ( service . IServiceMaker ) <EOL> self . loadedPlugins = { } <EOL> for plug in plugins : <EOL> self . loadedPlugins [ plug . tapname ] = plug <EOL> yield ( plug . tapname , None , lambda : plug . options ( ) , plug . description ) <EOL> subCommands = property ( subCommands ) <EOL> def run ( runApp , ServerOptions ) : <EOL> config = ServerOptions ( ) <EOL> try : <EOL> config . parseOptions ( ) <EOL> except usage . error , ue : <EOL> print config <EOL> print "<STR_LIT>" % ( sys . argv [ <NUM_LIT:0> ] , ue ) <EOL> else : <EOL> runApp ( config ) <EOL> def initialLog ( ) : <EOL> AppLogger ( { } ) . _initialLog ( ) <EOL> initialLog = deprecated ( Version ( "<STR_LIT>" , <NUM_LIT:8> , <NUM_LIT:2> , <NUM_LIT:0> ) ) ( initialLog ) <EOL> def convertStyle ( filein , typein , passphrase , fileout , typeout , encrypt ) : <EOL> application = service . loadApplication ( filein , typein , passphrase ) <EOL> sob . IPersistable ( application ) . setStyle ( typeout ) <EOL> passphrase = getSavePassphrase ( encrypt ) <EOL> if passphrase : <EOL> fileout = None <EOL> sob . IPersistable ( application ) . save ( filename = fileout , passphrase = passphrase ) <EOL> def startApplication ( application , save ) : <EOL> from twisted . internet import reactor <EOL> service . IService ( application ) . startService ( ) <EOL> if save : <EOL> p = sob . IPersistable ( application ) <EOL> reactor . addSystemEventTrigger ( '<STR_LIT>' , '<STR_LIT>' , p . save , '<STR_LIT>' ) <EOL> reactor . addSystemEventTrigger ( '<STR_LIT>' , '<STR_LIT>' , <EOL> service . IService ( application ) . stopService ) </s>
