forked from andrew-d/lua-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mon.py
executable file
·81 lines (58 loc) · 1.85 KB
/
mon.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
#!/usr/bin/env python
from __future__ import print_function
import os
import sys
import subprocess
import datetime
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
BASEDIR = os.path.abspath(os.path.dirname(__file__))
def now():
return datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
def build_docs(event):
print("Building docs at %s" % now(), file=sys.stderr)
print("-" * 79)
os.chdir(BASEDIR)
subprocess.call(r'ldoc.lua .', shell=True)
print("-" * 79)
def run_tests(event):
print("Running unit tests at %s" % now(), file=sys.stderr)
print("-" * 79)
os.chdir(BASEDIR)
subprocess.call(r'busted')
print("-" * 79)
def getext(filename):
return os.path.splitext(filename)[-1].lower()
class ChangeHandler(FileSystemEventHandler):
"""
React to changes in Python and Rest files by
running unit tests (Python) or building docs (.rst)
"""
def __init__(self, extensions, func):
if isinstance(extensions, str):
self.__exts = [extensions]
else:
self.__exts = list(extensions)
self.__func = func
def on_any_event(self, event):
if event.is_directory:
return
if getext(event.src_path) in self.__exts:
self.__func(event)
def main():
while 1:
test_handler = ChangeHandler('.lua', run_tests)
docs_handler = ChangeHandler(['.lua', '.ld'], build_docs)
observer = Observer()
observer.schedule(test_handler, BASEDIR, recursive=True)
observer.schedule(docs_handler, os.path.join(BASEDIR, 'src'), recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == '__main__':
main()