/
rsync_utils.py
85 lines (63 loc) · 2.7 KB
/
rsync_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os, rwkos
filesep = os.path.sep
src_base = '/Users/kraussry'
exclude_from_str = '--exclude-from=/Users/kraussry/scripts/rsync_excludes.txt'
def check_src(src_in):
assert src_in.find(src_base) == 0, 'bad src: %s' % src_in
def check_dst(dst_in):
assert dst_in.find('/Volumes/') == 0, 'bad dst: %s' % dst_in
def rsync_one(src, dst, flags=['-rv',exclude_from_str], \
droptrailingslash=True, verify_dst=True):
check_src(src)
if verify_dst:
check_dst(dst)
if droptrailingslash and src[-1] == filesep:
src = src[0:-1]
flag_str = ' '.join(flags)
cmd = 'rsync %s %s %s' % (flag_str, src, dst)
print('================')
print(cmd)
print('================')
os.system(cmd)
class rsync_runner(object):
def __init__(self, src_root, dst_root, dst_folder='', relpaths=[], \
flags=['-rv',exclude_from_str,"--exclude='.git/'"]):
self.src_root = os.path.expanduser(src_root)
check_src(self.src_root)
self.dst_root = dst_root
check_dst(self.dst_root)
self.dst_folder = dst_folder
self.relpaths = relpaths
self.flags = flags
def make_dst_dirs(self):
if self.dst_folder:
self.dst_path = os.path.join(self.dst_root, self.dst_folder)
else:
self.dst_path = self.dst_root
rwkos.make_dirs_recrusive(self.dst_path)
for item in self.relpaths:
# - if the src is a file, pop the filename
# - make the dest folders recursively
#####################
##if item.find(filesep) > -1:
## # - do nothing if item doesn't contain a filesep
#####################
#recursively make folders
src_path = os.path.join(self.src_root, item)
cur_dest = os.path.join(self.dst_path, item)
if os.path.isfile(src_path):
#this is a file path, pop the file name to get the dir path
cur_dest, cur_file = os.path.split(cur_dest)
#make the directory structure if it doesn't exist
if not os.path.exists(cur_dest):
rwkos.make_dirs_recrusive(cur_dest)
def run_rsync(self):
if not hasattr(self, 'dst_path'):
self.make_dst_dirs()
for item in self.relpaths:
src_path = os.path.join(self.src_root, item)
cur_dest = os.path.join(self.dst_path, item)
if os.path.isdir(src_path):
#pop last folder
cur_dest, folder = os.path.split(cur_dest)
rsync_one(src_path, cur_dest, flags=self.flags)