Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base: 2050f8e201
...
compare: 5a1cfc505b
Checking mergeability… Don't worry, you can still create the pull request.
  • 4 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 90 additions and 14 deletions.
  1. +39 −13 duviz.py
  2. +51 −1 test_duviz.py
View
52 duviz.py
@@ -214,39 +214,60 @@ def block_display(self, width, max_depth=5, top=True):
return '\n'.join(lines)
+class SubprocessException(Exception):
+ pass
+
+
##############################################################################
-def build_tree(directory, feedback=sys.stdout, terminal_width=80, options=None):
+def build_du_tree(directory, feedback=sys.stdout, terminal_width=80, one_filesystem=False, dereference=False):
'''
Build a tree of DirectoryTreeNodes, starting at the given directory.
'''
- durep = re.compile(r'([0-9]*)\s*(.*)')
# Measure size in 1024 byte blocks. The GNU-du option -b enables counting
# in bytes directely, but it is not available in BSD-du.
duargs = ['-k']
# Handling of symbolic links.
- if hasattr(options, 'onefilesystem') and options.onefilesystem:
+ if one_filesystem:
duargs.append('-x')
- if hasattr(options, 'dereference') and options.dereference:
+ if dereference:
duargs.append('-L')
- dupipe = subprocess.Popen(['du'] + duargs + [directory], stdout=subprocess.PIPE)
- dirtree = DirectoryTreeNode(directory)
- for line in dupipe.stdout:
- mo = durep.match(line)
+ try:
+ du_pipe = subprocess.Popen(['du'] + duargs + [directory], stdout=subprocess.PIPE)
+ except OSError:
+ raise SubprocessException('Failed to launch "du" utility subprocess. Is it installed and in your PATH?')
+
+ dir_tree = _build_du_tree(directory, du_pipe.stdout, feedback=feedback, terminal_width=terminal_width)
+
+ du_pipe.stdout.close()
+
+ return dir_tree
+
+
+def _build_du_tree(directory, du_pipe, feedback=None, terminal_width=80):
+ '''
+ Helper function
+ '''
+ du_rep = re.compile(r'([0-9]*)\s*(.*)')
+
+ dir_tree = DirectoryTreeNode(directory)
+
+ for line in du_pipe:
+ mo = du_rep.match(line)
# Size in bytes.
size = int(mo.group(1)) * 1024
path = mo.group(2)
if feedback:
feedback.write(('scanning %s' % path).ljust(terminal_width)[:terminal_width] + '\r')
- dirtree.import_path(path_split(path), size)
+ dir_tree.import_path(path_split(path), size)
if feedback:
feedback.write(' ' * terminal_width + '\r')
- dupipe.stdout.close()
- return dirtree
+
+ return dir_tree
##############################################################################
-if __name__ == '__main__':
+def main():
terminal_width = terminal_size()[1]
@@ -279,5 +300,10 @@ def build_tree(directory, feedback=sys.stdout, terminal_width=80, options=None):
cliargs = ['.']
for directory in cliargs:
- tree = build_tree(directory, terminal_width=clioptions.display_width, options=clioptions)
+ tree = build_du_tree(directory, terminal_width=clioptions.display_width, one_filesystem=clioptions.onefilesystem, dereference=clioptions.dereference)
print tree.block_display(clioptions.display_width, max_depth=clioptions.max_depth)
+
+
+
+if __name__ == '__main__':
+ main()
View
52 test_duviz.py
@@ -1,5 +1,8 @@
import unittest
+import StringIO
+import textwrap
+
import duviz
@@ -89,4 +92,51 @@ def test_path_split(self):
self.assertEqual(expected, duviz.path_split(input))
-
+class BuildDuTreeTest(unittest.TestCase):
+
+ def test_build_du_tree1(self):
+ dir = 'path/to'
+ du_pipe = StringIO.StringIO(textwrap.dedent('''\
+ 120 path/to/foo
+ 10 path/to/bar/a
+ 163 path/to/bar/b
+ 360 path/to/bar/c
+ 612 path/to/bar
+ 2 path/to/s p a c e s
+ 800 path/to
+ '''))
+ tree = duviz._build_du_tree(dir, du_pipe, feedback=None)
+ result = tree.block_display(width=40)
+ expected = textwrap.dedent('''\
+ ________________________________________
+ [ path/to ]
+ [_______________819.20KB_______________]
+ [ bar ][foo ]
+ [__________626.69KB__________][122.]
+ [ c ][ b ]|
+ [____368.64KB___][166.91]|
+ ''')
+ self.assertEqual(expected.split(), result.split())
+
+ def test_build_du_tree2(self):
+ dir = 'path/to'
+ du_pipe = StringIO.StringIO(textwrap.dedent('''\
+ 1 path/to/A
+ 1 path/to/b
+ 2 path/to/C
+ 4 path/to
+ '''))
+ tree = duviz._build_du_tree(dir, du_pipe, feedback=None)
+ result = tree.block_display(width=40)
+ expected = textwrap.dedent('''\
+ ________________________________________
+ [ path/to ]
+ [________________4.10KB________________]
+ [ C ][ A ][ b ]
+ [______2.05KB______][_1.02KB_][_1.02KB_]
+ ''')
+ self.assertEqual(expected.split(), result.split())
+
+
+if __name__ == '__main__':
+ unittest.main()

No commit comments for this range

Something went wrong with that request. Please try again.