]> arthur.barton.de Git - bup.git/commitdiff
Refactor: unify ls/ftp-ls code
authorGabriel Filion <lelutin@gmail.com>
Mon, 16 May 2011 04:27:24 +0000 (00:27 -0400)
committerAvery Pennarun <apenwarr@gmail.com>
Sun, 29 May 2011 23:02:47 +0000 (19:02 -0400)
Both the 'ls' command and the 'ls' subcommand of the 'ftp' command use
some code that is very similar. Modifications must be done in two places
instead of one and this can lead to inconsistencies.

Refactor code so that both paths use the same function with the same opt
spec.

Signed-off-by: Gabriel Filion <lelutin@gmail.com>
cmd/ftp-cmd.py
cmd/ls-cmd.py
lib/bup/ls.py [new file with mode: 0644]

index a81f01a089d702f4355db43ad9a555bcb2acbbb1..62f2cbc89af27d097c804265e9366338f85a5963 100755 (executable)
@@ -1,61 +1,22 @@
 #!/usr/bin/env python
 import sys, os, stat, fnmatch
-from bup import options, git, shquote, vfs
+from bup import options, git, shquote, vfs, ls
 from bup.helpers import *
 
 handle_ctrl_c()
 
 
-def node_name(opt, text, n):
-    prefix = ''
-    if opt.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)
-    else:
-        return '%s%s' % (prefix, text)
-
-
 class OptionError(Exception):
     pass
 
 
-ls_optspec = """
-ls [-a] [path...]
---
-s,hash   show hash for each file
-a,all   include hidden files in the listing
-"""
-ls_opt = options.Options(ls_optspec, onabort=OptionError)
-
+# Check out lib/bup/ls.py for the opt spec
 def do_ls(cmd_args):
     try:
-        (opt, flags, extra) = ls_opt.parse(cmd_args)
+        ls.do_ls(cmd_args, pwd, onabort=OptionError)
     except OptionError, e:
         return
 
-    L = []
-
-    for path in (extra or ['.']):
-        n = pwd.try_resolve(path)
-
-        if stat.S_ISDIR(n.mode):
-            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(opt, name, sub))
-                    else:
-                        print node_name(opt, name, sub)
-        else:
-            if istty1:
-                L.append(node_name(opt, path, n))
-            else:
-                print node_name(opt, path, n)
-        sys.stdout.write(columnate(L, ''))
-
 
 def write_to_file(inf, outf):
     for blob in chunkyreader(inf):
index f2252b34023637215411cedd3f557a44d4949f7f..581d6e116891387659dec7ee1d04fd6098bd37d0 100755 (executable)
@@ -1,57 +1,13 @@
 #!/usr/bin/env python
-import sys, stat
-from bup import options, git, vfs
+import sys
+from bup import git, vfs, ls
 from bup.helpers import *
 
-def node_name(text, n):
-    prefix = ''
-    if opt.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)
-    else:
-        return '%s%s' % (prefix, text)
-
-
-optspec = """
-bup ls <dirs...>
---
-s,hash   show hash for each file
-a,all    show hidden files
-"""
-o = options.Options(optspec)
-(opt, flags, extra) = o.parse(sys.argv[1:])
 
 git.check_repo_or_die()
 top = vfs.RefList(None)
 
-if not extra:
-    extra = ['/']
-
-ret = 0
-for d in extra:
-    L = []
-    try:
-        n = top.lresolve(d)
-        if stat.S_ISDIR(n.mode):
-            for sub in n:
-                if opt.all or not sub.name.startswith('.'):
-                    if istty1:
-                        L.append(node_name(sub.name, sub))
-                    else:
-                        print node_name(sub.name, sub)
-        else:
-            if istty1:
-                L.append(node_name(d, n))
-            else:
-                print node_name(d, n)
-    except vfs.NodeError, e:
-        log('error: %s\n' % e)
-        ret = 1
-
-if istty1:
-    sys.stdout.write(columnate(L, ''))
+# Check out lib/bup/ls.py for the opt spec
+ret = ls.do_ls(sys.argv[1:], top, default='/', spec_prefix='bup ')
 
 sys.exit(ret)
diff --git a/lib/bup/ls.py b/lib/bup/ls.py
new file mode 100644 (file)
index 0000000..fe04f8a
--- /dev/null
@@ -0,0 +1,67 @@
+"""Common code for listing files from a bup repository."""
+import stat
+from bup import options, vfs
+from helpers import *
+
+
+def node_name(text, n, show_hash):
+    """Add symbols to a node's name to differentiate file types."""
+    prefix = ''
+    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)
+    else:
+        return '%s%s' % (prefix, text)
+
+
+optspec = """
+%sls [-a] [path...]
+--
+s,hash   show hash for each file
+a,all    show hidden files
+"""
+
+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.
+    """
+    if onabort:
+        o = options.Options(optspec % spec_prefix, onabort=onabort)
+    else:
+        o = options.Options(optspec % spec_prefix)
+    (opt, flags, extra) = o.parse(args)
+
+    L = []
+
+    ret = 0
+    for path in (extra or [default]):
+        try:
+            n = pwd.try_resolve(path)
+
+            if stat.S_ISDIR(n.mode):
+                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)
+            else:
+                if istty1:
+                    L.append(node_name(path, n, opt.hash))
+                else:
+                    print node_name(path, n, opt.hash)
+        except vfs.NodeError, e:
+            log('error: %s\n' % e)
+            ret = 1
+
+    if istty1:
+        sys.stdout.write(columnate(L, ''))
+
+    return ret