]> arthur.barton.de Git - bup.git/blob - cmd/xstat-cmd.py
Restore any metadata during "bup restore"; add "bup meta --edit".
[bup.git] / cmd / xstat-cmd.py
1 #!/usr/bin/env python
2 # Copyright (C) 2010 Rob Browning
3 #
4 # This code is covered under the terms of the GNU Library General
5 # Public License as described in the bup LICENSE file.
6 import sys, stat, errno
7 from bup import metadata, options, xstat
8 from bup.helpers import handle_ctrl_c, saved_errors, add_error, log
9
10 optspec = """
11 bup xstat pathinfo [OPTION ...] <PATH ...>
12 --
13 v,verbose       increase log output (can be used more than once)
14 q,quiet         don't show progress meter
15 exclude-fields= exclude comma-separated fields
16 include-fields= include comma-separated fields (definitive if first)
17 """
18
19 target_filename = ''
20 active_fields = metadata.all_fields
21
22 handle_ctrl_c()
23
24 o = options.Options(optspec)
25 (opt, flags, remainder) = o.parse(sys.argv[1:])
26
27 treat_include_fields_as_definitive = True
28 for flag, value in flags:
29     if flag == '--exclude-fields':
30         exclude_fields = frozenset(value.split(','))
31         for f in exclude_fields:
32             if not f in metadata.all_fields:
33                 o.fatal(f + ' is not a valid field name')
34         active_fields = active_fields - exclude_fields
35         treat_include_fields_as_definitive = False
36     elif flag == '--include-fields':
37         include_fields = frozenset(value.split(','))
38         for f in include_fields:
39             if not f in metadata.all_fields:
40                 o.fatal(f + ' is not a valid field name')
41         if treat_include_fields_as_definitive:
42             active_fields = include_fields
43             treat_include_fields_as_definitive = False
44         else:
45             active_fields = active_fields | include_fields
46
47 opt.verbose = opt.verbose or 0
48 opt.quiet = opt.quiet or 0
49 metadata.verbose = opt.verbose - opt.quiet
50
51 first_path = True
52 for path in remainder:
53     try:
54         m = metadata.from_path(path, archive_path = path)
55     except (OSError,IOError), e:
56         if e.errno == errno.ENOENT:
57             add_error(e)
58             continue
59         else:
60             raise
61     if metadata.verbose >= 0:
62         if not first_path:
63             print
64         print metadata.detailed_str(m, active_fields)
65         first_path = False
66
67 if saved_errors:
68     log('WARNING: %d errors encountered.\n' % len(saved_errors))
69     sys.exit(1)
70 else:
71     sys.exit(0)