blob: c309212b502fb63f043db1802f3a1a387142d75d [file] [log] [blame]
Guido van Rossum2ba9f301992-03-02 16:20:32 +00001#! /usr/local/python
2# Format du output in a tree shape
3
4import posix, string, sys, path
5
6def main():
7 p = posix.popen('du ' + string.join(sys.argv[1:]), 'r')
8 total, d = None, {}
9 for line in p.readlines():
10 [num, file] = string.split(line)
11 size = eval(num)
12 comps = string.splitfields(file, '/')
13 if comps[0] == '': comps[0] = '/'
14 if comps[len(comps)-1] == '': del comps[len(comps)-1]
15 total, d = store(size, comps, total, d)
16 display(total, d)
17
18def store(size, comps, total, d):
19 if comps == []:
20 return size, d
21 if not d.has_key(comps[0]):
22 d[comps[0]] = None, {}
23 t1, d1 = d[comps[0]]
24 d[comps[0]] = store(size, comps[1:], t1, d1)
25 return total, d
26
27def display(total, d):
28 show(total, d, '')
29
30def show(total, d, prefix):
31 if not d: return
32 list = []
33 sum = 0
34 for key in d.keys():
35 tsub, dsub = d[key]
36 list.append((tsub, key))
37 if tsub is not None: sum = sum + tsub
38 if sum < total:
39 list.append((total - sum, '.'))
40 list.sort()
41 list.reverse()
42 width = len(`list[0][0]`)
43 for tsub, key in list:
44 if tsub is None:
45 psub = prefix
46 else:
47 print prefix + string.rjust(`tsub`, width) + ' ' + key
48 psub = prefix + ' '*(width-1) + '|' + ' '*(len(key)+1)
49 if d.has_key(key):
50 show(tsub, d[key][1], psub)
51
52main()