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
|
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
import click
from path import Path
import time
from .shell import sh
"""
Provides an API to monitor the file system
"""
class RunScriptChangeHandler(FileSystemEventHandler):
def __init__(self, script):
super().__init__()
self.script = script
self.is_running = False
def on_modified(self, event):
if event.is_directory:
return
self.run()
def run(self):
if self.is_running:
return
self.is_running = True
sh(str(self.script), cwd=Path.getcwd())
self.is_running = False
def monitor(script, src, dst, args):
"""
reloads the script given by argv when src files changes
"""
src = src if isinstance(src, (list, tuple)) else [src]
dst = Path(dst).expand().abspath()
src = [Path(entry).expand().abspath() for entry in src]
command = ' '.join(args)
print('command: ', command)
event_handler = RunScriptChangeHandler(command)
observer = Observer()
click.secho('watch recursive: {0}'.format(script.dirname()), fg='blue')
observer.schedule(event_handler, script.dirname(), recursive=True)
for entry in src:
if entry.isfile():
entry = entry.parent
click.secho('watch recursive: {0}'.format(entry), fg='blue')
observer.schedule(event_handler, entry, recursive=True)
event_handler.run() # run always once
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
|