Last active
March 17, 2016 15:12
-
-
Save seungjin/53b5ac41ca0cee0a86a3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
################################################################################ | |
## | |
## SSH multiplexing will boost your sync time! | |
## If you don't know about ssh multiplxing, google it. | |
## You will get lots of benefit from it | |
## | |
import sys | |
import time | |
import logging | |
import subprocess | |
from watchdog.observers import Observer | |
#from watchdog.events import LoggingEventHandler | |
from watchdog.events import FileSystemEventHandler | |
from unipath import Path | |
class My_event_handler(FileSystemEventHandler): | |
def __init__(self, local_path, remote_resource): | |
self.local_path = local_path | |
self.remote_resource = remote_resource | |
def rsync(self, event): | |
target = event.src_path.split("/")[-1:][0] | |
print("Updating file: {}".format(target)) | |
print("From: {}".format(Path(event.src_path))) | |
print("To: {}/{}".format(remote_resource ,Path(event.src_path).name)) | |
print("Action: {}".format(event.event_type)) | |
print("") | |
if event.event_type != "deleted" : | |
subprocess.call(["rsync", "-azr", "--delete", | |
"--exclude", ".git", | |
"--exclude", "sync.py", | |
"--exclude", "*~", | |
"--exclude", "*.pyc", | |
"--exclude", "*.swp", | |
"--exclude", ".DS_Store", | |
"--exclude", "#*", | |
"--exclude", ".#*", | |
"--rsh='ssh'", | |
#local_path+"/", remote_resource]) | |
Path(event.src_path), | |
remote_resource+"/"+Path(event.src_path).name]) | |
elif event.event_type == "deleted": | |
remote_local_file = (remote_resource+"/"+Path(event.src_path).name).split(":")[1] | |
subprocess.call(["ssh", remote_resource.split(":")[0], | |
"rm -fr {}".format(remote_local_file)]) | |
def on_any_event(self, event): | |
if event.is_directory != True : | |
self.rsync(event) | |
if __name__ == "__main__": | |
if len(sys.argv) > 2 : | |
local_path = sys.argv[1] if len(sys.argv) > 1 else '.' | |
remote_resource = sys.argv[2] | |
else : | |
print("Usage: sync.py /local/path/here [email protected]:/remote/path/here") | |
exit() | |
print("Initial synchronizing...") | |
print("Source: "+local_path) | |
print("Target: "+remote_resource) | |
print("") | |
# initial sync | |
subprocess.call(["rsync", "-azvr", "--delete", | |
"--exclude", ".git", | |
"--exclude", "sync.py", | |
"--exclude", "*~", | |
"--exclude", "*.pyc", | |
"--exclude", "*.swp", | |
"--exclude", ".DS_Store", | |
"--exclude", "#*", | |
"--exclude", ".#*", | |
"--rsh='ssh'", | |
local_path+"/", remote_resource]) | |
print("") | |
event_handler = My_event_handler(local_path, remote_resource) | |
observer = Observer() | |
observer.schedule(event_handler, local_path, recursive=True) | |
observer.start() | |
try: | |
while True: | |
time.sleep(1) | |
except KeyboardInterrupt: | |
observer.stop() | |
observer.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment