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