blob: a39f17293ef8fac5e3adccfdbe8079ee78d34a82 [file] [log] [blame]
Guido van Rossum2ba9f301992-03-02 16:20:32 +00001#! /usr/local/python
2# Format du output in a tree shape
3
Guido van Rossum4a5ab811992-03-30 11:31:39 +00004import os, string, sys
Guido van Rossum2ba9f301992-03-02 16:20:32 +00005
6def main():
Guido van Rossum4a5ab811992-03-30 11:31:39 +00007 p = os.popen('du ' + string.join(sys.argv[1:]), 'r')
Guido van Rossum2ba9f301992-03-02 16:20:32 +00008 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:
Guido van Rossum4a5ab811992-03-30 11:31:39 +000039 list.append((total - sum, os.curdir))
Guido van Rossum2ba9f301992-03-02 16:20:32 +000040 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()