-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapp_dev.py
35 lines (28 loc) · 1.17 KB
/
app_dev.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
import pyinotify
import subprocess
import os
# Get the directory where the script is located
script_dir = os.path.dirname(os.path.abspath(__file__))
storage_path = os.path.join(script_dir, 'storage')
program_path = os.path.join(script_dir, '.pio/build/linux/program')
wm = pyinotify.WatchManager() # Watch Manager
mask = pyinotify.IN_CLOSE_WRITE | pyinotify.IN_CREATE # Watched events
class EventHandler(pyinotify.ProcessEvent):
def process_IN_CLOSE_WRITE(self, event):
self.handle_event(event)
def process_IN_CREATE(self, event):
self.handle_event(event)
def handle_event(self, event):
if event.pathname.startswith(storage_path):
try:
subprocess.run(['pkill', '-f', 'program'], check=True)
except subprocess.CalledProcessError as e:
print(f"Error stopping program: {e}")
try:
subprocess.Popen([program_path])
except Exception as e:
print(f"Error starting program: {e}")
handler = EventHandler()
notifier = pyinotify.Notifier(wm, handler)
wdd = wm.add_watch(storage_path, mask, rec=True) # Monitor "storage" recursively
notifier.loop()