Debian Codegraph @

testsuite/cmptree.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Compare two directory trees and print any differences.
3
4 Reuses rsyncfns.compare_trees(), so it is special-file aware (never
5 opens a fifo/socket/device as a stream) and reports differences in: the tls
6 listing (type, mode, owner, size, mtime, symlink target for every inode),
7 regular-file contents, user xattrs, POSIX ACLs, and hard-link grouping.
8
9 Works on any two trees, not just ones built by mkvariety.py. Exit status is 0
10 when the trees match, 1 when they differ.
11
12 Examples:
13 testsuite/cmptree.py /tmp/vt/transfer_root /tmp/copy
14 testsuite/cmptree.py --no-acls --no-xattrs treeA treeB
15 """
16
17 import argparse
18 import os
19 import sys
20
21 _HERE = os.path.dirname(os.path.abspath(__file__))
22
23
24 def _resolve_tooldir(opt):
25 """Directory holding the `tls` helper (needed for the listing comparison)."""
26 for d in (opt, os.path.dirname(_HERE), os.getcwd()):
27 if d and os.path.exists(os.path.join(d, 'tls')):
28 return d
29 return None
30
31
32 def main():
33 ap = argparse.ArgumentParser(
34 description=__doc__,
35 formatter_class=argparse.RawDescriptionHelpFormatter)
36 ap.add_argument('tree_a')
37 ap.add_argument('tree_b')
38 ap.add_argument('--no-xattrs', dest='xattrs', action='store_false',
39 help='skip user-xattr comparison')
40 ap.add_argument('--no-acls', dest='acls', action='store_false',
41 help='skip POSIX ACL comparison')
42 ap.add_argument('--tooldir', default=None,
43 help='directory holding the tls helper '
44 '(default: the build tree, else cwd)')
45 ap.add_argument('-q', '--quiet', action='store_true',
46 help='print nothing; only set the exit status')
47 args = ap.parse_args()
48
49 for t in (args.tree_a, args.tree_b):
50 if not os.path.isdir(t):
51 ap.error(f"not a directory: {t}")
52
53 tooldir = _resolve_tooldir(args.tooldir)
54 if tooldir is None:
55 ap.error("cannot find the 'tls' helper (needed for the listing "
56 "comparison); build it with `make check-progs` or pass "
57 "--tooldir DIR")
58
59 # rsyncfns reads these at import time; compare_trees only needs
60 # TOOLDIR (for tls). scratchdir must be an existing dir but is unused here.
61 import tempfile
62 os.environ.setdefault('scratchdir', tempfile.gettempdir())
63 os.environ.setdefault('srcdir', os.getcwd())
64 os.environ.setdefault('TOOLDIR', tooldir)
65 os.environ.setdefault('RSYNC', 'rsync')
66 sys.path.insert(0, _HERE)
67 import rsyncfns as R
68
69 diffs = R.compare_trees(args.tree_a, args.tree_b,
70 with_acls=args.acls, with_xattrs=args.xattrs)
71 if diffs:
72 if not args.quiet:
73 print(f"trees DIFFER ({len(diffs)} difference(s)):")
74 print('\n'.join(diffs))
75 return 1
76 if not args.quiet:
77 print("trees are identical")
78 return 0
79
80
81 if __name__ == '__main__':
82 sys.exit(main())