#!/usr/bin/env python
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from subprocess import Popen
from argparse import ArgumentParser, REMAINDER
from time import sleep

parser = ArgumentParser(description='''
Allows to start a program, and to monitor changes in a folder, when changes are
detected in the folder, the command is restarted.

This can be useful to test a software you are developping and having immediate
feedback.
Or to restart a daemon when configuration or data changes.
Or any other use, the sky is the limit :)
''')
parser.add_argument('-p', '--path', type=str, default='.', help='set the path to monitor for changes')
parser.add_argument('-a', '--action', type=str, default='restart', help='what action to perform when changes are detected')
#parser.add_argument('-i', '--ignore', type=str, default='',
parser.add_argument('command', type=str, nargs=REMAINDER)


class RestartHandler(FileSystemEventHandler):
    def __init__(self, command, path, ignorelist, **kwargs):
        super(RestartHandler, self).__init__(**kwargs)
        self.command = command
        self.start()

    def stop(self):
        self._process.terminate()
        print 'TERMINATED'

    def start(self):
        self._process = Popen(self.command)
        print 'STARTED', self._process

    def on_any_event(self, event):
        print 'terminating', self
        self.stop()
        self.start()


def monitor(command, path, action, ignorelist=None):
        if action == 'restart':
            ev = RestartHandler(command, path=path, ignorelist=ignorelist)
        else:
            raise NotImplementedError('action %s not implemented' % action)

        ob = Observer()
        ob.schedule(ev, path=path, recursive=True)
        ob.start()
        try:
            while True:
                sleep(1)
        except KeyboardInterrupt:
            ob.stop()
        ob.join()

if __name__ == '__main__':
    args = parser.parse_args()
    print args
    monitor(args.command, args.path, args.action)  # args.ignorelist)

@@ -204,16 +204,26 @@
 rt sleep
+%0Aimport re
 %0A%0Aparser
@@ -809,17 +809,16 @@
 ected')%0A
-#
 parser.a
@@ -844,16 +844,20 @@
 --ignore
+list
 ', type=
@@ -872,16 +872,51 @@
 ault='',
+ nargs='*', help='files to ignore')
 %0Aparser.
@@ -1161,16 +1161,53 @@
 command%0A
+        self.ignorelist = ignorelist%0A
         
@@ -1442,16 +1442,157 @@
 event):%0A
+        for i in self.ignorelist:%0A            if re.compile('%5E' + i.replace('*', '.*') + '$').match(event.src_path):%0A                return%0A%0A
         
@@ -2267,12 +2267,9 @@
 tion
-)  #
+,
  arg

add ignorelist option, to ignore certain files