Guido van Rossum | 07a272d | 1995-04-10 11:40:52 +0000 | [diff] [blame] | 1 | """Compare local and remote dictionaries and transfer differing files -- like rdist.""" |
| 2 | |
| 3 | import sys |
| 4 | from repr import repr |
| 5 | import FSProxy |
| 6 | import time |
| 7 | import os |
| 8 | |
Neal Norwitz | ce96f69 | 2006-03-17 06:49:51 +0000 | [diff] [blame] | 9 | def raw_input(prompt): |
| 10 | sys.stdout.write(prompt) |
| 11 | sys.stdout.flush() |
| 12 | return sys.stdin.readline() |
| 13 | |
Guido van Rossum | 07a272d | 1995-04-10 11:40:52 +0000 | [diff] [blame] | 14 | def main(): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 15 | pwd = os.getcwd() |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 16 | s = input("chdir [%s] " % pwd) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 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 = '' |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 24 | print("""\ |
Guido van Rossum | 07a272d | 1995-04-10 11:40:52 +0000 | [diff] [blame] | 25 | Mode should be a string of characters, indicating what to do with differences. |
| 26 | r - read different files to local file system |
| 27 | w - write different files to remote file system |
| 28 | c - create new files, either remote or local |
| 29 | d - delete disappearing files, either remote or local |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 30 | """) |
| 31 | s = input("mode [%s] " % mode) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 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) |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 43 | print(mins, "minutes and", round(secs), "seconds") |
| 44 | input("[Return to exit] ") |
Guido van Rossum | 07a272d | 1995-04-10 11:40:52 +0000 | [diff] [blame] | 45 | |
| 46 | def ask(prompt, default): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 47 | s = input("%s [%s] " % (prompt, default)) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 48 | return s or default |
Guido van Rossum | 07a272d | 1995-04-10 11:40:52 +0000 | [diff] [blame] | 49 | |
| 50 | def askint(prompt, default): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 51 | s = input("%s [%s] " % (prompt, str(default))) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 52 | if s: return string.atoi(s) |
| 53 | return default |
Guido van Rossum | 07a272d | 1995-04-10 11:40:52 +0000 | [diff] [blame] | 54 | |
| 55 | def compare(local, remote, mode): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 56 | print() |
| 57 | print("PWD =", repr(os.getcwd())) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 58 | sums_id = remote._send('sumlist') |
| 59 | subdirs_id = remote._send('listsubdirs') |
| 60 | remote._flush() |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 61 | print("calculating local sums ...") |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 62 | lsumdict = {} |
| 63 | for name, info in local.sumlist(): |
| 64 | lsumdict[name] = info |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 65 | print("getting remote sums ...") |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 66 | sums = remote._recv(sums_id) |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 67 | print("got", len(sums)) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 68 | rsumdict = {} |
| 69 | for name, rsum in sums: |
| 70 | rsumdict[name] = rsum |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 71 | if name not in lsumdict: |
| 72 | print(repr(name), "only remote") |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 73 | if 'r' in mode and 'c' in mode: |
| 74 | recvfile(local, remote, name) |
| 75 | else: |
| 76 | lsum = lsumdict[name] |
| 77 | if lsum != rsum: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 78 | print(repr(name), end=' ') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 79 | rmtime = remote.mtime(name) |
| 80 | lmtime = local.mtime(name) |
| 81 | if rmtime > lmtime: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 82 | print("remote newer", end=' ') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 83 | if 'r' in mode: |
| 84 | recvfile(local, remote, name) |
| 85 | elif lmtime > rmtime: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 86 | print("local newer", end=' ') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 87 | if 'w' in mode: |
| 88 | sendfile(local, remote, name) |
| 89 | else: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 90 | print("same mtime but different sum?!?!", end=' ') |
| 91 | print() |
| 92 | for name in list(lsumdict.keys()): |
| 93 | if not list(rsumdict.keys()): |
| 94 | print(repr(name), "only locally", end=' ') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 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) |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 100 | print("removed.") |
| 101 | print() |
| 102 | print("gettin subdirs ...") |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 103 | subdirs = remote._recv(subdirs_id) |
| 104 | common = [] |
| 105 | for name in subdirs: |
| 106 | if local.isdir(name): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 107 | print("Common subdirectory", repr(name)) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 108 | common.append(name) |
| 109 | else: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 110 | print("Remote subdirectory", repr(name), "not found locally") |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 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) |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 120 | print("Subdirectory %s made" % \ |
| 121 | repr(name)) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 122 | common.append(name) |
| 123 | lsubdirs = local.listsubdirs() |
| 124 | for name in lsubdirs: |
| 125 | if name not in subdirs: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 126 | print("Local subdirectory", repr(name), "not found remotely") |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 127 | for name in common: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 128 | print("Entering subdirectory", repr(name)) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 129 | local.cd(name) |
| 130 | remote.cd(name) |
| 131 | compare(local, remote, mode) |
| 132 | remote.back() |
| 133 | local.back() |
Guido van Rossum | 07a272d | 1995-04-10 11:40:52 +0000 | [diff] [blame] | 134 | |
| 135 | def sendfile(local, remote, name): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 136 | try: |
| 137 | remote.create(name) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 138 | except (IOError, os.error) as msg: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 139 | print("cannot create:", msg) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 140 | return |
| 141 | |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 142 | print("sending ...", end=' ') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 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 |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 155 | print(len(data), "bytes in", round(dt), "seconds", end=' ') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 156 | if dt: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 157 | print("i.e.", round(len(data)/dt), "bytes/sec", end=' ') |
| 158 | print() |
Guido van Rossum | 07a272d | 1995-04-10 11:40:52 +0000 | [diff] [blame] | 159 | |
| 160 | def recvfile(local, remote, name): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 161 | ok = 0 |
| 162 | try: |
| 163 | rv = recvfile_real(local, remote, name) |
| 164 | ok = 1 |
| 165 | return rv |
| 166 | finally: |
| 167 | if not ok: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 168 | print("*** recvfile of %r failed, deleting" % (name,)) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 169 | local.delete(name) |
Guido van Rossum | bec818a | 1995-04-26 22:57:12 +0000 | [diff] [blame] | 170 | |
| 171 | def recvfile_real(local, remote, name): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 172 | try: |
| 173 | local.create(name) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 174 | except (IOError, os.error) as msg: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 175 | print("cannot create:", msg) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 176 | return |
| 177 | |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 178 | print("receiving ...", end=' ') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 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 |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 203 | print(size, "bytes in", round(dt), "seconds", end=' ') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 204 | if dt: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 205 | print("i.e.", int(size/dt), "bytes/sec", end=' ') |
| 206 | print() |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 207 | remote._recv(id) # ignored |
Guido van Rossum | 07a272d | 1995-04-10 11:40:52 +0000 | [diff] [blame] | 208 | |
| 209 | def fl(): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 210 | sys.stdout.flush() |
Guido van Rossum | 07a272d | 1995-04-10 11:40:52 +0000 | [diff] [blame] | 211 | |
| 212 | if __name__ == '__main__': |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 213 | main() |