0
|
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
|
1
|
13
|
0
|
14 def main(args=sys.argv[1:]):
|
|
15 """CLI"""
|
|
16
|
|
17 # parse command line
|
|
18 parser = argparse.ArgumentParser(description=__doc__)
|
|
19 parser.add_argument('command', help="command to run")
|
|
20 parser.add_argument('--code', dest='codes', default=(0,), nargs='+',
|
|
21 help="allowed exit codes")
|
|
22 parser.add_argument('-s', '--sleep', dest='sleep',
|
|
23 type=float, default=1.,
|
1
|
24 help="sleep between iterations [DEFAULT: %(default)s]")
|
0
|
25 options = parser.parse_args(args)
|
|
26
|
|
27 try:
|
|
28
|
|
29 ctr = 0
|
|
30 while True:
|
|
31
|
|
32 print ("Iteration {}".format(ctr))
|
|
33 ctr += 1
|
|
34
|
|
35 # run it
|
|
36 process = subprocess.Popen(options.command, shell=True)
|
|
37 _, _ = process.communicate()
|
|
38
|
|
39 # test it
|
|
40 if process.returncode not in options.codes:
|
|
41 sys.exit(process.returncode)
|
|
42
|
|
43 # loop control
|
|
44 time.sleep(options.sleep)
|
|
45
|
|
46
|
|
47 except KeyboardInterrupt:
|
|
48 sys.exit(0)
|
|
49
|
|
50 if __name__ == '__main__':
|
|
51 main()
|