comparison fail.py @ 0:02d077c5627a

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