]> arthur.barton.de Git - bup.git/blob - cmd/damage-cmd.py
Configure python, use it, and embed during install
[bup.git] / cmd / damage-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 import sys, os, random
8 from bup import options
9 from bup.helpers import *
10
11
12 def randblock(n):
13     l = []
14     for i in xrange(n):
15         l.append(chr(random.randrange(0,256)))
16     return ''.join(l)
17
18
19 optspec = """
20 bup damage [-n count] [-s maxsize] [-S seed] <filenames...>
21 --
22    WARNING: THIS COMMAND IS EXTREMELY DANGEROUS
23 n,num=   number of blocks to damage
24 s,size=  maximum size of each damaged block
25 percent= maximum size of each damaged block (as a percent of entire file)
26 equal    spread damage evenly throughout the file
27 S,seed=  random number seed (for repeatable tests)
28 """
29 o = options.Options(optspec)
30 (opt, flags, extra) = o.parse(sys.argv[1:])
31
32 if not extra:
33     o.fatal('filenames expected')
34
35 if opt.seed != None:
36     random.seed(opt.seed)
37
38 for name in extra:
39     log('Damaging "%s"...\n' % name)
40     f = open(name, 'r+b')
41     st = os.fstat(f.fileno())
42     size = st.st_size
43     if opt.percent or opt.size:
44         ms1 = int(float(opt.percent or 0)/100.0*size) or size
45         ms2 = opt.size or size
46         maxsize = min(ms1, ms2)
47     else:
48         maxsize = 1
49     chunks = opt.num or 10
50     chunksize = size/chunks
51     for r in range(chunks):
52         sz = random.randrange(1, maxsize+1)
53         if sz > size:
54             sz = size
55         if opt.equal:
56             ofs = r*chunksize
57         else:
58             ofs = random.randrange(0, size - sz + 1)
59         log('  %6d bytes at %d\n' % (sz, ofs))
60         f.seek(ofs)
61         f.write(randblock(sz))
62     f.close()