Last active
August 29, 2015 14:16
-
-
Save FedericoCeratto/29cea5478e67908b6e2d to your computer and use it in GitHub Desktop.
Fabric - Monitor files for change and execute tasks using inotify (like inotifywait) #1295
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import inotifyx | |
from glob import glob | |
def _run_monitoring_loop(fd, globs, tasks): | |
"""Setup file monitoring and run Fabric tasks | |
""" | |
fnames = [fn for g in globs for fn in glob(g)] | |
if len(fnames) > 5: | |
print("Monitoring %d files..." % len(fnames)) | |
else: | |
print("Monitoring %s..." % ','.join(sorted(fnames))) | |
for fn in fnames: | |
inotifyx.add_watch(fd, fn, inotifyx.IN_CLOSE_WRITE) | |
inotifyx.get_events(fd) | |
for t in tasks: | |
execute(t) | |
def monitor_files(globs, tasks): | |
"""Monitor files for change and execute tasks until interrupted. | |
globs: file globbing (str) or an iterable of globbings | |
tasks: fabric task or an iterable of tasks | |
""" | |
if not isinstance(globs, (list, tuple)): | |
globs = (globs, ) | |
if not isinstance(tasks, (list, tuple)): | |
tasks = (tasks, ) | |
fd = inotifyx.init() | |
try: | |
while True: | |
_run_monitoring_loop(fd, globs, tasks) | |
finally: | |
os.close(fd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment