]> arthur.barton.de Git - bup.git/blob - lib/bup/cmd/xstat.py
0cf8bdc6176a887d51c63c219d155b917a4153f5
[bup.git] / lib / bup / cmd / xstat.py
1 # Copyright (C) 2010 Rob Browning
2 #
3 # This code is covered under the terms of the GNU Library General
4 # Public License as described in the bup LICENSE file.
5
6 from __future__ import absolute_import, print_function
7
8 import errno, stat, sys
9
10 from bup import compat, metadata, options, xstat
11 from bup.compat import argv_bytes
12 from bup.helpers import add_error, handle_ctrl_c, parse_timestamp, saved_errors, \
13     add_error, log
14 from bup.io import byte_stream
15
16
17 def parse_timestamp_arg(field, value):
18     res = str(value) # Undo autoconversion.
19     try:
20         res = parse_timestamp(res)
21     except ValueError as ex:
22         if ex.args:
23             o.fatal('unable to parse %s resolution "%s" (%s)'
24                     % (field, value, ex))
25         else:
26             o.fatal('unable to parse %s resolution "%s"' % (field, value))
27
28     if res != 1 and res % 10:
29         o.fatal('%s resolution "%s" must be a power of 10' % (field, value))
30     return res
31
32
33 optspec = """
34 bup xstat pathinfo [OPTION ...] <PATH ...>
35 --
36 v,verbose       increase log output (can be used more than once)
37 q,quiet         don't show progress meter
38 exclude-fields= exclude comma-separated fields
39 include-fields= include comma-separated fields (definitive if first)
40 atime-resolution=  limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
41 mtime-resolution=  limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
42 ctime-resolution=  limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
43 """
44
45 def main(argv):
46
47     target_filename = b''
48     active_fields = metadata.all_fields
49
50     o = options.Options(optspec)
51     (opt, flags, remainder) = o.parse_bytes(argv[1:])
52
53     atime_resolution = parse_timestamp_arg('atime', opt.atime_resolution)
54     mtime_resolution = parse_timestamp_arg('mtime', opt.mtime_resolution)
55     ctime_resolution = parse_timestamp_arg('ctime', opt.ctime_resolution)
56
57     treat_include_fields_as_definitive = True
58     for flag, value in flags:
59         if flag == '--exclude-fields':
60             exclude_fields = frozenset(value.split(','))
61             for f in exclude_fields:
62                 if not f in metadata.all_fields:
63                     o.fatal(f + ' is not a valid field name')
64             active_fields = active_fields - exclude_fields
65             treat_include_fields_as_definitive = False
66         elif flag == '--include-fields':
67             include_fields = frozenset(value.split(','))
68             for f in include_fields:
69                 if not f in metadata.all_fields:
70                     o.fatal(f + ' is not a valid field name')
71             if treat_include_fields_as_definitive:
72                 active_fields = include_fields
73                 treat_include_fields_as_definitive = False
74             else:
75                 active_fields = active_fields | include_fields
76
77     opt.verbose = opt.verbose or 0
78     opt.quiet = opt.quiet or 0
79     metadata.verbose = opt.verbose - opt.quiet
80
81     sys.stdout.flush()
82     out = byte_stream(sys.stdout)
83
84     first_path = True
85     for path in remainder:
86         path = argv_bytes(path)
87         try:
88             m = metadata.from_path(path, archive_path = path)
89         except (OSError,IOError) as e:
90             if e.errno == errno.ENOENT:
91                 add_error(e)
92                 continue
93             else:
94                 raise
95         if metadata.verbose >= 0:
96             if not first_path:
97                 out.write(b'\n')
98             if atime_resolution != 1:
99                 m.atime = (m.atime / atime_resolution) * atime_resolution
100             if mtime_resolution != 1:
101                 m.mtime = (m.mtime / mtime_resolution) * mtime_resolution
102             if ctime_resolution != 1:
103                 m.ctime = (m.ctime / ctime_resolution) * ctime_resolution
104             out.write(metadata.detailed_bytes(m, active_fields))
105             out.write(b'\n')
106             first_path = False
107
108     if saved_errors:
109         log('WARNING: %d errors encountered.\n' % len(saved_errors))
110         sys.exit(1)
111     else:
112         sys.exit(0)