]> arthur.barton.de Git - bup.git/blob - lib/cmd/random-cmd.py
Prefer python 3, and mention intent to drop python 2 support
[bup.git] / lib / cmd / random-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 from __future__ import absolute_import
18 import os, sys
19
20 sys.path[:0] = [os.path.dirname(os.path.realpath(__file__)) + '/..']
21
22 from bup import compat, options, _helpers
23 from bup.helpers import atoi, handle_ctrl_c, log, parse_num
24
25
26 optspec = """
27 bup random [-S seed] <numbytes>
28 --
29 S,seed=   optional random number seed [1]
30 f,force   print random data to stdout even if it's a tty
31 v,verbose print byte counter to stderr
32 """
33 o = options.Options(optspec)
34 (opt, flags, extra) = o.parse(compat.argv[1:])
35
36 if len(extra) != 1:
37     o.fatal("exactly one argument expected")
38
39 total = parse_num(extra[0])
40
41 handle_ctrl_c()
42
43 if opt.force or (not os.isatty(1) and
44                  not atoi(os.environ.get('BUP_FORCE_TTY')) & 1):
45     _helpers.write_random(sys.stdout.fileno(), total, opt.seed,
46                           opt.verbose and 1 or 0)
47 else:
48     log('error: not writing binary data to a terminal. Use -f to force.\n')
49     sys.exit(1)