| 1 |
|
| 2 |
"""xrsync.py -- a small, runnable rsync client built on rsync_proto.py. |
| 3 |
|
| 4 |
It speaks the rsync daemon protocol (host::module/path or rsync://host/module/ |
| 5 |
path) and supports three operations against a real rsyncd: |
| 6 |
|
| 7 |
xrsync.py [opts] host::module/path # list (like rsync's listing) |
| 8 |
xrsync.py [opts] host::module/glob localdir # pull (download) |
| 9 |
xrsync.py [opts] localfile... host::mod/ # push (upload, regular files) |
| 10 |
|
| 11 |
This is deliberately a *subset* of rsync -- enough to be useful for protocol |
| 12 |
development and as a test harness, not a drop-in replacement. Supported flags: |
| 13 |
-a (= -rlpt), -r, -l, -t, -p, -v, --list-only, --port=N. |
| 14 |
|
| 15 |
Hookability: rsync_proto's DaemonClient exposes the protocol steps as small |
| 16 |
overridable methods (recv_flist / make_request / recv_file_transfer / |
| 17 |
make_file_token_stream / ...). main() takes a `client_factory`, so a test can |
| 18 |
pass a DaemonClient subclass that tampers with one step while reusing all of |
| 19 |
xrsync's machinery. See xrsync_test.py. |
| 20 |
""" |
| 21 |
|
| 22 |
import argparse |
| 23 |
import os |
| 24 |
import sys |
| 25 |
|
| 26 |
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
| 27 |
import rsync_proto as rp |
| 28 |
|
| 29 |
DEFAULT_PORT = 873 |
| 30 |
CAPS = 'e.LsfxCIu' |
| 31 |
|
| 32 |
|
| 33 |
def parse_remote(spec): |
| 34 |
"""Return (host, port, path) for a daemon spec, or None if `spec` is local. |
| 35 |
`path` keeps the module as its first component (what the daemon expects).""" |
| 36 |
if spec.startswith('rsync://'): |
| 37 |
rest = spec[len('rsync://'):] |
| 38 |
hostport, _, path = rest.partition('/') |
| 39 |
host, _, port = hostport.partition(':') |
| 40 |
return host, int(port) if port else DEFAULT_PORT, path |
| 41 |
if '::' in spec: |
| 42 |
host, _, path = spec.partition('::') |
| 43 |
return host, None, path |
| 44 |
return None |
| 45 |
|
| 46 |
|
| 47 |
def server_opts(args, listing): |
| 48 |
"""Build the daemon-side short-option string from the parsed flags.""" |
| 49 |
flags = '' |
| 50 |
if args.recurse: |
| 51 |
flags += 'r' |
| 52 |
if args.links: |
| 53 |
flags += 'l' |
| 54 |
if args.perms: |
| 55 |
flags += 'p' |
| 56 |
if args.times: |
| 57 |
flags += 't' |
| 58 |
if listing and not args.recurse: |
| 59 |
flags += 'd' |
| 60 |
return '-' + flags + CAPS |
| 61 |
|
| 62 |
|
| 63 |
def do_list(host, port, module, path, args, factory): |
| 64 |
c = factory(host, port) |
| 65 |
c.handshake(module, ['--server', '--sender', server_opts(args, True), |
| 66 |
'.', path], greeting_version=30) |
| 67 |
entries = rp.sort_entries(c.recv_flist(preserve_links=args.links)) |
| 68 |
for e in entries: |
| 69 |
tgt = '' |
| 70 |
if e.is_link and e.link_target is not None: |
| 71 |
tgt = ' -> ' + e.link_target.decode('utf-8', 'surrogateescape') |
| 72 |
print('%s %15d %s%s' % (rp.mode_to_perms(e.mode), e.length, |
| 73 |
e.name.decode('utf-8', 'surrogateescape'), tgt)) |
| 74 |
c.finish_no_transfer() |
| 75 |
c.drain(timeout=1.0) |
| 76 |
c.close() |
| 77 |
return 0 |
| 78 |
|
| 79 |
|
| 80 |
def do_pull(host, port, module, path, dest, args, factory): |
| 81 |
c = factory(host, port) |
| 82 |
c.handshake(module, ['--server', '--sender', server_opts(args, False), |
| 83 |
'.', path], greeting_version=30) |
| 84 |
c.pull(dest, verbose=args.verbose, preserve_times=args.times, |
| 85 |
preserve_perms=args.perms) |
| 86 |
c.drain(timeout=1.0) |
| 87 |
c.close() |
| 88 |
return 0 |
| 89 |
|
| 90 |
|
| 91 |
def do_push(srcs, host, port, module, path, args, factory): |
| 92 |
files = [] |
| 93 |
for s in srcs: |
| 94 |
if os.path.isfile(s): |
| 95 |
with open(s, 'rb') as fh: |
| 96 |
files.append((os.path.basename(s), fh.read())) |
| 97 |
elif os.path.isdir(s): |
| 98 |
for root, _dirs, names in os.walk(s): |
| 99 |
for nm in names: |
| 100 |
full = os.path.join(root, nm) |
| 101 |
if os.path.isfile(full) and not os.path.islink(full): |
| 102 |
rel = os.path.relpath(full, s) |
| 103 |
with open(full, 'rb') as fh: |
| 104 |
files.append((rel, fh.read())) |
| 105 |
c = factory(host, port) |
| 106 |
c.handshake(module, ['--server', server_opts(args, False), '.', path], |
| 107 |
greeting_version=30) |
| 108 |
c.push(files) |
| 109 |
if args.verbose: |
| 110 |
for name, _ in files: |
| 111 |
print(name) |
| 112 |
c.drain(timeout=1.0) |
| 113 |
c.close() |
| 114 |
return 0 |
| 115 |
|
| 116 |
|
| 117 |
def main(argv=None, client_factory=rp.DaemonClient): |
| 118 |
p = argparse.ArgumentParser(prog='xrsync.py', add_help=True) |
| 119 |
p.add_argument('-a', '--archive', action='store_true', help='= -rlpt') |
| 120 |
p.add_argument('-r', '--recursive', dest='recurse', action='store_true') |
| 121 |
p.add_argument('-l', '--links', action='store_true') |
| 122 |
p.add_argument('-p', '--perms', action='store_true') |
| 123 |
p.add_argument('-t', '--times', action='store_true') |
| 124 |
p.add_argument('-v', '--verbose', action='store_true') |
| 125 |
p.add_argument('--list-only', action='store_true') |
| 126 |
p.add_argument('--port', type=int, default=None) |
| 127 |
p.add_argument('paths', nargs='+') |
| 128 |
args = p.parse_args(argv) |
| 129 |
if args.archive: |
| 130 |
args.recurse = args.links = args.perms = args.times = True |
| 131 |
|
| 132 |
paths = args.paths |
| 133 |
remotes = [parse_remote(x) for x in paths] |
| 134 |
|
| 135 |
|
| 136 |
if args.list_only or (len(paths) == 1 and remotes[0] is not None): |
| 137 |
host, port, path = remotes[0] |
| 138 |
port = args.port or port or DEFAULT_PORT |
| 139 |
module = path.split('/')[0] |
| 140 |
return do_list(host, port, module, path, args, client_factory) |
| 141 |
|
| 142 |
if len(paths) < 2: |
| 143 |
p.error('need a source and a destination') |
| 144 |
*srcs, dest = paths |
| 145 |
*src_remotes, dest_remote = remotes |
| 146 |
|
| 147 |
if dest_remote is not None and all(r is None for r in src_remotes): |
| 148 |
host, port, path = dest_remote |
| 149 |
port = args.port or port or DEFAULT_PORT |
| 150 |
module = path.split('/')[0] |
| 151 |
return do_push(srcs, host, port, module, path, args, client_factory) |
| 152 |
|
| 153 |
if len(srcs) == 1 and src_remotes[0] is not None and dest_remote is None: |
| 154 |
host, port, path = src_remotes[0] |
| 155 |
port = args.port or port or DEFAULT_PORT |
| 156 |
module = path.split('/')[0] |
| 157 |
return do_pull(host, port, module, path, dest, args, client_factory) |
| 158 |
|
| 159 |
p.error('unsupported source/destination combination (one side must be ' |
| 160 |
'host::module/path and the other local)') |
| 161 |
|
| 162 |
|
| 163 |
if __name__ == '__main__': |
| 164 |
sys.exit(main()) |