@@ -0,0 +1,1722 @@
+# coding=utf-8%0A%0A__author__ = 'xubinggui'%0A%0Aimport os, sys, time, subprocess%0A%0Afrom watchdog.observers import Observer%0Afrom watchdog.events import FileSystemEventHandler%0A%0Adef log(s):%0A    print '%5BMonitor%5D %25s' %25 s%0A%0Aclass MyFileSystemEventHander(FileSystemEventHandler):%0A%0A    def __init__(self, fn):%0A        super(MyFileSystemEventHander, self).__init__()%0A        self.restart = fn%0A%0A    def on_any_event(self, event):%0A        if event.src_path.endswith('.py'):%0A            log('Python source file changed: %25s' %25 event.src_path)%0A            self.restart()%0A%0Acommand = %5B'echo', 'ok'%5D%0Aprocess = None%0A%0Adef kill_process():%0A    global process%0A    if process:%0A        log('Kill process %5B%25s%5D...' %25 process.pid)%0A        process.kill()%0A        process.wait()%0A        log('Process ended with code %25s.' %25 process.returncode)%0A        process = None%0A%0Adef start_process():%0A    global process, command%0A    log('Start process %25s...' %25 ' '.join(command))%0A    process = subprocess.Popen(command, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)%0A%0Adef restart_process():%0A    kill_process()%0A    start_process()%0A%0Adef start_watch(path, callback):%0A    observer = Observer()%0A    observer.schedule(MyFileSystemEventHander(restart_process), path, recursive=True)%0A    observer.start()%0A    log('Watching directory %25s...' %25 path)%0A    start_process()%0A    try:%0A        while True:%0A            time.sleep(0.5)%0A    except KeyboardInterrupt:%0A        observer.stop()%0A    observer.join()%0A%0Aif __name__ == '__main__':%0A    argv = sys.argv%5B1:%5D%0A    if not argv:%0A        print('Usage: ./pymonitor your-script.py')%0A        exit(0)%0A    if argv%5B0%5D!='python':%0A        argv.insert(0, 'python')%0A    command = argv%0A    path = os.path.abspath('.')%0A    start_watch(path, None)

add pymonitor.py