"""Common code for listing files from a bup repository."""
-import copy, os.path, stat, xstat
-from bup import metadata, options, vfs
-from helpers import *
+from __future__ import absolute_import
+from binascii import hexlify
+from itertools import chain
+from stat import S_ISDIR, S_ISLNK
+import copy, locale, os.path, stat, sys
-def node_info(n, name,
+from bup import metadata, options, vfs, xstat
+from bup.compat import argv_bytes
+from bup.io import path_msg
+from bup.options import Options
+from bup.repo import LocalRepo, RemoteRepo
+from bup.helpers import columnate, istty1, last, log
+
+def item_hash(item, tree_for_commit):
+ """If the item is a Commit, return its commit oid, otherwise return
+ the item's oid, if it has one.
+
+ """
+ if tree_for_commit and isinstance(item, vfs.Commit):
+ return item.coid
+ return getattr(item, 'oid', None)
+
+def item_info(item, name,
show_hash = False,
+ commit_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 = ''
+ """Return bytes containing the information to display for the VFS
+ item. Classification may be "all", "type", or None.
+
+ """
+ result = b''
if show_hash:
- result += "%s " % n.hash.encode('hex')
+ oid = item_hash(item, commit_hash)
+ result += b'%s ' % (hexlify(oid) if oid
+ else b'0000000000000000000000000000000000000000')
if long_fmt:
- meta = copy.copy(n.metadata())
- if meta:
- meta.path = name
- meta.size = n.size()
- 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)
+ meta = item.meta.copy()
+ meta.path = name
+ # FIXME: need some way to track fake vs real meta items?
+ result += metadata.summary_bytes(meta,
+ numeric_ids=numeric_ids,
+ classification=classification,
+ human_readable=human_readable)
else:
result += name
if classification:
- mode = n.metadata() and n.metadata().mode or n.mode
- result += xstat.classification_str(mode, classification == 'all')
+ cls = xstat.classification_str(vfs.item_mode(item),
+ classification == 'all')
+ result += cls.encode('iso-8859-1')
return result
optspec = """
-%sls [-a] [path...]
+bup ls [-r host:path] [-l] [-d] [-F] [-a] [-A] [-s] [-n] [path...]
--
+r,remote= remote repository path
s,hash show hash for each file
+commit-hash show commit hash instead of tree for commits (implies -s)
a,all show hidden files
A,almost-all show hidden files except . and ..
l use a detailed, long listing format
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 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.
+def opts_from_cmdline(args, onabort=None):
+ """Parse ls command line arguments and return a dictionary of ls
+ options, agumented with "classification", "long_listing",
+ "paths", and "show_hidden".
"""
if onabort:
- o = options.Options(optspec % spec_prefix, onabort=onabort)
+ opt, flags, extra = Options(optspec, onabort=onabort).parse(args)
else:
- o = options.Options(optspec % spec_prefix)
- (opt, flags, extra) = o.parse(args)
+ opt, flags, extra = Options(optspec).parse(args)
- # Handle order-sensitive options.
- classification = None
- show_hidden = None
+ opt.paths = [argv_bytes(x) for x in extra] or (b'/',)
+ opt.long_listing = opt.l
+ opt.classification = None
+ opt.show_hidden = None
for flag in flags:
- (option, parameter) = flag
+ option, parameter = flag
if option in ('-F', '--classify'):
- classification = 'all'
+ opt.classification = 'all'
elif option == '--file-type':
- classification = 'type'
+ opt.classification = 'type'
elif option in ('-a', '--all'):
- show_hidden = 'all'
+ opt.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
+ opt.show_hidden = 'almost'
+ return opt
+
+def within_repo(repo, opt, out):
+
+ if opt.commit_hash:
+ opt.hash = True
+
+ def item_line(item, name):
+ return item_info(item, name,
+ show_hash=opt.hash,
+ commit_hash=opt.commit_hash,
+ long_fmt=opt.long_listing,
+ classification=opt.classification,
+ numeric_ids=opt.numeric_ids,
+ human_readable=opt.human_readable)
ret = 0
- for path in (extra or [default]):
+ pending = []
+ for path in opt.paths:
try:
if opt.directory:
- n = pwd.lresolve(path)
+ resolved = vfs.resolve(repo, path, follow=False)
else:
- n = pwd.try_resolve(path)
+ resolved = vfs.try_resolve(repo, path)
- if not opt.directory and stat.S_ISDIR(n.mode):
- if show_hidden == 'all':
- output_node_info(n, '.')
+ leaf_name, leaf_item = resolved[-1]
+ if not leaf_item:
+ log('error: cannot access %r in %r\n'
+ % ('/'.join(path_msg(name) for name, item in resolved),
+ path_msg(path)))
+ ret = 1
+ continue
+ if not opt.directory and S_ISDIR(vfs.item_mode(leaf_item)):
+ items = vfs.contents(repo, leaf_item)
+ if opt.show_hidden == 'all':
# Match non-bup "ls -a ... /".
- if n.parent:
- output_node_info(n.parent, '..')
+ parent = resolved[-2] if len(resolved) > 1 else resolved[0]
+ items = chain(items, ((b'..', parent[1]),))
+ for sub_name, sub_item in sorted(items, key=lambda x: x[0]):
+ if opt.show_hidden != 'all' and sub_name == b'.':
+ continue
+ if sub_name.startswith(b'.') and \
+ opt.show_hidden not in ('almost', 'all'):
+ continue
+ if opt.l:
+ sub_item = vfs.ensure_item_has_metadata(repo, sub_item,
+ include_size=True)
+ else:
+ sub_item = vfs.augment_item_meta(repo, sub_item,
+ include_size=True)
+ line = item_line(sub_item, sub_name)
+ if not opt.long_listing and istty1:
+ pending.append(line)
else:
- output_node_info(n, '..')
- for sub in n:
- name = sub.name
- if show_hidden in ('almost', 'all') \
- or not len(name)>1 or not name.startswith('.'):
- output_node_info(sub, name)
+ out.write(line)
+ out.write(b'\n')
else:
- output_node_info(n, os.path.normpath(path))
- except vfs.NodeError, e:
- log('error: %s\n' % e)
+ leaf_item = vfs.augment_item_meta(repo, leaf_item,
+ include_size=True)
+ line = item_line(leaf_item, os.path.normpath(path))
+ if not opt.long_listing and istty1:
+ pending.append(line)
+ else:
+ out.write(line)
+ out.write(b'\n')
+ except vfs.IOError as ex:
+ log('bup: %s\n' % ex)
ret = 1
- if L:
- sys.stdout.write(columnate(L, ''))
+ if pending:
+ out.write(columnate(pending, b''))
return ret
+
+def via_cmdline(args, out=None, onabort=None):
+ """Write a listing of a file or directory in the bup repository to out.
+
+ 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.
+
+ """
+ assert out
+ opt = opts_from_cmdline(args, onabort=onabort)
+ repo = RemoteRepo(argv_bytes(opt.remote)) if opt.remote else LocalRepo()
+ return within_repo(repo, opt, out)