-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.py
37 lines (33 loc) · 1.17 KB
/
watcher.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
import sys
import time
import logging
from subprocess import Popen # , PIPE
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
# prc = None
class Restarter(PatternMatchingEventHandler):
pyt = None
def on_modified(self, event):
super(Restarter, self).on_modified(event)
what = 'directory' if event.is_directory else 'file'
logging.info("Modified %s: %s", what, event.src_path)
if self.pyt:
self.pyt.terminate()
logging.info("Python terminated")
self.pyt = Popen(['python', '-i', '-c', 'from tests.test_model import *'])
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
path = sys.argv[1] if len(sys.argv) > 1 else '.'
event_handler = Restarter(patterns=['*.py'])
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print('watcher stopped')
observer.stop()
observer.join()