]> arthur.barton.de Git - bup.git/blob - lib/bup/ls.py
fe04f8a3b3eb7d1e426b7250126682d51ce70a0d
[bup.git] / lib / bup / ls.py
1 """Common code for listing files from a bup repository."""
2 import stat
3 from bup import options, vfs
4 from helpers import *
5
6
7 def node_name(text, n, show_hash):
8     """Add symbols to a node's name to differentiate file types."""
9     prefix = ''
10     if show_hash:
11         prefix += "%s " % n.hash.encode('hex')
12     if stat.S_ISDIR(n.mode):
13         return '%s%s/' % (prefix, text)
14     elif stat.S_ISLNK(n.mode):
15         return '%s%s@' % (prefix, text)
16     else:
17         return '%s%s' % (prefix, text)
18
19
20 optspec = """
21 %sls [-a] [path...]
22 --
23 s,hash   show hash for each file
24 a,all    show hidden files
25 """
26
27 def do_ls(args, pwd, default='.', onabort=None, spec_prefix=''):
28     """Output a listing of a file or directory in the bup repository.
29
30     When stdout is attached to a tty, the output is formatted in columns. When
31     not attached to tty (for example when the output is piped to another
32     command), one file is listed per line.
33     """
34     if onabort:
35         o = options.Options(optspec % spec_prefix, onabort=onabort)
36     else:
37         o = options.Options(optspec % spec_prefix)
38     (opt, flags, extra) = o.parse(args)
39
40     L = []
41
42     ret = 0
43     for path in (extra or [default]):
44         try:
45             n = pwd.try_resolve(path)
46
47             if stat.S_ISDIR(n.mode):
48                 for sub in n:
49                     name = sub.name
50                     if opt.all or not len(name)>1 or not name.startswith('.'):
51                         if istty1:
52                             L.append(node_name(name, sub, opt.hash))
53                         else:
54                             print node_name(name, sub, opt.hash)
55             else:
56                 if istty1:
57                     L.append(node_name(path, n, opt.hash))
58                 else:
59                     print node_name(path, n, opt.hash)
60         except vfs.NodeError, e:
61             log('error: %s\n' % e)
62             ret = 1
63
64     if istty1:
65         sys.stdout.write(columnate(L, ''))
66
67     return ret