comparison hgext/inotify/server.py @ 8610:8ef1f63e554c

inotify: server: use a common 'pollable' interface for server & repowatcher Mainly for documentation purposes: it easily explains the role of handle_event and handle_timeout, and why both server & repowatcher implement those methods.
author Nicolas Dumazet <nicdumz.commits@gmail.com>
date Thu, 21 May 2009 17:09:12 +0900
parents aeaa0bd9dc24
children 55af9be4efac
comparison
equal deleted inserted replaced
8609:aeaa0bd9dc24 8610:8ef1f63e554c
111 (limit, newlimit)) 111 (limit, newlimit))
112 ui.warn(_('*** echo %d > %s\n') % (newlimit, path)) 112 ui.warn(_('*** echo %d > %s\n') % (newlimit, path))
113 raise util.Abort(_('cannot watch %s until inotify watch limit is raised') 113 raise util.Abort(_('cannot watch %s until inotify watch limit is raised')
114 % repo.root) 114 % repo.root)
115 115
116 class repowatcher(object): 116 class pollable(object):
117 """
118 Interface to support polling.
119 The file descriptor returned by fileno() is registered to a polling
120 object.
121 Usage:
122 Every tick, check if an event has happened since the last tick:
123 * If yes, call handle_events
124 * If no, call handle_timeout
125 """
117 poll_events = select.POLLIN 126 poll_events = select.POLLIN
127 def fileno(self):
128 raise NotImplementedError
129
130 def handle_events(self, events):
131 raise NotImplementedError
132
133 def handle_timeout(self):
134 raise NotImplementedError
135
136 def shutdown(self):
137 raise NotImplementedError
138
139 class repowatcher(pollable):
140 """
141 Watches inotify events
142 """
118 statuskeys = 'almr!?' 143 statuskeys = 'almr!?'
119 mask = ( 144 mask = (
120 inotify.IN_ATTRIB | 145 inotify.IN_ATTRIB |
121 inotify.IN_CREATE | 146 inotify.IN_CREATE |
122 inotify.IN_DELETE | 147 inotify.IN_DELETE |
538 if not self.registered: 563 if not self.registered:
539 if self.ui.debugflag: 564 if self.ui.debugflag:
540 self.ui.note(_('%s hooking back up with %d bytes readable\n') % 565 self.ui.note(_('%s hooking back up with %d bytes readable\n') %
541 (self.event_time(), self.threshold.readable())) 566 (self.event_time(), self.threshold.readable()))
542 self.read_events(0) 567 self.read_events(0)
543 self.master.poll.register(self, select.POLLIN) 568 self.master.poll.register(self, self.poll_events)
544 self.registered = True 569 self.registered = True
545 570
546 self.timeout = None 571 self.timeout = None
547 572
548 def shutdown(self): 573 def shutdown(self):
553 Returns a sorted list of relatives paths currently watched, 578 Returns a sorted list of relatives paths currently watched,
554 for debugging purposes. 579 for debugging purposes.
555 """ 580 """
556 return sorted(tuple[0][len(self.wprefix):] for tuple in self.watcher) 581 return sorted(tuple[0][len(self.wprefix):] for tuple in self.watcher)
557 582
558 class server(object): 583 class server(pollable):
559 poll_events = select.POLLIN 584 """
560 585 Listens for client queries on unix socket inotify.sock
586 """
561 def __init__(self, ui, repo, repowatcher, timeout): 587 def __init__(self, ui, repo, repowatcher, timeout):
562 self.ui = ui 588 self.ui = ui
563 self.repo = repo 589 self.repo = repo
564 self.repowatcher = repowatcher 590 self.repowatcher = repowatcher
565 self.timeout = timeout 591 self.timeout = timeout