Last active
November 16, 2015 01:48
-
-
Save yetone/dd09ec36b2092911a99a to your computer and use it in GitHub Desktop.
auto rsync by watch filesystem events.
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
import os | |
import sys | |
import time | |
import logging | |
import subprocess | |
from watchdog.observers import Observer | |
from watchdog.events import FileSystemEventHandler | |
class COLORS(object): | |
PURPLE = '\033[95m' | |
BLUE = '\033[94m' | |
GREEN = '\033[92m' | |
YELLOW = '\033[93m' | |
RED = '\033[91m' | |
BOLD = '\033[1m' | |
UNDERLINE = '\033[4m' | |
END = '\033[0m' | |
class RSyncEventHandler(FileSystemEventHandler): | |
"""RSync when the events captured.""" | |
def __init__(self, path, remote_path, args=None): | |
self.path = path | |
self.remote_path = remote_path | |
self.args = args or [] | |
self.rsync() | |
@staticmethod | |
def log(log, color): | |
logging.info('{}{}{}'.format(color, log, COLORS.END)) | |
def on_moved(self, event): | |
super(RSyncEventHandler, self).on_moved(event) | |
what = 'directory' if event.is_directory else 'file' | |
self.log('Moved {}: from {} to {}'.format(what, | |
event.src_path, | |
event.dest_path), | |
COLORS.BLUE) | |
self.rsync() | |
def on_created(self, event): | |
super(RSyncEventHandler, self).on_created(event) | |
what = 'directory' if event.is_directory else 'file' | |
self.log('Created {}: {}'.format(what, event.src_path), | |
COLORS.GREEN) | |
self.rsync() | |
def on_deleted(self, event): | |
super(RSyncEventHandler, self).on_deleted(event) | |
what = 'directory' if event.is_directory else 'file' | |
self.log('Deleted {}: {}'.format(what, event.src_path), | |
COLORS.RED) | |
self.rsync() | |
def on_modified(self, event): | |
super(RSyncEventHandler, self).on_modified(event) | |
what = 'directory' if event.is_directory else 'file' | |
self.log('Modified {}: {}'.format(what, event.src_path), | |
COLORS.YELLOW) | |
self.rsync() | |
def rsync(self): | |
self.log('RSyncing', COLORS.PURPLE) | |
cmd = 'rsync -avzP {} {} {}'.format( | |
' '.join(self.args), self.path, self.remote_path | |
) | |
self.log(cmd, COLORS.BOLD) | |
with open(os.devnull, 'w') as DEVNULL: | |
subprocess.call(['rsync', '-avzP'] + self.args | |
+ [self.path, self.remote_path], | |
stdout=DEVNULL, | |
stderr=subprocess.STDOUT) | |
def main(): | |
if len(sys.argv) < 3: | |
sys.exit('''USAGE: | |
auto_rsync.py [local_path] [remote_path] [rsync_args] | |
''') | |
logging.basicConfig(level=logging.INFO, | |
format='%(asctime)s - %(message)s', | |
datefmt='%Y-%m-%d %H:%M:%S') | |
path = sys.argv[1] | |
remote_path = sys.argv[2] | |
args = sys.argv[3:] | |
event_handler = RSyncEventHandler(path, remote_path, args) | |
observer = Observer() | |
observer.schedule(event_handler, path, recursive=True) | |
observer.start() | |
try: | |
while True: | |
time.sleep(1) | |
except KeyboardInterrupt: | |
observer.stop() | |
observer.join() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
USAGE: