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(): |
Guido van Rossum | 61700bb | 1992-08-31 10:54:54 +0000 | [diff] [blame] | 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] |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 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) |
| 20 | |
| 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 |
| 29 | |
| 30 | def display(total, d): |
| 31 | show(total, d, '') |
| 32 | |
| 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: |
Guido van Rossum | 4a5ab81 | 1992-03-30 11:31:39 +0000 | [diff] [blame] | 42 | list.append((total - sum, os.curdir)) |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 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) |
| 54 | |
| 55 | main() |