Skip to content

Instantly share code, notes, and snippets.

@SalemHarrache
Forked from jonathansick/watcher.py
Last active December 11, 2015 17:18
Show Gist options
  • Save SalemHarrache/4633323 to your computer and use it in GitHub Desktop.
Save SalemHarrache/4633323 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#
# to install : sudo easy_install watchdog
#
import subprocess
import os
import time
import pynotify
from gobject import GError
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class ChangeHandler(FileSystemEventHandler):
"""React to modified .tex files."""
def on_any_event(self, event):
"""If a file or folder is changed."""
if event.is_directory:
return
filename = os.path.basename(event.src_path)
if os.path.splitext(filename)[-1].lower() == ".tex":
os.chdir(os.path.dirname(event.src_path))
pynotify.init("Compilation .tex")
try:
subprocess.check_call("make", shell=True)
except subprocess.CalledProcessError:
message = pynotify.Notification("Build %s failed." % filename)
else:
message = pynotify.Notification("Build %s done." % filename)
try:
message.show()
except GError:
print('Gerror')
def main():
print("OK, I watch *.tex files !")
handler = ChangeHandler()
observer = Observer()
observer.schedule(handler, '.', recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment