Mercurial > hg > fail
annotate fail.py @ 4:8dc420754023
minor upgrades to prepare for streamlining
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 23 Aug 2016 16:29:13 -0700 |
parents | d1880117acb5 |
children | b407681e6f2c |
rev | line source |
---|---|
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='+', | |
4
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
21 help="allowed exit codes [DEFAULT: 0]") |
0 | 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 | |
4
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
29 # main loop |
0 | 30 ctr = 0 |
4
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
31 start_loop = time.time() |
0 | 32 while True: |
33 | |
4
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
34 # note program start time |
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
35 program_start_time = time.time() if ctr else start_loop |
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
36 |
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
37 # run the subcommand |
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
38 process = subprocess.Popen(options.command, shell=True) |
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
39 stdout, stderr = process.communicate() |
0 | 40 ctr += 1 |
41 | |
4
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
42 # print iteration information |
2
d1880117acb5
attempt to correct documentation display on pypi
Jeff Hammel <k0scist@gmail.com>
parents:
1
diff
changeset
|
43 print ("Iteration {}".format(ctr)) |
d1880117acb5
attempt to correct documentation display on pypi
Jeff Hammel <k0scist@gmail.com>
parents:
1
diff
changeset
|
44 |
0 | 45 # test it |
46 if process.returncode not in options.codes: | |
47 sys.exit(process.returncode) | |
48 | |
49 # loop control | |
50 time.sleep(options.sleep) | |
51 | |
52 | |
53 except KeyboardInterrupt: | |
54 sys.exit(0) | |
55 | |
56 if __name__ == '__main__': | |
57 main() |