]> arthur.barton.de Git - bup.git/blob - lib/cmd/xstat-cmd.py
Remove $(dirname "$0") from sys.path
[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
24 # Intentionally replace the dirname "$0" that python prepends
25 import os, sys
26 sys.path[0] = os.path.dirname(os.path.realpath(__file__)) + '/..'
27
28 import errno, os.path, stat
29
30 from bup import compat, metadata, options, xstat
31 from bup.compat import argv_bytes
32 from bup.helpers import add_error, handle_ctrl_c, parse_timestamp, saved_errors, \
33     add_error, log
34 from bup.io import byte_stream
35
36
37 def parse_timestamp_arg(field, value):
38     res = str(value) # Undo autoconversion.
39     try:
40         res = parse_timestamp(res)
41     except ValueError as ex:
42         if ex.args:
43             o.fatal('unable to parse %s resolution "%s" (%s)'
44                     % (field, value, ex))
45         else:
46             o.fatal('unable to parse %s resolution "%s"' % (field, value))
47
48     if res != 1 and res % 10:
49         o.fatal('%s resolution "%s" must be a power of 10' % (field, value))
50     return res
51
52
53 optspec = """
54 bup xstat pathinfo [OPTION ...] <PATH ...>
55 --
56 v,verbose       increase log output (can be used more than once)
57 q,quiet         don't show progress meter
58 exclude-fields= exclude comma-separated fields
59 include-fields= include comma-separated fields (definitive if first)
60 atime-resolution=  limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
61 mtime-resolution=  limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
62 ctime-resolution=  limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
63 """
64
65 target_filename = b''
66 active_fields = metadata.all_fields
67
68 handle_ctrl_c()
69
70 o = options.Options(optspec)
71 (opt, flags, remainder) = o.parse(compat.argv[1:])
72
73 atime_resolution = parse_timestamp_arg('atime', opt.atime_resolution)
74 mtime_resolution = parse_timestamp_arg('mtime', opt.mtime_resolution)
75 ctime_resolution = parse_timestamp_arg('ctime', opt.ctime_resolution)
76
77 treat_include_fields_as_definitive = True
78 for flag, value in flags:
79     if flag == '--exclude-fields':
80         exclude_fields = frozenset(value.split(','))
81         for f in exclude_fields:
82             if not f in metadata.all_fields:
83                 o.fatal(f + ' is not a valid field name')
84         active_fields = active_fields - exclude_fields
85         treat_include_fields_as_definitive = False
86     elif flag == '--include-fields':
87         include_fields = frozenset(value.split(','))
88         for f in include_fields:
89             if not f in metadata.all_fields:
90                 o.fatal(f + ' is not a valid field name')
91         if treat_include_fields_as_definitive:
92             active_fields = include_fields
93             treat_include_fields_as_definitive = False
94         else:
95             active_fields = active_fields | include_fields
96
97 opt.verbose = opt.verbose or 0
98 opt.quiet = opt.quiet or 0
99 metadata.verbose = opt.verbose - opt.quiet
100
101 sys.stdout.flush()
102 out = byte_stream(sys.stdout)
103
104 first_path = True
105 for path in remainder:
106     path = argv_bytes(path)
107     try:
108         m = metadata.from_path(path, archive_path = path)
109     except (OSError,IOError) as e:
110         if e.errno == errno.ENOENT:
111             add_error(e)
112             continue
113         else:
114             raise
115     if metadata.verbose >= 0:
116         if not first_path:
117             out.write(b'\n')
118         if atime_resolution != 1:
119             m.atime = (m.atime / atime_resolution) * atime_resolution
120         if mtime_resolution != 1:
121             m.mtime = (m.mtime / mtime_resolution) * mtime_resolution
122         if ctime_resolution != 1:
123             m.ctime = (m.ctime / ctime_resolution) * ctime_resolution
124         out.write(metadata.detailed_bytes(m, active_fields))
125         out.write(b'\n')
126         first_path = False
127
128 if saved_errors:
129     log('WARNING: %d errors encountered.\n' % len(saved_errors))
130     sys.exit(1)
131 else:
132     sys.exit(0)