-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncdirs.py
More file actions
executable file
·195 lines (170 loc) · 6.11 KB
/
syncdirs.py
File metadata and controls
executable file
·195 lines (170 loc) · 6.11 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/python
import sys, os, time, shutil, getopt, errno, traceback
class DirSyncFS(object):
def __init__(self, dryrun=True):
self.dryrun = dryrun
def copy(self, src, dst):
if not self.dryrun:
try:
shutil.copy2(src, dst)
except OSError as e:
if e.errno != errno.EOPNOTSUPP:
raise
def copystat(self, src, dst):
if not self.dryrun:
try:
shutil.copystat(src, dst)
except OSError as e:
if e.errno != errno.EOPNOTSUPP:
raise
def mkdir(self, path):
if not self.dryrun:
try:
os.makedirs(path)
except OSError as e:
if e.errno != errno.EEXIST:
raise
def rmdir(self, path):
if not self.dryrun:
os.rmdir(path)
def remove(self, path):
if not self.dryrun:
os.remove(path)
def walk(self, path):
try:
names = os.listdir(path)
except EnvironmentError:
return
names.sort()
dirnames = []
filenames = []
for name in names:
if os.path.isdir(os.path.join(path, name)):
dirnames.append(name)
else:
filenames.append(name)
yield path, dirnames, filenames
for name in dirnames:
for subpath, dirnames, filenames in os.walk(os.path.join(path, name), topdown=False):
dirnames.sort()
filenames.sort()
yield subpath, dirnames, filenames
def stat_changed(src, dst):
# Allow 2 second error for rubbish filesystems
# and 1 hour difference for unadjusted DST
time_diff = abs(src.st_mtime - dst.st_mtime)
return ((2.0 < time_diff < 3599.0)
or (time_diff > 3601.0)
or src.st_size != dst.st_size)
def relpath(path, root):
rpath = os.path.relpath(path, root)
return "" if rpath == "." else rpath
class DirSyncer(object):
def __init__(self, srcroot, dstroot, dryrun=True, logfile=None):
self.srcroot = srcroot
self.dstroot = dstroot
self.fs = DirSyncFS(dryrun=dryrun)
self.logfile = logfile
self.loglines = []
def log_write(self, s):
if self.logfile:
self.loglines.append(s)
def log(self, *args):
s = " ".join(str(arg) for arg in args) + "\n"
self.log_write(s)
encoding = sys.stdout.encoding
sys.stdout.write(s.encode(encoding, "replace").decode(encoding))
def log_marker(self, s):
self.log_write("==== [{0}] {1}\n".format(get_timestamp(), s))
def visit_cleanup(self, dstdir, filenames):
dir = relpath(dstdir, self.dstroot)
srcdir = os.path.join(self.srcroot, dir)
for filename in filenames:
srcfile = os.path.join(srcdir, filename)
dstfile = os.path.join(dstdir, filename)
if os.path.isfile(dstfile) and not os.path.isfile(srcfile):
self.log("REMOVE", os.path.join(dir, filename))
self.fs.remove(dstfile)
if not os.path.isdir(srcdir):
self.log("RMDIR", dir)
self.fs.rmdir(dstdir)
def visit_copy(self, srcdir, filenames):
dir = relpath(srcdir, self.srcroot)
dstdir = os.path.join(self.dstroot, dir)
if os.path.exists(dstdir):
if not os.path.isdir(dstdir):
self.log("REMOVE", dir)
self.fs.remove(dstdir)
self.log("MKDIR", dir)
self.fs.mkdir(dstdir)
else:
self.log("MKDIR", dir)
self.fs.mkdir(dstdir)
self.fs.copystat(srcdir, dstdir)
for filename in filenames:
srcfile = os.path.join(srcdir, filename)
dstfile = os.path.join(dstdir, filename)
if os.path.isfile(srcfile):
will_copy = not os.path.exists(dstfile)
if not will_copy:
srcstat = os.stat(srcfile)
dststat = os.stat(dstfile)
will_copy = stat_changed(srcstat, dststat)
if will_copy:
self.log("COPY", os.path.join(dir, filename))
self.fs.copy(srcfile, dstfile)
def run(self):
self.log_marker("START src={0} dst={1!r}{2}".format(
self.srcroot, self.dstroot, " dryrun" if self.fs.dryrun else ""))
try:
for dirpath, dirnames, filenames in self.fs.walk(self.dstroot):
self.visit_cleanup(dirpath, dirnames + filenames)
for dirpath, dirnames, filenames in self.fs.walk(self.srcroot):
self.visit_copy(dirpath, dirnames + filenames)
except:
print(traceback.format_exc())
self.log_write(traceback.format_exc())
self.log_marker("ABORT\n")
raise
else:
self.log_marker("FINISH\n")
if self.logfile:
with open(self.logfile, "a") as f:
f.write("".join(self.loglines))
self.loglines = []
def get_timestamp():
return time.strftime("%Y-%m-%d %H:%M:%S")
def syncdirs(srcroot, dstroot, dryrun=True, logfile=None):
dirsync = DirSyncer(srcroot, dstroot, dryrun=dryrun, logfile=logfile)
dirsync.run()
usage = "usage: %s [--commit] [--logfile=LOGFILE] SRCDIR DSTDIR" % sys.argv[0]
def main(args):
try:
opts, args = getopt.gnu_getopt(args, "", ["commit", "logfile="])
except getopt.GetoptError as e:
print(str(e) + "\n\n" + usage)
sys.exit(2)
if len(args) != 2:
print(usage)
sys.exit(2)
src, dst = args
dryrun = True
logfile = None
for o, a in opts:
if o == "--commit":
dryrun = False
elif o == "--logfile":
logfile = a
else:
print("Unknown option:", o)
print(usage)
sys.exit(2)
try:
syncdirs(src, dst, dryrun=dryrun, logfile=logfile)
except SystemExit:
raise
except:
print(sys.exc_info()[1])
sys.exit(1)
if __name__ == "__main__":
main(sys.argv[1:])