<s> import json <EOL> import os <EOL> import sys <EOL> import argparse <EOL> import twisted <EOL> from twisted . python import util <EOL> from twisted . scripts import twistd <EOL> import piped <EOL> def run_trial ( ) : <EOL> from twisted . python import modules <EOL> PythonModule = modules . PythonModule <EOL> class PatchedPythonModule ( PythonModule ) : <EOL> def walkModules ( self , * a , ** kw ) : <EOL> yielded = set ( ) <EOL> for module in super ( PythonModule , self ) . walkModules ( * a , ** kw ) : <EOL> if module . name in yielded : <EOL> continue <EOL> yielded . add ( module . name ) <EOL> yield module <EOL> modules . PythonModule = PatchedPythonModule <EOL> from twisted . scripts . trial import run <EOL> run ( ) <EOL> def run_piped ( ) : <EOL> """<STR_LIT>""" <EOL> parser = _make_parser ( ) <EOL> args , remaining = parser . parse_known_args ( ) <EOL> if args . script : <EOL> args . conf = args . script <EOL> sys . argv = [ sys . argv [ <NUM_LIT:0> ] ] + remaining <EOL> else : <EOL> args = parser . parse_args ( ) <EOL> if args . conf : <EOL> os . environ [ '<STR_LIT>' ] = args . conf <EOL> overrides = json . loads ( os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if args . repl : <EOL> overrides . append ( '<STR_LIT>' ) <EOL> os . environ [ '<STR_LIT>' ] = json . dumps ( overrides + args . override ) <EOL> twistd_config = _create_configuration_for_twistd ( args ) <EOL> from piped import log <EOL> log . configure ( args ) <EOL> _run_twistd_with_config ( twistd_config ) <EOL> class VersionAction ( argparse . Action ) : <EOL> """<STR_LIT>""" <EOL> def _find_plugin_versions ( self ) : <EOL> from twisted . python import modules <EOL> versions = dict ( ) <EOL> plugin_module = modules . getModule ( '<STR_LIT>' ) <EOL> plugin_module . load ( ) <EOL> for module in plugin_module . iterModules ( ) : <EOL> versions [ module . name ] = getattr ( module . load ( ) , '<STR_LIT:version>' , '<STR_LIT>' ) <EOL> return versions <EOL> def __call__ ( self , parser , namespace , values , option_string = None ) : <EOL> message = list ( ) <EOL> message . append ( '<STR_LIT>' % ( piped . version , twisted . version ) ) <EOL> message . append ( '<STR_LIT>' ) <EOL> message . append ( '<STR_LIT>' ) <EOL> for name , version in sorted ( self . _find_plugin_versions ( ) . items ( ) ) : <EOL> message . append ( '<STR_LIT>' % ( name , version ) ) <EOL> message . append ( '<STR_LIT>' ) <EOL> message . append ( '<STR_LIT>' % sys . version . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) ) <EOL> message . append ( '<STR_LIT>' ) <EOL> parser . exit ( message = os . linesep . join ( message ) ) <EOL> def _make_parser ( ) : <EOL> """<STR_LIT>""" <EOL> parser = argparse . ArgumentParser ( description = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = VersionAction , help = '<STR_LIT>' , nargs = <NUM_LIT:0> ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , default = '<STR_LIT:.>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT>' , metavar = '<STR_LIT>' , default = list ( ) , <EOL> help = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> group = parser . add_mutually_exclusive_group ( required = True ) <EOL> group . add_argument ( '<STR_LIT:-c>' , '<STR_LIT>' , help = '<STR_LIT>' , metavar = '<STR_LIT>' ) <EOL> group . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> help = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , metavar = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) <EOL> return parser <EOL> def _create_configuration_for_twistd ( args ) : <EOL> """<STR_LIT>""" <EOL> config = dict ( args . __dict__ ) <EOL> config [ '<STR_LIT>' ] = util . sibpath ( __file__ , '<STR_LIT>' ) <EOL> config [ '<STR_LIT>' ] = True <EOL> conf_without_extension = os . path . basename ( config [ '<STR_LIT>' ] ) . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> default_pidfile = '<STR_LIT>' % conf_without_extension <EOL> if not config . get ( '<STR_LIT>' ) and not config . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> config [ '<STR_LIT>' ] = default_pidfile <EOL> config [ '<STR_LIT>' ] = config [ '<STR_LIT>' ] . replace ( '<STR_LIT>' , default_pidfile ) <EOL> twistd_config = twistd . ServerOptions ( ) <EOL> twistd_config . update ( config ) <EOL> if config [ '<STR_LIT>' ] : <EOL> twistd_config . opt_help_reactors ( ) <EOL> if config [ '<STR_LIT>' ] : <EOL> twistd_config . opt_reactor ( config [ '<STR_LIT>' ] ) <EOL> from piped import log <EOL> twistd_config [ '<STR_LIT>' ] = lambda : log . observer . emit <EOL> return twistd_config <EOL> def _run_twistd_with_config ( twistd_config ) : <EOL> if twistd_config [ '<STR_LIT:D>' ] : <EOL> from twisted . python import failure <EOL> from piped import util <EOL> failure . _OriginalFailure = failure . Failure <EOL> failure . Failure = util . NonCleaningFailure <EOL> from twisted . internet import defer <EOL> defer . setDebugging ( True ) <EOL> sys . argv [ <NUM_LIT:0> ] = '<STR_LIT>' <EOL> twistd . runApp ( twistd_config ) </s>
