From: Brian Warner <warner@lothar.com>
Date: Wed, 15 Jul 2009 07:51:09 +0000 (-0700)
Subject: 'tahoe stats': tolerate empty directories. Closes #693.
X-Git-Tag: trac-4000~5
X-Git-Url: https://git.rkrishnan.org/components/com_hotproperty/css/flags?a=commitdiff_plain;h=efa608a1353e91fe6869ad3b0bb2f2462fb8e724;p=tahoe-lafs%2Ftahoe-lafs.git

'tahoe stats': tolerate empty directories. Closes #693.
---

diff --git a/src/allmydata/scripts/tahoe_manifest.py b/src/allmydata/scripts/tahoe_manifest.py
index a43b5b97..180bf7cb 100644
--- a/src/allmydata/scripts/tahoe_manifest.py
+++ b/src/allmydata/scripts/tahoe_manifest.py
@@ -117,24 +117,25 @@ class StatsGrabber(SlowOperationRunner):
                     print >>stdout, fmt % (k, data[k]), "  ", absize
                 else:
                     print >>stdout, fmt % (k, data[k])
-        print >>stdout, "Size Histogram:"
-        prevmax = None
-        maxlen = max([len(str(maxsize))
-                      for (minsize, maxsize, count)
-                      in data["size-files-histogram"]])
-        maxcountlen = max([len(str(count))
-                           for (minsize, maxsize, count)
-                           in data["size-files-histogram"]])
-        minfmt = "%" + str(maxlen) + "d"
-        maxfmt = "%-" + str(maxlen) + "d"
-        countfmt = "%-" + str(maxcountlen) + "d"
-        linefmt = minfmt + "-" + maxfmt + " : " + countfmt + "    %s"
-        for (minsize, maxsize, count) in data["size-files-histogram"]:
-            if prevmax is not None and minsize != prevmax+1:
-                print >>stdout, " "*(maxlen-1) + "..."
-            prevmax = maxsize
-            print >>stdout, linefmt % (minsize, maxsize, count,
-                                       abbreviate_space_both(maxsize))
+        if data["size-files-histogram"]:
+            print >>stdout, "Size Histogram:"
+            prevmax = None
+            maxlen = max([len(str(maxsize))
+                          for (minsize, maxsize, count)
+                          in data["size-files-histogram"]])
+            maxcountlen = max([len(str(count))
+                               for (minsize, maxsize, count)
+                               in data["size-files-histogram"]])
+            minfmt = "%" + str(maxlen) + "d"
+            maxfmt = "%-" + str(maxlen) + "d"
+            countfmt = "%-" + str(maxcountlen) + "d"
+            linefmt = minfmt + "-" + maxfmt + " : " + countfmt + "    %s"
+            for (minsize, maxsize, count) in data["size-files-histogram"]:
+                if prevmax is not None and minsize != prevmax+1:
+                    print >>stdout, " "*(maxlen-1) + "..."
+                prevmax = maxsize
+                print >>stdout, linefmt % (minsize, maxsize, count,
+                                           abbreviate_space_both(maxsize))
 
 def stats(options):
     return StatsGrabber().run(options)
diff --git a/src/allmydata/test/test_cli.py b/src/allmydata/test/test_cli.py
index 8b46502d..8efccb2b 100644
--- a/src/allmydata/test/test_cli.py
+++ b/src/allmydata/test/test_cli.py
@@ -1268,6 +1268,21 @@ class Check(GridTestMixin, CLITestMixin, unittest.TestCase):
                             in lines, out)
         d.addCallback(_check2)
 
+        d.addCallback(lambda ign: self.do_cli("stats", self.rooturi))
+        def _check_stats((rc, out, err)):
+            self.failUnlessEqual(err, "")
+            self.failUnlessEqual(rc, 0)
+            lines = out.splitlines()
+            self.failUnlessIn(" count-immutable-files: 1", lines)
+            self.failUnlessIn("   count-mutable-files: 1", lines)
+            self.failUnlessIn("   count-literal-files: 1", lines)
+            self.failUnlessIn("     count-directories: 1", lines)
+            self.failUnlessIn("  size-immutable-files: 400", lines)
+            self.failUnlessIn("Size Histogram:", lines)
+            self.failUnlessIn("   4-10   : 1    (10 B, 10 B)", lines)
+            self.failUnlessIn(" 317-1000 : 1    (1000 B, 1000 B)", lines)
+        d.addCallback(_check_stats)
+
         def _clobber_shares(ignored):
             shares = self.find_shares(self.uris["good"])
             self.failUnlessEqual(len(shares), 10)
@@ -1431,3 +1446,31 @@ class Errors(GridTestMixin, CLITestMixin, unittest.TestCase):
         d.addCallback(_check1)
 
         return d
+
+class Stats(GridTestMixin, CLITestMixin, unittest.TestCase):
+    def test_empty_directory(self):
+        self.basedir = "cli/Stats/empty_directory"
+        self.set_up_grid()
+        c0 = self.g.clients[0]
+        self.fileurls = {}
+        d = c0.create_empty_dirnode()
+        def _stash_root(n):
+            self.rootnode = n
+            self.rooturi = n.get_uri()
+        d.addCallback(_stash_root)
+
+        # make sure we can get stats on an empty directory too
+        d.addCallback(lambda ign: self.do_cli("stats", self.rooturi))
+        def _check_stats((rc, out, err)):
+            self.failUnlessEqual(err, "")
+            self.failUnlessEqual(rc, 0)
+            lines = out.splitlines()
+            self.failUnlessIn(" count-immutable-files: 0", lines)
+            self.failUnlessIn("   count-mutable-files: 0", lines)
+            self.failUnlessIn("   count-literal-files: 0", lines)
+            self.failUnlessIn("     count-directories: 1", lines)
+            self.failUnlessIn("  size-immutable-files: 0", lines)
+            self.failIfIn("Size Histogram:", lines)
+        d.addCallback(_check_stats)
+
+        return d