-
Notifications
You must be signed in to change notification settings - Fork 19
/
autocompile.py
executable file
·76 lines (65 loc) · 2.17 KB
/
autocompile.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
73
74
75
76
#!/usr/bin/env python
#
# Usage:
# ./autocompile.py path ext1,ext2,extn cmd
#
# Blocks monitoring |path| and its subdirectories for modifications on
# files ending with suffix |extk|. Run |cmd| each time a modification
# is detected. |cmd| is optional and defaults to 'make'.
#
# Example:
# ./autocompile.py /my-latex-document-dir .tex,.bib "make pdf"
#
# Dependancies:
# Linux, Python 2.6, Pyinotify
#
import subprocess
import sys
import pyinotify
class OnWriteHandler(pyinotify.ProcessEvent):
def __init__(self, cwd, extension, cmd):
self.cwd = cwd
self.extensions = extension.split(',')
self.cmd = cmd
def _run_cmd(self):
print '==> Modification detected'
subprocess.call(self.cmd.split(' '), cwd=self.cwd)
def _do_notify(self):
subprocess.call(["notify-send", "doc updated"], cwd=self.cwd)
def process_default(self, event): pass
def process_IN_MODIFY(self, event):
if all(not event.name.endswith(ext) for ext in self.extensions):
return
self._run_cmd()
fname = event.name
cmd = self.cmd
self._do_notify()
print '==> detected change in "%s", ran "%s" -- done!' % (fname, cmd)
def auto_compile(path, extension, cmd):
wm = pyinotify.WatchManager()
handler = OnWriteHandler(cwd=path, extension=extension, cmd=cmd)
notifier = pyinotify.Notifier(wm, default_proc_fun=handler)
wm.add_watch(path, 4095, rec=True, auto_add=True)
print '==> Started monitoring "%s" for changes (type ^C to exit)' % path
while True:
try:
if notifier.check_events():
notifier.read_events()
notifier.process_events()
except KeyboardInterrupt:
notifier.stop()
break
print '==> Stopped monitoring'
if __name__ == '__main__':
if len(sys.argv) < 3:
print >> sys.stderr, "Command line error: missing argument(s)."
sys.exit(1)
# Required arguments
path = sys.argv[1]
extension = sys.argv[2]
# Optional argument
cmd = 'make'
if len(sys.argv) == 4:
cmd = sys.argv[3]
# Blocks monitoring
auto_compile(path, extension, cmd)