comparison web/watch.py @ 189:101c66da848d

watcher script
author Matt Johnston <matt@ucc.asn.au>
date Sun, 09 Feb 2014 11:41:13 +0800
parents
children 8318d50d766d
comparison
equal deleted inserted replaced
188:ae5efca89001 189:101c66da848d
1 #!/usr/bin/env python3.3
2
3 import pyinotify
4 import glob
5 import sys
6 import fnmatch
7 import os
8
9 def GlobWatcher(object):
10 def __init__(self, g, watcher):
11 self.glob = g
12 self.watches = []
13 self.watcher = watcher
14
15 def add_glob(watcher, g):
16 d = os.path.dirname(g)
17
18 file_watches = add_glob_files
19
20 def main():
21 touchf = sys.argv[1]
22
23 watcher = pyinotify.WatchManager()
24 dirpatterns = {}
25 for g in sys.argv[2:]:
26 d = os.path.dirname(g)
27 pattern = os.path.basename(g)
28 dirpatterns.setdefault(d, []).append(pattern)
29
30 print(dirpatterns)
31
32 watchpatterns = {}
33 for d, patterns in dirpatterns.items():
34
35 w = watcher.add_watch(d,
36 (pyinotify.IN_MODIFY
37 |pyinotify.IN_CREATE
38 |pyinotify.IN_DELETE
39 |pyinotify.IN_MOVED_FROM
40 |pyinotify.IN_MOVED_TO))
41
42 wd = w[d]
43 watchpatterns[wd] = patterns
44
45 def triggered(event):
46 if event.name is None:
47 return
48
49 print("%s %s " % (event.name, event.maskname))
50 patterns = watchpatterns[event.wd]
51 for p in patterns:
52 print(p)
53 if fnmatch.fnmatch(event.name, p):
54 print("matched %s" % p)
55 os.utime(touchf, None)
56
57 n = pyinotify.Notifier(watcher, triggered)
58 n.loop()
59
60 if __name__ == '__main__':
61 main()
62