<s> from __future__ import print_function <EOL> from __future__ import absolute_import <EOL> from . modules_restore_point import ModulesRestorePoint <EOL> from . broadcasters import broadcaster <EOL> from functools import wraps <EOL> from termstyle import bg_red , bg_green , white <EOL> import platform <EOL> import os <EOL> import sys <EOL> from . import scent_picker <EOL> __all__ = [ '<STR_LIT>' ] <EOL> try : <EOL> _ = StandardError <EOL> except NameError : <EOL> StandardError = Exception <EOL> def echo ( text ) : <EOL> def wrapped ( filepath ) : <EOL> print ( text % { '<STR_LIT:file>' : filepath } ) <EOL> return wrapped <EOL> class Sniffer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . modules = ModulesRestorePoint ( ) <EOL> self . _scanners = [ ] <EOL> self . pass_colors = { '<STR_LIT>' : white , '<STR_LIT>' : bg_green } <EOL> self . fail_colors = { '<STR_LIT>' : white , '<STR_LIT>' : bg_red } <EOL> self . watch_paths = ( '<STR_LIT:.>' , ) <EOL> self . set_up ( ) <EOL> def set_up ( self , test_args = ( ) , clear = True , debug = False ) : <EOL> """<STR_LIT>""" <EOL> self . test_args = test_args <EOL> self . debug , self . clear = debug , clear <EOL> def absorb_args ( self , func ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( func ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> return func ( ) <EOL> return wrapper <EOL> def observe_scanner ( self , scanner ) : <EOL> """<STR_LIT>""" <EOL> scanner . observe ( scanner . ALL_EVENTS , <EOL> self . absorb_args ( self . modules . restore ) ) <EOL> if self . clear : <EOL> scanner . observe ( scanner . ALL_EVENTS , <EOL> self . absorb_args ( self . clear_on_run ) ) <EOL> scanner . observe ( scanner . ALL_EVENTS , self . absorb_args ( self . _run ) ) <EOL> if self . debug : <EOL> scanner . observe ( '<STR_LIT>' , echo ( "<STR_LIT>" ) ) <EOL> scanner . observe ( '<STR_LIT>' , echo ( "<STR_LIT>" ) ) <EOL> scanner . observe ( '<STR_LIT>' , echo ( "<STR_LIT>" ) ) <EOL> self . _scanners . append ( scanner ) <EOL> def clear_on_run ( self , prefix = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if platform . system ( ) == '<STR_LIT>' : <EOL> os . system ( '<STR_LIT>' ) <EOL> else : <EOL> os . system ( '<STR_LIT>' ) <EOL> if prefix : <EOL> print ( prefix ) <EOL> def _stop ( self ) : <EOL> """<STR_LIT>""" <EOL> for scanner in self . _scanners : <EOL> scanner . stop ( ) <EOL> def _run ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if self . run ( ) : <EOL> broadcaster . success ( self ) <EOL> else : <EOL> broadcaster . failure ( self ) <EOL> except StandardError : <EOL> import traceback <EOL> traceback . print_exc ( ) <EOL> self . _stop ( ) <EOL> raise <EOL> except Exception : <EOL> self . _stop ( ) <EOL> raise <EOL> return True <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import nose <EOL> arguments = [ sys . argv [ <NUM_LIT:0> ] ] + list ( self . test_args ) <EOL> return nose . run ( argv = arguments ) <EOL> except ImportError : <EOL> print ( ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( ) <EOL> raise <EOL> class ScentSniffer ( Sniffer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cwd = None , scent = "<STR_LIT>" ) : <EOL> self . cwd = cwd or os . getcwd ( ) <EOL> self . scent = scent_picker . exec_from_dir ( self . cwd , scent ) <EOL> super ( ScentSniffer , self ) . __init__ ( ) <EOL> self . update_from_scent ( ) <EOL> def update_from_scent ( self ) : <EOL> if self . scent : <EOL> self . pass_colors [ '<STR_LIT>' ] = self . scent . fg_pass <EOL> self . pass_colors [ '<STR_LIT>' ] = self . scent . bg_pass <EOL> self . fail_colors [ '<STR_LIT>' ] = self . scent . fg_fail <EOL> self . fail_colors [ '<STR_LIT>' ] = self . scent . bg_fail <EOL> self . watch_paths = self . scent . watch_paths <EOL> def refresh_scent ( self , filepath ) : <EOL> if self . scent and filepath == self . scent . filename : <EOL> print ( "<STR_LIT>" , filepath ) <EOL> for s in self . _scanners : <EOL> self . unobserve_scanner ( s ) <EOL> self . scent = self . scent . reload ( ) <EOL> self . update_from_scent ( ) <EOL> for s in self . _scanners : <EOL> self . scent_observe_scanner ( s ) <EOL> def unobserve_scanner ( self , scanner ) : <EOL> for v in self . scent . validators : <EOL> if self . debug : <EOL> print ( "<STR_LIT>" , repr ( v ) ) <EOL> scanner . remove_validator ( v ) <EOL> def scent_observe_scanner ( self , scanner ) : <EOL> if self . scent : <EOL> for v in self . scent . validators : <EOL> if self . debug : <EOL> print ( "<STR_LIT>" , repr ( v ) ) <EOL> scanner . add_validator ( v ) <EOL> def observe_scanner ( self , scanner ) : <EOL> scanner . observe ( '<STR_LIT>' , self . refresh_scent ) <EOL> scanner . observe ( '<STR_LIT>' , self . refresh_scent ) <EOL> self . scent_observe_scanner ( scanner ) <EOL> return super ( ScentSniffer , self ) . observe_scanner ( scanner ) <EOL> def clear_on_run ( self ) : <EOL> super ( ScentSniffer , self ) . clear_on_run ( None ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . scent or len ( self . scent . runners ) == <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" ) <EOL> return super ( ScentSniffer , self ) . run ( ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> arguments = [ sys . argv [ <NUM_LIT:0> ] ] + list ( self . test_args ) <EOL> return self . scent . run ( arguments ) <EOL> return True </s>
