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