Opened 8 years ago
Last modified 3 years ago
Initial code is checked in at sandbox/inotify.py
here's the fix to make it work with python < 2.3 Index: inotify.py =================================================================== --- inotify.py (revision 13515) +++ inotify.py (working copy) @@ -130,7 +130,7 @@ request = array.array('c', struct.pack(structWatchRequest, pathBuffer.buffer_info()[0], mask)) - handle = fcntl.ioctl(self.fd, INOTIFY_WATCH, request, 1) + handle = fcntl.ioctl(self.fd, INOTIFY_WATCH, request.buffer_info()[0]) port = iNotifyPort(path, handle, iNotifyFactory, self) @@ -142,7 +142,7 @@ return del self.ports[handle] request = array.array('c', struct.pack(structWatchIgnore, handle)) - return fcntl.ioctl(self.fd, INOTIFY_IGNORE, request, 1) + return fcntl.ioctl(self.fd, INOTIFY_IGNORE, request.buffer_info()[0]) def connectionLost(self, reason): for handle in self.ports.keys():
I tried /sandbox/dialtone/inotify.py and it works well for me, in a real application. The API is well documented and makes sense. I don't love having to pass a list of (callback, errback) tuples to the callbacks argument of the watch method, but it works, and I can't think of an API that's just as simple and allows the same multi-call functionality.
(In [26155]) Branching to 'inotify-support-972'
The branch is ready for review by anyone.
Both the points in thijs review were addressed in r26287 in the branch.
(In [26288]) Branching to 'inotify-support-972-2'
I have reviewed this code, here are my comments.
There is no unit test for when twisted.internet.inotify raises a SystemError. Either by ctypes not being available (this happens on import) or when INotify is created.
SystemError is very rarely used in twisted, so much so that it only occurs in cfsupport, inotify and test_process. Even if it is the correct exception to use, I don't like it being raised on import.
The constants defined at the top of the module for IN_ACCESS, etc, have extra spaces. This is contray to what PEP-8 says about whitespace in expressions, I would prefer to see thoses spaces collapsed to 1.
_FLAGS_TO_HUMAN is a dict, but is never treated like a dict, only a list of tuples. I suggest it is transformed to a list of tuples and used as one - as dicts don't have a stable order, and it would be nice if flagsToHuman returned elements in the correct order.
flagsToHuman should probably have a nicer name, as it's an externally exported function that will be used by developers.
There is no test for the IOError raised in INotify._addWatch.
INotify.notify is a dummy print statement and doesn't have a test, I don't think it should be included, or should be included in documentation.
for function in "inotify_add_watch inotify_init inotify_rm_watch".split(): should be a list or tuple containing the strings, instead of a string that we split().
_rmWatch(self, wd) has a line del iwp which is in effect a noop, it just removes a name from the locals of a method that is just about to have its locals removed.
INotify.release checks hasattr(self, '_fd'). It is impossible to get to the bottom of __init__ without this attribute being on the object, so the check isn't required.
while True: if len(self._buffer) < 16: break
should perhaps be expressed as:
while len(self._buffer) >= 16:
INotify._doRead handles a case where it gets a notification of something it's not caring about by doing
try: iwp = self._watchpoints[wd] except: continue # can this happen?
I suggest that the exact exception where a key is not in a dictionary is caught, instead of a bare except:.
I really don't like defining _addChildren in _doRead, it makes the entire method very complicated, if you can refactor it so that _addChildren is a method of INotify, I would much prefer that.
Please make these changes and resubmit for review.
Addressed all exarkun points in r26295. I'll be happy to receive further reviews about future compatibilities or other problems with the code.
I've changed my mind about worrying about cross platform compatibility. If someone implements something for another platform in the future, then we can worry about adding a compatibility layer on top of the two low-level APIs then.
Apparently someone at ITA is interested in using this when it's done.
So, I think I've addresses essentially all of exarkun's observations on this ticket. Two of them require some explanation (that is also present in the source).
I'm not sure, but I might have some comments about the test code as well. It's sleep time now, however.
Addressed also all of the new comments on this ticket branch.
On point 4 however I kept both hex and int because the masks, as they have been set up in the module, are hex, of course however hex is as good as int, although it's usually easier to picture them has hex numbers.
I'm not sure I'm totally convinced autoAdd and recursive belong here, but we'll give it a try. :) I hope this is the last of my feedback, and the next review will just be ok, merge. :)
To summarize, this still seems to be needed, from my reading.
The branch looks much better after the recent modifications from exarkun.
@exarkun: when you think this is ready to be reviewed again I can help with that.
(This would be my first Twisted review, mainly motivated by the fact I'd need inotify support myself. But it can be a start for others :)
So, I did a couple of cleanups. It seems to pass on Windows now. I removed the values passed to callLater. I looked at modifying watch to not return a value, but it's needed by the code itself. I think it's fine that it's not documented, because users shouldn't use it. Maybe there is a slightly better solution, but I didn't find it yet.
If buildbot is green, please merge.
(In [28488]) Branching to 'inotify-support-972-3'
(In [28495]) Merge inotify-support-972-3
Authors: dialtone, exarkun, moonfallen, therve Reviewers: jerub, exarkun Fixes #972
Add linux inotify support, allowing monitoring of file system events.
I couldn't find a ticket for cross-platform filesystem change notifications. Is there one? If not, somebody should file it. In case somebody does feel like filing it, here are two relevant resources:
Oh. That's where I wrote that first review comment. I thought Firefox ate it.