Fix an amazing number of typos & malformed sentences reported by Detlef
[python/dscho.git] / Tools / scripts / dutree.py
blob3098f2b6210d036dcfe92fceda129a09ba834325
1 #! /usr/bin/env python
2 # Format du output in a tree shape
4 import os, string, sys
6 def main():
7 p = os.popen('du ' + string.join(sys.argv[1:]), 'r')
8 total, d = None, {}
9 for line in p.readlines():
10 i = 0
11 while line[i] in '0123456789': i = i+1
12 size = eval(line[:i])
13 while line[i] in ' \t': i = i+1
14 file = line[i:-1]
15 comps = string.splitfields(file, '/')
16 if comps[0] == '': comps[0] = '/'
17 if comps[len(comps)-1] == '': del comps[len(comps)-1]
18 total, d = store(size, comps, total, d)
19 display(total, d)
21 def store(size, comps, total, d):
22 if comps == []:
23 return size, d
24 if not d.has_key(comps[0]):
25 d[comps[0]] = None, {}
26 t1, d1 = d[comps[0]]
27 d[comps[0]] = store(size, comps[1:], t1, d1)
28 return total, d
30 def display(total, d):
31 show(total, d, '')
33 def show(total, d, prefix):
34 if not d: return
35 list = []
36 sum = 0
37 for key in d.keys():
38 tsub, dsub = d[key]
39 list.append((tsub, key))
40 if tsub is not None: sum = sum + tsub
41 ## if sum < total:
42 ## list.append((total - sum, os.curdir))
43 list.sort()
44 list.reverse()
45 width = len(`list[0][0]`)
46 for tsub, key in list:
47 if tsub is None:
48 psub = prefix
49 else:
50 print prefix + string.rjust(`tsub`, width) + ' ' + key
51 psub = prefix + ' '*(width-1) + '|' + ' '*(len(key)+1)
52 if d.has_key(key):
53 show(tsub, d[key][1], psub)
55 main()