Created
December 23, 2011 17:22
-
-
Save sigmaris/1514835 to your computer and use it in GitHub Desktop.
Watch a directory for filesystem events, and rsync individually changed files to a remote path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# requires the "MacFSEvents" package | |
import fsevents | |
import time | |
import sys | |
import os | |
import subprocess | |
observer = fsevents.Observer() | |
if len(sys.argv) < 4 or (not os.path.exists(sys.argv[1])): | |
print("Usage: {0} <srcpath> <desthost> <destpath> [rsyncargs]".format(sys.argv[0])) | |
sys.exit(1) | |
else: | |
srcpath = os.path.abspath(sys.argv[1]) | |
desthost = sys.argv[2] | |
destpath = sys.argv[3] | |
rsyncargs = sys.argv[4:] | |
def callback(event): | |
if event.mask & (fsevents.IN_ATTRIB | fsevents.IN_CREATE | |
| fsevents.IN_MODIFY | fsevents.IN_MOVED_TO): | |
relpath = os.path.relpath(event.name, srcpath) | |
if os.path.isdir(event.name): | |
#For directories, use the parent as the dest path | |
fulldestpath, _ = os.path.split(os.path.join(destpath,relpath)) | |
else: | |
fulldestpath = os.path.join(destpath,relpath) | |
args = ["/usr/bin/rsync"] | |
args.extend(rsyncargs) | |
args.append(event.name) | |
args.append("{0}:{1}".format(desthost, fulldestpath)) | |
print(" ".join(args)) | |
subprocess.call(args) | |
stream = fsevents.Stream(callback, srcpath, file_events=True) | |
observer.schedule(stream) | |
try: | |
observer.start() | |
while True: time.sleep(86400) | |
except KeyboardInterrupt: | |
observer.unschedule(stream) | |
observer.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment