annotate fail.py @ 0:02d077c5627a

initial program
author Jeff Hammel <k0scist@gmail.com>
date Mon, 29 Feb 2016 13:48:39 -0800
parents
children a4680e54c481
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 run a program until it fails
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 """
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 # imports
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import argparse
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import subprocess
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import sys
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import time
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 def main(args=sys.argv[1:]):
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 """CLI"""
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 # parse command line
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 parser = argparse.ArgumentParser(description=__doc__)
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 parser.add_argument('command', help="command to run")
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 parser.add_argument('--code', dest='codes', default=(0,), nargs='+',
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 help="allowed exit codes")
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 parser.add_argument('-s', '--sleep', dest='sleep',
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 type=float, default=1.,
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 help="sleep between iterations [DEFAULT: %(default)]")
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 options = parser.parse_args(args)
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 try:
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 ctr = 0
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 while True:
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 print ("Iteration {}".format(ctr))
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 ctr += 1
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 # run it
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 process = subprocess.Popen(options.command, shell=True)
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 _, _ = process.communicate()
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 # test it
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 if process.returncode not in options.codes:
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 sys.exit(process.returncode)
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 # loop control
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 time.sleep(options.sleep)
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 except KeyboardInterrupt:
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 sys.exit(0)
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 if __name__ == '__main__':
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 main()