]> arthur.barton.de Git - bup.git/blob - dev/hardlink-sets
Add any pending exception to context in all __exit__ methods
[bup.git] / dev / hardlink-sets
1 #!/bin/sh
2 """": # -*-python-*-
3 bup_exec="$(dirname "$0")/bup-exec" || exit $?
4 exec "$bup_exec" "$0" ${1+"$@"}
5 """
6
7 from __future__ import absolute_import, print_function
8 import os, stat, sys
9
10 from bup.compat import get_argvb
11 from bup.io import byte_stream
12
13
14 # Print the full paths of all the files in each hardlink set
15 # underneath one of the paths.  Separate sets with a blank line, sort
16 # the paths within each set, and sort the sets by their first path.
17
18 def usage():
19     print("Usage: hardlink-sets <paths ...>", file=sys.stderr)
20
21 argvb = get_argvb()
22
23 if len(argvb) < 2:
24     usage()
25     sys.exit(1)
26
27 def on_walk_error(e):
28     raise e
29
30 sys.stdout.flush()
31 out = byte_stream(sys.stdout)
32
33 hardlink_set = {}
34
35 for p in argvb[1:]:
36   for root, dirs, files in os.walk(p, onerror = on_walk_error):
37       for filename in files:
38           full_path = os.path.join(root, filename)
39           st = os.lstat(full_path)
40           if not stat.S_ISDIR(st.st_mode):
41               node = b'%d:%d' % (st.st_dev, st.st_ino)
42               link_paths = hardlink_set.get(node)
43               if link_paths:
44                   link_paths.append(full_path)
45               else:
46                   hardlink_set[node] = [full_path]
47
48 # Sort the link sets.
49 for node, link_paths in hardlink_set.items():
50     link_paths.sort()
51
52 first_set = True
53 for link_paths in sorted(hardlink_set.values(), key = lambda x : x[0]):
54     if len(link_paths) > 1:
55         if first_set:
56             first_set = False
57         else:
58             out.write(b'\n')
59         for p in sorted(link_paths):
60             out.write(p + b'\n')
61
62 sys.exit(0)