def __init__(self, statedir=DEFAULT_STATEDIR): featuredir = os.path.join(statedir, "features") try: os.mkdir(featuredir, 0o755) except OSError as e: if e.errno != errno.EEXIST: raise self._featuredir = os.path.realpath(featuredir) self._wd_featurepaths = {} self._featurename_wds = {} self._queued_features = [] self._fd = inotify.init(inotify.CLOEXEC | inotify.NONBLOCK) ok = False try: flags = inotify.ONLYDIR | inotify.CREATE | inotify.DELETE | inotify.DELETE_SELF self._featuredir_wd = inotify.add_watch(self._fd, self._featuredir, flags) ok = True finally: if not ok: os.close(self._fd) try: featurenames = os.listdir(self._featuredir) except Exception: log.exception("listing %s", self._featuredir) else: for featurename in featurenames: self._add_feature(featurename)
def __init__(self, flags=0, bufsize=65536, map=None): """ Initialize an inotify event queue and register it with asyncore. flags is passed to inotify.init(). bufsize is used when receiving events.""" fd = inotify.init(flags) asyncore.file_dispatcher.__init__(self, fd, map) self.bufsize = bufsize
def watcher(): fd = inotify.init() wds = {} for name, mod in sys.modules.items(): try: filepath = mod.__file__ except AttributeError: continue wd = inotify.add_watch(fd, filepath, IN.CLOSE_WRITE) wds[wd] = filepath for event in inotify.get_events(fd): print(wds[event.wd], 'changed') print('reloading...') os._exit(0)
#!/usr/bin/pypy import os, sys, select, time, ctypes import inotify import unistd fd = inotify.init() assert fd != -1 print('fd',fd) wd = inotify.add_watch(fd, '/tmp/hi', inotify.ALL_EVENTS) assert wd != -1 print('wd',wd) size = ctypes.sizeof( inotify.inotify_event.CSTRUCT ) #print('size of struct', size ) buff = (ctypes.c_char * size)() ptr = ctypes.cast( buff, ctypes.POINTER(ctypes.c_void_p) ) ACTIVE = True def loop( fd ): while ACTIVE: read, write, errors = select.select( [fd], [], [] ) # releases the GIL if read: unistd.read( fd, ptr, size ) # also releases the GIL #print(''.join(buff)) event = ctypes.cast( ptr, ctypes.POINTER(inotify.inotify_event.CSTRUCT) ).contents print(event) #print('wd',event.wd) #print('mask',event.mask) #print('cookie',event.cookie)