Mercurial > templog
view web/watch.py @ 203:11a1b59b0624
Move old stuff to its own place
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 30 Mar 2014 20:21:56 +0800 |
parents | 8318d50d766d |
children | 9c2de3f977e4 |
line wrap: on
line source
#!/usr/bin/env python3.3 import pyinotify import glob import sys import fnmatch import os def GlobWatcher(object): def __init__(self, g, watcher): self.glob = g self.watches = [] self.watcher = watcher def add_glob(watcher, g): d = os.path.dirname(g) file_watches = add_glob_files def main(): touchf = sys.argv[1] watcher = pyinotify.WatchManager() dirpatterns = {} for g in sys.argv[2:]: d = os.path.dirname(g) pattern = os.path.basename(g) dirpatterns.setdefault(d, []).append(pattern) watchpatterns = {} for d, patterns in dirpatterns.items(): w = watcher.add_watch(d, (pyinotify.IN_MODIFY |pyinotify.IN_CREATE |pyinotify.IN_DELETE |pyinotify.IN_MOVED_FROM |pyinotify.IN_MOVED_TO)) wd = w[d] watchpatterns[wd] = patterns def triggered(event): if event.name is None: return patterns = watchpatterns[event.wd] for p in patterns: if fnmatch.fnmatch(event.name, p): print("matched %s %s" % (event.name, p, event.maskname)) os.utime(touchf, None) n = pyinotify.Notifier(watcher, triggered) n.loop() if __name__ == '__main__': main()