1 | n/a | #! /usr/bin/env python3 |
---|
2 | n/a | # Format du output in a tree shape |
---|
3 | n/a | |
---|
4 | n/a | import os, sys, errno |
---|
5 | n/a | |
---|
6 | n/a | def main(): |
---|
7 | n/a | p = os.popen('du ' + ' '.join(sys.argv[1:]), 'r') |
---|
8 | n/a | total, d = None, {} |
---|
9 | n/a | for line in p.readlines(): |
---|
10 | n/a | i = 0 |
---|
11 | n/a | while line[i] in '0123456789': i = i+1 |
---|
12 | n/a | size = eval(line[:i]) |
---|
13 | n/a | while line[i] in ' \t': i = i+1 |
---|
14 | n/a | filename = line[i:-1] |
---|
15 | n/a | comps = filename.split('/') |
---|
16 | n/a | if comps[0] == '': comps[0] = '/' |
---|
17 | n/a | if comps[len(comps)-1] == '': del comps[len(comps)-1] |
---|
18 | n/a | total, d = store(size, comps, total, d) |
---|
19 | n/a | try: |
---|
20 | n/a | display(total, d) |
---|
21 | n/a | except IOError as e: |
---|
22 | n/a | if e.errno != errno.EPIPE: |
---|
23 | n/a | raise |
---|
24 | n/a | |
---|
25 | n/a | def store(size, comps, total, d): |
---|
26 | n/a | if comps == []: |
---|
27 | n/a | return size, d |
---|
28 | n/a | if comps[0] not in d: |
---|
29 | n/a | d[comps[0]] = None, {} |
---|
30 | n/a | t1, d1 = d[comps[0]] |
---|
31 | n/a | d[comps[0]] = store(size, comps[1:], t1, d1) |
---|
32 | n/a | return total, d |
---|
33 | n/a | |
---|
34 | n/a | def display(total, d): |
---|
35 | n/a | show(total, d, '') |
---|
36 | n/a | |
---|
37 | n/a | def show(total, d, prefix): |
---|
38 | n/a | if not d: return |
---|
39 | n/a | list = [] |
---|
40 | n/a | sum = 0 |
---|
41 | n/a | for key in d.keys(): |
---|
42 | n/a | tsub, dsub = d[key] |
---|
43 | n/a | list.append((tsub, key)) |
---|
44 | n/a | if tsub is not None: sum = sum + tsub |
---|
45 | n/a | ## if sum < total: |
---|
46 | n/a | ## list.append((total - sum, os.curdir)) |
---|
47 | n/a | list.sort() |
---|
48 | n/a | list.reverse() |
---|
49 | n/a | width = len(repr(list[0][0])) |
---|
50 | n/a | for tsub, key in list: |
---|
51 | n/a | if tsub is None: |
---|
52 | n/a | psub = prefix |
---|
53 | n/a | else: |
---|
54 | n/a | print(prefix + repr(tsub).rjust(width) + ' ' + key) |
---|
55 | n/a | psub = prefix + ' '*(width-1) + '|' + ' '*(len(key)+1) |
---|
56 | n/a | if key in d: |
---|
57 | n/a | show(tsub, d[key][1], psub) |
---|
58 | n/a | |
---|
59 | n/a | if __name__ == '__main__': |
---|
60 | n/a | main() |
---|