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