Monitor events in a filesystem as they happen
I want to to write a script in either Ruby or Python that will look at开发者_JAVA技巧 the contents of a directory and monitor for
1. Any new files
2. Any saves to existing files.
I'm not bothered about what is different in the files - just that they have been saved or are new.
The script then returns the name of the file (with its full path) perhaps once every 2 seconds or something.
I want to scan files within folders to so
mainfile.txt
images/image1.jpg
images/icons/icon1.png
All these files / folders need to be montitored.
I eventually want to FTP the files that have changed / been added but I'm happy to settle for a solution to the first part of the problem as I know how to ftp (but by all means include this if you want to!)
I am using OSX 10.5.8
For ruby here is a gem which can help you: https://github.com/thibaudgg/rb-fsevent
FSEvent is the system you want to use on OS X and this is a binding bringing it to Ruby, this gem is used by Guard that is how I found it. (https://github.com/guard/guard)
You could even consider using Guard which provide a nice api to monitor files/paths and run action on changes.
Edit: Since I posted this a new gem was released to abstract platform specific stuff: https://github.com/guard/listen , this gem provide a portable api to monitor filesystem events on multiple platforms
Ah, watching FTP. As it happens, I built something like this for work. While mine was dealing with several constraints I hope you don't have, the part that sat on linux was easy. OSX in this day and age is mostly a repackaged linux.
So, you should be able to install iwatch, which uses the inotify kernel feature. If you can't use iwatch itself, which would be very surprising as it's written in perl, then the source should point you to how to directly communicate with inotify. This link has stuff on using python with inotify.
There are a few other alternatives to iwatch, but they all hook into inotify. The only other alternative is to do manual directory scans on a timer, which is clunky and should be avoided. I did it on windows, and it has.... problems. If I ever get the chance, I'll be reworking that section.
http://pypi.python.org/pypi/MacFSEvents/0.2.1 http://pypi.python.org/pypi/pyfsevents/0.2b1 http://pypi.python.org/pypi/pyobjc-framework-FSEvents/2.3
I created a ruby gem based on guard to sync files to an ftp when filesystem events occur, in a Dreamweaver-like fashion. It's a bit young but I've been using it in the past weeks without a problem.
It's called guard-ftpsync and I believe it solves the exact problem you have.
I'd recommend using a tool designed for what you're describing -- rsync.
rsync will do all this legwork for you. It will check for files modified or new, and scp them (and optimize by only copying the chunks of the file that changed, not the entire file) to the target host.
rsync -a mainfile.txt images/image1.jpg images/icons/icon1.png remotehost:/directory/to/sync/files/to
If you also want a list of the files that got synced over, you can tweak rsync's logging by adding:
-q --log-format="%f"
However, even this doesn't sound quite like what you're after. It sounds like you want a fast replicating filesystem, not a frequent frequent copy.
A replicating filesystem will know the instant changes are made (you're making them to the filesystem, after all) and can make the same changes on the other side. No work needed on your part after the initial setup. That may not be possible, you'd have to describe the situation more fully.
精彩评论