]> arthur.barton.de Git - bup.git/blob - cmd/xstat-cmd.py
cleanup-mounts-under: Don't fail when /proc/mounts isn't readable
[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, parse_timestamp, saved_errors, \
9     add_error, log
10
11
12 def parse_timestamp_arg(field, value):
13     res = str(value) # Undo autoconversion.
14     try:
15         res = parse_timestamp(res)
16     except ValueError, ex:
17         if ex.args:
18             o.fatal('unable to parse %s resolution "%s" (%s)'
19                     % (field, value, ex))
20         else:
21             o.fatal('unable to parse %s resolution "%s"' % (field, value))
22
23     if res != 1 and res % 10:
24         o.fatal('%s resolution "%s" must be a power of 10' % (field, value))
25     return res
26
27
28 optspec = """
29 bup xstat pathinfo [OPTION ...] <PATH ...>
30 --
31 v,verbose       increase log output (can be used more than once)
32 q,quiet         don't show progress meter
33 exclude-fields= exclude comma-separated fields
34 include-fields= include comma-separated fields (definitive if first)
35 atime-resolution=  limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
36 mtime-resolution=  limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
37 ctime-resolution=  limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
38 """
39
40 target_filename = ''
41 active_fields = metadata.all_fields
42
43 handle_ctrl_c()
44
45 o = options.Options(optspec)
46 (opt, flags, remainder) = o.parse(sys.argv[1:])
47
48 atime_resolution = parse_timestamp_arg('atime', opt.atime_resolution)
49 mtime_resolution = parse_timestamp_arg('mtime', opt.mtime_resolution)
50 ctime_resolution = parse_timestamp_arg('ctime', opt.ctime_resolution)
51
52 treat_include_fields_as_definitive = True
53 for flag, value in flags:
54     if flag == '--exclude-fields':
55         exclude_fields = frozenset(value.split(','))
56         for f in exclude_fields:
57             if not f in metadata.all_fields:
58                 o.fatal(f + ' is not a valid field name')
59         active_fields = active_fields - exclude_fields
60         treat_include_fields_as_definitive = False
61     elif flag == '--include-fields':
62         include_fields = frozenset(value.split(','))
63         for f in include_fields:
64             if not f in metadata.all_fields:
65                 o.fatal(f + ' is not a valid field name')
66         if treat_include_fields_as_definitive:
67             active_fields = include_fields
68             treat_include_fields_as_definitive = False
69         else:
70             active_fields = active_fields | include_fields
71
72 opt.verbose = opt.verbose or 0
73 opt.quiet = opt.quiet or 0
74 metadata.verbose = opt.verbose - opt.quiet
75
76 first_path = True
77 for path in remainder:
78     try:
79         m = metadata.from_path(path, archive_path = path)
80     except (OSError,IOError), e:
81         if e.errno == errno.ENOENT:
82             add_error(e)
83             continue
84         else:
85             raise
86     if metadata.verbose >= 0:
87         if not first_path:
88             print
89         if atime_resolution != 1:
90             m.atime = (m.atime / atime_resolution) * atime_resolution
91         if mtime_resolution != 1:
92             m.mtime = (m.mtime / mtime_resolution) * mtime_resolution
93         if ctime_resolution != 1:
94             m.ctime = (m.ctime / ctime_resolution) * ctime_resolution
95         print metadata.detailed_str(m, active_fields)
96         first_path = False
97
98 if saved_errors:
99     log('WARNING: %d errors encountered.\n' % len(saved_errors))
100     sys.exit(1)
101 else:
102     sys.exit(0)