1 """Common code for listing files from a bup repository."""
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.
19 result += "%s " % n.hash.encode('hex')
21 meta = copy.copy(n.metadata())
23 result += metadata.summary_str(meta,
24 numeric_ids = numeric_ids,
25 human_readable = human_readable) + ' '
29 mode = n.metadata().mode
32 if stat.S_ISREG(mode):
33 if classification == 'all' \
34 and stat.S_IMODE(mode) & (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH):
36 elif stat.S_ISDIR(mode):
38 elif stat.S_ISLNK(mode):
40 elif stat.S_ISFIFO(mode):
42 elif stat.S_ISSOCK(mode):
50 s,hash show hash for each file
51 a,all show hidden files
52 A,almost-all show hidden files except . and ..
53 l use a detailed, long listing format
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]):
105 n = pwd.try_resolve(path)
107 if stat.S_ISDIR(n.mode):
108 if show_hidden == 'all':
109 for implied, name in ((n, '.'), (n.parent, '..')):
110 output_node_info(implied, name)
113 if show_hidden in ('almost', 'all') \
114 or not len(name)>1 or not name.startswith('.'):
115 output_node_info(sub, name)
117 output_node_info(n, path)
118 except vfs.NodeError, e:
119 log('error: %s\n' % e)
123 sys.stdout.write(columnate(L, ''))