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