]> arthur.barton.de Git - bup.git/blob - cmd/xstat-cmd.py
Replace remaining print statements with print function
[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.helpers import add_error, handle_ctrl_c, parse_timestamp, saved_errors, \
18     add_error, log
19
20
21 def parse_timestamp_arg(field, value):
22     res = str(value) # Undo autoconversion.
23     try:
24         res = parse_timestamp(res)
25     except ValueError as ex:
26         if ex.args:
27             o.fatal('unable to parse %s resolution "%s" (%s)'
28                     % (field, value, ex))
29         else:
30             o.fatal('unable to parse %s resolution "%s"' % (field, value))
31
32     if res != 1 and res % 10:
33         o.fatal('%s resolution "%s" must be a power of 10' % (field, value))
34     return res
35
36
37 optspec = """
38 bup xstat pathinfo [OPTION ...] <PATH ...>
39 --
40 v,verbose       increase log output (can be used more than once)
41 q,quiet         don't show progress meter
42 exclude-fields= exclude comma-separated fields
43 include-fields= include comma-separated fields (definitive if first)
44 atime-resolution=  limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
45 mtime-resolution=  limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
46 ctime-resolution=  limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
47 """
48
49 target_filename = ''
50 active_fields = metadata.all_fields
51
52 handle_ctrl_c()
53
54 o = options.Options(optspec)
55 (opt, flags, remainder) = o.parse(sys.argv[1:])
56
57 atime_resolution = parse_timestamp_arg('atime', opt.atime_resolution)
58 mtime_resolution = parse_timestamp_arg('mtime', opt.mtime_resolution)
59 ctime_resolution = parse_timestamp_arg('ctime', opt.ctime_resolution)
60
61 treat_include_fields_as_definitive = True
62 for flag, value in flags:
63     if flag == '--exclude-fields':
64         exclude_fields = frozenset(value.split(','))
65         for f in exclude_fields:
66             if not f in metadata.all_fields:
67                 o.fatal(f + ' is not a valid field name')
68         active_fields = active_fields - exclude_fields
69         treat_include_fields_as_definitive = False
70     elif flag == '--include-fields':
71         include_fields = frozenset(value.split(','))
72         for f in include_fields:
73             if not f in metadata.all_fields:
74                 o.fatal(f + ' is not a valid field name')
75         if treat_include_fields_as_definitive:
76             active_fields = include_fields
77             treat_include_fields_as_definitive = False
78         else:
79             active_fields = active_fields | include_fields
80
81 opt.verbose = opt.verbose or 0
82 opt.quiet = opt.quiet or 0
83 metadata.verbose = opt.verbose - opt.quiet
84
85 first_path = True
86 for path in remainder:
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             print()
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         print(metadata.detailed_str(m, active_fields))
105         first_path = False
106
107 if saved_errors:
108     log('WARNING: %d errors encountered.\n' % len(saved_errors))
109     sys.exit(1)
110 else:
111     sys.exit(0)