2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-16 00:12:48 +00:00
Comrad/client/watcher.py

62 lines
1.5 KiB
Python
Raw Normal View History

2020-08-03 10:25:58 +00:00
import time,os,subprocess
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
p = None
def run_command():
global p
if p!=None: p.terminate()
cmd = 'python3 main.py'
p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
class Watcher:
DIRECTORY_TO_WATCH = "/home/ryan/github/Gyre/client"
def __init__(self):
self.observer = Observer()
def run(self):
event_handler = Handler()
self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
self.observer.start()
try:
while True:
time.sleep(5)
except:
self.observer.stop()
print("Error")
self.observer.join()
class Handler(FileSystemEventHandler):
@staticmethod
def on_any_event(event):
if event.is_directory:
return None
elif event.event_type == 'created':
# Take any action here when a file is first created.
print("Received created event - %s." % event.src_path)
elif event.event_type == 'modified':
# Taken any action here when a file is modified.
2020-08-06 10:24:09 +00:00
if event.src_path.endswith('.json'): return None
if '.json' in str(event.src_path): return None
if event.src_path.endswith('log.txt'): return None
2020-08-03 10:25:58 +00:00
print(" \n\n\n\n\nReceived modified event - %s." % event.src_path)
run_command()
#
if __name__ == '__main__':
run_command()
w = Watcher()
w.run()
2020-08-03 15:48:47 +00:00