]> arthur.barton.de Git - bup.git/commitdiff
Add -l and --human-readable options to "bup ls".
authorKumar Appaiah <a.kumar@alumni.iitm.ac.in>
Sun, 25 Aug 2013 19:02:18 +0000 (15:02 -0400)
committerRob Browning <rlb@defaultvalue.org>
Mon, 26 Aug 2013 00:28:44 +0000 (19:28 -0500)
When -l is specified, include the size in bytes for each item.

When --human-readable is also 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; adjust newlines; squash
 two of Kumar's patches into this one.]
Reviewed-by: Rob Browning <rlb@defaultvalue.org>
Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Documentation/bup-ls.md
lib/bup/ls.py

index 443f1788f35efbe7525f9180e81b5fbeafa34c49..359d91486a3a088e3f8b8dc79f9c9f3ac57932b3 100644 (file)
@@ -44,6 +44,12 @@ you can view its contents using `bup join` or `git show`.
 -a, \--all
 :   show hidden files.
 
+-l
+:   show file sizes.
+
+\--human-readable
+:   print human readable file sizes (i.e. 3.9K, 4.7M)
+
 # EXAMPLE
 
     bup ls /myserver/latest/etc/profile
index fe04f8a3b3eb7d1e426b7250126682d51ce70a0d..a86a904a0a83d28dc7a7f155c2e40896b71bfc22 100644 (file)
@@ -4,11 +4,19 @@ from bup import options, vfs
 from helpers import *
 
 
-def node_name(text, n, show_hash):
+def node_name(text, n, show_hash = False,
+              show_filesize = False,
+              filesize = None,
+              human_readable = False):
     """Add symbols to a node's name to differentiate file types."""
     prefix = ''
     if show_hash:
         prefix += "%s " % n.hash.encode('hex')
+    if show_filesize:
+        if human_readable:
+            prefix += "%10s " % format_filesize(filesize)
+        else:
+            prefix += "%14d " % filesize
     if stat.S_ISDIR(n.mode):
         return '%s%s/' % (prefix, text)
     elif stat.S_ISLNK(n.mode):
@@ -22,6 +30,8 @@ optspec = """
 --
 s,hash   show hash for each file
 a,all    show hidden files
+l        show file sizes
+human-readable    print human readable file sizes (i.e. 3.9K, 4.7M)
 """
 
 def do_ls(args, pwd, default='.', onabort=None, spec_prefix=''):
@@ -47,16 +57,21 @@ def do_ls(args, pwd, default='.', onabort=None, spec_prefix=''):
             if stat.S_ISDIR(n.mode):
                 for sub in n:
                     name = sub.name
+                    fsize = sub.size() if opt.l else None
+                    nname = node_name(name, sub, opt.hash, opt.l, fsize,
+                                      opt.human_readable)
                     if opt.all or not len(name)>1 or not name.startswith('.'):
                         if istty1:
-                            L.append(node_name(name, sub, opt.hash))
+                            L.append(nname)
                         else:
-                            print node_name(name, sub, opt.hash)
+                            print nname
             else:
+                nname = node_name(path, n, opt.hash, opt.l, None,
+                                  opt.human_readable)
                 if istty1:
-                    L.append(node_name(path, n, opt.hash))
+                    L.append(nname)
                 else:
-                    print node_name(path, n, opt.hash)
+                    print nname
         except vfs.NodeError, e:
             log('error: %s\n' % e)
             ret = 1