1 """Common code for listing files from a bup repository."""
3 import copy, os.path, stat, sys, xstat
5 from bup import metadata, options, vfs
6 from helpers import columnate, istty1, log
12 classification = None,
14 human_readable = False):
15 """Return a string containing the information to display for the node
16 n. Classification may be "all", "type", or None."""
19 result += "%s " % n.hash.encode('hex')
21 meta = copy.copy(n.metadata())
26 # Fake it -- summary_str() is designed to handle a fake.
27 meta = metadata.Metadata()
31 meta.atime, meta.mtime, meta.ctime = n.atime, n.mtime, n.ctime
32 if stat.S_ISLNK(meta.mode):
33 meta.symlink_target = n.readlink()
34 result += metadata.summary_str(meta,
35 numeric_ids = numeric_ids,
36 classification = classification,
37 human_readable = human_readable)
41 mode = n.metadata() and n.metadata().mode or n.mode
42 result += xstat.classification_str(mode, classification == 'all')
49 s,hash show hash for each file
50 a,all show hidden files
51 A,almost-all show hidden files except . and ..
52 l use a detailed, long listing format
53 d,directory show directories, not contents; don't follow symlinks
54 F,classify append type indicator: dir/ sym@ fifo| sock= exec*
55 file-type append type indicator: dir/ sym@ fifo| sock=
56 human-readable print human readable file sizes (i.e. 3.9K, 4.7M)
57 n,numeric-ids list numeric IDs (user, group, etc.) rather than names
60 def do_ls(args, pwd, default='.', onabort=None, spec_prefix=''):
61 """Output a listing of a file or directory in the bup repository.
63 When a long listing is not requested and stdout is attached to a
64 tty, the output is formatted in columns. When not attached to tty
65 (for example when the output is piped to another command), one
66 file is listed per line.
70 o = options.Options(optspec % spec_prefix, onabort=onabort)
72 o = options.Options(optspec % spec_prefix)
73 (opt, flags, extra) = o.parse(args)
75 # Handle order-sensitive options.
79 (option, parameter) = flag
80 if option in ('-F', '--classify'):
81 classification = 'all'
82 elif option == '--file-type':
83 classification = 'type'
84 elif option in ('-a', '--all'):
86 elif option in ('-A', '--almost-all'):
87 show_hidden = 'almost'
90 def output_node_info(node, name):
91 info = node_info(node, name,
94 classification = classification,
95 numeric_ids = opt.numeric_ids,
96 human_readable = opt.human_readable)
97 if not opt.l and istty1:
103 for path in (extra or [default]):
106 n = pwd.lresolve(path)
108 n = pwd.try_resolve(path)
110 if not opt.directory and stat.S_ISDIR(n.mode):
111 if show_hidden == 'all':
112 output_node_info(n, '.')
113 # Match non-bup "ls -a ... /".
115 output_node_info(n.parent, '..')
117 output_node_info(n, '..')
120 if show_hidden in ('almost', 'all') \
121 or not len(name)>1 or not name.startswith('.'):
122 output_node_info(sub, name)
124 output_node_info(n, os.path.normpath(path))
125 except vfs.NodeError as e:
126 log('error: %s\n' % e)
130 sys.stdout.write(columnate(L, ''))