Last active
August 15, 2017 09:36
-
-
Save pavdmyt/495f4f6bafa38003419b93ae7932cb2d to your computer and use it in GitHub Desktop.
Daemon to sync current dir contents with remote
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
# -*- coding: utf-8 -*- | |
""" | |
A daemon that monitors files mtimes and automaticaly syncs it | |
with remote if files have changed. | |
How to use: | |
~~~~~~~~~~~ | |
1. $ python syncer.py | |
2. Ctrl+Z # get back the shell | |
3. $ bg # run it in background | |
""" | |
import os | |
import signal | |
import sys | |
import time | |
SLEEP = 3 # seconds | |
PATH = '.' # directory to monitor | |
def get_mtime_map(path=PATH): | |
flist = [] | |
for root, dirs, files in os.walk(top=path): | |
for d in dirs: | |
path = os.path.join(root, d) | |
mtime = os.path.getmtime(path) | |
flist.append((path, mtime)) | |
for f in files: | |
path = os.path.join(root, f) | |
mtime = os.path.getmtime(path) | |
flist.append((path, mtime)) | |
return dict(flist) | |
def main(): | |
while True: | |
history = get_mtime_map() | |
time.sleep(SLEEP) | |
if get_mtime_map() != history: | |
# rsync is used in :sync target | |
# avoids printing stdout | |
os.system('make sync > /dev/null') | |
if __name__ == '__main__': | |
signal.signal(signal.SIGINT, lambda signal, frame: sys.exit(0)) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment