2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-05 21:20:51 +00:00
Comrad/komrade/app/watcher.py

68 lines
1.8 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:
2020-08-21 11:49:39 +00:00
DIRECTORY_TO_WATCH = '.' #os.path.join(os.path.expanduser('~'),"/github/Komrade/")
2020-08-03 10:25:58 +00:00
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):
2020-08-11 13:01:48 +00:00
if '/cache/' in str(event.src_path): return None
2020-08-13 17:41:31 +00:00
if '__pycache__' in str(event.src_path): return None
2020-08-17 16:23:40 +00:00
if 'sto.dat' in str(event.src_path): return None
2020-08-21 11:49:39 +00:00
if '/dbm.' in str(event.src_path): return None
2020-08-17 16:23:40 +00:00
2020-08-03 10:25:58 +00:00
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
2020-08-11 13:01:48 +00:00
2020-08-06 10:24:09 +00:00
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