<s> """<STR_LIT>""" <EOL> import sys <EOL> import argparse <EOL> import os <EOL> import types <EOL> from combi . _python_toolbox . third_party import six <EOL> from combi . _python_toolbox . third_party . unittest2 import loader , runner <EOL> try : <EOL> from combi . _python_toolbox . third_party . unittest2 . signals import installHandler <EOL> except ImportError : <EOL> installHandler = None <EOL> __unittest = True <EOL> MAIN_EXAMPLES = """<STR_LIT>""" <EOL> MODULE_EXAMPLES = """<STR_LIT>""" <EOL> def _convert_name ( name ) : <EOL> if os . path . isfile ( name ) and name . lower ( ) . endswith ( '<STR_LIT>' ) : <EOL> if os . path . isabs ( name ) : <EOL> rel_path = os . path . relpath ( name , os . getcwd ( ) ) <EOL> if os . path . isabs ( rel_path ) or rel_path . startswith ( os . pardir ) : <EOL> return name <EOL> name = rel_path <EOL> return name [ : - <NUM_LIT:3> ] . replace ( '<STR_LIT:\\>' , '<STR_LIT:.>' ) . replace ( '<STR_LIT:/>' , '<STR_LIT:.>' ) <EOL> return name <EOL> def _convert_names ( names ) : <EOL> return [ _convert_name ( name ) for name in names ] <EOL> class TestProgram ( object ) : <EOL> """<STR_LIT>""" <EOL> module = None <EOL> verbosity = <NUM_LIT:1> <EOL> failfast = catchbreak = buffer = progName = None <EOL> _discovery_parser = None <EOL> def __init__ ( self , module = '<STR_LIT:__main__>' , defaultTest = None , <EOL> argv = None , testRunner = None , <EOL> testLoader = loader . defaultTestLoader , exit = True , <EOL> verbosity = <NUM_LIT:1> , failfast = None , catchbreak = None , buffer = None , <EOL> tb_locals = False ) : <EOL> if isinstance ( module , six . string_types ) : <EOL> self . module = __import__ ( module ) <EOL> for part in module . split ( '<STR_LIT:.>' ) [ <NUM_LIT:1> : ] : <EOL> self . module = getattr ( self . module , part ) <EOL> else : <EOL> self . module = module <EOL> if argv is None : <EOL> argv = sys . argv <EOL> self . exit = exit <EOL> self . verbosity = verbosity <EOL> self . failfast = failfast <EOL> self . catchbreak = catchbreak <EOL> self . buffer = buffer <EOL> self . tb_locals = tb_locals <EOL> self . defaultTest = defaultTest <EOL> self . testRunner = testRunner <EOL> self . testLoader = testLoader <EOL> self . progName = os . path . basename ( argv [ <NUM_LIT:0> ] ) <EOL> self . parseArgs ( argv ) <EOL> self . runTests ( ) <EOL> def usageExit ( self , msg = None ) : <EOL> if msg : <EOL> print ( msg ) <EOL> if self . _discovery_parser is None : <EOL> self . _initArgParsers ( ) <EOL> self . _print_help ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> def _print_help ( self , * args , ** kwargs ) : <EOL> if self . module is None : <EOL> print ( self . _main_parser . format_help ( ) ) <EOL> print ( MAIN_EXAMPLES % { '<STR_LIT>' : self . progName } ) <EOL> self . _discovery_parser . print_help ( ) <EOL> else : <EOL> print ( self . _main_parser . format_help ( ) ) <EOL> print ( MODULE_EXAMPLES % { '<STR_LIT>' : self . progName } ) <EOL> def parseArgs ( self , argv ) : <EOL> self . _initArgParsers ( ) <EOL> if self . module is None : <EOL> if len ( argv ) > <NUM_LIT:1> and argv [ <NUM_LIT:1> ] . lower ( ) == '<STR_LIT>' : <EOL> self . _do_discovery ( argv [ <NUM_LIT:2> : ] ) <EOL> return <EOL> self . _main_parser . parse_args ( argv [ <NUM_LIT:1> : ] , self ) <EOL> if not self . tests : <EOL> self . _do_discovery ( [ ] ) <EOL> return <EOL> else : <EOL> self . _main_parser . parse_args ( argv [ <NUM_LIT:1> : ] , self ) <EOL> if self . tests : <EOL> self . testNames = _convert_names ( self . tests ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> self . module = None <EOL> elif self . defaultTest is None : <EOL> self . testNames = None <EOL> elif isinstance ( self . defaultTest , str ) : <EOL> self . testNames = ( self . defaultTest , ) <EOL> else : <EOL> self . testNames = list ( self . defaultTest ) <EOL> self . createTests ( ) <EOL> def createTests ( self ) : <EOL> if self . testNames is None : <EOL> self . test = self . testLoader . loadTestsFromModule ( self . module ) <EOL> else : <EOL> self . test = self . testLoader . loadTestsFromNames ( self . testNames , <EOL> self . module ) <EOL> def _initArgParsers ( self ) : <EOL> parent_parser = self . _getParentArgParser ( ) <EOL> self . _main_parser = self . _getMainArgParser ( parent_parser ) <EOL> self . _discovery_parser = self . _getDiscoveryArgParser ( parent_parser ) <EOL> def _getParentArgParser ( self ) : <EOL> parser = argparse . ArgumentParser ( add_help = False ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , const = <NUM_LIT:2> , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , const = <NUM_LIT:0> , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> if self . failfast is None : <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> self . failfast = False <EOL> if self . catchbreak is None : <EOL> parser . add_argument ( '<STR_LIT:-c>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> self . catchbreak = False <EOL> if self . buffer is None : <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> self . buffer = False <EOL> return parser <EOL> def _getMainArgParser ( self , parent ) : <EOL> parser = argparse . ArgumentParser ( parents = [ parent ] ) <EOL> parser . prog = self . progName <EOL> parser . print_help = self . _print_help <EOL> parser . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:*>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return parser <EOL> def _getDiscoveryArgParser ( self , parent ) : <EOL> parser = argparse . ArgumentParser ( parents = [ parent ] ) <EOL> parser . prog = '<STR_LIT>' % self . progName <EOL> parser . epilog = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT:start>' , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> for arg in ( '<STR_LIT:start>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> parser . add_argument ( arg , nargs = '<STR_LIT:?>' , <EOL> default = argparse . SUPPRESS , <EOL> help = argparse . SUPPRESS ) <EOL> return parser <EOL> def _do_discovery ( self , argv , Loader = None ) : <EOL> self . start = '<STR_LIT:.>' <EOL> self . pattern = '<STR_LIT>' <EOL> self . top = None <EOL> if argv is not None : <EOL> if self . _discovery_parser is None : <EOL> self . _initArgParsers ( ) <EOL> self . _discovery_parser . parse_args ( argv , self ) <EOL> loader = self . testLoader if Loader is None else Loader ( ) <EOL> self . test = loader . discover ( self . start , self . pattern , self . top ) <EOL> def runTests ( self ) : <EOL> if self . catchbreak : <EOL> installHandler ( ) <EOL> if self . testRunner is None : <EOL> self . testRunner = runner . TextTestRunner <EOL> if isinstance ( self . testRunner , six . class_types ) : <EOL> try : <EOL> try : <EOL> testRunner = self . testRunner ( verbosity = self . verbosity , <EOL> failfast = self . failfast , <EOL> buffer = self . buffer , <EOL> tb_locals = self . tb_locals ) <EOL> except TypeError : <EOL> testRunner = self . testRunner ( verbosity = self . verbosity , <EOL> failfast = self . failfast , <EOL> buffer = self . buffer ) <EOL> except TypeError : <EOL> testRunner = self . testRunner ( ) <EOL> else : <EOL> testRunner = self . testRunner <EOL> self . result = testRunner . run ( self . test ) <EOL> if self . exit : <EOL> sys . exit ( not self . result . wasSuccessful ( ) ) <EOL> main = TestProgram </s>
