Created
July 30, 2013 02:52
-
-
Save LeoDT/6109807 to your computer and use it in GitHub Desktop.
touch the target file when any file in source directory changed, useful for auto reload apache wsgi.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import time | |
import optparse | |
from watchdog.observers import Observer | |
from watchdog.events import FileSystemEventHandler | |
def touch(fname, times=None): | |
""" | |
borrow from http://stackoverflow.com/questions/1158076/implement-touch-using-python | |
emulate the touch command on linux | |
""" | |
with file(fname, 'a'): | |
os.utime(fname, times) | |
class FileChangeHandler(FileSystemEventHandler): | |
def __init__(self, target): | |
self.target = target | |
def on_modified(self, event): | |
if not event.is_directory and event.src_path.endswith(".py"): | |
touch(self.target) | |
if __name__ == "__main__": | |
p = optparse.OptionParser() | |
p.add_option('--source', '-s', default='.') | |
p.add_option('--target', '-t', default='.') | |
options, arguments = p.parse_args() | |
event_handler = FileChangeHandler(options.target) | |
observer = Observer() | |
observer.schedule(event_handler, path=options.source, recursive=True) | |
observer.start() | |
try: | |
while True: | |
time.sleep(1) | |
except KeyboardInterrupt: | |
observer.stop() | |
observer.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment