Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 2 | # Format du output in a tree shape |
| 3 | |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 4 | import os, sys, errno |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 5 | |
| 6 | def main(): |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 7 | p = os.popen('du ' + ' '.join(sys.argv[1:]), 'r') |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 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 |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 14 | filename = line[i:-1] |
| 15 | comps = filename.split('/') |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 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 | try: |
| 20 | display(total, d) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 21 | except IOError as e: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 22 | if e.errno != errno.EPIPE: |
| 23 | raise |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 24 | |
| 25 | def store(size, comps, total, d): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 26 | if comps == []: |
| 27 | return size, d |
Guido van Rossum | ba1fffa | 2007-08-29 23:37:34 +0000 | [diff] [blame] | 28 | if comps[0] not in d: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 29 | 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 Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 33 | |
| 34 | def display(total, d): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 35 | show(total, d, '') |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 36 | |
| 37 | def show(total, d, prefix): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 38 | 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 Peters | 79b334b | 2001-01-17 09:13:33 +0000 | [diff] [blame] | 45 | ## if sum < total: |
| 46 | ## list.append((total - sum, os.curdir)) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 47 | list.sort() |
| 48 | list.reverse() |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 49 | width = len(repr(list[0][0])) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 50 | for tsub, key in list: |
| 51 | if tsub is None: |
| 52 | psub = prefix |
| 53 | else: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 54 | print(prefix + repr(tsub).rjust(width) + ' ' + key) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 55 | psub = prefix + ' '*(width-1) + '|' + ' '*(len(key)+1) |
Guido van Rossum | ba1fffa | 2007-08-29 23:37:34 +0000 | [diff] [blame] | 56 | if key in d: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 57 | show(tsub, d[key][1], psub) |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 58 | |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 59 | if __name__ == '__main__': |
| 60 | main() |