Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/dutree.py @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 28948181fb2f |
| children | 73758db2f464 |
| rev | line source |
|---|---|
|
1269
28948181fb2f
*** empty log message ***
Guido van Rossum <guido@python.org>
parents:
1002
diff
changeset
|
1 #! /usr/local/bin/python |
| 687 | 2 # Format du output in a tree shape |
| 3 | |
| 760 | 4 import os, string, sys |
| 687 | 5 |
| 6 def main(): | |
| 760 | 7 p = os.popen('du ' + string.join(sys.argv[1:]), 'r') |
| 687 | 8 total, d = None, {} |
| 9 for line in p.readlines(): | |
|
1002
0ce404e4139c
cope with filenames containing spaces
Guido van Rossum <guido@python.org>
parents:
760
diff
changeset
|
10 i = 0 |
|
0ce404e4139c
cope with filenames containing spaces
Guido van Rossum <guido@python.org>
parents:
760
diff
changeset
|
11 while line[i] in '0123456789': i = i+1 |
|
0ce404e4139c
cope with filenames containing spaces
Guido van Rossum <guido@python.org>
parents:
760
diff
changeset
|
12 size = eval(line[:i]) |
|
0ce404e4139c
cope with filenames containing spaces
Guido van Rossum <guido@python.org>
parents:
760
diff
changeset
|
13 while line[i] in ' \t': i = i+1 |
|
0ce404e4139c
cope with filenames containing spaces
Guido van Rossum <guido@python.org>
parents:
760
diff
changeset
|
14 file = line[i:-1] |
| 687 | 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: | |
| 760 | 42 list.append((total - sum, os.curdir)) |
| 687 | 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() |
