configure: match git's shell script style
[git-cola.git] / ugitlibs / inotify.py
blobaab1c423f915352e0170e4576c6034f177793d90
1 #!/usr/bin/env python
2 import os
3 import time
4 from PyQt4.QtCore import QCoreApplication
5 from PyQt4.QtCore import QThread
6 from PyQt4.QtCore import QEvent
7 from PyQt4.QtCore import SIGNAL
8 from pyinotify import ProcessEvent
9 from pyinotify import WatchManager, Notifier, EventsCodes
11 import git
12 import defaults
14 class FileSysEvent(ProcessEvent):
15 def __init__(self, parent):
16 ProcessEvent.__init__(self)
17 self.parent = parent
18 self.last_event_time = time.time()
19 def process_default(self, event):
20 # Prevent notificaiton floods
21 if time.time() - self.last_event_time > 1.0:
22 self.parent.notify()
23 self.last_event_time = time.time()
25 class GitNotifier(QThread):
26 def __init__(self, receiver, path):
27 QThread.__init__(self)
28 self.receiver = receiver
29 self.path = path
30 self.abort = False
32 def notify(self):
33 if not self.abort:
34 event_type = QEvent.Type(defaults.INOTIFY_EVENT)
35 event = QEvent(event_type)
36 QCoreApplication.postEvent(self.receiver, event)
38 def run(self):
39 # Only capture those events that git cares about
40 mask = ( EventsCodes.IN_CREATE
41 | EventsCodes.IN_DELETE
42 | EventsCodes.IN_MODIFY
43 | EventsCodes.IN_MOVED_TO)
44 wm = WatchManager()
45 notifier = Notifier(wm, FileSysEvent(self))
46 self.notifier = notifier
47 dirs_seen = {}
48 added_flag = False
49 while not self.abort:
50 if not added_flag:
51 wm.add_watch(self.path, mask)
52 # Register files/directories known to git
53 for file in git.ls_files():
54 wm.add_watch(file, mask)
55 directory = os.path.dirname(file)
56 if directory not in dirs_seen:
57 wm.add_watch(directory, mask)
58 dirs_seen[directory] = True
59 added_flag = True
60 notifier.process_events()
61 if notifier.check_events():
62 notifier.read_events()
63 notifier.stop()