blob: 84ed3f0b1911b55282f5f0ba54a57ae808eb3fe3 [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
9def main():
10 pwd = os.getcwd()
11 s = raw_input("chdir [%s] " % pwd)
12 if s:
13 os.chdir(s)
14 pwd = os.getcwd()
15 host = ask("host", 'voorn.cwi.nl')
16 port = 4127
17 verbose = 1
18 mode = ''
19 print """\
20Mode should be a string of characters, indicating what to do with differences.
21r - read different files to local file system
22w - write different files to remote file system
23c - create new files, either remote or local
24d - delete disappearing files, either remote or local
25"""
26 s = raw_input("mode [%s] " % mode)
27 if s: mode = s
28 address = (host, port)
29 t1 = time.time()
30 local = FSProxy.FSProxyLocal()
31 remote = FSProxy.FSProxyClient(address, verbose)
32 compare(local, remote, mode)
33 remote._close()
34 local._close()
35 t2 = time.time()
36 dt = t2-t1
37 mins, secs = divmod(dt, 60)
38 print mins, "minutes and", secs, "seconds"
39 raw_input("[Return to exit] ")
40
41def ask(prompt, default):
42 s = raw_input("%s [%s] " % (prompt, default))
43 return s or default
44
45def askint(prompt, default):
46 s = raw_input("%s [%s] " % (prompt, str(default)))
47 if s: return string.atoi(s)
48 return default
49
50def compare(local, remote, mode):
51 print
52 print "PWD =", `os.getcwd()`
53 sums_id = remote._send('sumlist')
54 subdirs_id = remote._send('listsubdirs')
55 remote._flush()
56 print "calculating local sums ..."
57 lsumdict = {}
58 for name, info in local.sumlist():
59 lsumdict[name] = info
60 print "getting remote sums ..."
61 sums = remote._recv(sums_id)
62 print "got", len(sums)
63 rsumdict = {}
64 for name, rsum in sums:
65 rsumdict[name] = rsum
66 if not lsumdict.has_key(name):
67 print `name`, "only remote"
68 if 'r' in mode and 'c' in mode:
69 recvfile(local, remote, name)
70 else:
71 lsum = lsumdict[name]
72 if lsum != rsum:
73 print `name`,
74 rmtime = remote.mtime(name)
75 lmtime = local.mtime(name)
76 if rmtime > lmtime:
77 print "remote newer",
78 if 'r' in mode:
79 recvfile(local, remote, name)
80 elif lmtime > rmtime:
81 print "local newer",
82 if 'w' in mode:
83 sendfile(local, remote, name)
84 else:
85 print "same mtime but different sum?!?!",
86 print
87 for name in lsumdict.keys():
88 if not rsumdict.keys():
89 print `name`, "only locally",
90 fl()
91 if 'w' in mode and 'c' in mode:
92 sendfile(local, remote, name)
93 elif 'r' in mode and 'd' in mode:
94 os.unlink(name)
95 print "removed."
96 print
97 print "gettin subdirs ..."
98 subdirs = remote._recv(subdirs_id)
99 common = []
100 for name in subdirs:
101 if local.isdir(name):
102 print "Common subdirectory", repr(name)
103 common.append(name)
104 else:
105 print "Remote subdirectory", repr(name), "not found locally"
106 lsubdirs = local.listsubdirs()
107 for name in lsubdirs:
108 if name not in subdirs:
109 print "Local subdirectory", repr(name), "not found remotely"
110 for name in common:
111 print "Entering subdirectory", repr(name)
112 local.cd(name)
113 remote.cd(name)
114 compare(local, remote, mode)
115 remote.back()
116 local.back()
117
118def sendfile(local, remote, name):
119 try:
120 remote.create(name)
121 except (IOError, os.error), msg:
122 print "cannot create:", msg
123 return
124
125 print "sending ...",
126 fl()
127
128 data = open(name).read()
129
130 t1 = time.time()
131
132 remote._send_noreply('write', name, data)
133 remote._flush()
134
135 t2 = time.time()
136
137 dt = t2-t1
138 print len(data), "bytes in", t2-t1, "seconds",
139 if dt:
140 print "i.e.", len(data)/dt, "bytes/sec",
141 print
142
143def recvfile(local, remote, name):
144 try:
145 local.create(name)
146 except (IOError, os.error), msg:
147 print "cannot create:", msg
148 return
149
150 print "receiving ...",
151 fl()
152
153 f = open(name, 'w')
154 t1 = time.time()
155
156 length = 4*1024
157 offset = 0
158 id = remote._send('read', name, offset, length)
159 remote._flush()
160 while 1:
161 newoffset = offset + length
162 newid = remote._send('read', name, newoffset, length)
163 data = remote._recv(id)
164 id = newid
165 if not data: break
166 f.seek(offset)
167 f.write(data)
168 offset = newoffset
169 size = f.tell()
170
171 t2 = time.time()
172 f.close()
173
174 dt = t2-t1
175 print size, "bytes in", dt, "seconds",
176 if dt:
177 print "i.e.", int(size/dt), "bytes/sec",
178 print
179 remote._recv(id) # ignored
180
181def fl():
182 sys.stdout.flush()
183
184if __name__ == '__main__':
185 main()