-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.py
More file actions
executable file
·102 lines (87 loc) · 2.45 KB
/
sync.py
File metadata and controls
executable file
·102 lines (87 loc) · 2.45 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
import argparse
import getpass
import os
import subprocess
import syncfile
import sys
import target
def parse_args():
descr = "Sync stuff - a frontend for unison and rsync."
p = argparse.ArgumentParser(description = descr)
p.add_argument("syncfile", type = argparse.FileType("r"))
p.add_argument("target", type = str)
tofrom = p.add_mutually_exclusive_group(required = False)
tofrom.add_argument("--oneway", "-o", action = "store_true")
return p.parse_args()
# Print a command (array of strings) out in such a way that
# it can be copied-and-pasted back into the shell.
def printCommand(cmd, fp = sys.stdout):
i = 0
while i < len(cmd):
s = cmd[i]
if " " in s or "\t" in s:
s = "'" + s + "'"
fp.write(s)
if i < len(cmd) - 1:
fp.write(" ")
i += 1
fp.write("\n")
def addFollows(p):
cmd = []
if os.path.isdir(p) or os.path.islink(p):
cmd += ["-follow", "Path " + p]
cmd += addFollows(os.path.dirname(p))
return cmd
def unison(t1, t2, sf):
cmd = ["unison", t1.unison(), t2.unison()]
cmd += ["-dumbtty"]
cmd += ["-times=false"]
cmd += ["-auto"]
cmd += ["-perms=0"]
cmd += ["-fastcheck=true"]
for i in sf.glob(t1.unison()):
cmd += ["-path", i]
cmd += addFollows(i)
for i in sf.ignore_paths:
cmd += ["-ignore", "Path " + i]
for i in sf.ignore_names:
cmd += ["-ignore", "Name " + i]
printCommand(cmd)
print()
try:
subprocess.call(cmd)
except KeyboardInterrupt:
sys.exit(1)
def rsync(src, dst, sf):
cmd = ["rsync", "-avuRL", "--modify-window=1", "--progress", "--delete"]
cmd += ["--delete-excluded"]
for i in sf.glob(src.rsync()):
print(i)
cmd += [i]
for i in sf.ignore_paths:
cmd += ["--exclude", i]
for i in sf.ignore_names:
cmd += ["--exclude", i]
cmd += [dst.rsync()]
printCommand(cmd)
try:
subprocess.call(cmd)
except KeyboardInterrupt:
sys.exit(1)
def main():
args = parse_args()
sf = syncfile.SyncFile(args.syncfile)
t2 = target.parse(args.target)
sf.display()
print()
print("Target:", t2)
print()
first_root = os.path.join("/home", getpass.getuser())
t1 = target.parse(first_root)
if not args.oneway:
unison(t1, t2, sf)
else: # args.oneway
rsync(t1, t2, sf)
if __name__ == "__main__":
main()