annotate web/watch.py @ 572:453c4c0eab3a

syntax fixes, doesn't make sense yet for asyncio
author Matt Johnston <matt@ucc.asn.au>
date Tue, 17 Nov 2015 22:26:00 +0800
parents d96042bce9d3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
488
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 #!/usr/bin/env python3.3
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import pyinotify
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import glob
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 import sys
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 import fnmatch
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 import os
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 def GlobWatcher(object):
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 def __init__(self, g, watcher):
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 self.glob = g
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 self.watches = []
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 self.watcher = watcher
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 def add_glob(watcher, g):
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 d = os.path.dirname(g)
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 file_watches = add_glob_files
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 def main():
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 touchf = sys.argv[1]
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 watcher = pyinotify.WatchManager()
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 dirpatterns = {}
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 for g in sys.argv[2:]:
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 d = os.path.dirname(g)
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 pattern = os.path.basename(g)
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 dirpatterns.setdefault(d, []).append(pattern)
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 watchpatterns = {}
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 for d, patterns in dirpatterns.items():
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 w = watcher.add_watch(d,
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 (pyinotify.IN_MODIFY
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 |pyinotify.IN_CREATE
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 |pyinotify.IN_DELETE
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 |pyinotify.IN_MOVED_FROM
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 |pyinotify.IN_MOVED_TO))
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 wd = w[d]
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 watchpatterns[wd] = patterns
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 def triggered(event):
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 if event.name is None:
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 return
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 patterns = watchpatterns[event.wd]
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 for p in patterns:
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 if fnmatch.fnmatch(event.name, p):
503
d96042bce9d3 - fermentation heat prediction kind of works?
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
50 print("matched %s %s %s" % (event.name, p, event.maskname))
488
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 os.utime(touchf, None)
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 n = pyinotify.Notifier(watcher, triggered)
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 n.loop()
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 if __name__ == '__main__':
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 main()
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58