1 """Common code for listing files from a bup repository."""
2 import copy, stat, xstat
3 from bup import metadata, options, vfs
10 classification = None,
12 human_readable = False):
13 """Return a string containing the information to display for the node
14 n. Classification may be "all", "type", or None."""
17 result += "%s " % n.hash.encode('hex')
19 meta = copy.copy(n.metadata())
24 # Fake it -- summary_str() is designed to handle a fake.
25 meta = metadata.Metadata()
29 meta.atime, meta.mtime, meta.ctime = n.atime, n.mtime, n.ctime
30 if stat.S_ISLNK(meta.mode):
31 meta.symlink_target = n.readlink()
32 result += metadata.summary_str(meta,
33 numeric_ids = numeric_ids,
34 classification = classification,
35 human_readable = human_readable)
39 mode = n.metadata() and n.metadata().mode or n.mode
40 result += xstat.classification_str(mode, classification == 'all')
47 s,hash show hash for each file
48 a,all show hidden files
49 A,almost-all show hidden files except . and ..
50 l use a detailed, long listing format
51 F,classify append type indicator: dir/ sym@ fifo| sock= exec*
52 file-type append type indicator: dir/ sym@ fifo| sock=
53 human-readable print human readable file sizes (i.e. 3.9K, 4.7M)
54 n,numeric-ids list numeric IDs (user, group, etc.) rather than names
57 def do_ls(args, pwd, default='.', onabort=None, spec_prefix=''):
58 """Output a listing of a file or directory in the bup repository.
60 When a long listing is not requested and stdout is attached to a
61 tty, the output is formatted in columns. When not attached to tty
62 (for example when the output is piped to another command), one
63 file is listed per line.
67 o = options.Options(optspec % spec_prefix, onabort=onabort)
69 o = options.Options(optspec % spec_prefix)
70 (opt, flags, extra) = o.parse(args)
72 # Handle order-sensitive options.
76 (option, parameter) = flag
77 if option in ('-F', '--classify'):
78 classification = 'all'
79 elif option == '--file-type':
80 classification = 'type'
81 elif option in ('-a', '--all'):
83 elif option in ('-A', '--almost-all'):
84 show_hidden = 'almost'
87 def output_node_info(node, name):
88 info = node_info(node, name,
91 classification = classification,
92 numeric_ids = opt.numeric_ids,
93 human_readable = opt.human_readable)
94 if not opt.l and istty1:
100 for path in (extra or [default]):
102 n = pwd.try_resolve(path)
104 if stat.S_ISDIR(n.mode):
105 if show_hidden == 'all':
106 output_node_info(n, '.')
107 # Match non-bup "ls -a ... /".
109 output_node_info(n.parent, '..')
111 output_node_info(n, '..')
114 if show_hidden in ('almost', 'all') \
115 or not len(name)>1 or not name.startswith('.'):
116 output_node_info(sub, name)
118 output_node_info(n, path)
119 except vfs.NodeError, e:
120 log('error: %s\n' % e)
124 sys.stdout.write(columnate(L, ''))