Small `du' reformatter :-)
François Pinard
pinard at iro.umontreal.ca
Thu Jan 20 18:11:42 EST 2000
Hi, people.
I knew I wrote a small Python tool to reformat `du' output, but I lost it
for a while, and unexpectedly stumbled on it today, looking for something
unrelated. Here it is. So if I loose it again, I'll ask you! :-)
-------------- next part --------------
#!/usr/bin/python
# Produce directory hierarchy with sizes, well sorted and indented.
# Copyright ? 1996, 1999 Progiciels Bourbeau-Pinard inc.
# Fran?ois Pinard <pinard at iro.umontreal.ca>, 1996.
# Idea from Pierre Rioux <riouxp at bch.umontreal.ca>, 1996-07-12.
import os, re, string, sys
def main(*arguments):
# Get data properly sorted.
items = map(presort,
os.popen('du %s' % string.join(arguments)).readlines())
items.sort()
items = map(postsort, items)
# Erase unneeded fragments of vertical lines.
clean(items)
items.reverse()
clean(items)
items.reverse()
# Erase leftmost white columns.
skip = 0
split, size = items[0]
while not split[skip]:
skip = skip + 1
# Produce display.
for split, size in items:
sys.stdout.write('%7s ' % size)
for item in split[skip:-1]:
if item:
sys.stdout.write('| ')
else:
sys.stdout.write(' ')
sys.stdout.write('\\_ %s' % split[-1])
def presort(line):
size, path = string.split(line, '\t')
return path, size
def postsort((path, size)):
return string.split(path, '/'), size
def clean(items):
# The basename is always written. Intermediate directories
# normally print as `|', yet None is used to inhibit the line.
draw = []
for split, size in items:
while len(draw) > len(split) - 1:
draw.pop()
while len(draw) < len(split) - 1:
draw.append(0)
draw.append(1)
for counter in range(len(split) - 1):
if not draw[counter]:
split[counter] = None
if __name__ == '__main__':
apply(main, sys.argv[1:])
-------------- next part --------------
--
Fran?ois Pinard http://www.iro.umontreal.ca/~pinard
More information about the Python-list
mailing list