]> arthur.barton.de Git - bup.git/blob - lib/cmd/drecurse-cmd.py
Bypass Python 3 glibc argv problems by routing args through env
[bup.git] / lib / cmd / drecurse-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")/bup-python" || exit $?
13 exec "$bup_python" "$0"
14 """
15 # end of bup preamble
16
17 from __future__ import absolute_import, print_function
18 from os.path import relpath
19 import sys
20
21 from bup import compat, options, drecurse
22 from bup.compat import argv_bytes
23 from bup.helpers import log, parse_excludes, parse_rx_excludes, saved_errors
24 from bup.io import byte_stream
25
26
27 optspec = """
28 bup drecurse <path>
29 --
30 x,xdev,one-file-system   don't cross filesystem boundaries
31 exclude= a path to exclude from the backup (can be used more than once)
32 exclude-from= a file that contains exclude paths (can be used more than once)
33 exclude-rx= skip paths matching the unanchored regex (may be repeated)
34 exclude-rx-from= skip --exclude-rx patterns in file (may be repeated)
35 q,quiet  don't actually print filenames
36 profile  run under the python profiler
37 """
38 o = options.Options(optspec)
39 opt, flags, extra = o.parse(compat.argv[1:])
40
41 if len(extra) != 1:
42     o.fatal("exactly one filename expected")
43
44 drecurse_top = argv_bytes(extra[0])
45 excluded_paths = parse_excludes(flags, o.fatal)
46 if not drecurse_top.startswith(b'/'):
47     excluded_paths = [relpath(x) for x in excluded_paths]
48 exclude_rxs = parse_rx_excludes(flags, o.fatal)
49 it = drecurse.recursive_dirlist([drecurse_top], opt.xdev,
50                                 excluded_paths=excluded_paths,
51                                 exclude_rxs=exclude_rxs)
52 if opt.profile:
53     import cProfile
54     def do_it():
55         for i in it:
56             pass
57     cProfile.run('do_it()')
58 else:
59     if opt.quiet:
60         for i in it:
61             pass
62     else:
63         sys.stdout.flush()
64         out = byte_stream(sys.stdout)
65         for (name,st) in it:
66             out.write(name + b'\n')
67
68 if saved_errors:
69     log('WARNING: %d errors encountered.\n' % len(saved_errors))
70     sys.exit(1)