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 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)
def _add_feature(self, name): log.debug("adding feature %s", name) path = os.path.join(self._featuredir, name) try: wd = inotify.add_watch(self._fd, path, inotify.ONLYDIR|inotify.DELETE|inotify.MOVED_TO) except Exception: log.exception("adding watch for %s", path) return self._wd_featurepaths[wd] = path self._featurename_wds[name] = wd try: hostnames = os.listdir(path) except Exception: log.exception("listing %s", path) return for hostname in hostnames: self._add_host(wd, hostname)
def add_watch(self, name, mask): """ Calls inotify.add_watch() for the event queue. """ return inotify.add_watch(self.socket.fileno(), name, mask)
#!/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)