<s> """<STR_LIT>""" <EOL> import os <EOL> import errno <EOL> import pyinotify <EOL> from Bcfg2 . Compat import reduce <EOL> from Bcfg2 . Server . FileMonitor import Event <EOL> from Bcfg2 . Server . FileMonitor . Pseudo import Pseudo <EOL> class Inotify ( Pseudo , pyinotify . ProcessEvent ) : <EOL> """<STR_LIT>""" <EOL> __rmi__ = Pseudo . __rmi__ + [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> __priority__ = <NUM_LIT> <EOL> action_map = { pyinotify . IN_CREATE : '<STR_LIT>' , <EOL> pyinotify . IN_DELETE : '<STR_LIT>' , <EOL> pyinotify . IN_MODIFY : '<STR_LIT>' , <EOL> pyinotify . IN_MOVED_FROM : '<STR_LIT>' , <EOL> pyinotify . IN_MOVED_TO : '<STR_LIT>' } <EOL> mask = reduce ( lambda x , y : x | y , action_map . keys ( ) ) <EOL> def __init__ ( self ) : <EOL> Pseudo . __init__ ( self ) <EOL> pyinotify . ProcessEvent . __init__ ( self ) <EOL> self . event_filter = dict ( ) <EOL> self . watches_by_path = dict ( ) <EOL> self . notifier = None <EOL> self . watchmgr = None <EOL> self . add_q = [ ] <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> Pseudo . start ( self ) <EOL> self . watchmgr = pyinotify . WatchManager ( ) <EOL> self . notifier = pyinotify . ThreadedNotifier ( self . watchmgr , self ) <EOL> self . notifier . start ( ) <EOL> for monitor in self . add_q : <EOL> self . AddMonitor ( * monitor ) <EOL> self . add_q = [ ] <EOL> def fileno ( self ) : <EOL> if self . started : <EOL> return self . watchmgr . get_fd ( ) <EOL> else : <EOL> return None <EOL> fileno . __doc__ = Pseudo . fileno . __doc__ <EOL> def process_default ( self , ievent ) : <EOL> """<STR_LIT>""" <EOL> action = ievent . maskname <EOL> for amask , aname in self . action_map . items ( ) : <EOL> if ievent . mask & amask : <EOL> action = aname <EOL> break <EOL> else : <EOL> self . debug_log ( "<STR_LIT>" % ( action , <EOL> ievent . pathname ) ) <EOL> return <EOL> try : <EOL> watch = self . watchmgr . watches [ ievent . wd ] <EOL> except KeyError : <EOL> self . logger . error ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( action , ievent . pathname , ievent . wd ) ) <EOL> return <EOL> if ( watch . path == ievent . pathname or ievent . wd in self . event_filter ) : <EOL> path = ievent . pathname <EOL> else : <EOL> path = os . path . basename ( ievent . pathname ) <EOL> handleID = ievent . pathname <EOL> if handleID not in self . handles : <EOL> handleID = watch . path <EOL> evt = Event ( handleID , path , action ) <EOL> if ( ievent . wd not in self . event_filter or <EOL> ievent . pathname in self . event_filter [ ievent . wd ] ) : <EOL> self . events . append ( evt ) <EOL> def AddMonitor ( self , path , obj , handleID = None ) : <EOL> path = path . rstrip ( "<STR_LIT:/>" ) <EOL> if not self . started : <EOL> self . add_q . append ( ( path , obj ) ) <EOL> return path <EOL> if not os . path . isdir ( path ) : <EOL> watch_path = os . path . dirname ( path ) <EOL> is_dir = False <EOL> else : <EOL> watch_path = path <EOL> is_dir = True <EOL> try : <EOL> watchdir = self . watches_by_path [ watch_path ] <EOL> except KeyError : <EOL> if not os . path . exists ( watch_path ) : <EOL> raise OSError ( errno . ENOENT , <EOL> "<STR_LIT>" % path ) <EOL> watchdir = self . watchmgr . add_watch ( watch_path , self . mask , <EOL> quiet = False ) [ watch_path ] <EOL> self . watches_by_path [ watch_path ] = watchdir <EOL> produce_exists = True <EOL> if not is_dir : <EOL> if watchdir not in self . event_filter : <EOL> self . event_filter [ watchdir ] = [ path ] <EOL> elif path not in self . event_filter [ watchdir ] : <EOL> self . event_filter [ watchdir ] . append ( path ) <EOL> else : <EOL> produce_exists = False <EOL> if produce_exists : <EOL> return Pseudo . AddMonitor ( self , path , obj , handleID = path ) <EOL> else : <EOL> self . handles [ path ] = obj <EOL> return path <EOL> AddMonitor . __doc__ = Pseudo . AddMonitor . __doc__ <EOL> def shutdown ( self ) : <EOL> if self . started and self . notifier : <EOL> self . notifier . stop ( ) <EOL> Pseudo . shutdown ( self ) <EOL> shutdown . __doc__ = Pseudo . shutdown . __doc__ <EOL> def list_watches ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . watches_by_path . keys ( ) ) <EOL> def list_paths ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . handles . keys ( ) ) </s>
