Created
May 26, 2016 13:09
-
-
Save jannson/6357e74cb48d324974b2adddc0465d5c 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
#!/usr/bin/python | |
import sys, os, os.path | |
from dbclient import Beansdb, db, MCStore | |
def lock(fd): | |
import fcntl, errno | |
try: | |
fcntl.lockf(fd, fcntl.LOCK_EX|fcntl.LOCK_NB) | |
except IOError, e: | |
if e.errno in (errno.EACCES, errno.EAGAIN): | |
print "There is an instance of", sys.argv[0], "running. Quit" | |
sys.exit(0) | |
else: | |
raise | |
def get_dir(s, dir): | |
def parse(line): | |
p,h,c = line.split(' ') | |
return p, (int(h), int(c)) | |
return dict(parse(line) for line in | |
filter(None, (s.get(dir) or '').split('\n'))) | |
def is_dir(d): | |
return len(d) == 16 and len([k for k in d if k.endswith('/')]) == 16 | |
def sdb_copy(dst, src, path): | |
s = get_dir(src, path) | |
if is_dir(s): | |
for k in sorted(s): | |
sdb_copy(dst, src, path+k[0]) | |
else: | |
for k in sorted(s.keys()): | |
print k | |
dst.set(k, src.get(k)) | |
def stat(s): | |
st = {} | |
for d,h,c in [line.split(' ') for line in (s.get('@') or '').split('\n') if line]: | |
if len(d) != 2 and not d.endswith('/'): | |
return {} | |
try: | |
st[int(d[0],16)] = (h,int(c)) | |
except: | |
pass | |
return st | |
def main(): | |
import os | |
s = MCStore("10.1.1.101:7900") | |
d = MCStore("10.1.1.87:7900") | |
lock_file_path = '/tmp/lsync.lock' | |
fd = os.open(lock_file_path, os.O_CREAT|os.O_RDWR, 0660) | |
try: | |
lock(fd) | |
st = stat(s) | |
for b, v in st.items(): | |
sdb_copy(d, s, "@%0x"%b) | |
finally: | |
os.close(fd) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment