]> arthur.barton.de Git - bup.git/blob - cmd/xstat-cmd.py
9f328458c3a343c7bf87fc6c471dd411815418c8
[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 # Copyright (C) 2010 Rob Browning
8 #
9 # This code is covered under the terms of the GNU Library General
10 # Public License as described in the bup LICENSE file.
11 import sys, stat, errno
12 from bup import metadata, options, xstat
13 from bup.helpers import add_error, handle_ctrl_c, parse_timestamp, saved_errors, \
14     add_error, log
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 target_filename = ''
46 active_fields = metadata.all_fields
47
48 handle_ctrl_c()
49
50 o = options.Options(optspec)
51 (opt, flags, remainder) = o.parse(sys.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 first_path = True
82 for path in remainder:
83     try:
84         m = metadata.from_path(path, archive_path = path)
85     except (OSError,IOError) as e:
86         if e.errno == errno.ENOENT:
87             add_error(e)
88             continue
89         else:
90             raise
91     if metadata.verbose >= 0:
92         if not first_path:
93             print
94         if atime_resolution != 1:
95             m.atime = (m.atime / atime_resolution) * atime_resolution
96         if mtime_resolution != 1:
97             m.mtime = (m.mtime / mtime_resolution) * mtime_resolution
98         if ctime_resolution != 1:
99             m.ctime = (m.ctime / ctime_resolution) * ctime_resolution
100         print metadata.detailed_str(m, active_fields)
101         first_path = False
102
103 if saved_errors:
104     log('WARNING: %d errors encountered.\n' % len(saved_errors))
105     sys.exit(1)
106 else:
107     sys.exit(0)