<s> import logging <EOL> import os <EOL> import sys <EOL> import time <EOL> from watchdog . events import FileSystemEventHandler <EOL> from watchdog . observers import Observer <EOL> import yaml <EOL> from entropy import exceptions <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def get_filename_and_path ( path ) : <EOL> return os . path . dirname ( path ) , os . path . basename ( path ) <EOL> def get_key_path ( ) : <EOL> home_dir = os . path . expanduser ( "<STR_LIT>" ) <EOL> ssh_dir = os . path . join ( home_dir , "<STR_LIT>" ) <EOL> if not os . path . isdir ( ssh_dir ) : <EOL> return None <EOL> for k in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> path = os . path . join ( ssh_dir , k ) <EOL> if os . path . isfile ( path ) : <EOL> return path <EOL> return None <EOL> def load_yaml ( filename ) : <EOL> with open ( filename , "<STR_LIT:rb>" ) as fh : <EOL> return yaml . safe_load ( fh . read ( ) ) <EOL> def import_module ( module_name ) : <EOL> __import__ ( module_name ) <EOL> return sys . modules [ module_name ] <EOL> def find_module ( base_name , search_paths , required_attrs = None ) : <EOL> found_places = [ ] <EOL> if not required_attrs : <EOL> required_attrs = [ ] <EOL> real_paths = [ ] <EOL> for path in search_paths : <EOL> real_path = [ ] <EOL> if path : <EOL> real_path . extend ( path . split ( "<STR_LIT:.>" ) ) <EOL> real_path . append ( base_name ) <EOL> full_path = '<STR_LIT:.>' . join ( real_path ) <EOL> real_paths . append ( full_path ) <EOL> LOG . info ( "<STR_LIT>" , <EOL> real_paths , required_attrs ) <EOL> for full_path in real_paths : <EOL> mod = None <EOL> try : <EOL> mod = import_module ( full_path ) <EOL> except ImportError as e : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> full_path , e ) <EOL> if not mod : <EOL> continue <EOL> found_attrs = <NUM_LIT:0> <EOL> for attr in required_attrs : <EOL> if hasattr ( mod , attr ) : <EOL> found_attrs += <NUM_LIT:1> <EOL> if found_attrs == len ( required_attrs ) : <EOL> found_places . append ( full_path ) <EOL> LOG . info ( "<STR_LIT>" , base_name , <EOL> required_attrs , found_places ) <EOL> return found_places <EOL> class WatchdogHandler ( FileSystemEventHandler ) : <EOL> def __init__ ( self , event_fn ) : <EOL> self . event_fn = event_fn <EOL> def on_modified ( self , event ) : <EOL> if event . src_path in self . event_fn : <EOL> self . event_fn [ event . src_path ] ( ) <EOL> else : <EOL> LOG . error ( '<STR_LIT>' , event . src_path ) <EOL> def watch_dir_for_change ( dirs_to_watch , event_fn ) : <EOL> event_handler = WatchdogHandler ( event_fn ) <EOL> observer = Observer ( ) <EOL> for directory in dirs_to_watch : <EOL> observer . schedule ( event_handler , path = directory ) <EOL> observer . setDaemon ( True ) <EOL> observer . start ( ) <EOL> return observer <EOL> def check_exists_and_enabled ( name , cfg_file ) : <EOL> engines = load_yaml ( cfg_file ) <EOL> return engines and name in engines and engines [ name ] [ '<STR_LIT>' ] <EOL> def check_exists_and_disabled ( name , cfg_file ) : <EOL> engines = load_yaml ( cfg_file ) <EOL> return engines and name in engines and not engines [ name ] [ '<STR_LIT>' ] <EOL> def purge_disabled ( cfg_file ) : <EOL> engines = load_yaml ( cfg_file ) <EOL> final_engines = { } <EOL> if not engines : <EOL> return <EOL> for engine in engines : <EOL> if engines [ engine ] [ '<STR_LIT>' ] : <EOL> final_engines [ engine ] = engines [ engine ] <EOL> if final_engines : <EOL> write_yaml ( final_engines , cfg_file , append = False ) <EOL> else : <EOL> with open ( cfg_file , '<STR_LIT:w>' ) : <EOL> pass <EOL> def disable_engine ( name , cfg_file ) : <EOL> engines = load_yaml ( cfg_file ) <EOL> if not engines : <EOL> raise exceptions . NoEnginesException ( "<STR_LIT>" ) <EOL> if name not in engines : <EOL> raise exceptions . NoSuchEngineException ( "<STR_LIT>" , name ) <EOL> engines [ name ] [ '<STR_LIT>' ] = False <EOL> write_yaml ( engines , cfg_file , append = False ) <EOL> return engines [ name ] [ '<STR_LIT>' ] <EOL> def reset_logger ( log ) : <EOL> if not log : <EOL> return <EOL> handlers = list ( log . handlers ) <EOL> for h in handlers : <EOL> h . flush ( ) <EOL> h . close ( ) <EOL> log . removeHandler ( h ) <EOL> log . setLevel ( logging . NOTSET ) <EOL> log . addHandler ( logging . NullHandler ( ) ) <EOL> def write_yaml ( data , filename , append = True ) : <EOL> mode = "<STR_LIT:a>" if append else "<STR_LIT:w>" <EOL> with open ( filename , mode ) as cfg_file : <EOL> cfg_file . write ( yaml . safe_dump ( data , <EOL> default_flow_style = False , <EOL> canonical = False ) ) <EOL> def wallclock ( ) : <EOL> return time . time ( ) <EOL> class StopWatch ( object ) : <EOL> """<STR_LIT>""" <EOL> _STARTED = '<STR_LIT>' <EOL> _STOPPED = '<STR_LIT>' <EOL> def __init__ ( self , duration = None ) : <EOL> self . _duration = duration <EOL> self . _started_at = None <EOL> self . _stopped_at = None <EOL> self . _state = None <EOL> def start ( self ) : <EOL> if self . _state == self . _STARTED : <EOL> return self <EOL> self . _started_at = wallclock ( ) <EOL> self . _stopped_at = None <EOL> self . _state = self . _STARTED <EOL> return self <EOL> def elapsed ( self ) : <EOL> if self . _state == self . _STOPPED : <EOL> return float ( self . _stopped_at - self . _started_at ) <EOL> elif self . _state == self . _STARTED : <EOL> return float ( wallclock ( ) - self . _started_at ) <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def __enter__ ( self ) : <EOL> self . start ( ) <EOL> return self <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> try : <EOL> self . stop ( ) <EOL> except RuntimeError : <EOL> pass <EOL> return False <EOL> def leftover ( self ) : <EOL> if self . _duration is None : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if self . _state != self . _STARTED : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> end_time = self . _started_at + self . _duration <EOL> return max ( <NUM_LIT:0.0> , end_time - wallclock ( ) ) <EOL> def expired ( self ) : <EOL> if self . _duration is None : <EOL> return False <EOL> if self . elapsed ( ) > self . _duration : <EOL> return True <EOL> return False <EOL> def resume ( self ) : <EOL> if self . _state == self . _STOPPED : <EOL> self . _state = self . _STARTED <EOL> return self <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def stop ( self ) : <EOL> if self . _state == self . _STOPPED : <EOL> return self <EOL> if self . _state != self . _STARTED : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . _stopped_at = wallclock ( ) <EOL> self . _state = self . _STOPPED <EOL> return self <EOL> def create_files ( list_of_files ) : <EOL> if not list_of_files : <EOL> return <EOL> for filename in list_of_files : <EOL> if not os . path . isfile ( filename ) : <EOL> with open ( filename , '<STR_LIT:w>' ) : <EOL> pass </s>
