blob: dbf4f1aa6c1e5fb5864c9776ff3dd0323232d975 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossum2ba9f301992-03-02 16:20:32 +00002# Format du output in a tree shape
3
Walter Dörwaldaaab30e2002-09-11 20:36:02 +00004import os, sys, errno
Guido van Rossum2ba9f301992-03-02 16:20:32 +00005
6def main():
Walter Dörwaldaaab30e2002-09-11 20:36:02 +00007 p = os.popen('du ' + ' '.join(sys.argv[1:]), 'r')
Tim Peters70c43782001-01-17 08:48:39 +00008 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
Andrew M. Kuchlingac6df952003-05-13 18:14:25 +000014 filename = line[i:-1]
15 comps = filename.split('/')
Tim Peters70c43782001-01-17 08:48:39 +000016 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 try:
20 display(total, d)
Guido van Rossumb940e112007-01-10 16:19:56 +000021 except IOError as e:
Tim Peters70c43782001-01-17 08:48:39 +000022 if e.errno != errno.EPIPE:
23 raise
Guido van Rossum2ba9f301992-03-02 16:20:32 +000024
25def store(size, comps, total, d):
Tim Peters70c43782001-01-17 08:48:39 +000026 if comps == []:
27 return size, d
Guido van Rossumba1fffa2007-08-29 23:37:34 +000028 if comps[0] not in d:
Tim Peters70c43782001-01-17 08:48:39 +000029 d[comps[0]] = None, {}
30 t1, d1 = d[comps[0]]
31 d[comps[0]] = store(size, comps[1:], t1, d1)
32 return total, d
Guido van Rossum2ba9f301992-03-02 16:20:32 +000033
34def display(total, d):
Tim Peters70c43782001-01-17 08:48:39 +000035 show(total, d, '')
Guido van Rossum2ba9f301992-03-02 16:20:32 +000036
37def show(total, d, prefix):
Tim Peters70c43782001-01-17 08:48:39 +000038 if not d: return
39 list = []
40 sum = 0
41 for key in d.keys():
42 tsub, dsub = d[key]
43 list.append((tsub, key))
44 if tsub is not None: sum = sum + tsub
Tim Peters79b334b2001-01-17 09:13:33 +000045## if sum < total:
46## list.append((total - sum, os.curdir))
Tim Peters70c43782001-01-17 08:48:39 +000047 list.sort()
48 list.reverse()
Walter Dörwald70a6b492004-02-12 17:35:32 +000049 width = len(repr(list[0][0]))
Tim Peters70c43782001-01-17 08:48:39 +000050 for tsub, key in list:
51 if tsub is None:
52 psub = prefix
53 else:
Collin Winter6afaeb72007-08-03 17:06:41 +000054 print(prefix + repr(tsub).rjust(width) + ' ' + key)
Tim Peters70c43782001-01-17 08:48:39 +000055 psub = prefix + ' '*(width-1) + '|' + ' '*(len(key)+1)
Guido van Rossumba1fffa2007-08-29 23:37:34 +000056 if key in d:
Tim Peters70c43782001-01-17 08:48:39 +000057 show(tsub, d[key][1], psub)
Guido van Rossum2ba9f301992-03-02 16:20:32 +000058
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000059if __name__ == '__main__':
60 main()