Skip to content

Instantly share code, notes, and snippets.

@lwjef
Created November 15, 2011 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lwjef/1366347 to your computer and use it in GitHub Desktop.
Save lwjef/1366347 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from os import listdir, symlink, makedirs
from os.path import basename, join, isdir, isfile, islink, getsize, exists
def all_entry(entry):
return True
def to_bytes(size):
if size.endswith("K"):
return int(size[:-1]) * 1024
elif size.endwith("M"):
return int(size[:-1]) * 1024 * 1024
elif size.endwith("G"):
return int(size[:-1]) * 1024 * 1024 * 1024
else:
return int(size)
def bigger_than(size):
def inner(entry):
return getsize(entry) > to_bytes(size)
return inner
def vc_dir(entry):
return entry == ".git" or entry == ".svn"
def is_hidden(entry):
return basename(entry).startswith(".")
def has_exts(*exts):
def inner(entry):
return any(map(lambda ext:entry.endswith(ext), exts))
return inner
def a(*rules):
def inner(entry):
return all(map(lambda rule: rule(entry), rules))
return inner
def o(*rules):
def inner(entry):
return any(map(lambda rule: rule(entry), rules))
return inner
def n(rule):
def inner(entry):
return not(rule(entry))
return inner
def mirror(src, dest, dir_rule, file_rule):
def inner(src, dest):
for entry in listdir(src):
try:
cur_src = join(src, entry)
cur_dest = join(dest, entry)
if islink(cur_src):
continue
if isdir(cur_src) and dir_rule(cur_src) and not exists(cur_dest):
makedirs(cur_dest)
inner(cur_src, cur_dest)
continue
if isfile(cur_src) and file_rule(cur_src) and not exists(cur_dest) :
symlink(cur_src, cur_dest)
continue
except Exception as e:
print e
inner(src, dest)
# must edit by mannual
dropbox_dir="/home/yourname/Dropbox"
rules = [
("/home/yourname/workspace1", all_entry, all_entry),
("/home/yourname/workspace2", n(is_hidden), all_entry),
("/home/yourname/workspace3", n(vc_dir), has_exts(".txt", ".py")),
("/home/yourname/workspace4", n(vc_dir), a(has_exts(".log"), n(bigger_than("500M"))))
]
if __name__ == '__main__':
for src, dir_rule, file_rule in rules:
mirror(src, join(dropbox_dir, basename(src)), dir_rule, file_rule)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment