home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Demo / scripts / dutree.py < prev    next >
Text File  |  1992-10-02  |  1KB  |  56 lines

  1. #! /usr/local/bin/python
  2. # Format du output in a tree shape
  3.  
  4. import os, string, sys
  5.  
  6. def main():
  7.     p = os.popen('du ' + string.join(sys.argv[1:]), 'r')
  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
  14.         file = line[i:-1]
  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:
  42.         list.append((total - sum, os.curdir))
  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()
  56.