-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_shell.py
64 lines (50 loc) · 1.94 KB
/
server_shell.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Example: loops monitoring events forever.
import re, os
from pyinotify import WatchManager, IN_DELETE, IN_CREATE, IN_CLOSE_WRITE, ProcessEvent, Notifier
import subprocess
class Process(ProcessEvent):
def __init__(self, script, regex='.*'):
self.script = script
self.regex = re.compile(regex)
def process_IN_CREATE(self, event):
if self.match_event( event ):
self.run_kill_script()
def process_IN_DELETE(self, event):
if self.match_event( event ):
self.run_kill_script()
def process_IN_CLOSE_WRITE(self, event):
if self.match_event( event ):
self.run_kill_script()
def match_event(self, event):
target = os.path.join(event.path, event.name)
match = self.regex.match(target)
# print target, match
return match
def run_kill_script(self):
print '<'*30 + '[ Restarting twistd ]' + '>'*30
print ' ' + self.script
# subprocess.Popen(self.script, stdout=subprocess.PIPE, shell=True)#.stdout.read()
subprocess.Popen(self.script, shell=True)
# Instanciate a new WatchManager (will be used to store watches).
wm = WatchManager()
# Associate this WatchManager with a Notifier (will be used to report and process events).
notifier = Notifier(wm)
# Add a new watch on /tmp for ALL_EVENTS.
mask = IN_DELETE | IN_CREATE | IN_CLOSE_WRITE
wm.add_watch('.', mask)
subprocess.Popen("twistd -ny cyclone_server.tac", shell=True)
# Loop forever and handle events.
while True:
wm = WatchManager()
process = Process('sudo bash twistd-restart.sh', '(\.|handlers)/(.*?)\.(py|tac)$')
notifier = Notifier(wm, process)
mask = IN_DELETE | IN_CREATE | IN_CLOSE_WRITE
wdd = wm.add_watch('.', mask, rec=True)
try:
while True:
notifier.process_events()
if notifier.check_events():
notifier.read_events()
except KeyboardInterrupt:
notifier.stop()
break