blob: 5031869123b09982a9edae2ae64d0b4dca16b52f [file] [log] [blame]
Guido van Rossum07a272d1995-04-10 11:40:52 +00001"""Compare local and remote dictionaries and transfer differing files -- like rdist."""
2
3import sys
4from repr import repr
5import FSProxy
6import time
7import os
8
Neal Norwitzce96f692006-03-17 06:49:51 +00009def raw_input(prompt):
10 sys.stdout.write(prompt)
11 sys.stdout.flush()
12 return sys.stdin.readline()
13
Guido van Rossum07a272d1995-04-10 11:40:52 +000014def main():
Tim Peterse6ddc8b2004-07-18 05:56:09 +000015 pwd = os.getcwd()
16 s = raw_input("chdir [%s] " % pwd)
17 if s:
18 os.chdir(s)
19 pwd = os.getcwd()
20 host = ask("host", 'voorn.cwi.nl')
21 port = 4127
22 verbose = 1
23 mode = ''
24 print """\
Guido van Rossum07a272d1995-04-10 11:40:52 +000025Mode should be a string of characters, indicating what to do with differences.
26r - read different files to local file system
27w - write different files to remote file system
28c - create new files, either remote or local
29d - delete disappearing files, either remote or local
30"""
Tim Peterse6ddc8b2004-07-18 05:56:09 +000031 s = raw_input("mode [%s] " % mode)
32 if s: mode = s
33 address = (host, port)
34 t1 = time.time()
35 local = FSProxy.FSProxyLocal()
36 remote = FSProxy.FSProxyClient(address, verbose)
37 compare(local, remote, mode)
38 remote._close()
39 local._close()
40 t2 = time.time()
41 dt = t2-t1
42 mins, secs = divmod(dt, 60)
43 print mins, "minutes and", round(secs), "seconds"
44 raw_input("[Return to exit] ")
Guido van Rossum07a272d1995-04-10 11:40:52 +000045
46def ask(prompt, default):
Tim Peterse6ddc8b2004-07-18 05:56:09 +000047 s = raw_input("%s [%s] " % (prompt, default))
48 return s or default
Guido van Rossum07a272d1995-04-10 11:40:52 +000049
50def askint(prompt, default):
Tim Peterse6ddc8b2004-07-18 05:56:09 +000051 s = raw_input("%s [%s] " % (prompt, str(default)))
52 if s: return string.atoi(s)
53 return default
Guido van Rossum07a272d1995-04-10 11:40:52 +000054
55def compare(local, remote, mode):
Tim Peterse6ddc8b2004-07-18 05:56:09 +000056 print
57 print "PWD =", repr(os.getcwd())
58 sums_id = remote._send('sumlist')
59 subdirs_id = remote._send('listsubdirs')
60 remote._flush()
61 print "calculating local sums ..."
62 lsumdict = {}
63 for name, info in local.sumlist():
64 lsumdict[name] = info
65 print "getting remote sums ..."
66 sums = remote._recv(sums_id)
67 print "got", len(sums)
68 rsumdict = {}
69 for name, rsum in sums:
70 rsumdict[name] = rsum
71 if not lsumdict.has_key(name):
72 print repr(name), "only remote"
73 if 'r' in mode and 'c' in mode:
74 recvfile(local, remote, name)
75 else:
76 lsum = lsumdict[name]
77 if lsum != rsum:
78 print repr(name),
79 rmtime = remote.mtime(name)
80 lmtime = local.mtime(name)
81 if rmtime > lmtime:
82 print "remote newer",
83 if 'r' in mode:
84 recvfile(local, remote, name)
85 elif lmtime > rmtime:
86 print "local newer",
87 if 'w' in mode:
88 sendfile(local, remote, name)
89 else:
90 print "same mtime but different sum?!?!",
91 print
92 for name in lsumdict.keys():
93 if not rsumdict.keys():
94 print repr(name), "only locally",
95 fl()
96 if 'w' in mode and 'c' in mode:
97 sendfile(local, remote, name)
98 elif 'r' in mode and 'd' in mode:
99 os.unlink(name)
100 print "removed."
101 print
102 print "gettin subdirs ..."
103 subdirs = remote._recv(subdirs_id)
104 common = []
105 for name in subdirs:
106 if local.isdir(name):
107 print "Common subdirectory", repr(name)
108 common.append(name)
109 else:
110 print "Remote subdirectory", repr(name), "not found locally"
111 if 'r' in mode and 'c' in mode:
112 pr = "Create local subdirectory %s? [y] " % \
113 repr(name)
114 if 'y' in mode:
115 ok = 'y'
116 else:
117 ok = ask(pr, "y")
118 if ok[:1] in ('y', 'Y'):
119 local.mkdir(name)
120 print "Subdirectory %s made" % \
121 repr(name)
122 common.append(name)
123 lsubdirs = local.listsubdirs()
124 for name in lsubdirs:
125 if name not in subdirs:
126 print "Local subdirectory", repr(name), "not found remotely"
127 for name in common:
128 print "Entering subdirectory", repr(name)
129 local.cd(name)
130 remote.cd(name)
131 compare(local, remote, mode)
132 remote.back()
133 local.back()
Guido van Rossum07a272d1995-04-10 11:40:52 +0000134
135def sendfile(local, remote, name):
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000136 try:
137 remote.create(name)
Guido van Rossumb940e112007-01-10 16:19:56 +0000138 except (IOError, os.error) as msg:
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000139 print "cannot create:", msg
140 return
141
142 print "sending ...",
143 fl()
144
145 data = open(name).read()
146
147 t1 = time.time()
148
149 remote._send_noreply('write', name, data)
150 remote._flush()
151
152 t2 = time.time()
153
154 dt = t2-t1
155 print len(data), "bytes in", round(dt), "seconds",
156 if dt:
157 print "i.e.", round(len(data)/dt), "bytes/sec",
158 print
Guido van Rossum07a272d1995-04-10 11:40:52 +0000159
160def recvfile(local, remote, name):
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000161 ok = 0
162 try:
163 rv = recvfile_real(local, remote, name)
164 ok = 1
165 return rv
166 finally:
167 if not ok:
168 print "*** recvfile of %r failed, deleting" % (name,)
169 local.delete(name)
Guido van Rossumbec818a1995-04-26 22:57:12 +0000170
171def recvfile_real(local, remote, name):
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000172 try:
173 local.create(name)
Guido van Rossumb940e112007-01-10 16:19:56 +0000174 except (IOError, os.error) as msg:
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000175 print "cannot create:", msg
176 return
177
178 print "receiving ...",
179 fl()
180
181 f = open(name, 'w')
182 t1 = time.time()
183
184 length = 4*1024
185 offset = 0
186 id = remote._send('read', name, offset, length)
187 remote._flush()
188 while 1:
189 newoffset = offset + length
190 newid = remote._send('read', name, newoffset, length)
191 data = remote._recv(id)
192 id = newid
193 if not data: break
194 f.seek(offset)
195 f.write(data)
196 offset = newoffset
197 size = f.tell()
198
199 t2 = time.time()
200 f.close()
201
202 dt = t2-t1
203 print size, "bytes in", round(dt), "seconds",
204 if dt:
205 print "i.e.", int(size/dt), "bytes/sec",
206 print
207 remote._recv(id) # ignored
Guido van Rossum07a272d1995-04-10 11:40:52 +0000208
209def fl():
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000210 sys.stdout.flush()
Guido van Rossum07a272d1995-04-10 11:40:52 +0000211
212if __name__ == '__main__':
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000213 main()