Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 1 | #! /usr/local/python |
| 2 | # Format du output in a tree shape |
| 3 | |
Guido van Rossum | 4a5ab81 | 1992-03-30 11:31:39 +0000 | [diff] [blame] | 4 | import os, string, sys |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 5 | |
| 6 | def main(): |
Guido van Rossum | 4a5ab81 | 1992-03-30 11:31:39 +0000 | [diff] [blame] | 7 | p = os.popen('du ' + string.join(sys.argv[1:]), 'r') |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 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 | |
| 18 | def 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 | |
| 27 | def display(total, d): |
| 28 | show(total, d, '') |
| 29 | |
| 30 | def 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 Rossum | 4a5ab81 | 1992-03-30 11:31:39 +0000 | [diff] [blame] | 39 | list.append((total - sum, os.curdir)) |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 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 | |
| 52 | main() |