]> arthur.barton.de Git - bup.git/blob - cmd/random-cmd.py
868c3afd3fcebf2a775bc7e5e5a0b63dfd31239b
[bup.git] / cmd / random-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
8 import os, sys
9
10 from bup import options, _helpers
11 from bup.helpers import atoi, handle_ctrl_c, log, parse_num
12
13
14 optspec = """
15 bup random [-S seed] <numbytes>
16 --
17 S,seed=   optional random number seed [1]
18 f,force   print random data to stdout even if it's a tty
19 v,verbose print byte counter to stderr
20 """
21 o = options.Options(optspec)
22 (opt, flags, extra) = o.parse(sys.argv[1:])
23
24 if len(extra) != 1:
25     o.fatal("exactly one argument expected")
26
27 total = parse_num(extra[0])
28
29 handle_ctrl_c()
30
31 if opt.force or (not os.isatty(1) and
32                  not atoi(os.environ.get('BUP_FORCE_TTY')) & 1):
33     _helpers.write_random(sys.stdout.fileno(), total, opt.seed,
34                           opt.verbose and 1 or 0)
35 else:
36     log('error: not writing binary data to a terminal. Use -f to force.\n')
37     sys.exit(1)