]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/ls.py
Fix the ls -laAF options with respect to the VFS synthetic directories.
[bup.git] / lib / bup / ls.py
index fe04f8a3b3eb7d1e426b7250126682d51ce70a0d..66e421d40c254b33744aa4f85f91a26418c84753 100644 (file)
@@ -1,20 +1,43 @@
 """Common code for listing files from a bup repository."""
-import stat
-from bup import options, vfs
+import copy, stat, xstat
+from bup import metadata, options, vfs
 from helpers import *
 
 
-def node_name(text, n, show_hash):
-    """Add symbols to a node's name to differentiate file types."""
-    prefix = ''
+def node_info(n, name,
+              show_hash = False,
+              long_fmt = False,
+              classification = None,
+              numeric_ids = False,
+              human_readable = False):
+    """Return a string containing the information to display for the node
+    n.  Classification may be "all", "type", or None."""
+    result = ''
     if show_hash:
-        prefix += "%s " % n.hash.encode('hex')
-    if stat.S_ISDIR(n.mode):
-        return '%s%s/' % (prefix, text)
-    elif stat.S_ISLNK(n.mode):
-        return '%s%s@' % (prefix, text)
+        result += "%s " % n.hash.encode('hex')
+    if long_fmt:
+        meta = copy.copy(n.metadata())
+        if meta:
+            meta.path = name
+        else:
+            # Fake it -- summary_str() is designed to handle a fake.
+            meta = metadata.Metadata()
+            meta.size = n.size()
+            meta.mode = n.mode
+            meta.path = name
+            meta.atime, meta.mtime, meta.ctime = n.atime, n.mtime, n.ctime
+            if stat.S_ISLNK(meta.mode):
+                meta.symlink_target = n.readlink()
+        result += metadata.summary_str(meta,
+                                       numeric_ids = numeric_ids,
+                                       classification = classification,
+                                       human_readable = human_readable)
     else:
-        return '%s%s' % (prefix, text)
+        result += name
+        if classification:
+            mode = n.metadata() and n.metadata().mode or n.mode
+            result += xstat.classification_str(mode, classification == 'all')
+    return result
 
 
 optspec = """
@@ -22,14 +45,22 @@ optspec = """
 --
 s,hash   show hash for each file
 a,all    show hidden files
+A,almost-all    show hidden files except . and ..
+l        use a detailed, long listing format
+F,classify append type indicator: dir/ sym@ fifo| sock= exec*
+file-type append type indicator: dir/ sym@ fifo| sock=
+human-readable    print human readable file sizes (i.e. 3.9K, 4.7M)
+n,numeric-ids list numeric IDs (user, group, etc.) rather than names
 """
 
 def do_ls(args, pwd, default='.', onabort=None, spec_prefix=''):
     """Output a listing of a file or directory in the bup repository.
 
-    When stdout is attached to a tty, the output is formatted in columns. When
-    not attached to tty (for example when the output is piped to another
-    command), one file is listed per line.
+    When a long listing is not requested and stdout is attached to a
+    tty, the output is formatted in columns. When not attached to tty
+    (for example when the output is piped to another command), one
+    file is listed per line.
+
     """
     if onabort:
         o = options.Options(optspec % spec_prefix, onabort=onabort)
@@ -37,7 +68,32 @@ def do_ls(args, pwd, default='.', onabort=None, spec_prefix=''):
         o = options.Options(optspec % spec_prefix)
     (opt, flags, extra) = o.parse(args)
 
+    # Handle order-sensitive options.
+    classification = None
+    show_hidden = None
+    for flag in flags:
+        (option, parameter) = flag
+        if option in ('-F', '--classify'):
+            classification = 'all'
+        elif option == '--file-type':
+            classification = 'type'
+        elif option in ('-a', '--all'):
+            show_hidden = 'all'
+        elif option in ('-A', '--almost-all'):
+            show_hidden = 'almost'
+
     L = []
+    def output_node_info(node, name):
+        info = node_info(node, name,
+                         show_hash = opt.hash,
+                         long_fmt = opt.l,
+                         classification = classification,
+                         numeric_ids = opt.numeric_ids,
+                         human_readable = opt.human_readable)
+        if not opt.l and istty1:
+            L.append(info)
+        else:
+            print info
 
     ret = 0
     for path in (extra or [default]):
@@ -45,23 +101,25 @@ def do_ls(args, pwd, default='.', onabort=None, spec_prefix=''):
             n = pwd.try_resolve(path)
 
             if stat.S_ISDIR(n.mode):
+                if show_hidden == 'all':
+                    output_node_info(n, '.')
+                    # Match non-bup "ls -a ... /".
+                    if n.parent:
+                        output_node_info(n.parent, '..')
+                    else:
+                        output_node_info(n, '..')
                 for sub in n:
                     name = sub.name
-                    if opt.all or not len(name)>1 or not name.startswith('.'):
-                        if istty1:
-                            L.append(node_name(name, sub, opt.hash))
-                        else:
-                            print node_name(name, sub, opt.hash)
+                    if show_hidden in ('almost', 'all') \
+                       or not len(name)>1 or not name.startswith('.'):
+                        output_node_info(sub, name)
             else:
-                if istty1:
-                    L.append(node_name(path, n, opt.hash))
-                else:
-                    print node_name(path, n, opt.hash)
+                output_node_info(n, path)
         except vfs.NodeError, e:
             log('error: %s\n' % e)
             ret = 1
 
-    if istty1:
+    if L:
         sys.stdout.write(columnate(L, ''))
 
     return ret