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