-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocompile.py
executable file
·90 lines (74 loc) · 2.58 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
"""
Usage:
./autocompile.py path 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 "make pdf"
Dependencies:
Linux, Python 2.x, Pyinotify
"""
from __future__ import print_function
import sys
import pyinotify
if sys.version_info.major >= 3:
from subprocess import getoutput
else:
from commands import getoutput
import re
def checkResult(command_output, regex, doc_type):
result_value = True
search_result = re.search(regex, command_output)
if search_result and search_result.group(0):
print('SUCCESS: %s built okay.' % doc_type)
else:
print('FAIL: %s build not okay.' % doc_type)
result_value = False
return result_value
class OnWriteHandler(pyinotify.ProcessEvent):
def my_init(self, cwd, extension, cmd):
self.cwd = cwd
self.extensions = extension.split(',')
self.cmd = cmd
def _run_cmd(self):
print('==> Modification detected')
result = getoutput(self.cmd)
html_result = checkResult(result,
'The HTML pages are in (.*)\.', 'HTML')
latex_result = checkResult(result,
'Output written on (.*)\.pdf', 'LaTeX')
if html_result and latex_result:
print('OKAY: Waiting now until next ReST modification')
else:
print('BUILD FAILED: Traceback in-bound...')
print(result)
def process_IN_MODIFY(self, event):
for ext in self.extensions:
if not event.pathname.endswith(ext):
return
self._run_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, pyinotify.ALL_EVENTS, rec=True, auto_add=True)
print('==> Start monitoring %s (type c^c to exit)' % path)
notifier.loop()
if __name__ == '__main__':
if len(sys.argv) < 3:
import autocompile
error = "Command line error: missing argument(s).\n" + \
autocompile.__doc__
print(error, file=sys.stderr)
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)