[Zodb-checkins] CVS: StandaloneZODB/Tools - space.py:1.2

Guido van Rossum guido@python.org
Fri, 3 May 2002 14:13:24 -0400


Update of /cvs-repository/StandaloneZODB/Tools
In directory cvs.zope.org:/tmp/cvs-serv32062

Modified Files:
	space.py 
Log Message:
Add summary printing, proper command line option parsing, and a -v
option.


=== StandaloneZODB/Tools/space.py 1.1 => 1.2 ===
 from ZODB.fsdump import get_pickle_metadata
 
-def main(path):
+def run(path, v=0):
     fs = FileStorage(path, read_only=1)
     # break into the file implementation
     if hasattr(fs._index, 'iterkeys'):
         iter = fs._index.iterkeys()
     else:
         iter = fs._index.keys()
+    totals = {}
     for oid in iter:
         data, serialno = fs.load(oid, '')
         mod, klass = get_pickle_metadata(data)
-        print "%8s %5d %s.%s" % (U64(oid), len(data), mod, klass)
+        key = "%s.%s" % (mod, klass)
+        bytes, count = totals.get(key, (0, 0))
+        bytes += len(data)
+        count += 1
+        totals[key] = bytes, count
+        if v:
+            print "%8s %5d %s" % (U64(oid), len(data), key)
+    L = totals.items()
+    L.sort(lambda a, b: cmp(b, a))
+    L.reverse()
+    print "Totals per object class:"
+    for key, (bytes, count) in L:
+        print "%8d %8d %s" % (count, bytes, key)
 
-if __name__ == "__main__":
+def main():
     import sys
+    import getopt
     try:
-        path, = sys.argv[1:]
-    except ValueError:
-        print __doc__
+        opts, args = getopt.getopt(sys.argv[1:], "v")
+    except getopt.error, msg:
+        print msg
+        print "usage: space.py [-v] Data.fs"
+        sys.exit(2)
+    if len(args) != 1:
+        print "usage: space.py [-v] Data.fs"
         sys.exit(2)
-    main(path)
+    v = 0
+    for o, a in opts:
+        if o == "-v":
+            v += 1
+    path = args[0]
+    run(path, v)
+
+if __name__ == "__main__":
+    main()