<s> import os <EOL> import time <EOL> import threading <EOL> import logging <EOL> from ctypes import * <EOL> from cactus . utils . filesystem import fileList <EOL> from cactus . utils . network import retry <EOL> from fsevents import Observer , Stream <EOL> class struct_timespec ( Structure ) : <EOL> _fields_ = [ ( '<STR_LIT>' , c_long ) , ( '<STR_LIT>' , c_long ) ] <EOL> class struct_stat64 ( Structure ) : <EOL> _fields_ = [ <EOL> ( '<STR_LIT>' , c_int32 ) , <EOL> ( '<STR_LIT>' , c_uint16 ) , <EOL> ( '<STR_LIT>' , c_uint16 ) , <EOL> ( '<STR_LIT>' , c_uint64 ) , <EOL> ( '<STR_LIT>' , c_uint32 ) , <EOL> ( '<STR_LIT>' , c_uint32 ) , <EOL> ( '<STR_LIT>' , c_int32 ) , <EOL> ( '<STR_LIT>' , struct_timespec ) , <EOL> ( '<STR_LIT>' , struct_timespec ) , <EOL> ( '<STR_LIT>' , struct_timespec ) , <EOL> ( '<STR_LIT>' , struct_timespec ) , <EOL> ( '<STR_LIT>' , c_uint64 * <NUM_LIT:8> ) <EOL> ] <EOL> libc = CDLL ( '<STR_LIT>' ) <EOL> stat64 = libc . stat64 <EOL> stat64 . argtypes = [ c_char_p , POINTER ( struct_stat64 ) ] <EOL> def get_creation_time ( path ) : <EOL> buf = struct_stat64 ( ) <EOL> rv = stat64 ( path , pointer ( buf ) ) <EOL> if rv != <NUM_LIT:0> : <EOL> raise OSError ( "<STR_LIT>" % path ) <EOL> return buf . st_birthtimespec . tv_sec <EOL> def createStream ( real_path , link_path , callback ) : <EOL> def cb ( event ) : <EOL> if not os . path . isdir ( link_path ) : <EOL> event . name = link_path <EOL> else : <EOL> translated_path = os . path . join ( link_path , os . path . relpath ( event . name , real_path ) ) <EOL> event . name = translated_path <EOL> callback ( event ) <EOL> return Stream ( cb , real_path , file_events = True ) <EOL> class FSEventsListener ( object ) : <EOL> def __init__ ( self , path , f , ignore = None ) : <EOL> logging . info ( "<STR_LIT>" ) <EOL> self . path = path <EOL> self . f = f <EOL> self . ignore = ignore <EOL> self . observer = Observer ( ) <EOL> self . observer . daemon = True <EOL> self . _streams = [ ] <EOL> self . _streams . append ( createStream ( self . path , path , self . _update ) ) <EOL> self . _streamed_folders = [ self . path ] <EOL> def add_stream ( p ) : <EOL> if p in self . _streamed_folders : <EOL> return <EOL> self . _streams . append ( <EOL> createStream ( p , file_path , self . _update ) ) <EOL> self . _streamed_folders . append ( p ) <EOL> for file_path in fileList ( self . path , folders = True ) : <EOL> if os . path . islink ( file_path ) : <EOL> if os . path . isdir ( file_path ) : <EOL> add_stream ( os . path . realpath ( file_path ) ) <EOL> else : <EOL> add_stream ( os . path . dirname ( os . path . realpath ( file_path ) ) ) <EOL> def run ( self ) : <EOL> self . resume ( ) <EOL> self . observer . start ( ) <EOL> def pause ( self ) : <EOL> logging . debug ( "<STR_LIT>" ) <EOL> for stream in self . _streams : <EOL> self . observer . unschedule ( stream ) <EOL> def resume ( self ) : <EOL> logging . debug ( "<STR_LIT>" ) <EOL> for stream in self . _streams : <EOL> self . observer . schedule ( stream ) <EOL> def stop ( ) : <EOL> self . observer . stop ( ) <EOL> def _update ( self , event ) : <EOL> path = event . name <EOL> if self . ignore and self . ignore ( path ) : <EOL> return <EOL> logging . debug ( "<STR_LIT>" , event ) <EOL> result = { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> } <EOL> if os . path . exists ( path ) : <EOL> seconds_since_created = int ( time . time ( ) ) - get_creation_time ( os . path . realpath ( path ) ) <EOL> if seconds_since_created < <NUM_LIT:1.0> : <EOL> result [ "<STR_LIT>" ] . append ( path ) <EOL> else : <EOL> result [ "<STR_LIT>" ] . append ( path ) <EOL> else : <EOL> result [ "<STR_LIT>" ] . append ( path ) <EOL> self . f ( result ) </s>
