From: Gabriel Filion Date: Mon, 16 May 2011 04:27:24 +0000 (-0400) Subject: Refactor: unify ls/ftp-ls code X-Git-Tag: bup-0.25-rc1~11 X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=bup.git;a=commitdiff_plain;h=36f565f3d201cabc40c4e6ca235a497686ac39e0 Refactor: unify ls/ftp-ls code 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 --- diff --git a/cmd/ftp-cmd.py b/cmd/ftp-cmd.py index a81f01a..62f2cbc 100755 --- a/cmd/ftp-cmd.py +++ b/cmd/ftp-cmd.py @@ -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): diff --git a/cmd/ls-cmd.py b/cmd/ls-cmd.py index f2252b3..581d6e1 100755 --- a/cmd/ls-cmd.py +++ b/cmd/ls-cmd.py @@ -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 --- -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 index 0000000..fe04f8a --- /dev/null +++ b/lib/bup/ls.py @@ -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