]> arthur.barton.de Git - bup.git/commitdiff
Add --human-readable option to "bup web".
authorKumar Appaiah <a.kumar@alumni.iitm.ac.in>
Sun, 25 Aug 2013 19:02:16 +0000 (15:02 -0400)
committerRob Browning <rlb@defaultvalue.org>
Mon, 26 Aug 2013 00:28:29 +0000 (19:28 -0500)
When --human-readable is specified, print sizes like 3.5K, 1.8G,
etc., instead of the exact byte count.

Signed-Off-By: Kumar Appaiah <a.kumar@alumni.iitm.ac.in>
Reviewed-by: Zoran Zaric <zz@zoranzaric.de>
[rlb@defaultvalue.org: adjust commit message; squash two of Kumar's
 patches into this one.]
Reviewed-by: Rob Browning <rlb@defaultvalue.org>
Documentation/bup-web.md
cmd/web-cmd.py
lib/bup/helpers.py

index 706fab0566f9a80cb43394bb16563147a1fb624b..11002e54794c3d46f9c62a934ceeca330e6bfdf7 100644 (file)
@@ -21,6 +21,11 @@ hierarchy is the same as that shown by `bup-fuse`(1), `bup-ls`(1) and
 you'd like to expose the web server to anyone on your network (dangerous!) you
 can omit the bind address to bind to all available interfaces: `:8080`.
 
+# OPTIONS
+
+--human-readable
+:   display human readable file sizes (i.e. 3.9K, 4.7M)
+
 # EXAMPLE
 
     $ bup web
index c930f38657bc9e58413ac02364d57534344305c2..484041ac112c8993fd8e37652451ae7975203133 100755 (executable)
@@ -64,6 +64,7 @@ def _compute_dir_contents(n, path, show_hidden=False):
             display = sub.name + '@'
         else:
             size = sub.size()
+            size = (opt.human_readable and format_filesize(size)) or size
 
         yield (display, link + url_append, size)
 
@@ -182,6 +183,7 @@ class BupRequestHandler(tornado.web.RequestHandler):
 optspec = """
 bup web [[hostname]:port]
 --
+human-readable    display human readable file sizes (i.e. 3.9K, 4.7M)
 """
 o = options.Options(optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
index afbf18daa8e402395edaf9afa0117a50e34f3576..6e85fda82dedee928805a075f890a545424043e6 100644 (file)
@@ -4,6 +4,7 @@ import sys, os, pwd, subprocess, errno, socket, select, mmap, stat, re, struct
 import hashlib, heapq, operator, time, grp
 from bup import _version, _helpers
 import bup._helpers as _helpers
+import math
 
 # This function should really be in helpers, not in bup.options.  But we
 # want options.py to be standalone so people can include it in other projects.
@@ -320,6 +321,15 @@ def resource_path(subdir=''):
         _resource_path = os.environ.get('BUP_RESOURCE_PATH') or '.'
     return os.path.join(_resource_path, subdir)
 
+def format_filesize(size):
+    unit = 1024.0
+    size = float(size)
+    if size < unit:
+        return "%d" % (size)
+    exponent = int(math.log(size) / math.log(unit))
+    size_prefix = "KMGTPE"[exponent - 1]
+    return "%.1f%s" % (size / math.pow(unit, exponent), size_prefix)
+
 
 class NotOk(Exception):
     pass