-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvufwatcher.py
executable file
·72 lines (62 loc) · 1.93 KB
/
vufwatcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
#
# Usage:
# .vufwatcher.py -p path
#
# Dependancies:
# Linux ( with inotify ), Python > 2.6, Pyinotify > 2.8
#
import subprocess
import sys
import pyinotify
from optparse import OptionParser
class OnWriteHandler(pyinotify.ProcessEvent):
def my_init(self, cwd, extension, cmd):
self.cwd = cwd
self.extensions = extension
self.cmd = cmd
def _run_cmd(self, pathname ):
print '==> Modification detected: %s' % pathname
cmd = self.cmd.split(' ')
cmd.append(pathname)
subprocess.call(cmd, cwd=self.cwd)
def process_IN_CLOSE_WRITE(self,event):
if (not event.pathname.endswith(ext)):
return
self._run_cmd(event.pathname)
def process_IN_MODIFY(self,event):
if (not event.pathname.endswith(ext)):
return
self._run_cmd(event.pathname)
def vuf_watch(path, extension, cmd, daemon):
wm = pyinotify.WatchManager()
handler = OnWriteHandler(cwd=path, extension=extension, cmd=cmd)
notifier = pyinotify.Notifier(wm, default_proc_fun=handler)
wm.add_watch(path, pyinotify.ALL_EVENTS, rec=True, auto_add=True)
print '==> Start Monitoring for %s files in %s ( Type C^c to exit )' % (extension, path)
if(daemon):
notifier.loop(daemonize=True)
else:
notifier.loop()
def main():
usage = "usage: %prog [options] arg"
parser = OptionParser(usage, conflict_handler='resolve')
parser.add_option("-p", "--path", nargs=1, help="Path to the directory to monitor.", dest="path")
parser.add_option("-d", "--daemon", action="store_true", dest="daemon", help="Run this script as daemon.")
(options, args) = parser.parse_args()
daemon = 0
if len(sys.argv) <=2:
parser.error("Path is a required argument.")
sys.exit(2)
if options.daemon:
daemon = 1
if options.path == '':
parser.error("Path is a required argument.")
sys.exit(2)
#monitor for vuf files
vuf_watch(options.path, ext, cmd, daemon)
if __name__ == '__main__':
#Mediamosa settings
ext = 'vuf'
cmd = 'php /path/' #path to process.php
main()