| 1 |
|
| 2 |
"""Generate a "variety tree" for manual inspection and ad-hoc rsync testing. |
| 3 |
|
| 4 |
Builds exactly the tree that rsyncfns.make_variety_tree() produces for the |
| 5 |
regression suite (every inode type rsync handles, with heavy symlink, perm, |
| 6 |
xattr, ACL, hard-link and -- as root -- ownership coverage), so you can drive |
| 7 |
rsync at it by hand. Transfer the transfer_root/ subdir; the escape/ links |
| 8 |
deliberately point up into the sibling above/ tree. |
| 9 |
|
| 10 |
Examples: |
| 11 |
testsuite/mkvariety.py /tmp/vt |
| 12 |
testsuite/mkvariety.py /tmp/vt --list |
| 13 |
sudo testsuite/mkvariety.py /tmp/vt # adds device nodes + owners |
| 14 |
testsuite/mkvariety.py /tmp/vt --no-acls --depth 4 |
| 15 |
|
| 16 |
rsync -aHAX --specials --devices /tmp/vt/transfer_root/ /tmp/copy/ |
| 17 |
rsync -a --safe-links /tmp/vt/transfer_root/ /tmp/copy2/ |
| 18 |
""" |
| 19 |
|
| 20 |
import argparse |
| 21 |
import os |
| 22 |
import stat |
| 23 |
import sys |
| 24 |
from pathlib import Path |
| 25 |
|
| 26 |
_HERE = os.path.dirname(os.path.abspath(__file__)) |
| 27 |
|
| 28 |
|
| 29 |
def _resolve_rsync(opt): |
| 30 |
"""Find an rsync binary to probe xattr/ACL capability with.""" |
| 31 |
if opt: |
| 32 |
return os.path.abspath(opt) |
| 33 |
cand = os.path.join(os.path.dirname(_HERE), 'rsync') |
| 34 |
if os.path.exists(cand): |
| 35 |
return cand |
| 36 |
from shutil import which |
| 37 |
return which('rsync') or 'rsync' |
| 38 |
|
| 39 |
|
| 40 |
def _detect(flag, prober, default): |
| 41 |
if flag is not None: |
| 42 |
return flag |
| 43 |
try: |
| 44 |
return prober() |
| 45 |
except Exception: |
| 46 |
return default |
| 47 |
|
| 48 |
|
| 49 |
def list_tree(root): |
| 50 |
rootp = Path(root) |
| 51 |
for dp, dns, fns in os.walk(root): |
| 52 |
dns.sort() |
| 53 |
for name in sorted(dns + fns): |
| 54 |
p = Path(dp) / name |
| 55 |
m = p.lstat().st_mode |
| 56 |
tgt = ' -> ' + os.readlink(p) if stat.S_ISLNK(m) else '' |
| 57 |
print(f"{stat.filemode(m)} {p.relative_to(rootp)}{tgt}") |
| 58 |
|
| 59 |
|
| 60 |
def main(): |
| 61 |
ap = argparse.ArgumentParser( |
| 62 |
description=__doc__, |
| 63 |
formatter_class=argparse.RawDescriptionHelpFormatter) |
| 64 |
ap.add_argument('target', help='directory to create (REMOVED first if it exists)') |
| 65 |
ap.add_argument('--depth', type=int, default=8, help='backbone depth (default 8)') |
| 66 |
ap.add_argument('--seed', default='0x5A17', |
| 67 |
help='int seed for the fixed choices (default 0x5A17)') |
| 68 |
ap.add_argument('--rsync', default=None, |
| 69 |
help='rsync binary for xattr/ACL capability probes ' |
| 70 |
'(default: ./rsync in the build tree, else PATH rsync)') |
| 71 |
ap.add_argument('--list', action='store_true', |
| 72 |
help='print a recursive listing of the tree afterwards') |
| 73 |
for cap, helptext in (('xattrs', 'user xattrs'), ('acls', 'POSIX ACLs'), |
| 74 |
('devices', 'char/block device nodes (needs root)'), |
| 75 |
('owners', 'mixed uid/gid (needs root)')): |
| 76 |
g = ap.add_mutually_exclusive_group() |
| 77 |
g.add_argument(f'--{cap}', dest=cap, action='store_true', default=None, |
| 78 |
help=f'force {helptext} on') |
| 79 |
g.add_argument(f'--no-{cap}', dest=cap, action='store_false', |
| 80 |
help=f'force {helptext} off') |
| 81 |
args = ap.parse_args() |
| 82 |
|
| 83 |
target = Path(args.target).resolve() |
| 84 |
for bad in (Path('/'), Path.home(), Path.cwd()): |
| 85 |
if target == bad: |
| 86 |
ap.error(f"refusing to use {target} as the target (it is wiped first)") |
| 87 |
|
| 88 |
rsync = _resolve_rsync(args.rsync) |
| 89 |
|
| 90 |
|
| 91 |
os.environ.setdefault('scratchdir', str(target.parent)) |
| 92 |
os.environ.setdefault('srcdir', os.getcwd()) |
| 93 |
os.environ.setdefault('TOOLDIR', os.path.dirname(rsync) or os.getcwd()) |
| 94 |
os.environ.setdefault('RSYNC', rsync) |
| 95 |
sys.path.insert(0, _HERE) |
| 96 |
import rsyncfns as R |
| 97 |
|
| 98 |
caps = dict( |
| 99 |
with_xattrs=_detect(args.xattrs, R.xattrs_supported, False), |
| 100 |
with_acls=_detect(args.acls, R.acls_supported, False), |
| 101 |
with_devices=args.devices if args.devices is not None |
| 102 |
else R.devices_supported(), |
| 103 |
with_owners=args.owners if args.owners is not None |
| 104 |
else R.owners_supported(), |
| 105 |
) |
| 106 |
|
| 107 |
info = R.make_variety_tree(target, depth=args.depth, |
| 108 |
seed=int(args.seed, 0), **caps) |
| 109 |
|
| 110 |
tr = info['transfer_root'] |
| 111 |
print(f"created {sum(info['counts'].values())} entries under {target}") |
| 112 |
print(f" counts: {info['counts']}") |
| 113 |
print(f" caps: xattrs={caps['with_xattrs']} acls={caps['with_acls']} " |
| 114 |
f"devices={caps['with_devices']} owners={caps['with_owners']}") |
| 115 |
if not caps['with_devices'] or not caps['with_owners']: |
| 116 |
print(" (run as root to add device nodes and mixed ownership)") |
| 117 |
print(f"\ntransfer this: {tr}/") |
| 118 |
print(f"e.g. rsync -aHAX --specials --devices {tr}/ /tmp/copy/") |
| 119 |
|
| 120 |
if args.list: |
| 121 |
print() |
| 122 |
list_tree(target) |
| 123 |
|
| 124 |
|
| 125 |
if __name__ == '__main__': |
| 126 |
main() |