Skip to content

Instantly share code, notes, and snippets.

@jbq
Last active April 4, 2019 11:21
Show Gist options
  • Save jbq/4546993 to your computer and use it in GitHub Desktop.
Save jbq/4546993 to your computer and use it in GitHub Desktop.
Python script using pyinotify to detect changes in a working copy, and notify UWSGI to reload app. To be used along with UWSGI's "attach-daemon" option.
#! /usr/bin/python
import pyinotify, sys, os, signal, threading, time, syslog
lastEventTime = None
def iswatched(path):
return (path.endswith(".tmpl") or path.endswith(".py"))
def callback(evt):
if not iswatched(evt.pathname):
return
t = time.time()
syslog.syslog(syslog.LOG_INFO, "[%s] Change detected in %s" % (t, evt))
globals()['lastEventTime'] = t
class EventThread(threading.Thread):
def run(self):
while lastEventTime is None:
time.sleep(.1)
while time.time() - lastEventTime < 0.2:
print "Waiting..."
time.sleep(.1)
print "Sending SIGHUP to", os.getppid()
os.kill(os.getppid(), signal.SIGHUP)
sys.exit(0)
t = EventThread()
t.setDaemon(True)
t.start()
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm, default_proc_fun=callback)
root = sys.argv[1]
for parentdir, dirs, files in os.walk(root):
for file in files:
if iswatched(file):
wm.add_watch(os.path.join(parentdir, file), pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_MODIFY)
for dir in dirs:
wm.add_watch(os.path.join(parentdir, dir), pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_MODIFY)
wm.add_watch(root, pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_MODIFY)
notifier.loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment