view web/watch.py @ 544:b0b0c7e7133c

merge from server
author Matt Johnston <matt@ucc.asn.au>
date Wed, 20 May 2015 00:03:53 +0800
parents d96042bce9d3
children
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 %s" % (event.name, p, event.maskname))
				os.utime(touchf, None)

	n = pyinotify.Notifier(watcher, triggered)
	n.loop()

if __name__ == '__main__':
	main()